Ogre中显示Kinect的彩色图像

首先创建材质文件以显示所需的图片

//-------------------------------------------------------------------------------------
void SetupImageMaterial()
{
	// Create the texture
	TexturePtr texture = TextureManager::getSingleton().createManual(
			"MyImageTexture", // name
			ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
			TEX_TYPE_2D,      // type
			m_Width, m_Height,         // width & height
			0,                // number of mipmaps
			PF_B8G8R8A8,     // pixel format PF_BYTE_BGRA
			TU_DYNAMIC_WRITE_ONLY_DISCARDABLE);// TU_WRITE_ONLY

	// Create a material using the texture
	MaterialPtr material = MaterialManager::getSingleton().create(
			"ImageTextureMaterial", // name
			ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);

	material->getTechnique(0)->getPass(0)->createTextureUnitState("MyImageTexture");
}
作为前期测试,我们可以设定某一个颜色以在Ogre中生成动态纹理,方式如下:

void UpdateImageTexture()
{
	TexturePtr texture = TextureManager::getSingleton().getByName("MyImageTexture");
	// Get the pixel buffer
	Ogre::HardwarePixelBufferSharedPtr pixelBuffer = texture->getBuffer(); 

	// Lock the pixel buffer and get a pixel box
	pixelBuffer->lock(0, 480*640*4, Ogre::HardwareBuffer::HBL_DISCARD);
	const Ogre::PixelBox &pixelBox = pixelBuffer->getCurrentLock();  

	unsigned char* pDest = static_cast<unsigned char*>(pixelBox.data);  

	for(size_t j = 0; j<640; j++)
	{
		for(size_t i = 0; i<480; i++) // width
		{
			pDest[ ((j*480)+i)*4 + 0 ] = 255;
			pDest[ ((j*480)+i)*4 + 1 ] = 255;
			pDest[ ((j*480)+i)*4 + 2 ] = 0;
			pDest[ ((j*480)+i)*4 + 3 ] = 255;
		}
	}
	
	pixelBuffer->unlock(); 
}
最后,换成利用Kinect更新数据,如下所示

void UpdateImageTexture()
{
	TexturePtr texture = TextureManager::getSingleton().getByName("MyImageTexture");

	Ogre::HardwarePixelBufferSharedPtr buffer=texture->getBuffer(0,0);  
	buffer->lock(Ogre::HardwareBuffer::HBL_NORMAL);//HBL_DISCARD);  
	const Ogre::PixelBox &pb = buffer->getCurrentLock();  
	Ogre::uint32 *data = static_cast<Ogre::uint32*>(pb.data);  
	size_t height = pb.getHeight();  
	size_t width = pb.getWidth();   
	size_t pitch = pb.rowPitch; // Skip between rows of image  
	xn::ImageMetaData  imageMD;
	m_ImageGenerator.GetMetaData(imageMD);
	XnUInt16 g_nXRes = imageMD.XRes();
	XnUInt16 g_nYRes = imageMD.YRes();
	const XnUInt8* pImage = imageMD.Data(); 

	for(size_t j = 0; j<height; j++)
	{
		for(size_t i = 0; i< width; i++)
		{
			if(imageMD.PixelFormat()==XN_PIXEL_FORMAT_RGB24)
			{
				unsigned char R=pImage[0];  
				unsigned char G=pImage[1];  
				unsigned char B=pImage[2];  
				pImage+=3;
				Ogre::uint32 pixel=(R<<16)+(G<<8)+(B);  
				data[pitch*j + width-1-i] =pixel ;
			}
			else
			{
				unsigned char R=12;  
				unsigned char G=120;  
				unsigned char B=12;  
				Ogre::uint32 pixel=(R<<16)+(G<<8)+(B);  
				data[pitch*j + width-1-i] =pixel ;
			}
		}
	}

	buffer->unlock(); 
}





评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值