Ejemplo n.º 1
0
/// <summary>
/// Create the first connected Kinect found 
/// </summary>
/// <returns>indicates success or failure</returns>
HRESULT CSkeletonBasics::CreateFirstConnected()
{
    INuiSensor * pNuiSensor;

    int iSensorCount = 0;
    HRESULT hr = NuiGetSensorCount(&iSensorCount);
    if (FAILED(hr))
    {
        return hr;
    }

    // Look at each Kinect sensor
    for (int i = 0; i < iSensorCount; ++i)
    {
        // Create the sensor so we can check status, if we can't create it, move on to the next
        hr = NuiCreateSensorByIndex(i, &pNuiSensor);
        if (FAILED(hr))
        {
            continue;
        }

        // Get the status of the sensor, and if connected, then we can initialize it
        hr = pNuiSensor->NuiStatus();
        if (S_OK == hr)
        {
            m_pNuiSensor = pNuiSensor;
            break;
        }

        // This sensor wasn't OK, so release it since we're not using it
        pNuiSensor->Release();
    }

    if (NULL != m_pNuiSensor)
    {
        // Initialize the Kinect and specify that we'll be using skeleton
        hr = m_pNuiSensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_SKELETON); 
        if (SUCCEEDED(hr))
        {
            // Create an event that will be signaled when skeleton data is available
            m_hNextSkeletonEvent = CreateEventW(NULL, TRUE, FALSE, NULL);

            // Open a skeleton stream to receive skeleton data
            hr = m_pNuiSensor->NuiSkeletonTrackingEnable(m_hNextSkeletonEvent, 0); 
        }
    }

    if (NULL == m_pNuiSensor || FAILED(hr))
    {
        SetStatusMessage(L"No ready Kinect found!");
        return E_FAIL;
    }

    return hr;
}
Ejemplo n.º 2
0
  void createInstance()
  {
    // 接続されているKinectの数を取得する
    int count = 0;
    ERROR_CHECK( ::NuiGetSensorCount( &count ) );
    if ( count == 0 ) {
      throw std::runtime_error( "Kinect を接続してください" );
    }

    // 最初のKinectのインスタンスを作成する
    ERROR_CHECK( ::NuiCreateSensorByIndex( 0, &kinect ) );

    // Kinectの状態を取得する
    HRESULT status = kinect->NuiStatus();
    if ( status != S_OK ) {
      throw std::runtime_error( "Kinect が利用可能ではありません" );
    }
  }
Ejemplo n.º 3
0
HRESULT KinectManager::initialize()
{
	INuiSensor * nui;
	int nuiCount = 0;
	HRESULT hr;


	NuiSetDeviceStatusCallback(OnSensorStatusChanged, NULL);

	hr = NuiGetSensorCount(&nuiCount);
	if ( FAILED(hr))
	{
		return hr;
	}

	// Look at each kinect sensor
	for (int i = 0; i < nuiCount; i++)
	{
		// Create the sensor so we can check status, if we can't create it, move on.
		hr = NuiCreateSensorByIndex(i, &nui);
		if (FAILED(hr))
		{
			continue;
		}

		// Get the status of the sensor, and if connected, then we can initialize it.
		hr = nui->NuiStatus();

		if (S_OK == hr)
		{
			nuiList.push_front(nui);
		}

		// This sensor was not okay, so we release it (into the wild!) since we're not using it.
		nui->Release();
	}

	return hr;


}
Ejemplo n.º 4
0
INuiSensor*
GetNuiPointer()
{
    int sensorCount = 0;
    INuiSensor *nuiSensor = NULL;
    HRESULT hr = NuiGetSensorCount(&sensorCount);

	if (FAILED(hr))
	{
		throw "Failed to get NUI sensor count";
	}
	

    // 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 (S_OK == hr) break;

        // This sensor wasn't OK, so release it since we're not using it
        nuiSensor->Release();
    }

    // If we couldn't get an instance, return failure
	if (NULL == nuiSensor)
	{
		throw "Failed to get NUI Sensor instance";
	}

    // Initialize the Kinect and specify that we'll be using depth
    hr = nuiSensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_COLOR
            | NUI_INITIALIZE_FLAG_USES_DEPTH_AND_PLAYER_INDEX);

    if (FAILED(hr)) return NULL;
    else return nuiSensor;
}
Ejemplo n.º 5
0
/// <summary>
/// Enumerate and construct all the sensors when the app starts up
/// </summary>
void CMainWindow::EnumerateSensors()
{
    int iCount = 0;
    HRESULT hr = NuiGetSensorCount(&iCount);
    if (FAILED(hr))
    {
        return;
    }

    for (int i = 0; i < iCount; ++i)
    {
        INuiSensor* pNuiSensor = nullptr;

        if (SUCCEEDED(NuiCreateSensorByIndex(i, &pNuiSensor)))
        {
            UpdateMainWindow(pNuiSensor->NuiDeviceConnectionId(), pNuiSensor->NuiStatus());
        }

        SafeRelease(pNuiSensor);
    }
}
Ejemplo n.º 6
0
/// <summary>
/// Funcion que crea la primera conexion de la aplicacion con Kinect. Comprueba e inicializa los sensores.
/// </summary>
/// <returns>Devuelve un HRESULT con las banderas de los flags inicializados y los que no.</returns>
HRESULT CreateFirstConnected() {
    INuiSensor * pNuiSensor;
    HRESULT hr;

    int iSensorCount = 0;
    hr = NuiGetSensorCount(&iSensorCount);
    if (FAILED(hr)) {
        return hr;
    }

    // Look at each Kinect sensor
    for (int i = 0; i < iSensorCount; ++i) {
        // Create the sensor so we can check status, if we can't create it, move on to the next
        hr = NuiCreateSensorByIndex(i, &pNuiSensor);
        if (FAILED(hr)) {
            continue;
        }

        // Get the status of the sensor, and if connected, then we can initialize it
        hr = pNuiSensor->NuiStatus();
        if (S_OK == hr) {
            m_pNuiSensor = pNuiSensor;
            break;
        }

        // This sensor wasn't OK, so release it since we're not using it
        pNuiSensor->Release();
    }

	// Initialize the Kinect and specify that we'll be using depth
	DWORD dwFlags = 0;
	if (m_bProcessColor) dwFlags |= NUI_INITIALIZE_FLAG_USES_COLOR;
	if (m_bProcessDepth) 
		m_bUserDetection ? (dwFlags |= NUI_INITIALIZE_FLAG_USES_DEPTH_AND_PLAYER_INDEX) : (dwFlags |= NUI_INITIALIZE_FLAG_USES_DEPTH);
	if (m_bProcessSkeleton)
		dwFlags |= NUI_INITIALIZE_FLAG_USES_SKELETON;

    hr = m_pNuiSensor->NuiInitialize(dwFlags);
    return hr;
}
Ejemplo n.º 7
0
/// <summary>
/// Create the first connected Kinect found 
/// </summary>
/// <returns>indicates success or failure</returns>
HRESULT CColorBasics::CreateFirstConnected()
{
    INuiSensor * pNuiSensor;
    HRESULT hr;

    int iSensorCount = 0;
    hr = NuiGetSensorCount(&iSensorCount);
    if (FAILED(hr))
    {
        return hr;
    }

    // Look at each Kinect sensor
    for (int i = 0; i < iSensorCount; ++i)
    {
        // Create the sensor so we can check status, if we can't create it, move on to the next
        hr = NuiCreateSensorByIndex(i, &pNuiSensor);
        if (FAILED(hr))
        {
            continue;
        }

        // Get the status of the sensor, and if connected, then we can initialize it
        hr = pNuiSensor->NuiStatus();
        if (S_OK == hr)
        {
            m_pNuiSensor = pNuiSensor;
            break;
        }

        // This sensor wasn't OK, so release it since we're not using it
        pNuiSensor->Release();
    }

    if (NULL != m_pNuiSensor)
    {
        // Initialize the Kinect and specify that we'll be using color
        hr = m_pNuiSensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_COLOR | NUI_INITIALIZE_FLAG_USES_SKELETON); 
        if (SUCCEEDED(hr))
        {
            // Create an event that will be signaled when color data is available
            m_hNextColorFrameEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

            // Create an event that will be signaled when skeleton data is available
            m_hNextSkeletonEvent = CreateEventW(NULL, TRUE, FALSE, NULL);

            // Open a color image stream to receive color frames
			//TODO: Check image format https://msdn.microsoft.com/en-us/library/nuiimagecamera.nui_image_type.aspx (we now have RGB) 
            hr = m_pNuiSensor->NuiImageStreamOpen(
                NUI_IMAGE_TYPE_COLOR,
                NUI_IMAGE_RESOLUTION_640x480,
                0,
                2,
                m_hNextColorFrameEvent,
                &m_pColorStreamHandle);

			if(!FAILED(hr)){
				// Open a skeleton stream to receive skeleton data
				hr = m_pNuiSensor->NuiSkeletonTrackingEnable(m_hNextSkeletonEvent, 0);
			}

        }
    }

    if (NULL == m_pNuiSensor || FAILED(hr))
    {
        SetStatusMessage(L"No ready Kinect found!");
        return E_FAIL;
    }

    return hr;
}
Ejemplo n.º 8
0
KinectImageSource::KinectImageSource(int device) : ImageSource(lexical_cast<string>(device)), frame() {

#ifdef WITH_MSKINECT_SDK
    m_pColorStreamHandle = INVALID_HANDLE_VALUE;
    /// Create the first connected Kinect found
    INuiSensor * pNuiSensor;
    HRESULT hr;

    int iSensorCount = 0;
    hr = NuiGetSensorCount(&iSensorCount);
    if (FAILED(hr))
    {
        std::cout << "Error getting sensor count. No Kinect plugged in?" << hr << std::endl;
    }

    // Look at each Kinect sensor
    for (int i = 0; i < iSensorCount; ++i)
    {
        // Create the sensor so we can check status, if we can't create it, move on to the next
        hr = NuiCreateSensorByIndex(i, &pNuiSensor);
        if (FAILED(hr))
        {
            continue;
        }

        // Get the status of the sensor, and if connected, then we can initialize it
        hr = pNuiSensor->NuiStatus();
        if (S_OK == hr)
        {
            m_pNuiSensor = pNuiSensor;
            break;
        }

        // This sensor wasn't OK, so release it since we're not using it
        pNuiSensor->Release();
    }

    if (NULL != m_pNuiSensor)
    {
        // Initialize the Kinect and specify that we'll be using color
        hr = m_pNuiSensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_COLOR);
        if (SUCCEEDED(hr))
        {
            // Create an event that will be signaled when color data is available
            //m_hNextColorFrameEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

            // Open a color image stream to receive color frames
            hr = m_pNuiSensor->NuiImageStreamOpen(
                     NUI_IMAGE_TYPE_COLOR,
                     NUI_IMAGE_RESOLUTION_640x480,
                     0,
                     2,
                     NULL,
                     &m_pColorStreamHandle);
        }
    }

    if (NULL == m_pNuiSensor || FAILED(hr))
    {
        std::cout << "No ready Kinect found!" << std::endl;
    }

    //std::cout << "hr: " << hr << std::endl;

#else
    std::cerr << "Error! This is the Microsoft Kinect SDK interface and not available under Linux." << std::endl;
#endif

}
int Init()
{

	//Make sure our image types are the same as the OpenNI image types.
	//assert(sizeof(XnRGB24Pixel) == sizeof(ColorPixel));
	//assert(sizeof(XnDepthPixel) == sizeof(DepthPixel));
	//assert(sizeof(XnStatus) == sizeof(int));
	
	INuiSensor* nuiSensorPtr;
	int ret;

	// Get number of sensors
	int sensorCount = 0;
	ret = NuiGetSensorCount(&sensorCount);
	if(ret < 0)
	{
		return -1;
	}

	// Connect to first sensor
	for(int i=0; i<sensorCount; i++)
	{
		// Create sensor so we can check the status
		ret = NuiCreateSensorByIndex(i, &nuiSensorPtr);
		if(ret < 0)
			continue;

		// Get the status of the sensor
		ret = nuiSensorPtr->NuiStatus();
		if(ret < 0)
		{
			(void)nuiSensorPtr->Release();
			nuiSensorPtr = NULL;
			continue;
		}
		
		sensor = nuiSensorPtr;
	}

	// Make sure we have a sensor
	if(sensor == NULL)
	{
		return -1;
	}
	
	// Initialize Kinect
	ret = sensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_COLOR | NUI_INITIALIZE_FLAG_USES_DEPTH);
	if(ret < 0)
	{
		return -1;
	}
	
	// Create an event that will be signaled when color data is available
    colorImageReadyEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

	// Create an event that will be signaled when depth data is available
    depthImageReadyEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

	// Open a color image stream to receive color frames
	ret = sensor->NuiImageStreamOpen(NUI_IMAGE_TYPE_COLOR, NUI_IMAGE_RESOLUTION_640x480, 0, 2, colorImageReadyEvent, &colorStream);
	if(ret < 0)
	{
		return -1;
	}

	// Open a depth image stream to receive depth frames
    ret = sensor->NuiImageStreamOpen(NUI_IMAGE_TYPE_DEPTH, NUI_IMAGE_RESOLUTION_640x480, 0, 2, depthImageReadyEvent, &depthStream);
	if(ret < 0)
	{
		return -1;
	}

	return 0;
}
Ejemplo n.º 10
0
HRESULT KinectSensor::createFirstConnected()
{
	INuiSensor* pNuiSensor = NULL;
	HRESULT hr = S_OK;

	int iSensorCount = 0;
	hr = NuiGetSensorCount(&iSensorCount);
	if (FAILED(hr) ) { return hr; }

	// Look at each Kinect sensor
	for (int i = 0; i < iSensorCount; ++i) {
		// Create the sensor so we can check status, if we can't create it, move on to the next
		hr = NuiCreateSensorByIndex(i, &pNuiSensor);
		if (FAILED(hr))	{
			continue;
		}

		// Get the status of the sensor, and if connected, then we can initialize it
		hr = pNuiSensor->NuiStatus();
		if (S_OK == hr)	{
			m_pNuiSensor = pNuiSensor;
			break;
		}

		// This sensor wasn't OK, so release it since we're not using it
		pNuiSensor->Release();
	}

	if (NULL == m_pNuiSensor) {
		return E_FAIL;
	}

	// Initialize the Kinect and specify that we'll be using depth
	//hr = m_pNuiSensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_COLOR | NUI_INITIALIZE_FLAG_USES_DEPTH_AND_PLAYER_INDEX); 
	hr = m_pNuiSensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_COLOR | NUI_INITIALIZE_FLAG_USES_DEPTH); 
	if (FAILED(hr) ) { return hr; }

	// Create an event that will be signaled when depth data is available
	m_hNextDepthFrameEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

	// Open a depth image stream to receive depth frames
	hr = m_pNuiSensor->NuiImageStreamOpen(
		NUI_IMAGE_TYPE_DEPTH,
		//NUI_IMAGE_TYPE_DEPTH_AND_PLAYER_INDEX,
		cDepthResolution,
		(8000 << NUI_IMAGE_PLAYER_INDEX_SHIFT),
		2,
		m_hNextDepthFrameEvent,
		&m_pDepthStreamHandle);
	if (FAILED(hr) ) { return hr; }

	// Create an event that will be signaled when color data is available
	m_hNextColorFrameEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

	// Open a color image stream to receive color frames
	hr = m_pNuiSensor->NuiImageStreamOpen(
		NUI_IMAGE_TYPE_COLOR,
		cColorResolution,
		0,
		2,
		m_hNextColorFrameEvent,
		&m_pColorStreamHandle );
	if (FAILED(hr) ) { return hr; }

	INuiColorCameraSettings* colorCameraSettings;
	HRESULT hrFlag = m_pNuiSensor->NuiGetColorCameraSettings(&colorCameraSettings);

	if (hr != E_NUI_HARDWARE_FEATURE_UNAVAILABLE)
	{
		m_kinect4Windows = true;
	}

	//TODO MATTHIAS: does this function have to be called every frame?

	USHORT* test = new USHORT[getDepthWidth()*getDepthHeight()];
	// Get offset x, y coordinates for color in depth space
	// This will allow us to later compensate for the differences in location, angle, etc between the depth and color cameras
	m_pNuiSensor->NuiImageGetColorPixelCoordinateFrameFromDepthPixelFrameAtResolution(
		cColorResolution,
		cDepthResolution,
		getDepthWidth()*getDepthHeight(),
		test,
		getDepthWidth()*getDepthHeight()*2,
		m_colorCoordinates
		);
	SAFE_DELETE_ARRAY(test);

	// Start with near mode on (if possible)
	m_bNearMode = false;
	if (m_kinect4Windows) {
		toggleNearMode();
	}

	//toggleAutoWhiteBalance();

	return hr;
}
Ejemplo n.º 11
0
int main(void)
{
	//Set the error callback
	glfwSetErrorCallback(error_callback);

	//Initialize GLFW
	if (!glfwInit())
	{
		exit(EXIT_FAILURE);
	}

	//Set the GLFW window creation hints - these are optional
	//glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); //Request a specific OpenGL version
	//glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); //Request a specific OpenGL version
	//glfwWindowHint(GLFW_SAMPLES, 4); //Request 4x antialiasing
	//glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);


	//Create a window and create its OpenGL context
	window = glfwCreateWindow(960, 720, "Test Window", NULL, NULL);

	//If the window couldn't be created
	if (!window)
	{
		fprintf(stderr, "Failed to open GLFW window.\n");
		glfwTerminate();
		//exit(EXIT_FAILURE);
	}

	//This function makes the context of the specified window current on the calling thread. 
	glfwMakeContextCurrent(window);

	//Sets the key callback
	glfwSetKeyCallback(window, key_callback);

	//Initialize GLEW
	GLenum err = glewInit();

	//If GLEW hasn't initialized
	if (err != GLEW_OK)
	{
		fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
		return -1;
	}

	//Set a background color
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	glfwSetCursorPos(window, 1024 / 2, 768 / 2);

	GLuint VertexArrayID;
	glGenVertexArrays(1, &VertexArrayID);
	glBindVertexArray(VertexArrayID);

	// Create and compile our GLSL program from the shaders
	GLuint red = LoadShaders("SimpleTransform.vertexshader", "SingleColorRed.fragmentshader");
	GLuint grid = LoadShaders("SimpleTransform.vertexshader", "SingleColorGrid.fragmentshader");
	glBindFragDataLocation(red, 0, "red");
	glBindFragDataLocation(grid, 1, "grid");
	// Get a handle for our "MVP" uniform
	GLuint MatrixID = glGetUniformLocation(red, "MVP");

	// Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
	glm::mat4 Projection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 1000.0f);
	// Or, for an ortho camera :
	//glm::mat4 Projection = glm::ortho(-10.0f,10.0f,-10.0f,10.0f,0.0f,100.0f); // In world coordinates

	// Camera matrix
	glm::mat4 View = glm::lookAt(
		glm::vec3(4, 3, 3), // Camera is at (4,3,3), in World Space
		glm::vec3(0, 0, 0), // and looks at the origin
		glm::vec3(0, 1, 0)  // Head is up (set to 0,-1,0 to look upside-down)
		);


	static const GLfloat g_vertex_buffer_data[] = {
		-1.0f, -1.0f, 0.0f,
		1.0f, -1.0f, 0.0f,
		0.0f, 1.0f, 0.0f,
	};

	static const GLushort g_element_buffer_data[] = { 0, 1, 2 };

	GLuint vertexbuffer;
	glGenBuffers(1, &vertexbuffer);
	glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
	glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);

	static const GLfloat g_triangle_buffer_data[] = {
		-1.0f, -1.0f, -1.0f,
		1.0f, -1.0f, -1.0f,
		0.0f, 1.0f, -1.0f,
	};

	GLuint triangle;
	glGenBuffers(1, &triangle);
	glBindBuffer(GL_ARRAY_BUFFER, triangle);
	glBufferData(GL_ARRAY_BUFFER, sizeof(g_triangle_buffer_data), g_triangle_buffer_data, GL_STATIC_DRAW);

	// Enable depth test
	glEnable(GL_DEPTH_TEST);
	// Accept fragment if it closer to the camera than the former one
	glDepthFunc(GL_LESS);
	glEnable(GL_CULL_FACE);
	glEnable(GL_LIGHTING);
	glEnable(GL_SMOOTH);//OPENGL INSTANTIATION
	HRESULT hr;
	NUI_IMAGE_FRAME depthFrame;
	HANDLE hDepth;
	INuiSensor* pNuiSensor = NULL;
	int iSensorCount = 0;
	hr = NuiGetSensorCount(&iSensorCount);

	if (FAILED(hr))
		return hr;

	for (int i = 0; i < iSensorCount; i++)
	{
		INuiSensor* tempSensor;
		hr = NuiCreateSensorByIndex(i, &tempSensor);

		if (FAILED(hr))
			continue;

		hr = tempSensor->NuiStatus();
		if (S_OK == hr)
		{
			pNuiSensor = tempSensor;
			break;
		}

		tempSensor->Release();
	}

	for (int i = 0; i < 2048; i++) {
		depthLookUp[i] = rawDepthToMeters(i);
	}

	rotation = getRotationMatrix(theta, psi, fi);

	pNuiSensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_DEPTH);
	pNuiSensor->NuiImageStreamOpen(
		NUI_IMAGE_TYPE_DEPTH,
		NUI_IMAGE_RESOLUTION_320x240,
		0,
		2,
		NULL,
		&hDepth);//KINECT INSTANTIATION

	cout << "Starting Main Loop";

	static double lastTime = glfwGetTime();
	//Main Loop
	do
	{
		double currentTime = glfwGetTime();
		float deltaTime = float(currentTime - lastTime);
		//Clear color buffer
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

		glUseProgram(grid);
		modelMatrix(MatrixID);


		hr = pNuiSensor->NuiImageStreamGetNextFrame(hDepth, 0, &depthFrame);
		if (!FAILED(hr))
		{

			INuiFrameTexture* pTexture;
			NUI_LOCKED_RECT LockedRect;

			hr = pNuiSensor->NuiImageFrameGetDepthImagePixelFrameTexture(
				hDepth, &depthFrame, false, &pTexture);

			if (FAILED(hr))
			{
				pNuiSensor->NuiImageStreamReleaseFrame(hDepth, &depthFrame);
				continue;
			}

			pTexture->LockRect(0, &LockedRect, NULL, 0);//Kinect Image Grab
			int skipX = 1;
			int skipY = 1;
			float scalar = 4.0f;

			if (LockedRect.Pitch != 0)
			{
				for (int x = 0; x < width; x += skipX)
				{
					for (int y = 0; y < height; y += skipY)
					{
						const NUI_DEPTH_IMAGE_PIXEL * pBufferRun = reinterpret_cast<const NUI_DEPTH_IMAGE_PIXEL *>(LockedRect.pBits) + x + y * width;
						
						//float depth = (float)(pBufferRun->depth);
						//glm::vec3 location = realWorld(depth, height - y, x, 500.0f, 1000.0f);
						//createCube(0.006f, location);
						Vector4 locationDepth = NuiTransformDepthImageToSkeleton(x, y, (short)(pBufferRun->depth << 3));
						glm::vec3 locationDepthxyz = glm::vec3(locationDepth.x * scalar, locationDepth.y * scalar, locationDepth.z * scalar);
						createCube(0.009f, locationDepthxyz);
					}
				}
			}

			pTexture->UnlockRect(0);
			pTexture->Release();

			pNuiSensor->NuiImageStreamReleaseFrame(hDepth, &depthFrame);
		}

		createGrid();

		//Test drawings
		/*
		glUseProgram(red);
		modelMatrix(MatrixID);
		//createCube(0.05f, glm::vec3(1.0f,1.0f,1.0f));
		// 1rst attribute buffer : vertices
		glEnableVertexAttribArray(0);
		//createObject(vertexbuffer, GL_TRIANGLES, 3);
		//createObject(triangle, GL_TRIANGLES, 3);
		glDisableVertexAttribArray(0);
		*/

		//Swap buffers
		glfwSwapBuffers(window);
		//Get and organize events, like keyboard and mouse input, window resizing, etc...
		glfwPollEvents();

		std::string title = "Title | DELTA TIME " + std::to_string(1.0f/deltaTime);
		const char* pszConstString = title.c_str();
		glfwSetWindowTitle(window, pszConstString);

		lastTime = currentTime;
	} //Check if the ESC key had been pressed or if the window had been closed
	while (!glfwWindowShouldClose(window));


	//Close OpenGL window and terminate GLFW
	glfwDestroyWindow(window);
	//Finalize and clean up GLFW
	glfwTerminate();

	exit(EXIT_SUCCESS);
}
Ejemplo n.º 12
0
HRESULT KTM::KinectWrapper::connectDevice(){
	if(isConnected())
		disconnectDevice();

    INuiSensor * pTmpKinectSensor;
    HRESULT hr;

    int iSensorCount = 0;
    hr = NuiGetSensorCount(&iSensorCount);
    if (FAILED(hr))
        return hr;

    // Look at each Kinect sensor
    for (int i = 0; i < iSensorCount; ++i){
        // Create the sensor so we can check status, if we can't create it, move on to the next
        hr = NuiCreateSensorByIndex(i, &pTmpKinectSensor);
        if (FAILED(hr))
            continue;

        // Get the status of the sensor, and if connected, then we can initialize it
        hr = pTmpKinectSensor->NuiStatus();
        if (S_OK == hr){
            pKinectSensor = pTmpKinectSensor;
            break;
        }

        // This sensor wasn't OK, so release it since we're not using it
        pTmpKinectSensor->Release();
    }

    if (NULL != pKinectSensor){
        // Initialize the Kinect and specify that we'll be using depth

		hr = pKinectSensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_COLOR | NUI_INITIALIZE_FLAG_USES_DEPTH);
        if (SUCCEEDED(hr)){
            // Create an event that will be signaled when depth data is available
			hNextDepthFrameEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
			hNextColorFrameEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

            // Open a depth image stream to receive depth frames
			hr = pKinectSensor->NuiImageStreamOpen(
				NUI_IMAGE_TYPE_COLOR,
                RGBResolutionCode,
                0,
                NUI_IMAGE_STREAM_FRAME_LIMIT_MAXIMUM,
                hNextColorFrameEvent,
                &hColorStreamHandle
			);

			hr = pKinectSensor->NuiImageStreamOpen(
				NUI_IMAGE_TYPE_DEPTH,
				depthResolutionCode,
                0,
                NUI_IMAGE_STREAM_FRAME_LIMIT_MAXIMUM,
                hNextDepthFrameEvent,
                &hDepthStreamHandle
			);
        }
    }

    if (NULL == pKinectSensor || FAILED(hr))
        return E_FAIL;

    return hr;
}
Ejemplo n.º 13
0
/// <summary>
/// Create the first connected Kinect found 
/// </summary>
/// <returns>indicates success or failure</returns>
HRESULT CDepthBasics::CreateFirstConnected()
{
    INuiSensor * pNuiSensor;
    HRESULT hr;

    int iSensorCount = 0;
    hr = NuiGetSensorCount(&iSensorCount);
    if (FAILED(hr))
    {
        return hr;
    }

    // Look at each Kinect sensor
    for (int i = 0; i < iSensorCount; ++i)
    {
        // Create the sensor so we can check status, if we can't create it, move on to the next
        hr = NuiCreateSensorByIndex(i, &pNuiSensor);
        if (FAILED(hr))
        {
            continue;
        }

        // Get the status of the sensor, and if connected, then we can initialize it
        hr = pNuiSensor->NuiStatus();
        if (S_OK == hr)
        {
            m_pNuiSensor = pNuiSensor;
            break;
        }

        // This sensor wasn't OK, so release it since we're not using it
        pNuiSensor->Release();
    }

    if (NULL != m_pNuiSensor)
    {
        // Initialize the Kinect and specify that we'll be using depth
        hr = m_pNuiSensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_DEPTH); 
        if (SUCCEEDED(hr))
        {
            // Create an event that will be signaled when depth data is available
            m_hNextDepthFrameEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

            // Open a depth image stream to receive depth frames
            hr = m_pNuiSensor->NuiImageStreamOpen(
                NUI_IMAGE_TYPE_DEPTH,
                NUI_IMAGE_RESOLUTION_640x480,
                0,
                2,
                m_hNextDepthFrameEvent,
                &m_pDepthStreamHandle);
        }
    }

    if (NULL == m_pNuiSensor || FAILED(hr))
    {
        SetStatusMessage(L"No ready Kinect found!");
        return E_FAIL;
    }

    return hr;
}
Ejemplo n.º 14
0
/// <summary>
/// Create the first connected Kinect found 
/// </summary>
/// <returns>indicates success or failure</returns>
HRESULT CKinectFusion::CreateFirstConnected()
{
    INuiSensor * pNuiSensor;
    HRESULT hr;

    int iSensorCount = 0;
    hr = NuiGetSensorCount(&iSensorCount);
    if (FAILED(hr))
    {
        std::cout << "No ready Kinect found!" << std::endl;
        return hr;
    }

    // Look at each Kinect sensor
    for (int i = 0; i < iSensorCount; ++i)
    {
        // Create the sensor so we can check status, if we can't create it, move on to the next
        hr = NuiCreateSensorByIndex(i, &pNuiSensor);
        if (FAILED(hr))
        {
            continue;
        }

        // Get the status of the sensor, and if connected, then we can initialize it
        hr = pNuiSensor->NuiStatus();
        if (S_OK == hr)
        {
            m_pNuiSensor = pNuiSensor;
            break;
        }

        // This sensor wasn't OK, so release it since we're not using it
        pNuiSensor->Release();
    }

    if (nullptr != m_pNuiSensor)
    {
        // Initialize the Kinect and specify that we'll be using depth
        hr = m_pNuiSensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_DEPTH); 
        if (SUCCEEDED(hr))
        {
            // Create an event that will be signaled when depth data is available
            m_hNextDepthFrameEvent = CreateEvent(nullptr, TRUE, FALSE, nullptr);

            // Open a depth image stream to receive depth frames
            hr = m_pNuiSensor->NuiImageStreamOpen(
                NUI_IMAGE_TYPE_DEPTH,
                m_imageResolution,
                0,
                2,
                m_hNextDepthFrameEvent,
                &m_pDepthStreamHandle);
        }

        if (m_bNearMode)
        {
            // Set near mode based on our internal state
            HRESULT nearHr = m_pNuiSensor->NuiImageStreamSetImageFrameFlags(m_pDepthStreamHandle, NUI_IMAGE_STREAM_FLAG_ENABLE_NEAR_MODE);
        }
    }

    if (nullptr == m_pNuiSensor || FAILED(hr))
    {
		std::cout << "No ready Kinect found!" << std::endl;
        return E_FAIL;
    }


    return hr;
}
Ejemplo n.º 15
0
/*!
//\par		Description:
//			Sets all memeber to their default values,
//			reads configuration from settings file and tries to find first connected kinect device.
//
//\par		Return value:
//			True value is a expected value. When this method returns false then something wrong is with
//			with kinect sensor. It can be disconnected or drivers are incompatible. First thing to do
//			in this case is to check green led on kinect sensor. It should shines all the time. Flashing
//			is not allowed.
//
//\retval	true	kinect is ready to work
//\retval	false	kinect is not found
*/
bool CKinectHandler::createFirstConnectedKinectSensor()
{
	qDebug() << "createFirstConnectedKinectSensor()";

	INuiSensor * pSensor;
	int iSensorCount = 0;

	// HRESULT - just standard long
	HRESULT hr = NuiGetSensorCount( & iSensorCount );
	if ( FAILED( hr ) ) // checks if hr is less than 0
	{
		qDebug() << "Cannot get available kinect sensor count";
		return false;
	}

	// checks all sensors and takes first one which can be created
	for ( int i = 0 ; i < iSensorCount ; i++ )
	{
		hr = NuiCreateSensorByIndex( i, & pSensor );

		if ( FAILED( hr ) )
		{
			qDebug() << "Cannot create sensor with index: " << i << ", check next index";
			continue;
		}

		hr = pSensor->NuiStatus();

		if ( hr == S_OK ) // sensor is connected, it can be initialized now
		{
			m_pSensor = pSensor;
			qDebug() << "Sensor with index: " << i << " is connected - breaking loop";

			break;
		}

		// release not ok sensor
		pSensor->Release();
	}

	if ( m_pSensor != NULL )
	{
		hr = m_pSensor->NuiInitialize( NUI_INITIALIZE_FLAG_USES_SKELETON ); // in order to use skeleton

		if ( SUCCEEDED( hr ) )
		{
			// create event to inform application about new skeleton frame
			m_hNextSkeletonEvent = CreateEventW( NULL, TRUE, FALSE, NULL );

			// open skeleton stream to get skeleton data
			hr = m_pSensor->NuiSkeletonTrackingEnable( m_hNextSkeletonEvent, 0 );
		}
	}

	if ( m_pSensor == NULL || FAILED( hr ) )
	{
		qDebug() << "No ready kinect found";
		m_kinectDetails.insert( "connection", false );

		return false;
	}

	m_kinectDetails.insert( "connection", true );
	m_kinectDetails.insert( "skeleton_found", false );

	return true;
}
// In current version, we can't set the resolution
// All sensors of Kinect is activated. 
HRESULT Kinect::createFirstConnected(){
	INuiSensor * pNuiSensor;
	HRESULT hr;

	int iSensorCount = 0;
	hr = NuiGetSensorCount(&iSensorCount);
	if (FAILED(hr))
	{
		return hr;
	}

	// Look at each Kinect sensor
	for (int i = 0; i < iSensorCount; ++i)
	{
		// Create the sensor so we can check status, if we can't create it, move on to the next
		hr = NuiCreateSensorByIndex(i, &pNuiSensor);
		if (FAILED(hr))
		{
			continue;
		}

		// Get the status of the sensor, and if connected, then we can initialize it
		hr = pNuiSensor->NuiStatus();
		if (S_OK == hr)
		{
			m_pNuiSensor = pNuiSensor;
			break;
		}

		// This sensor wasn't OK, so release it since we're not using it
		pNuiSensor->Release();
	}

	if (NULL != m_pNuiSensor)
	{
		// Initialize the Kinect and specify that we'll be using color, depth and skeletion 
		hr = m_pNuiSensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_COLOR | 
			NUI_INITIALIZE_FLAG_USES_DEPTH_AND_PLAYER_INDEX |
			NUI_INITIALIZE_FLAG_USES_SKELETON);
		if (SUCCEEDED(hr))
		{
			// Create an event that will be signaled when depth data is available
			m_hNextDepthFrameEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

			// Open a depth image stream to receive depth frames
			hr = m_pNuiSensor->NuiImageStreamOpen(
				NUI_IMAGE_TYPE_DEPTH_AND_PLAYER_INDEX,
				NUI_IMAGE_RESOLUTION_640x480,
				NUI_IMAGE_STREAM_FLAG_ENABLE_NEAR_MODE|NUI_IMAGE_STREAM_FLAG_DISTINCT_OVERFLOW_DEPTH_VALUES,
				//NUI_IMAGE_STREAM_FRAME_LIMIT_MAXIMUM,
				2,
				m_hNextDepthFrameEvent,
				&m_hDepthStreamHandle);
			//m_pNuiSensor->NuiImageStreamSetImageFrameFlags(&m_hDepthStreamHandle, NUI_IMAGE_STREAM_FLAG_ENABLE_NEAR_MODE|NUI_IMAGE_STREAM_FLAG_DISTINCT_OVERFLOW_DEPTH_VALUES);

		}

		if (SUCCEEDED(hr))
		{
			// Open a color image stream to receive depth frames
			m_hNextColorFrameEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

			hr = m_pNuiSensor->NuiImageStreamOpen(
				NUI_IMAGE_TYPE_COLOR,
				NUI_IMAGE_RESOLUTION_640x480,
				0,
				2,
				m_hNextColorFrameEvent,
				&m_hColorStreamHandle);
		}
		if (SUCCEEDED(hr)){
			// Enable the skeleton tracking
			m_hNextSkeletonEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
			//hr = m_pNuiSensor->NuiSkeletonTrackingEnable(m_hNextSkeletonEvent, 0); 
			hr = m_pNuiSensor->NuiSkeletonTrackingEnable(
				m_hNextSkeletonEvent,  NUI_SKELETON_TRACKING_FLAG_ENABLE_IN_NEAR_RANGE | NUI_SKELETON_TRACKING_FLAG_ENABLE_SEATED_SUPPORT);
		}
	}

	if (NULL == m_pNuiSensor || FAILED(hr))
	{
		return E_FAIL;
	}
	m_bInitialized = true;
	return hr;
}
Ejemplo n.º 17
0
// TODO: allow for customizing which streams are initialized
HRESULT SimpleKinect::Initialize()
{
	INuiSensor * pNuiSensor;
    HRESULT hr;

    int iSensorCount = 0;
    hr = NuiGetSensorCount(&iSensorCount);
    if (FAILED(hr))
    {
        return hr;
    }

    // Find the correct sensor
	cout << "SimpleKinect: Finding sensor..." << endl;

    for (int i = 0; i < iSensorCount; ++i)
    {
        // Create the sensor so we can check status, if we can't create it, move on to the next
        hr = NuiCreateSensorByIndex(i, &pNuiSensor);
        if (FAILED(hr))
        {
            continue;
        }

        // Get the status of the sensor, and if connected, then we can initialize it
        hr = pNuiSensor->NuiStatus();
        if (S_OK == hr)
        {
            m_pNuiSensor = pNuiSensor;
            break;
        }

        // This sensor wasn't OK, so release it since we're not using it
        pNuiSensor->Release();
    }

	
	// Open depth stream
    if (NULL != m_pNuiSensor)
    {
		cout << "SimpleKinect: Opening depth stream.." << endl;
        // Initialize the Kinect and specify that we'll be using depth, player index and color
		hr = m_pNuiSensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_DEPTH_AND_PLAYER_INDEX | NUI_INITIALIZE_FLAG_USES_COLOR | NUI_INITIALIZE_FLAG_USES_SKELETON ); 
        if (SUCCEEDED(hr))
        {

            // Open a depth image stream to receive depth frames
            hr = m_pNuiSensor->NuiImageStreamOpen(
				NUI_IMAGE_TYPE_DEPTH_AND_PLAYER_INDEX,
                NUI_IMAGE_RESOLUTION_640x480,
                0,
                2,
                NULL,
                &m_pDepthStreamHandle);
        } else {
			cerr << "SimpleKinect: Failed to open depth and player stream!" << endl;
			return E_FAIL;
		}

		// open color stream
		cout << "SimpleKinect: Opening color stream.." << endl;
		hr = m_pNuiSensor->NuiImageStreamOpen(
			NUI_IMAGE_TYPE_COLOR,
			NUI_IMAGE_RESOLUTION_640x480,
			0,
			2,
			NULL,
			&m_pColorStreamHandle);
		if(!SUCCEEDED(hr))
		{
			cerr << "SimpleKinect: Failed to open color stream!" << endl;
			return E_FAIL;
		}

		cout << "SimpleKinect: Opening skeleton stream.." << endl;
		// open skeleton stream
		if(HasSkeletalEngine(m_pNuiSensor))
		{
			hr = m_pNuiSensor->NuiSkeletonTrackingEnable(NULL, 
				NUI_SKELETON_TRACKING_FLAG_ENABLE_IN_NEAR_RANGE | 
				NUI_SKELETON_TRACKING_FLAG_ENABLE_SEATED_SUPPORT |
				NUI_SKELETON_TRACKING_FLAG_SUPPRESS_NO_FRAME_DATA);
			if(!SUCCEEDED(hr))
			{
				cerr << "SimpleKinect: Failed to open skeleton stream!" << endl;
				return E_FAIL;
			}
		}
		else 
		{
			cerr << "SimpleKinect: Sensor does not have skeletal engine!" << endl;
		}
		
    }

    if (NULL == m_pNuiSensor || FAILED(hr))
    {
        cerr << "SimpleKinect: No ready Kinect found!" << endl;
        return E_FAIL;
    } else 
	{
		BSTR uniqueId = m_pNuiSensor->NuiUniqueId();
		cout << "SimpleKinect Connected to sensor " << uniqueId << endl;
	}
	m_isInitialized = true;
    return hr;
}
Ejemplo n.º 18
0
/// <summary>
/// Create the first connected Kinect found.
/// </summary>
/// <returns>S_OK on success, otherwise failure code.</returns>
HRESULT KinectReader::CreateFirstConnected()
{
    INuiSensor * pNuiSensor = NULL;
    HRESULT hr;

    int iSensorCount = 0;
    hr = NuiGetSensorCount(&iSensorCount);
    if (FAILED(hr))
    {
        return hr;
    }

    // Look at each Kinect sensor
    for (int i = 0; i < iSensorCount; ++i)
    {
        // Create the sensor so we can check status, if we can't create it, move on to the next
        hr = NuiCreateSensorByIndex(i, &pNuiSensor);
        if (FAILED(hr))
        {
            continue;
        }

        // Get the status of the sensor, and if connected, then we can initialize it
        hr = pNuiSensor->NuiStatus();
        if (S_OK == hr)
        {
            m_pNuiSensor = pNuiSensor;
            break;
        }

        // This sensor wasn't OK, so release it since we're not using it
        pNuiSensor->Release();
    }

    if (NULL != m_pNuiSensor)
    {
		// Initialize the Kinect and specify that we'll be using audio signal
        // Initialize the Kinect and specify that we'll be using depth
        hr = m_pNuiSensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_AUDIO | NUI_INITIALIZE_FLAG_USES_DEPTH); 
		
        if (SUCCEEDED(hr))
        {
            // Create an event that will be signaled when depth data is available
            m_hNextDepthFrameEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

            // Open a depth image stream to receive depth frames
            hr = m_pNuiSensor->NuiImageStreamOpen(
                NUI_IMAGE_TYPE_DEPTH,
                NUI_IMAGE_RESOLUTION_640x480,
                0,
                2,
                m_hNextDepthFrameEvent,
                &m_pDepthStreamHandle);
        }
    }

    if (NULL == m_pNuiSensor || FAILED(hr))
    {
		std::cout<<"No ready Kinect found!"<<std::endl;
		m_pNuiSensor = NULL;
        //SetStatusMessage(L"No ready Kinect found!");
        return E_FAIL;
    }
	
	printf("Kinect found. \n");

	/* Speech start */

	hr = InitializeAudioStream();
	if (FAILED(hr))
	{
		printf("Could not initialize audio stream.\n");
		return hr;
	}
	printf("Initialize Audio Stream. \n");
	
	hr = CreateSpeechRecognizer();
	if (FAILED(hr))
    {
        printf("Could not create speech recognizer. Please ensure that Microsoft Speech SDK and other sample requirements are installed.\n");
        return hr;
    }
	printf("Create Speech Recognizer. \n");

	hr = LoadSpeechGrammar();
	if (FAILED(hr))
    {
        printf("Could not load speech grammar. Please ensure that grammar configuration file was properly deployed.\n");
        return hr;
    }
	printf("Start Speech Recognition. \n");

	hr = StartSpeechRecognition();
    if (FAILED(hr))
    {
        printf("Could not start recognizing speech. \n");
        return hr;
    }
	/* Speech end */


    return hr;
}
bool KinectCapture::InitCapture()
{
	INuiSensor * pNuiSensor;
	HRESULT hr;
	int iSensorCount = 0;
	hr = NuiGetSensorCount(&iSensorCount);
	if (FAILED(hr))
	{
		std::cout << "No ready Kinect found!" << std::endl;
		return false;
	}

	// Look at each Kinect sensor
	for (int i = 0; i < iSensorCount; ++i)
	{
		// Create the sensor so we can check status, if we can't create it, move on to the next
		hr = NuiCreateSensorByIndex(i, &pNuiSensor);
		if (FAILED(hr))
		{
			continue;
		}

		// Get the status of the sensor, and if connected, then we can initialize it
		hr = pNuiSensor->NuiStatus();
		if (S_OK == hr)
		{
			m_NuiSensor = pNuiSensor;
			break;
		}

		// This sensor wasn't OK, so release it since we're not using it
		pNuiSensor->Release();
	}

	if (nullptr != m_NuiSensor)
	{
		// Initialize the Kinect and specify that we'll be using depth
		hr = m_NuiSensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_DEPTH | NUI_INITIALIZE_FLAG_USES_COLOR);
		if (SUCCEEDED(hr))
		{
			// Open a depth image stream to receive depth frames
			hr = m_NuiSensor->NuiImageStreamOpen(
				NUI_IMAGE_TYPE_DEPTH,
				m_depthImageResolution,
				0,
				2,
				NULL,
				&m_DepthStreamHandle);
			if (SUCCEEDED(hr))
			{
				// Open a color image stream to receive color frames
				hr = m_NuiSensor->NuiImageStreamOpen(
					NUI_IMAGE_TYPE_COLOR,
					m_colorImageResolution,
					0,
					2,
					NULL,
					&m_ColorStreamHandle);
			}
			if (FAILED(hr))
			{
				std::cout << "Couldn't open color stream" << std::endl;
			}
		}
	}

	if (m_NearMode)
	{
		// Set near mode based on our internal state
		HRESULT nearHr = m_NuiSensor->NuiImageStreamSetImageFrameFlags(m_DepthStreamHandle, NUI_IMAGE_STREAM_FLAG_ENABLE_NEAR_MODE);
	}
	if (nullptr == m_NuiSensor || FAILED(hr))
	{
		std::cout << "No ready Kinect found!" << std::endl;
		return false;
	}
	return (S_OK == hr);
}
Ejemplo n.º 20
0
HRESULT AcquisitionKinect1::CreateFirstConnected(){
    INuiSensor * pNuiSensor = NULL;
    HRESULT hr;

    int iSensorCount = 0;
    hr = NuiGetSensorCount(&iSensorCount);
    cout << "sensori collegati: " << iSensorCount << endl;
    if (FAILED(hr))
    {
        return hr;
    }

    // Look at each Kinect sensor
    for (int i = 0; i < iSensorCount; ++i)
    {
        // Create the sensor so we can check status, if we can't create it, move on to the next
        hr = NuiCreateSensorByIndex(i, &pNuiSensor);
        if (FAILED(hr))
        {
            continue;
        }

        cout << "Sensore " << i << " creato." << endl;

        hr = m_frameHelper.Initialize(pNuiSensor);  // QUI E' DOVE CI METTE TANTO TEMPO

        cout << "Sensore " << i << " inizializzato." << endl;

        // Get the status of the sensor, and if connected, then we can initialize it
        hr = pNuiSensor->NuiStatus();
        if (S_OK == hr)
        {
            m_pNuiSensor = pNuiSensor;
            break;
        }

        // This sensor wasn't OK, so release it since we're not using it
        pNuiSensor->Release();
    }

    /*
    if (NULL != m_pNuiSensor)
    {
        // Initialize the Kinect and specify that we'll be using color, depth and skeleton
        hr = m_pNuiSensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_COLOR | NUI_INITIALIZE_FLAG_USES_DEPTH_AND_PLAYER_INDEX | NUI_INITIALIZE_FLAG_USES_SKELETON);

        if (SUCCEEDED(hr))
        {
            // Create an event that will be signaled when depth data is available
            m_hNextDepthFrameEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

            // Open a color image stream to receive depth frames
            hr = m_pNuiSensor->NuiImageStreamOpen(
                        NUI_IMAGE_TYPE_DEPTH_AND_PLAYER_INDEX,
                        NUI_IMAGE_RESOLUTION_640x480,
                        0,
                        2,
                        m_hNextDepthFrameEvent,
                        &m_pDepthStreamHandle);

            if (FAILED(hr)) return hr;

            // Create an event that will be signaled when color data is available
            m_hNextColorFrameEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

            // Open a color image stream to receive color frames
            hr = m_pNuiSensor->NuiImageStreamOpen(
                        NUI_IMAGE_TYPE_COLOR,
                        NUI_IMAGE_RESOLUTION_640x480,
                        0,
                        2,
                        m_hNextColorFrameEvent,
                        &m_pColorStreamHandle);

            if (FAILED(hr)) return hr;

        }
    }

    */
    return hr;

}
/// <summary>
/// Create the first connected Kinect found 
/// </summary>
/// <returns>S_OK on success, otherwise failure code</returns>
HRESULT KinectEasyGrabber::CreateFirstConnected()
{
    INuiSensor * pNuiSensor;
    HRESULT hr;

    int iSensorCount = 0;
    hr = NuiGetSensorCount(&iSensorCount);
    if (FAILED(hr))
    {
        return hr;
    }

    // Look at each Kinect sensor
    for (int i = 0; i < iSensorCount; ++i)
    {
        // Create the sensor so we can check status, if we can't create it, move on to the next
        hr = NuiCreateSensorByIndex(i, &pNuiSensor);
        if (FAILED(hr))
        {
            continue;
        }

        // Get the status of the sensor, and if connected, then we can initialize it
        hr = pNuiSensor->NuiStatus();
        if (S_OK == hr)
        {
            m_pNuiSensor = pNuiSensor;
            break;
        }

        // This sensor wasn't OK, so release it since we're not using it
        pNuiSensor->Release();
    }

    if (NULL != m_pNuiSensor)
    {
        // Initialize the Kinect and specify that we'll be using depth
		#ifdef RECORD_PLAYER
        hr = m_pNuiSensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_DEPTH_AND_PLAYER_INDEX | NUI_INITIALIZE_FLAG_USES_COLOR); 
		#else
		hr = m_pNuiSensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_DEPTH | NUI_INITIALIZE_FLAG_USES_COLOR); 
		#endif
        if (SUCCEEDED(hr))
        {
            // Create an event that will be signaled when depth data is available
            m_hNextDepthFrameEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

#ifdef RECORD_PLAYER
            // Open a depth image stream to receive depth frames
            hr = m_pNuiSensor->NuiImageStreamOpen(
                NUI_IMAGE_TYPE_DEPTH_AND_PLAYER_INDEX,
                cDepthResolution,
                0,
                2,
                m_hNextDepthFrameEvent,
                &m_pDepthStreamHandle);
#else
            // Open a depth image stream to receive depth frames
            hr = m_pNuiSensor->NuiImageStreamOpen(
                NUI_IMAGE_TYPE_DEPTH,
                cDepthResolution,
                0,
                2,
                m_hNextDepthFrameEvent,
                &m_pDepthStreamHandle);

#endif
            // Create an event that will be signaled when color data is available
            m_hNextColorFrameEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

            // Open a color image stream to receive depth frames
            hr = m_pNuiSensor->NuiImageStreamOpen(
                NUI_IMAGE_TYPE_COLOR,
                cColorResolution,
                0,
                2,
                m_hNextColorFrameEvent,
                &m_pColorStreamHandle);
        }
    }

    if (NULL == m_pNuiSensor || FAILED(hr))
    {
        SetStatusMessage(L"No ready Kinect found!");
        return E_FAIL;
    }

    m_pNuiSensor->NuiSkeletonTrackingDisable();

    return hr;
}
Ejemplo n.º 22
0
bool KinectInput::initSensor()
{
	INuiSensor* tempSensor;
	int numSensors = 0;
	if (NuiGetSensorCount(&numSensors) != S_OK ) 
	{
		std::cout << "Kinect Error: Sensor lookup failed, Probably no sensors connected" << std::endl;
		std::getchar();
		return false;
	}

	// Loop through all sensors
	for (int i = 0; i < numSensors; ++i)
	{
		// Create the sensor so we can check status, if we can't create it, move on to the next
		if (NuiCreateSensorByIndex(i, &tempSensor) < 0)
		{
			continue;
		}

		// Get the status of the sensor, and if connected, then we can initialize it
		if (tempSensor->NuiStatus() == S_OK)
		{
			kinectSensor = tempSensor;
			break;
		}

		// This sensor wasn't OK, so release it since we're not using it
		tempSensor->Release();
	}

	if (kinectSensor != NULL)
	{
		// Initialize the Kinect and specify that we'll be using skeleton
		if (kinectSensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_SKELETON) >= 0)
		{
			// Create an event that will be signaled when skeleton data is available
			nextSkeletonUpdate = CreateEventW(NULL, TRUE, FALSE, NULL);

			// Open a skeleton stream to receive skeleton data
			if (kinectSensor->NuiSkeletonTrackingEnable(nextSkeletonUpdate, 0) < 0)
			{
				std::cout << "Kinect Error: Couldn't open a skeleton stream" << std::endl;
				std::getchar();
				return false;
			}
		}
		else 
		{
			std::cout << "Kinect Error: Sensor failed initialization" << std::endl;
			std::getchar();
			return false;
		}
	}
	else
	{
		std::cout << "Kinect Error: No sensor could be initialized" << std::endl;
		std::getchar();
		return false;
	}

	return true;
}
Ejemplo n.º 23
0
    HRESULT KinectSDKGrabber::CreateFirstConnected()
    {
        INuiSensor *pNuiSensor;
        HRESULT hr;

        int iSensorCount = 0;
        hr = NuiGetSensorCount(&iSensorCount);
        if (FAILED(hr))
        {
            return hr;
        }
        std::cout << "Count" << std::endl;

        // Look at each Kinect sensor
        for (int i = 0; i < iSensorCount; ++i)
        {
            // Create the sensor so we can check status, if we can't create it, move on to the next
            hr = NuiCreateSensorByIndex(i, &pNuiSensor);
            if (FAILED(hr))
            {
                continue;
            }
            std::cout << "Created" << std::endl;

            // Get the status of the sensor, and if connected, then we can initialize it
            hr = pNuiSensor->NuiStatus();
            if (S_OK == hr)
            {
                m_pNuiSensor = pNuiSensor;
                break;
            }

            // This sensor wasn't OK, so release it since we're not using it
            pNuiSensor->Release();
        }

        if (NULL != m_pNuiSensor)
        {
            // Initialize the Kinect and specify that we'll be using depth
            hr = m_pNuiSensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_DEPTH | NUI_INITIALIZE_FLAG_USES_COLOR);
            if (SUCCEEDED(hr))
            {
                // Create an event that will be signaled when depth data is available
                m_hNextDepthFrameEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

                // Open a depth image stream to receive depth frames
                hr = m_pNuiSensor->NuiImageStreamOpen(
                         NUI_IMAGE_TYPE_DEPTH,
                         NUI_IMAGE_RESOLUTION_640x480,
                         0,
                         2,
                         m_hNextDepthFrameEvent,
                         &m_pDepthStreamHandle);

                m_pNuiSensor->NuiImageStreamSetImageFrameFlags(m_pDepthStreamHandle, NUI_IMAGE_STREAM_FLAG_ENABLE_NEAR_MODE);

                std::cout << "Depth" << std::endl;

            }

            if (SUCCEEDED(hr))
            {
                m_hNextColorFrameEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

                hr = m_pNuiSensor->NuiImageStreamOpen(
                         NUI_IMAGE_TYPE_COLOR,
                         NUI_IMAGE_RESOLUTION_640x480,
                         0,
                         2,
                         m_hNextColorFrameEvent,
                         &m_pColorStreamHandle);
                std::cout << "Color" << std::endl;
            }
        }

        if (NULL == m_pNuiSensor || FAILED(hr))
        {
            return E_FAIL;
        }

        return hr;
    }