Skip to content

Instantly share code, notes, and snippets.

@Furkanzmc
Last active January 2, 2016 19:49
Show Gist options
  • Save Furkanzmc/8352687 to your computer and use it in GitHub Desktop.
Save Furkanzmc/8352687 to your computer and use it in GitHub Desktop.
Go through all Kinects...
#include <iostream>
#include <Windows.h>
#include <NuiApi.h>
int main() {
INuiSensor *nuiSensor;
HRESULT hr;
HANDLE mHandleSkeletonStream;
HANDLE mHandleNextSkeletonEvent;
int sensorCount = 0;
hr = NuiGetSensorCount(&sensorCount);
if (FAILED(hr))
return hr;
// Look at each Kinect sensor
for (int i = 0; i < sensorCount; ++i) {
// Create the sensor so we can check status, if we can't create it, move on to the next
hr = NuiCreateSensorByIndex(i, &nuiSensor);
if (FAILED(hr))
continue;
// Get the status of the sensor, and if connected, then we can initialize it
hr = nuiSensor->NuiStatus();
if (hr == S_OK) {
break;
}
// This sensor wasn't OK, so release it since we're not using it
nuiSensor->Release();
}
if (nuiSensor != nullptr) {
// Initialize the Kinect and specify that we'll be using color and skeleton tracking
hr = nuiSensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_SKELETON);
if (SUCCEEDED(hr)) {
// Create an event that will be signaled when skeleton data is available
mHandleNextSkeletonEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
// Open a skeleton stream to receive skeleton data
hr = nuiSensor->NuiSkeletonTrackingEnable(mHandleNextSkeletonEvent, 0);
}
}
if (NULL == nuiSensor || FAILED(hr)) {
//No Kinect found
return E_FAIL;
}
while (true) {
if (nuiSensor == nullptr)
return -1;
// Wait for 0ms, just quickly test if it is time to process a skeleton
if (WaitForSingleObject(mHandleNextSkeletonEvent, 0) == WAIT_OBJECT_0) {
NUI_SKELETON_FRAME skeletonFrame = {0};
HRESULT hr = nuiSensor->NuiSkeletonGetNextFrame(0, &skeletonFrame);
if (FAILED(hr))
return hr;
// smooth out the skeleton data
nuiSensor->NuiTransformSmooth(&skeletonFrame, NULL);
for (int i = 0 ; i < NUI_SKELETON_COUNT; ++i)
{
NUI_SKELETON_TRACKING_STATE trackingState = skeletonFrame.SkeletonData[i].eTrackingState;
if (trackingState == NUI_SKELETON_TRACKED)
{
// We're tracking the skeleton
NUI_SKELETON_DATA *skeletonData = &skeletonFrame.SkeletonData[i];
std::cout << skeletonData->SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].y << std::endl;
}
else if (trackingState == NUI_SKELETON_POSITION_ONLY)
{
// we've only received the center point of the skeleton
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment