From 2351068572ce5d06a023a3ff100adc3d09b39c59 Mon Sep 17 00:00:00 2001 From: micuat Date: Sun, 18 Oct 2020 18:12:23 +0200 Subject: [PATCH] copied body example --- zoneb2/addons.make | 1 + zoneb2/bin/data/shaders/render.frag | 17 ++ zoneb2/bin/data/shaders/render.vert | 57 ++++++ zoneb2/config.make | 141 +++++++++++++ zoneb2/icon.rc | 8 + zoneb2/src/main.cpp | 11 + zoneb2/src/ofApp.cpp | 303 ++++++++++++++++++++++++++++ zoneb2/src/ofApp.h | 38 ++++ 8 files changed, 576 insertions(+) create mode 100644 zoneb2/addons.make create mode 100644 zoneb2/bin/data/shaders/render.frag create mode 100644 zoneb2/bin/data/shaders/render.vert create mode 100644 zoneb2/config.make create mode 100644 zoneb2/icon.rc create mode 100644 zoneb2/src/main.cpp create mode 100644 zoneb2/src/ofApp.cpp create mode 100644 zoneb2/src/ofApp.h diff --git a/zoneb2/addons.make b/zoneb2/addons.make new file mode 100644 index 0000000..b155bef --- /dev/null +++ b/zoneb2/addons.make @@ -0,0 +1 @@ +ofxAzureKinect diff --git a/zoneb2/bin/data/shaders/render.frag b/zoneb2/bin/data/shaders/render.frag new file mode 100644 index 0000000..1012330 --- /dev/null +++ b/zoneb2/bin/data/shaders/render.frag @@ -0,0 +1,17 @@ +#version 150 + +// Custom attributes. + +in vec4 vColor; + +out vec4 fragColor; + +void main() +{ + if (vColor.a == 0) + { + discard; + } + + fragColor = vColor; +} \ No newline at end of file diff --git a/zoneb2/bin/data/shaders/render.vert b/zoneb2/bin/data/shaders/render.vert new file mode 100644 index 0000000..7660ac9 --- /dev/null +++ b/zoneb2/bin/data/shaders/render.vert @@ -0,0 +1,57 @@ +#version 150 + +// OF built-in attributes. + +uniform mat4 modelViewProjectionMatrix; + +// Custom attributes. + +#define BODY_INDEX_MAP_BACKGROUND 255 + +const vec4[6] COLORS = vec4[] +( + vec4(211 / 255.0, 248 / 255.0, 226 / 255.0, 1.0), + vec4(228 / 255.0, 193 / 255.0, 249 / 255.0, 1.0), + vec4(237 / 255.0, 231 / 255.0, 177 / 255.0, 1.0), + vec4(246 / 255.0, 148 / 255.0, 193 / 255.0, 1.0), + vec4(169 / 255.0, 222 / 255.0, 249 / 255.0, 1.0), + vec4(255 / 255.0, 135 / 255.0, 111 / 255.0, 1.0) +); + +uniform sampler2DRect uDepthTex; // Sampler for the depth space data +uniform sampler2DRect uBodyIndexTex; // Sampler for the body index data +uniform sampler2DRect uWorldTex; // Transformation from kinect depth space to kinect world space + +uniform ivec2 uFrameSize; + +uniform int[6] uBodyIDs; + +out vec4 vColor; + +void main() +{ + vec2 texCoord = vec2(gl_InstanceID % uFrameSize.x, gl_InstanceID / uFrameSize.x); + + float depth = texture(uDepthTex, texCoord).x; + int bodyIndex = int(texture(uBodyIndexTex, texCoord).x * 255); + vec4 ray = texture(uWorldTex, texCoord); + + if (depth != 0 && + bodyIndex != BODY_INDEX_MAP_BACKGROUND && + ray.x != 0 && ray.y != 0) + { + int bodyID = uBodyIDs[bodyIndex]; + vColor = COLORS[bodyID % 6]; + } + else + { + vColor = vec4(0.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; + + gl_Position = modelViewProjectionMatrix * posWorld; +} \ No newline at end of file diff --git a/zoneb2/config.make b/zoneb2/config.make new file mode 100644 index 0000000..836fce7 --- /dev/null +++ b/zoneb2/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/zoneb2/icon.rc b/zoneb2/icon.rc new file mode 100644 index 0000000..7e26eb3 --- /dev/null +++ b/zoneb2/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/zoneb2/src/main.cpp b/zoneb2/src/main.cpp new file mode 100644 index 0000000..f74d4cf --- /dev/null +++ b/zoneb2/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/zoneb2/src/ofApp.cpp b/zoneb2/src/ofApp.cpp new file mode 100644 index 0000000..cb42d6f --- /dev/null +++ b/zoneb2/src/ofApp.cpp @@ -0,0 +1,303 @@ +#include "ofApp.h" + +class Particle +{ +public: + ofVec3f p; + int id; + Particle() + { + p = ofVec3f(ofRandom(-100, 100), ofRandom(-100, 100), ofRandom(-100, 100)); + id = ofRandom(K4ABT_JOINT_COUNT); + } +}; + +std::vector particles; + +//-------------------------------------------------------------- +void ofApp::setup() +{ + for (int i = 0; i < 10000; i++) + { + particles.push_back(Particle()); + } + + //ofSetLogLevel(OF_LOG_VERBOSE); + + ofLogNotice(__FUNCTION__) << "Found " << ofxAzureKinect::Device::getInstalledCount() << " installed devices."; + + auto deviceSettings = ofxAzureKinect::DeviceSettings(); + deviceSettings.syncImages = false; + deviceSettings.depthMode = K4A_DEPTH_MODE_NFOV_UNBINNED; + deviceSettings.updateIr = false; + deviceSettings.updateColor = false; + //deviceSettings.colorResolution = K4A_COLOR_RESOLUTION_1080P; + deviceSettings.updateWorld = true; + deviceSettings.updateVbo = false; + auto bodyTrackingSettings = ofxAzureKinect::BodyTrackingSettings(); + //bodyTrackingSettings.processingMode = K4ABT_TRACKER_PROCESSING_MODE_CPU; + bodyTrackingSettings.updateBodies = true; + if (kinectDevice.open()) + { + kinectDevice.startCameras(deviceSettings, bodyTrackingSettings); + } + + // Load shader. + auto shaderSettings = ofShaderSettings(); + shaderSettings.shaderFiles[GL_VERTEX_SHADER] = "shaders/render.vert"; + shaderSettings.shaderFiles[GL_FRAGMENT_SHADER] = "shaders/render.frag"; + shaderSettings.intDefines["BODY_INDEX_MAP_BACKGROUND"] = K4ABT_BODY_INDEX_MAP_BACKGROUND; + shaderSettings.bindDefaults = true; + if (shader.setup(shaderSettings)) + { + ofLogNotice(__FUNCTION__) << "Success loading shader!"; + } + + // Setup vbo. + std::vector verts(1); + pointsVbo.setVertexData(verts.data(), verts.size(), GL_STATIC_DRAW); +} + +//-------------------------------------------------------------- +void ofApp::exit() +{ + kinectDevice.close(); +} + +//-------------------------------------------------------------- +void ofApp::update() +{ +} + +//-------------------------------------------------------------- +void ofApp::draw() +{ + ofBackground(0); + + if (kinectDevice.isStreaming()) + { + kinectDevice.getBodyIndexTex().draw(0, 0, 360, 360); + } + + camera.begin(); + { + ofPushMatrix(); + { + ofRotateXDeg(180); + + ofEnableDepthTest(); + + constexpr int kMaxBodies = 6; + int bodyIDs[kMaxBodies]; + int i = 0; + while (i < kinectDevice.getNumBodies()) + { + bodyIDs[i] = kinectDevice.getBodyIDs()[i]; + ++i; + } + while (i < kMaxBodies) + { + bodyIDs[i] = 0; + ++i; + } + + shader.begin(); + { + shader.setUniformTexture("uDepthTex", kinectDevice.getDepthTex(), 1); + shader.setUniformTexture("uBodyIndexTex", kinectDevice.getBodyIndexTex(), 2); + shader.setUniformTexture("uWorldTex", kinectDevice.getDepthToWorldTex(), 3); + shader.setUniform2i("uFrameSize", kinectDevice.getDepthTex().getWidth(), kinectDevice.getDepthTex().getHeight()); + shader.setUniform1iv("uBodyIDs", bodyIDs, kMaxBodies); + + int numPoints = kinectDevice.getDepthTex().getWidth() * kinectDevice.getDepthTex().getHeight(); + pointsVbo.drawInstanced(GL_POINTS, 0, 1, numPoints); + } + shader.end(); + + ofDisableDepthTest(); + + auto &bodySkeletons = kinectDevice.getBodySkeletons(); + for (auto &skeleton : bodySkeletons) + { + // Draw joints. + for (int i = 0; i < K4ABT_JOINT_COUNT; ++i) + { + auto joint = skeleton.joints[i]; + ofPushMatrix(); + { + glm::mat4 transform = glm::translate(toGlm(joint.position)) * glm::toMat4(toGlm(joint.orientation)); + ofMultMatrix(transform); + + ofDrawAxis(50.0f); + } + ofPopMatrix(); + } + + // Draw connections. + skeletonMesh.setMode(OF_PRIMITIVE_LINES); + auto &vertices = skeletonMesh.getVertices(); + vertices.resize(50); + int vdx = 0; + + // Spine. + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_PELVIS].position); + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_SPINE_NAVEL].position); + + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_SPINE_NAVEL].position); + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_SPINE_CHEST].position); + + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_SPINE_CHEST].position); + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_NECK].position); + + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_NECK].position); + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_HEAD].position); + + // Head. + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_HEAD].position); + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_NOSE].position); + + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_NOSE].position); + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_EYE_LEFT].position); + + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_EYE_LEFT].position); + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_EAR_LEFT].position); + + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_NOSE].position); + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_EYE_RIGHT].position); + + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_EYE_RIGHT].position); + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_EAR_RIGHT].position); + + // Left Leg. + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_PELVIS].position); + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_HIP_LEFT].position); + + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_HIP_LEFT].position); + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_KNEE_LEFT].position); + + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_KNEE_LEFT].position); + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_ANKLE_LEFT].position); + + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_ANKLE_LEFT].position); + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_FOOT_LEFT].position); + + // Right leg. + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_PELVIS].position); + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_HIP_RIGHT].position); + + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_HIP_RIGHT].position); + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_KNEE_RIGHT].position); + + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_KNEE_RIGHT].position); + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_ANKLE_RIGHT].position); + + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_ANKLE_RIGHT].position); + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_FOOT_RIGHT].position); + + // Left arm. + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_NECK].position); + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_CLAVICLE_LEFT].position); + + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_CLAVICLE_LEFT].position); + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_SHOULDER_LEFT].position); + + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_SHOULDER_LEFT].position); + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_ELBOW_LEFT].position); + + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_ELBOW_LEFT].position); + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_WRIST_LEFT].position); + + // Right arm. + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_NECK].position); + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_CLAVICLE_RIGHT].position); + + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_CLAVICLE_RIGHT].position); + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_SHOULDER_RIGHT].position); + + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_SHOULDER_RIGHT].position); + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_ELBOW_RIGHT].position); + + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_ELBOW_RIGHT].position); + vertices[vdx++] = toGlm(skeleton.joints[K4ABT_JOINT_WRIST_RIGHT].position); + + skeletonMesh.draw(); + + for (int i = 0; i < particles.size(); i++) + { + auto& pt = particles.at(i); + pt.p.interpolate(vertices[pt.id], 0.01); + } + } + for (int i = 0; i < particles.size(); i++) + { + ofDrawCircle(particles.at(i).p, 1); + } + } + ofPopMatrix(); + } + camera.end(); + + std::ostringstream oss; + oss << ofToString(ofGetFrameRate(), 2) + " FPS" << std::endl; + oss << "Joint Smoothing: " << kinectDevice.jointSmoothing; + ofDrawBitmapStringHighlight(oss.str(), 10, 20); +} + +//-------------------------------------------------------------- +void ofApp::keyPressed(int key) +{ +} + +//-------------------------------------------------------------- +void ofApp::keyReleased(int key) +{ +} + +//-------------------------------------------------------------- +void ofApp::mouseMoved(int x, int y) +{ +} + +//-------------------------------------------------------------- +void ofApp::mouseDragged(int x, int y, int button) +{ + if (button == 1) + { + kinectDevice.jointSmoothing = ofMap(x, 0, ofGetWidth(), 0.0f, 1.0f, true); + } +} + +//-------------------------------------------------------------- +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/zoneb2/src/ofApp.h b/zoneb2/src/ofApp.h new file mode 100644 index 0000000..4d5e0c2 --- /dev/null +++ b/zoneb2/src/ofApp.h @@ -0,0 +1,38 @@ +#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 camera; + + ofVbo pointsVbo; + ofShader shader; + + ofVboMesh skeletonMesh; +};