1、OGRE渲染输出到MFCOGRE渲染输出到MFC作者:qlwuu | 日期:2005-06-28 折腾了一天基本搞定,其实也挺容易,关键要注意几个地方 1)因为不在使用OGRE默认的配置窗口来初始化RenderSystem,现在这一步必须自己来做。首先设置配置文件路径,然后轮询可用的RenderSystem。这里默认使用Direct3D9RenderSystem。/和原来一样设置插件、资源路径,设置过后就可以轮讯可用的RenderSystem了setupResources(); /指定使用Direct3D9RenderSystem渲染子系统。RenderSystemList*rl=Root:g
2、etSingleton().getAvailableRenderers();RenderSystemList:iteratorit=rl-begin(); D3D9RenderSystem*mRSys=NULL;while(it!=rl-end() if(-1!=(*it)-getName().find(3D9)mRSys=(D3D9RenderSystem*)(*it);break;it+;/配置框中的选项需要手动设置。mRSys-initConfigOptions(); mRSys-setConfigOption(Antialiasing,None);mRSys-setConfigOpti
3、on(Floating-pointmode,Fastest);mRSys-setConfigOption(FullScreen,No);mRSys-setConfigOption(RenderingDevice,NVIDIAGeForce2MX/MX400);mRSys-setConfigOption(VSync,No);mRSys-setConfigOption(VideoMode,640x48032-bitcolour);/起用mRoot-setRenderSystem(mRSys); 2)剩下的初始化过程和ExampleApplication基本一致,只不过现在不需要由OGRE自动创建窗
4、体,应该将MFC视图的句柄传过去初始化。/初始化,传入false表示不需要OGRE自动创建窗口mRoot-initialise(false);/手动创建渲染窗口,在这里我们将MFC视图的句柄传入NamevaluePairListmiscParams; miscParamsexternalWindowHandle=StringConverter:toString(size_t)mWnd);mWindow=mRoot-createRenderWindow(View,640,480,false,&miscParams);到次初始化过程已经完毕,这时候运行程序就得到一个漆黑的MFC窗口。3)因为现在使
5、用是视图的WndProc,我们必须自己处理更新和触发FrameStart事件./触发FrameStart,FrameEnd事件voidupdate(void)mRoot-_fireFrameStarted();mWindow-update();mRoot-_fireFrameEnded();/在视图的OnDraw时间里,调用updatevoidCMFCRenderView:OnDraw(CDC*/*pDC*/)CMFCRenderDoc*pDoc=GetDocument();ASSERT_VALID(pDoc);if(!pDoc)return;/TODO:在此处为本机数据添加绘制代码updat
6、e(); 这样还不够,WM_PAINT事件并不由我们控制,当窗体静静的趟在那的时候是不会触发的,因此我们通过设置一个timer来模拟没帧的更新,/20ms触发一次SetTimer(100,20,0);voidCMFCRenderView:OnTimer(UINTnIDEvent)/TODO:在此添加消息处理程序代码和/或调用默认值update();_super:OnTimer(nIDEvent);注意:(1) 初始化可以在OnDraw(CDC* pDC)的第一次执行时候进行,而不能在PreCreateWindow(CREATESTRUCT& cs) 或者COgreMFCView( )或者OnC
7、reate()这个CView的子类初始化的时候进行因为这个时候,View对应的HWND实际上还没初始化出来呢. (2) 初始化完了 一定记得用RenderWindow* window-update()一下,否则看不出来-_-|MFC OGRE 代码:/ OgreMFCView.cpp : COgreMFCView 类的实现/#include stdafx.h#include OgreMFC.h#include OgreMFCDoc.h#include OgreMFCView.h#include OgreRenderSystem.h#include OgreRoot.h#include Ogre
8、GLRenderSystem.h #include OgreD3D9RenderSystem.h#include OgreCommon.h#include OgreRenderWindow.h #include OgreStringConverter.h #include OgreConfigFile.h #include OgreCamera.h #include OgreViewport.h #include OgreMeshManager.h #include OgreEntity.h #include .ogremfcview.husing namespace Ogre;#ifdef
9、_DEBUG#define new DEBUG_NEW#endif/ COgreMFCViewIMPLEMENT_DYNCREATE(COgreMFCView, CView)BEGIN_MESSAGE_MAP(COgreMFCView, CView) / 标准打印命令 ON_COMMAND(ID_FILE_PRINT, CView:OnFilePrint) ON_COMMAND(ID_FILE_PRINT_DIRECT, CView:OnFilePrint) ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView:OnFilePrintPreview) ON_WM_CR
10、EATE()END_MESSAGE_MAP()/ COgreMFCView 构造/析构COgreMFCView:COgreMFCView()COgreMFCView:COgreMFCView() BOOL COgreMFCView:PreCreateWindow(CREATESTRUCT& cs) / TODO: 在此处通过修改 CREATESTRUCT cs 来修改窗口类或 / 样式 return CView:PreCreateWindow(cs);/ COgreMFCView 绘制void COgreMFCView:OnDraw(CDC* pDC) static bool once = t
11、rue; COgreMFCDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if (!pDoc) return; / TODO: 在此处为本机数据添加绘制代码 /RECT rect = 20,30,180,230; /pDC-DrawText(ZTZTZTZTZTZTTZT!,&rect,DT_CENTER); static Root* mRoot; static RenderWindow* mWindow; static SceneManager* mSceneMgr; static Camera* mCamera; if(once) mRoot
12、= new Root(); setupResources(); RenderSystemList* rl = Root:getSingleton().getAvailableRenderers(); /D3D9RenderSystem GLRenderSystem* rsys = NULL; RenderSystemList:iterator it = rl-begin(); while( it != rl-end() ) if( -1 != ( *it )-getName().find( GL ) ) rsys = (GLRenderSystem*)( *it ); break; it+;
13、/rsys-initConfigOptions(); rsys-setConfigOption(Colour Depth, 32 ); /rsys-setConfigOption(Anti aliasing, None ); /rsys-setConfigOption(Floating-point mode, Fastest ); rsys-setConfigOption( Full Screen, No ); /rsys-setConfigOption(Rendering Device,NVIDIA GeForce FX 5700VE); rsys-setConfigOption( VSyn
14、c, No ); /rsys-setConfigOption( Video Mode, 800 x 600 32-bit colour ); rsys-setConfigOption( Video Mode, 800 x 600 ); rsys-setConfigOption( Display Frequency, 60 ); / 起用 mRoot-setRenderSystem( rsys ); mRoot-initialise( false ); NameValuePairList miscParams; unsigned int h = (unsigned int)this-GetSaf
15、eHwnd(); miscParamsexternalWindowHandle = StringConverter:toString(h); mWindow = NULL; mWindow = mRoot-createRenderWindow( View, 640, 480, false, &miscParams ); once = false; mSceneMgr = mRoot-getSceneManager(ST_GENERIC); / Create the camera mCamera = mSceneMgr-createCamera(PlayerCam); / Position it
16、 at 500 in Z direction mCamera-setPosition(Vector3(0,0,500); / Look back along -Z mCamera-lookAt(Vector3(0,0,-300); mCamera-setNearClipDistance(5); / Create one viewport, entire window Viewport* vp = mWindow-addViewport(mCamera); vp-setBackgroundColour(ColourValue(0,0,0); / Alter the camera aspect r
17、atio to match the viewport mCamera-setAspectRatio( Real(vp-getActualWidth() / Real(vp-getActualHeight(); ResourceGroupManager:getSingleton().initialiseAllResourceGroups(); /createScene(); / Set ambient light mSceneMgr-setAmbientLight(ColourValue(0.5, 0.5, 0.5); / Create a skydome mSceneMgr-setSkyDom
18、e(true, Examples/CloudySky, 5, 8); / Create a light Light* l = mSceneMgr-createLight(MainLight); / Accept default settings: point light, white diffuse, just set position / NB I could attach the light to a SceneNode if I wanted it to move automatically with / other objects, but I dont l-setPosition(2
19、0,80,50); Entity *ent; / Define a floor plane mesh Plane p; p.normal = Vector3:UNIT_Y; p.d = 200; MeshManager:getSingleton().createPlane(FloorPlane, ResourceGroupManager:DEFAULT_RESOURCE_GROUP_NAME, p,2000,2000,1,1,true,1,5,5,Vector3:UNIT_Z); / Create an entity (the floor) ent = mSceneMgr-createEnti
20、ty(floor, FloorPlane); ent-setMaterialName(Examples/RustySteel); mSceneMgr-getRootSceneNode()-attachObject(ent); ent = mSceneMgr-createEntity(head, ogrehead.mesh); / Attach to child of root node, better for culling (otherwise bounds are the combination of the 2) mSceneMgr-getRootSceneNode()-createCh
21、ildSceneNode()-attachObject(ent); mWindow-update(); else /mRoot-startRendering(); mWindow-update(); /drawScene(); / COgreMFCView 打印BOOL COgreMFCView:OnPreparePrinting(CPrintInfo* pInfo) / 默认准备 return DoPreparePrinting(pInfo);void COgreMFCView:OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) / TO
22、DO: 打印前添加额外的初始化void COgreMFCView:OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) / TODO: 打印后添加清除过程/ COgreMFCView 诊断#ifdef _DEBUGvoid COgreMFCView:AssertValid() const CView:AssertValid();void COgreMFCView:Dump(CDumpContext& dc) const CView:Dump(dc);COgreMFCDoc* COgreMFCView:GetDocument() const / 非
23、调试版本是内联的 ASSERT(m_pDocument-IsKindOf(RUNTIME_CLASS(COgreMFCDoc); return (COgreMFCDoc*)m_pDocument;#endif /_DEBUG/ COgreMFCView 消息处理程序int COgreMFCView:OnCreate(LPCREATESTRUCT lpCreateStruct) if (CView:OnCreate(lpCreateStruct) = -1) return -1; / TODO: 在此处添加构造代码 return 0;void COgreMFCView:setupResource
24、s(void) / Load resource paths from config file ConfigFile cf; cf.load(resources.cfg); / Go through all sections & settings in the file ConfigFile:SectionIterator seci = cf.getSectionIterator(); String secName, typeName, archName; while (seci.hasMoreElements() secName = seci.peekNextKey(); ConfigFile:SettingsMultiMap *settings = seci.getNext(); ConfigFile:SettingsMultiMap:iterator i; for (i = settings-begin(); i != settings-end(); +i) typeName = i-first; archName = i-second; ResourceGroupManager:getSingleton().addResourceLocation( archName, typeName, secName);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1