From 26e86edebfec8a519483ccb4b0a76669ef30ea64 Mon Sep 17 00:00:00 2001 From: micuat Date: Sun, 18 Oct 2020 12:22:08 +0200 Subject: [PATCH] init (copied from ofxAzureKinect/example-shader) --- .gitignore | 113 ++++++++++++++++++++ zoneb/addons.make | 1 + zoneb/bin/data/shaders/render.frag | 14 +++ zoneb/bin/data/shaders/render.geom | 41 ++++++++ zoneb/bin/data/shaders/render.vert | 36 +++++++ zoneb/config.make | 141 +++++++++++++++++++++++++ zoneb/icon.rc | 8 ++ zoneb/src/main.cpp | 11 ++ zoneb/src/ofApp.cpp | 164 +++++++++++++++++++++++++++++ zoneb/src/ofApp.h | 37 +++++++ 10 files changed, 566 insertions(+) create mode 100644 .gitignore create mode 100644 zoneb/addons.make create mode 100644 zoneb/bin/data/shaders/render.frag create mode 100644 zoneb/bin/data/shaders/render.geom create mode 100644 zoneb/bin/data/shaders/render.vert create mode 100644 zoneb/config.make create mode 100644 zoneb/icon.rc create mode 100644 zoneb/src/main.cpp create mode 100644 zoneb/src/ofApp.cpp create mode 100644 zoneb/src/ofApp.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9076da4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,113 @@ +######################### +# general patterns +######################### + +docs/html +docs/tagfile.xml + +*/bin/* +!*/bin/data/ + +# for bin folder in root +/bin/* +!/bin/data/ + +[Bb]uild/ +[Oo]bj/ +*.o +[Dd]ebug*/ +[Rr]elease*/ +*.mode* +*.app/ +*.pyc +.svn/ + +######################### +# IDE +######################### + +# XCode +*.pbxuser +*.perspective +*.perspectivev3 +*.mode1v3 +*.mode2v3 +#XCode 4 +xcuserdata +*.xcworkspace + +# Code::Blocks +*.depend +*.layout +*.cbTemp + +# Visual Studio +*.sdf +*.opensdf +*.suo +*.pdb +*.ilk +*.aps +.vs +ipch/ + +# Eclipse +.metadata +local.properties +.externalToolBuilders + +# Codelite +*.session +*.tags +*.workspace.* + +######################### +# operating system +######################### + +# Linux +*~ +# KDE +.directory +.AppleDouble + +# OSX +.DS_Store +*.swp +*~.nib +# Thumbnails +._* + +# Windows +# Windows image file caches +Thumbs.db +# Folder config file +Desktop.ini + +#Android +.csettings + +######################### +# packages +######################### + +# it's better to unpack these files and commit the raw source +# git has its own built in compression methods +*.7z +*.dmg +*.gz +*.iso +*.jar +*.rar +*.tar +*.zip + +# Logs and databases +*.log +*.sql +*.sqlite + +*.qbs +*.sln +Makefile +*.vcxproj* \ No newline at end of file diff --git a/zoneb/addons.make b/zoneb/addons.make new file mode 100644 index 0000000..b155bef --- /dev/null +++ b/zoneb/addons.make @@ -0,0 +1 @@ +ofxAzureKinect diff --git a/zoneb/bin/data/shaders/render.frag b/zoneb/bin/data/shaders/render.frag new file mode 100644 index 0000000..4098e35 --- /dev/null +++ b/zoneb/bin/data/shaders/render.frag @@ -0,0 +1,14 @@ +#version 150 + +// Custom attributes. + +uniform sampler2DRect uColorTex; // Sampler for the color registered data + +in vec2 gTexCoord; + +out vec4 fragColor; + +void main() +{ + fragColor = texture(uColorTex, gTexCoord); +} \ No newline at end of file diff --git a/zoneb/bin/data/shaders/render.geom b/zoneb/bin/data/shaders/render.geom new file mode 100644 index 0000000..bdea1a0 --- /dev/null +++ b/zoneb/bin/data/shaders/render.geom @@ -0,0 +1,41 @@ +#version 150 + +layout (points) in; +layout (triangle_strip) out; +layout (max_vertices = 4) out; + +// OF handled uniforms and attributes. +uniform mat4 projectionMatrix; + +// App specific uniforms and attributes. +uniform float uSpriteSize; + +in vec4 vPosition[]; +in vec2 vTexCoord[]; +flat in int vValid[]; + +out vec2 gTexCoord; + +void main() +{ + if (vValid[0] == 0) return; + + gTexCoord = vTexCoord[0]; + + for (int i = 0; i < gl_in.length(); ++i) + { + gl_Position = projectionMatrix * (vPosition[i] + vec4(1.0, -1.0, 0.0, 0.0) * uSpriteSize); + EmitVertex(); + + gl_Position = projectionMatrix * (vPosition[i] + vec4(1.0, 1.0, 0.0, 0.0) * uSpriteSize); + EmitVertex(); + + gl_Position = projectionMatrix * (vPosition[i] + vec4(-1.0, -1.0, 0.0, 0.0) * uSpriteSize); + EmitVertex(); + + gl_Position = projectionMatrix * (vPosition[i] + vec4(-1.0, 1.0, 0.0, 0.0) * uSpriteSize); + EmitVertex(); + + EndPrimitive(); + } +} diff --git a/zoneb/bin/data/shaders/render.vert b/zoneb/bin/data/shaders/render.vert new file mode 100644 index 0000000..3786f21 --- /dev/null +++ b/zoneb/bin/data/shaders/render.vert @@ -0,0 +1,36 @@ +#version 150 + +// OF built-in attributes. + +uniform mat4 modelViewMatrix; + +// Custom attributes. + +uniform sampler2DRect uDepthTex; // Sampler for the depth space data +uniform sampler2DRect uWorldTex; // Transformation from kinect depth/color space to kinect world space + +uniform ivec2 uFrameSize; + +out vec4 vPosition; +out vec2 vTexCoord; +flat out int vValid; + +void main() +{ + vTexCoord = vec2(gl_InstanceID % uFrameSize.x, gl_InstanceID / uFrameSize.x); + + float depth = texture(uDepthTex, vTexCoord).x; + vec4 ray = texture(uWorldTex, vTexCoord); + + vValid = (depth != 0 && ray.x != 0 && ray.y != 0) ? 1 : 0; + + vec4 posWorld = vec4(1); + posWorld.z = depth * 65535.0; // Remap to float range. + posWorld.x = ray.x * posWorld.z; + posWorld.y = ray.y * posWorld.z; + + // Flip X as OpenGL and K4A have different conventions on which direction is positive. + posWorld.x *= -1; + + vPosition = modelViewMatrix * posWorld; +} \ No newline at end of file diff --git a/zoneb/config.make b/zoneb/config.make new file mode 100644 index 0000000..836fce7 --- /dev/null +++ b/zoneb/config.make @@ -0,0 +1,141 @@ +################################################################################ +# CONFIGURE PROJECT MAKEFILE (optional) +# This file is where we make project specific configurations. +################################################################################ + +################################################################################ +# OF ROOT +# The location of your root openFrameworks installation +# (default) OF_ROOT = ../../.. +################################################################################ +# OF_ROOT = ../../.. + +################################################################################ +# PROJECT ROOT +# The location of the project - a starting place for searching for files +# (default) PROJECT_ROOT = . (this directory) +# +################################################################################ +# PROJECT_ROOT = . + +################################################################################ +# PROJECT SPECIFIC CHECKS +# This is a project defined section to create internal makefile flags to +# conditionally enable or disable the addition of various features within +# this makefile. For instance, if you want to make changes based on whether +# GTK is installed, one might test that here and create a variable to check. +################################################################################ +# None + +################################################################################ +# PROJECT EXTERNAL SOURCE PATHS +# These are fully qualified paths that are not within the PROJECT_ROOT folder. +# Like source folders in the PROJECT_ROOT, these paths are subject to +# exlclusion via the PROJECT_EXLCUSIONS list. +# +# (default) PROJECT_EXTERNAL_SOURCE_PATHS = (blank) +# +# Note: Leave a leading space when adding list items with the += operator +################################################################################ +# PROJECT_EXTERNAL_SOURCE_PATHS = + +################################################################################ +# PROJECT EXCLUSIONS +# These makefiles assume that all folders in your current project directory +# and any listed in the PROJECT_EXTERNAL_SOURCH_PATHS are are valid locations +# to look for source code. The any folders or files that match any of the +# items in the PROJECT_EXCLUSIONS list below will be ignored. +# +# Each item in the PROJECT_EXCLUSIONS list will be treated as a complete +# string unless teh user adds a wildcard (%) operator to match subdirectories. +# GNU make only allows one wildcard for matching. The second wildcard (%) is +# treated literally. +# +# (default) PROJECT_EXCLUSIONS = (blank) +# +# Will automatically exclude the following: +# +# $(PROJECT_ROOT)/bin% +# $(PROJECT_ROOT)/obj% +# $(PROJECT_ROOT)/%.xcodeproj +# +# Note: Leave a leading space when adding list items with the += operator +################################################################################ +# PROJECT_EXCLUSIONS = + +################################################################################ +# PROJECT LINKER FLAGS +# These flags will be sent to the linker when compiling the executable. +# +# (default) PROJECT_LDFLAGS = -Wl,-rpath=./libs +# +# Note: Leave a leading space when adding list items with the += operator +# +# Currently, shared libraries that are needed are copied to the +# $(PROJECT_ROOT)/bin/libs directory. The following LDFLAGS tell the linker to +# add a runtime path to search for those shared libraries, since they aren't +# incorporated directly into the final executable application binary. +################################################################################ +# PROJECT_LDFLAGS=-Wl,-rpath=./libs + +################################################################################ +# PROJECT DEFINES +# Create a space-delimited list of DEFINES. The list will be converted into +# CFLAGS with the "-D" flag later in the makefile. +# +# (default) PROJECT_DEFINES = (blank) +# +# Note: Leave a leading space when adding list items with the += operator +################################################################################ +# PROJECT_DEFINES = + +################################################################################ +# PROJECT CFLAGS +# This is a list of fully qualified CFLAGS required when compiling for this +# project. These CFLAGS will be used IN ADDITION TO the PLATFORM_CFLAGS +# defined in your platform specific core configuration files. These flags are +# presented to the compiler BEFORE the PROJECT_OPTIMIZATION_CFLAGS below. +# +# (default) PROJECT_CFLAGS = (blank) +# +# Note: Before adding PROJECT_CFLAGS, note that the PLATFORM_CFLAGS defined in +# your platform specific configuration file will be applied by default and +# further flags here may not be needed. +# +# Note: Leave a leading space when adding list items with the += operator +################################################################################ +# PROJECT_CFLAGS = + +################################################################################ +# PROJECT OPTIMIZATION CFLAGS +# These are lists of CFLAGS that are target-specific. While any flags could +# be conditionally added, they are usually limited to optimization flags. +# These flags are added BEFORE the PROJECT_CFLAGS. +# +# PROJECT_OPTIMIZATION_CFLAGS_RELEASE flags are only applied to RELEASE targets. +# +# (default) PROJECT_OPTIMIZATION_CFLAGS_RELEASE = (blank) +# +# PROJECT_OPTIMIZATION_CFLAGS_DEBUG flags are only applied to DEBUG targets. +# +# (default) PROJECT_OPTIMIZATION_CFLAGS_DEBUG = (blank) +# +# Note: Before adding PROJECT_OPTIMIZATION_CFLAGS, please note that the +# PLATFORM_OPTIMIZATION_CFLAGS defined in your platform specific configuration +# file will be applied by default and further optimization flags here may not +# be needed. +# +# Note: Leave a leading space when adding list items with the += operator +################################################################################ +# PROJECT_OPTIMIZATION_CFLAGS_RELEASE = +# PROJECT_OPTIMIZATION_CFLAGS_DEBUG = + +################################################################################ +# PROJECT COMPILERS +# Custom compilers can be set for CC and CXX +# (default) PROJECT_CXX = (blank) +# (default) PROJECT_CC = (blank) +# Note: Leave a leading space when adding list items with the += operator +################################################################################ +# PROJECT_CXX = +# PROJECT_CC = diff --git a/zoneb/icon.rc b/zoneb/icon.rc new file mode 100644 index 0000000..7e26eb3 --- /dev/null +++ b/zoneb/icon.rc @@ -0,0 +1,8 @@ +// Icon Resource Definition +#define MAIN_ICON 102 + +#if defined(_DEBUG) +MAIN_ICON ICON "icon_debug.ico" +#else +MAIN_ICON ICON "icon.ico" +#endif diff --git a/zoneb/src/main.cpp b/zoneb/src/main.cpp new file mode 100644 index 0000000..f74d4cf --- /dev/null +++ b/zoneb/src/main.cpp @@ -0,0 +1,11 @@ +#include "ofApp.h" + +int main() +{ + ofGLFWWindowSettings settings; + settings.setGLVersion(3, 2); + settings.setSize(1280, 720); + ofCreateWindow(settings); + + ofRunApp(new ofApp()); +} diff --git a/zoneb/src/ofApp.cpp b/zoneb/src/ofApp.cpp new file mode 100644 index 0000000..27235c4 --- /dev/null +++ b/zoneb/src/ofApp.cpp @@ -0,0 +1,164 @@ +#include "ofApp.h" + +//-------------------------------------------------------------- +void ofApp::setup() +{ + //ofSetLogLevel(OF_LOG_VERBOSE); + + ofLogNotice(__FUNCTION__) << "Found " << ofxAzureKinect::Device::getInstalledCount() << " installed devices."; + + // Open Kinect. + if (this->kinectDevice.open()) + { + auto kinectSettings = ofxAzureKinect::DeviceSettings(); + kinectSettings.updateIr = false; + kinectSettings.updateColor = true; + kinectSettings.colorResolution = K4A_COLOR_RESOLUTION_1080P; + kinectSettings.updateVbo = false; + this->kinectDevice.startCameras(kinectSettings); + } + + // Load shader. + auto shaderSettings = ofShaderSettings(); + shaderSettings.shaderFiles[GL_VERTEX_SHADER] = "shaders/render.vert"; + shaderSettings.shaderFiles[GL_GEOMETRY_SHADER] = "shaders/render.geom"; + shaderSettings.shaderFiles[GL_FRAGMENT_SHADER] = "shaders/render.frag"; + shaderSettings.bindDefaults = true; + if (this->shader.setup(shaderSettings)) + { + ofLogNotice(__FUNCTION__) << "Success loading shader!"; + } + + // Setup vbo. + std::vector verts(1); + this->vbo.setVertexData(verts.data(), verts.size(), GL_STATIC_DRAW); + + this->pointSize = 1.0f; + this->useColorSpace = false; +} + +//-------------------------------------------------------------- +void ofApp::exit() +{ + this->kinectDevice.close(); +} + +//-------------------------------------------------------------- +void ofApp::update() +{ +} + +//-------------------------------------------------------------- +void ofApp::draw() +{ + ofBackground(0); + + if (this->kinectDevice.isStreaming()) + { + this->cam.begin(); + ofEnableDepthTest(); + + ofDrawAxis(100.0f); + + ofPushMatrix(); + ofRotateXDeg(180); + + this->shader.begin(); + this->shader.setUniform1f("uSpriteSize", this->pointSize); + + int numPoints; + + if (this->useColorSpace) + { + this->shader.setUniformTexture("uDepthTex", this->kinectDevice.getDepthInColorTex(), 1); + this->shader.setUniformTexture("uWorldTex", this->kinectDevice.getColorToWorldTex(), 2); + this->shader.setUniformTexture("uColorTex", this->kinectDevice.getColorTex(), 3); + this->shader.setUniform2i("uFrameSize", this->kinectDevice.getColorTex().getWidth(), this->kinectDevice.getColorTex().getHeight()); + + numPoints = this->kinectDevice.getColorTex().getWidth() * this->kinectDevice.getColorTex().getHeight(); + } + else + { + this->shader.setUniformTexture("uDepthTex", this->kinectDevice.getDepthTex(), 1); + this->shader.setUniformTexture("uWorldTex", this->kinectDevice.getDepthToWorldTex(), 2); + this->shader.setUniformTexture("uColorTex", this->kinectDevice.getColorInDepthTex(), 3); + this->shader.setUniform2i("uFrameSize", this->kinectDevice.getDepthTex().getWidth(), this->kinectDevice.getDepthTex().getHeight()); + + numPoints = this->kinectDevice.getDepthTex().getWidth() * this->kinectDevice.getDepthTex().getHeight(); + } + + this->vbo.drawInstanced(GL_POINTS, 0, 1, numPoints); + this->shader.end(); + ofPopMatrix(); + this->cam.end(); + } + + ofDrawBitmapStringHighlight(ofToString(ofGetFrameRate(), 2) + " FPS", 10, 20); +} + +//-------------------------------------------------------------- +void ofApp::keyPressed(int key) +{ + if (key == OF_KEY_UP) + { + this->pointSize *= 2; + } + else if (key == OF_KEY_DOWN) + { + this->pointSize /= 2; + } + else if (key == ' ') + { + this->useColorSpace ^= 1; + } +} + +//-------------------------------------------------------------- +void ofApp::keyReleased(int key) +{ +} + +//-------------------------------------------------------------- +void ofApp::mouseMoved(int x, int y) +{ +} + +//-------------------------------------------------------------- +void ofApp::mouseDragged(int x, int y, int button) +{ +} + +//-------------------------------------------------------------- +void ofApp::mousePressed(int x, int y, int button) +{ +} + +//-------------------------------------------------------------- +void ofApp::mouseReleased(int x, int y, int button) +{ +} + +//-------------------------------------------------------------- +void ofApp::mouseEntered(int x, int y) +{ +} + +//-------------------------------------------------------------- +void ofApp::mouseExited(int x, int y) +{ +} + +//-------------------------------------------------------------- +void ofApp::windowResized(int w, int h) +{ +} + +//-------------------------------------------------------------- +void ofApp::gotMessage(ofMessage msg) +{ +} + +//-------------------------------------------------------------- +void ofApp::dragEvent(ofDragInfo dragInfo) +{ +} diff --git a/zoneb/src/ofApp.h b/zoneb/src/ofApp.h new file mode 100644 index 0000000..ddcc4e1 --- /dev/null +++ b/zoneb/src/ofApp.h @@ -0,0 +1,37 @@ +#pragma once + +#include "ofMain.h" + +#include "ofxAzureKinect.h" + +class ofApp + : public ofBaseApp +{ +public: + void setup(); + void exit(); + + void update(); + void draw(); + + void keyPressed(int key); + void keyReleased(int key); + void mouseMoved(int x, int y); + void mouseDragged(int x, int y, int button); + void mousePressed(int x, int y, int button); + void mouseReleased(int x, int y, int button); + void mouseEntered(int x, int y); + void mouseExited(int x, int y); + void windowResized(int w, int h); + void dragEvent(ofDragInfo dragInfo); + void gotMessage(ofMessage msg); + +private: + ofxAzureKinect::Device kinectDevice; + ofEasyCam cam; + ofVbo vbo; + ofShader shader; + + float pointSize; + bool useColorSpace; +};