在Ogre中直接使用D3D
使用Ogre时可以通过RenderSystem_Direct3D9直接访问D3D
直接使用D3D需要安装D3D SDK,典型的为2010 June SDK
需指定包含路径:
C:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Include
$(Ogre181)\include\OGRE
库路径:
$(Ogre181)\lib\Debug\opt\RenderSystem_Direct3D9_d.lib
C:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Lib\x86\d3d9.lib
C:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Lib\x86\d3dx9.lib
C:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Lib\x86\DxErr.lib
C:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Lib\x86\dxguid.lib
这些和RenderSystem_Direct3D9项目设置一样
实际代码需包含头文件:
#include <Ogre/RenderSystems/Direct3D9/OgreD3D9RenderWindow.h>
#include <Ogre/RenderSystems/Direct3D9/OgreD3D9RenderSystem.h>
获取D3D 设备:
IDirect3DDevice9* pDevice = Ogre::D3D9RenderSystem::getActiveD3D9Device(); mWindow->update(false); //这里不翻转,由获取到的Deviece9处理 pDevice->Present(NULL, NULL, NULL, NULL); //测试获取到的Device9是否工作
可以看到获取到的IDirect3DDevice9可以正常使用
再演示一下如何解决上一篇文章中提到的实际问题:获取Ogre或D3D的渲染结果的效率问题
该问题需要获取IDirect3DSurface9和创建离屏表面等
通过查看RenderSystem_Direct3D9源码,可以使用以下办法:
IDirect3DDevice9* pDevice = Ogre::D3D9RenderSystem::getActiveD3D9Device(); IDirect3DSurface9 *surface = NULL; D3DLOCKED_RECT lrect; // Filled in by D3D IDirect3DSurface9 *backSurface; pDevice->CreateOffscreenPlainSurface(renderTarget->getWidth(), renderTarget->getHeight(), D3DFMT_X8R8G8B8, D3DPOOL_SYSTEMMEM, &surface, 0); //renderTarget可以为RenderWindow或者RenderTexture renderTarget->getCustomAttribute("DDBACKBUFFER", &backSurface); pDevice->GetRenderTargetData(backSurface, surface); surface->LockRect(&lrect, NULL, D3DLOCK_READONLY); char* data = (char *)lrect.pBits; //需要获取的数据指针 surface->UnlockRect();
代码段中未定义的变量renderTarget为RenderTarget*
因为D3D9RenerWindow和D3D9RenderTexture的getCustomAttribute方式都提供了通过"DDBACKBUFFER"获取后表面
所以以上代码既可以用于渲染窗口也可以用于渲染纹理
后附: D3DDevice可以直接使用renderTarget->getCustomAttribute("D3DDEVICE", &pDevice)获取, 避免引用OgreD3D9RenderSystem那么麻烦:)