kinectV2をopenFrameworksで動かす

## 環境
* of_v0.8.4_vs_release
* Visual Studio Express 2012 for Windows
* kinect v2
* Windows Embedded 8


## 各種パスの設定
まずプロジェクトファイルのプロパティを開く

1.構成プロパティ > C/C++ > 追加のインクルードディレクトリに**$(KINECTSDK20_DIR)¥inc;**を追加

![1.JPG](https://qiita-image-store.s3.amazonaws.com/0/42289/ae6aa710-6899-1b9e-16ad-fd7cabc3d6e7.jpeg)




2.構成プロパティ > リンカー > 全般 > 追加のライブラリディレクトリに**$(KINECTSDK20_DIR)¥lib¥x86;**を追加

![2.JPG](https://qiita-image-store.s3.amazonaws.com/0/42289/fe82b13d-116f-74b5-69b7-f4d6018ed9ce.jpeg)




3.構成プロパティ > リンカー > 入力に**Kinect20.lib**を追加

![5.JPG](https://qiita-image-store.s3.amazonaws.com/0/42289/0cd84c0b-9dc1-697d-7e2f-4e8a5b16b3e0.jpeg)





4.構成プロパティ > VC++ディレクトリ > インクルードディレクトリに$(KINECTSDK20_DIR)\inc;を入力
5.構成プロパティ > VC++ディレクトリ > ライブラリディレクトリに$(KINECTSDK20_DIR)\lib\x86;を入力

![4.JPG](https://qiita-image-store.s3.amazonaws.com/0/42289/f72c8b01-1c77-7011-ac22-23ca0d650a43.jpeg)

##カラー画像の表示
```cpp:ofApp.h

#pragma once

#include "ofMain.h"
#include <kinect.h>

template<class Interface>
inline void SafeRelease(Interface *& pInterfaceToRelease)
{
	if (pInterfaceToRelease != NULL) {
		pInterfaceToRelease->Release();
		pInterfaceToRelease = NULL;
	}
}

class ofApp : public ofBaseApp{

	public:
		void setup();
		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 windowResized(int w, int h);
		void dragEvent(ofDragInfo dragInfo);
		void gotMessage(ofMessage msg);
		bool initKinect();

		IKinectSensor *sensor;

		IColorFrameSource *colorSource;
		IColorFrameSource *colorFrameSource;
		IColorFrameReader *colorReader;
		IFrameDescription* colorDescription;
		int colorWidth, colorHeight;
		unsigned int colorBytesPerPixels;

		ofImage colorImage;
		
};


```

```cpp:ofApp.cpp

#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup(){
	ofSetWindowShape(1920, 1080);
	ofSetFrameRate(30);

	if (!initKinect()) exit();

	colorImage.allocate(colorWidth, colorHeight, OF_IMAGE_COLOR_ALPHA);
}

//--------------------------------------------------------------
void ofApp::update(){
	IColorFrame* colorFrame = nullptr;
	HRESULT hResult = colorReader->AcquireLatestFrame(&colorFrame);

	if (SUCCEEDED(hResult)) {
		hResult = colorFrame->CopyConvertedFrameDataToArray(colorHeight * colorWidth * colorBytesPerPixels, colorImage.getPixels(), ColorImageFormat_Rgba);
		colorImage.update();
	}

	SafeRelease(colorFrame);
}

//--------------------------------------------------------------
void ofApp::draw(){
	colorImage.draw(0,0);
}

bool ofApp:: initKinect() {
	HRESULT hResult = S_OK;
	hResult = GetDefaultKinectSensor(&sensor);
	if (FAILED(hResult)) {
		std::cerr << "Error : GetDefaultKinectSensor" << std::endl;
		return -1;
	}

	hResult = sensor->Open();
	if (FAILED(hResult)) {
		std::cerr << "Error : IKinectSensor::Open()" << std::endl;
		return -1;
	}

	hResult = sensor->get_ColorFrameSource(&colorSource);
	if (FAILED(hResult)) {
		std::cerr << "Error : IKinectSensor::get_ColorFrameSource()" << std::endl;
		return -1;
	}

	hResult = colorSource->OpenReader(&colorReader);
	if (FAILED(hResult)) {
		std::cerr << "Error : IColorFrameSource::OpenReader()" << std::endl;
		return -1;
	}

	hResult = colorSource->CreateFrameDescription(ColorImageFormat::ColorImageFormat_Rgba, &colorDescription);
	if (FAILED(hResult)) {
		std::cerr << "Error : IColorFrameSource::get_FrameDescription()" << std::endl;
		return -1;
	}
	colorDescription->get_Width(&colorWidth);
	colorDescription->get_Height(&colorHeight);
	colorDescription->get_BytesPerPixel(&colorBytesPerPixels);

	return true;
}

//--------------------------------------------------------------
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){

}

//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){

}

//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){

}

//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){

}

//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){

}

//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){ 

}


```


## 参考
> http://www.hirotakaster.com/weblog/openframeworks-0-8-3-and-kinect-v2/
> http://www.buildinsider.net/small/kinectv2cpp/02