VS2013配置openNI1.5

在参考了一些作者的文章之后,终于在VS2013的环境下,将openNI1.5配置完成了,在这里要感谢广大牛人!下面进入正题:

 

因为学习的是《openNI体感应用开发实战》,为了可以尽可能和书中用的东西一样,所以使用 openNI 1.5 而没有使用 openNI 2.0

 

一、需要的安装文件


在下面的网盘中可以下载:

链接:http://pan.baidu.com/s/1geudD3H 密码:j5lh

 

一、安装三个文件

首先需要安装的就是 OpenNI-Win32-1.5.7-Dev.msi

后面的两个任意顺序安装

 

具体的安装过程就不说了,在安装过程中只要注意将安装路径记住就可以了!(当然是可以修改默认的安装路径的)

 

二、在VS2013中配置

首先建立一个C++工程,打开属性管理器




上面的是OpenNI-Win32-1.5.7-Dev.msi文件的安装路径下的 Include 文件夹的路径


上面的是OpenNI-Win32-1.5.7-Dev.msi文件的安装路径下的 Lib 文件夹的路径


在这里面 添加:

openNI.lib

glut32.lib


四、修改版本

如果进行到上一步就开始运行工程,那么将会出现错误:大概的意思是,不能在VS2010版本以上的环境中运行,此时,就需要定位到该发生错误的地方,发现这里是一个异常处理机制,为了使VS版本号在 2003 ~ 2010 之间,所以用 VS2013 编译会出现错误,此时,就需要将 >1600 改为  >1900 就可以使用VS2013来编译了!

 

五、配置glut32.lib

因为openNI用到openGL,所以需要glut.lib这个库,所以需要将glut32.dll这个动态链接库添加到运行环境中,添加的方式有两个

(该 glut32.dll 可以从:链接:http://pan.baidu.com/s/1mhgwvm4 密码:6p3x中下载):

(1)、将 glut32.dll 文件复制到 C:\Windows\SysWOW64 路径下

(2)、因为在安装完 openNI 的文件后,会自动创建环境变量,而安装路径下的 Bin 文件夹就在环境变量下,所以将 glut32.dll 文件复制到Bin这个文件夹下也是可以的

 

六、测试配置环境

拿官方的例子来测试

首先,将安装路径下,找到:..\Samples\NiSimpleViewer 文件夹,将下面画出的三个文件复制到工程目录下




然后打开工程,在 项目 -> 属性中分别添加刚刚复制的几个文件夹的库文件和包含文件,如下图:








然后将openNI的安装路径下的 Data 文件夹中的 SamplesConfig.xml 文件复制到工程目录下:



测试代码(注意得插上Xtion哦!):

/*****************************************************************************
*                                                                            *
*  OpenNI 1.x Alpha                                                          *
*  Copyright (C) 2012 PrimeSense Ltd.                                        *
*                                                                            *
*  This file is part of OpenNI.                                              *
*                                                                            *
*  Licensed under the Apache License, Version 2.0 (the "License");           *
*  you may not use this file except in compliance with the License.          *
*  You may obtain a copy of the License at                                   *
*                                                                            *
*      http://www.apache.org/licenses/LICENSE-2.0                            *
*                                                                            *
*  Unless required by applicable law or agreed to in writing, software       *
*  distributed under the License is distributed on an "AS IS" BASIS,         *
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  *
*  See the License for the specific language governing permissions and       *
*  limitations under the License.                                            *
*                                                                            *
*****************************************************************************/
//---------------------------------------------------------------------------
// Includes
//---------------------------------------------------------------------------
#include <XnOS.h>
#if (XN_PLATFORM == XN_PLATFORM_MACOSX)
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <math.h>


#include <XnCppWrapper.h>
using namespace xn;


//---------------------------------------------------------------------------
// Defines
//---------------------------------------------------------------------------
#define SAMPLE_XML_PATH "SamplesConfig.xml"


#define GL_WIN_SIZE_X 1280
#define GL_WIN_SIZE_Y 1024


#define DISPLAY_MODE_OVERLAY<span style="white-space:pre">	</span>1
#define DISPLAY_MODE_DEPTH<span style="white-space:pre">		</span>2
#define DISPLAY_MODE_IMAGE<span style="white-space:pre">		</span>3
#define DEFAULT_DISPLAY_MODE<span style="white-space:pre">	</span>DISPLAY_MODE_DEPTH


//---------------------------------------------------------------------------
// Globals
//---------------------------------------------------------------------------
float* g_pDepthHist;
XnRGB24Pixel* g_pTexMap = NULL;
unsigned int g_nTexMapX = 0;
unsigned int g_nTexMapY = 0;
XnDepthPixel g_nZRes;


unsigned int g_nViewState = DEFAULT_DISPLAY_MODE;


Context g_context;
ScriptNode g_scriptNode;
DepthGenerator g_depth;
ImageGenerator g_image;
DepthMetaData g_depthMD;
ImageMetaData g_imageMD;


//---------------------------------------------------------------------------
// Code
//---------------------------------------------------------------------------


void glutIdle(void)
{
<span style="white-space:pre">	</span>// Display the frame
<span style="white-space:pre">	</span>glutPostRedisplay();
}


void glutDisplay(void)
{
<span style="white-space:pre">	</span>XnStatus rc = XN_STATUS_OK;


<span style="white-space:pre">	</span>// Read a new frame
<span style="white-space:pre">	</span>rc = g_context.WaitAnyUpdateAll();
<span style="white-space:pre">	</span>if (rc != XN_STATUS_OK)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>printf("Read failed: %s\n", xnGetStatusString(rc));
<span style="white-space:pre">		</span>return;
<span style="white-space:pre">	</span>}


<span style="white-space:pre">	</span>g_depth.GetMetaData(g_depthMD);
<span style="white-space:pre">	</span>g_image.GetMetaData(g_imageMD);


<span style="white-space:pre">	</span>const XnDepthPixel* pDepth = g_depthMD.Data();


<span style="white-space:pre">	</span>// Copied from SimpleViewer
<span style="white-space:pre">	</span>// Clear the OpenGL buffers
<span style="white-space:pre">	</span>glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


<span style="white-space:pre">	</span>// Setup the OpenGL viewpoint
<span style="white-space:pre">	</span>glMatrixMode(GL_PROJECTION);
<span style="white-space:pre">	</span>glPushMatrix();
<span style="white-space:pre">	</span>glLoadIdentity();
<span style="white-space:pre">	</span>glOrtho(0, GL_WIN_SIZE_X, GL_WIN_SIZE_Y, 0, -1.0, 1.0);


<span style="white-space:pre">	</span>// Calculate the accumulative histogram (the yellow display...)
<span style="white-space:pre">	</span>xnOSMemSet(g_pDepthHist, 0, g_nZRes*sizeof(float));


<span style="white-space:pre">	</span>unsigned int nNumberOfPoints = 0;
<span style="white-space:pre">	</span>for (XnUInt y = 0; y < g_depthMD.YRes(); ++y)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>for (XnUInt x = 0; x < g_depthMD.XRes(); ++x, ++pDepth)
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>if (*pDepth != 0)
<span style="white-space:pre">			</span>{
<span style="white-space:pre">				</span>g_pDepthHist[*pDepth]++;
<span style="white-space:pre">				</span>nNumberOfPoints++;
<span style="white-space:pre">			</span>}
<span style="white-space:pre">		</span>}
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>for (int nIndex = 1; nIndex<g_nZRes; nIndex++)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>g_pDepthHist[nIndex] += g_pDepthHist[nIndex - 1];
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>if (nNumberOfPoints)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>for (int nIndex = 1; nIndex<g_nZRes; nIndex++)
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>g_pDepthHist[nIndex] = (unsigned int)(256 * (1.0f - (g_pDepthHist[nIndex] / nNumberOfPoints)));
<span style="white-space:pre">		</span>}
<span style="white-space:pre">	</span>}


<span style="white-space:pre">	</span>xnOSMemSet(g_pTexMap, 0, g_nTexMapX*g_nTexMapY*sizeof(XnRGB24Pixel));


<span style="white-space:pre">	</span>// check if we need to draw image frame to texture
<span style="white-space:pre">	</span>if (g_nViewState == DISPLAY_MODE_OVERLAY ||
<span style="white-space:pre">		</span>g_nViewState == DISPLAY_MODE_IMAGE)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>const XnRGB24Pixel* pImageRow = g_imageMD.RGB24Data();
<span style="white-space:pre">		</span>XnRGB24Pixel* pTexRow = g_pTexMap + g_imageMD.YOffset() * g_nTexMapX;


<span style="white-space:pre">		</span>for (XnUInt y = 0; y < g_imageMD.YRes(); ++y)
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>const XnRGB24Pixel* pImage = pImageRow;
<span style="white-space:pre">			</span>XnRGB24Pixel* pTex = pTexRow + g_imageMD.XOffset();


<span style="white-space:pre">			</span>for (XnUInt x = 0; x < g_imageMD.XRes(); ++x, ++pImage, ++pTex)
<span style="white-space:pre">			</span>{
<span style="white-space:pre">				</span>*pTex = *pImage;
<span style="white-space:pre">			</span>}


<span style="white-space:pre">			</span>pImageRow += g_imageMD.XRes();
<span style="white-space:pre">			</span>pTexRow += g_nTexMapX;
<span style="white-space:pre">		</span>}
<span style="white-space:pre">	</span>}


<span style="white-space:pre">	</span>// check if we need to draw depth frame to texture
<span style="white-space:pre">	</span>if (g_nViewState == DISPLAY_MODE_OVERLAY ||
<span style="white-space:pre">		</span>g_nViewState == DISPLAY_MODE_DEPTH)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>const XnDepthPixel* pDepthRow = g_depthMD.Data();
<span style="white-space:pre">		</span>XnRGB24Pixel* pTexRow = g_pTexMap + g_depthMD.YOffset() * g_nTexMapX;


<span style="white-space:pre">		</span>for (XnUInt y = 0; y < g_depthMD.YRes(); ++y)
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>const XnDepthPixel* pDepth = pDepthRow;
<span style="white-space:pre">			</span>XnRGB24Pixel* pTex = pTexRow + g_depthMD.XOffset();


<span style="white-space:pre">			</span>for (XnUInt x = 0; x < g_depthMD.XRes(); ++x, ++pDepth, ++pTex)
<span style="white-space:pre">			</span>{
<span style="white-space:pre">				</span>if (*pDepth != 0)
<span style="white-space:pre">				</span>{
<span style="white-space:pre">					</span>int nHistValue = g_pDepthHist[*pDepth];
<span style="white-space:pre">					</span>pTex->nRed = nHistValue;
<span style="white-space:pre">					</span>pTex->nGreen = nHistValue;
<span style="white-space:pre">					</span>pTex->nBlue = 0;
<span style="white-space:pre">				</span>}
<span style="white-space:pre">			</span>}


<span style="white-space:pre">			</span>pDepthRow += g_depthMD.XRes();
<span style="white-space:pre">			</span>pTexRow += g_nTexMapX;
<span style="white-space:pre">		</span>}
<span style="white-space:pre">	</span>}


<span style="white-space:pre">	</span>// Create the OpenGL texture map
<span style="white-space:pre">	</span>glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
<span style="white-space:pre">	</span>glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
<span style="white-space:pre">	</span>glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
<span style="white-space:pre">	</span>glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, g_nTexMapX, g_nTexMapY, 0, GL_RGB, GL_UNSIGNED_BYTE, g_pTexMap);


<span style="white-space:pre">	</span>// Display the OpenGL texture map
<span style="white-space:pre">	</span>glColor4f(1, 1, 1, 1);


<span style="white-space:pre">	</span>glBegin(GL_QUADS);


<span style="white-space:pre">	</span>int nXRes = g_depthMD.FullXRes();
<span style="white-space:pre">	</span>int nYRes = g_depthMD.FullYRes();


<span style="white-space:pre">	</span>// upper left
<span style="white-space:pre">	</span>glTexCoord2f(0, 0);
<span style="white-space:pre">	</span>glVertex2f(0, 0);
<span style="white-space:pre">	</span>// upper right
<span style="white-space:pre">	</span>glTexCoord2f((float)nXRes / (float)g_nTexMapX, 0);
<span style="white-space:pre">	</span>glVertex2f(GL_WIN_SIZE_X, 0);
<span style="white-space:pre">	</span>// bottom right
<span style="white-space:pre">	</span>glTexCoord2f((float)nXRes / (float)g_nTexMapX, (float)nYRes / (float)g_nTexMapY);
<span style="white-space:pre">	</span>glVertex2f(GL_WIN_SIZE_X, GL_WIN_SIZE_Y);
<span style="white-space:pre">	</span>// bottom left
<span style="white-space:pre">	</span>glTexCoord2f(0, (float)nYRes / (float)g_nTexMapY);
<span style="white-space:pre">	</span>glVertex2f(0, GL_WIN_SIZE_Y);


<span style="white-space:pre">	</span>glEnd();


<span style="white-space:pre">	</span>// Swap the OpenGL display buffers
<span style="white-space:pre">	</span>glutSwapBuffers();
}


void glutKeyboard(unsigned char key, int /*x*/, int /*y*/)
{
<span style="white-space:pre">	</span>switch (key)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">	</span>case 27:
<span style="white-space:pre">		</span>exit(1);
<span style="white-space:pre">	</span>case '1':
<span style="white-space:pre">		</span>g_nViewState = DISPLAY_MODE_OVERLAY;
<span style="white-space:pre">		</span>g_depth.GetAlternativeViewPointCap().SetViewPoint(g_image);
<span style="white-space:pre">		</span>break;
<span style="white-space:pre">	</span>case '2':
<span style="white-space:pre">		</span>g_nViewState = DISPLAY_MODE_DEPTH;
<span style="white-space:pre">		</span>g_depth.GetAlternativeViewPointCap().ResetViewPoint();
<span style="white-space:pre">		</span>break;
<span style="white-space:pre">	</span>case '3':
<span style="white-space:pre">		</span>g_nViewState = DISPLAY_MODE_IMAGE;
<span style="white-space:pre">		</span>g_depth.GetAlternativeViewPointCap().ResetViewPoint();
<span style="white-space:pre">		</span>break;
<span style="white-space:pre">	</span>case 'm':
<span style="white-space:pre">		</span>g_context.SetGlobalMirror(!g_context.GetGlobalMirror());
<span style="white-space:pre">		</span>break;
<span style="white-space:pre">	</span>}
}


int main(int argc, char* argv[])
{
<span style="white-space:pre">	</span>XnStatus rc;


<span style="white-space:pre">	</span>EnumerationErrors errors;
<span style="white-space:pre">	</span>rc = g_context.InitFromXmlFile(SAMPLE_XML_PATH, g_scriptNode, &errors);
<span style="white-space:pre">	</span>if (rc == XN_STATUS_NO_NODE_PRESENT)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>XnChar strError[1024];
<span style="white-space:pre">		</span>errors.ToString(strError, 1024);
<span style="white-space:pre">		</span>printf("%s\n", strError);
<span style="white-space:pre">		</span>return (rc);
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>else if (rc != XN_STATUS_OK)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>printf("Open failed: %s\n", xnGetStatusString(rc));
<span style="white-space:pre">		</span>return (rc);
<span style="white-space:pre">	</span>}


<span style="white-space:pre">	</span>rc = g_context.FindExistingNode(XN_NODE_TYPE_DEPTH, g_depth);
<span style="white-space:pre">	</span>if (rc != XN_STATUS_OK)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>printf("No depth node exists! Check your XML.");
<span style="white-space:pre">		</span>return 1;
<span style="white-space:pre">	</span>}


<span style="white-space:pre">	</span>rc = g_context.FindExistingNode(XN_NODE_TYPE_IMAGE, g_image);
<span style="white-space:pre">	</span>if (rc != XN_STATUS_OK)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>printf("No image node exists! Check your XML.");
<span style="white-space:pre">		</span>return 1;
<span style="white-space:pre">	</span>}


<span style="white-space:pre">	</span>g_depth.GetMetaData(g_depthMD);
<span style="white-space:pre">	</span>g_image.GetMetaData(g_imageMD);


<span style="white-space:pre">	</span>// Hybrid mode isn't supported in this sample
<span style="white-space:pre">	</span>if (g_imageMD.FullXRes() != g_depthMD.FullXRes() || g_imageMD.FullYRes() != g_depthMD.FullYRes())
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>printf("The device depth and image resolution must be equal!\n");
<span style="white-space:pre">		</span>return 1;
<span style="white-space:pre">	</span>}


<span style="white-space:pre">	</span>// RGB is the only image format supported.
<span style="white-space:pre">	</span>if (g_imageMD.PixelFormat() != XN_PIXEL_FORMAT_RGB24)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>printf("The device image format must be RGB24\n");
<span style="white-space:pre">		</span>return 1;
<span style="white-space:pre">	</span>}


<span style="white-space:pre">	</span>// Texture map init
<span style="white-space:pre">	</span>g_nTexMapX = (((unsigned short)(g_depthMD.FullXRes() - 1) / 512) + 1) * 512;
<span style="white-space:pre">	</span>g_nTexMapY = (((unsigned short)(g_depthMD.FullYRes() - 1) / 512) + 1) * 512;
<span style="white-space:pre">	</span>g_pTexMap = (XnRGB24Pixel*)malloc(g_nTexMapX * g_nTexMapY * sizeof(XnRGB24Pixel));


<span style="white-space:pre">	</span>g_nZRes = g_depthMD.ZRes();
<span style="white-space:pre">	</span>g_pDepthHist = (float*)malloc(g_nZRes * sizeof(float));


<span style="white-space:pre">	</span>// OpenGL init
<span style="white-space:pre">	</span>glutInit(&argc, argv);
<span style="white-space:pre">	</span>glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
<span style="white-space:pre">	</span>glutInitWindowSize(GL_WIN_SIZE_X, GL_WIN_SIZE_Y);
<span style="white-space:pre">	</span>glutCreateWindow("OpenNI Simple Viewer");
<span style="white-space:pre">	</span>glutFullScreen();
<span style="white-space:pre">	</span>glutSetCursor(GLUT_CURSOR_NONE);


<span style="white-space:pre">	</span>glutKeyboardFunc(glutKeyboard);
<span style="white-space:pre">	</span>glutDisplayFunc(glutDisplay);
<span style="white-space:pre">	</span>glutIdleFunc(glutIdle);


<span style="white-space:pre">	</span>glDisable(GL_DEPTH_TEST);
<span style="white-space:pre">	</span>glEnable(GL_TEXTURE_2D);


<span style="white-space:pre">	</span>// Per frame code is in glutDisplay
<span style="white-space:pre">	</span>glutMainLoop();


<span style="white-space:pre">	</span>return 0;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值