《2022年OGRE基础教程基础教程八 .pdf》由会员分享,可在线阅读,更多相关《2022年OGRE基础教程基础教程八 .pdf(10页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、文档: 教程: 基础教程 : 基础教程八出自 Ogre3D开放资源地带跳转到 : 导航, 搜索Basic Tutorial 8: Using Multiple Scene ManagersFile:Forum icon question2.gif Any problems you encounter while working with this tutorial should be posted to the Help Forum. 目录?1 摘要?2 先决条件?3 Setting up the Applicationo3.1 Creating the SceneManagerso3.2 C
2、reating the Cameraso3.3 Creating the Viewportso3.4 Creating the Scene?4 Adding Functionalityo4.1 Dual SceneManagerso4.2 Swapping SceneManagers?5 Conclusiono5.1 Overlayso5.2 One Last Note摘要在这个简短的教程中我们将介绍如何转换多场景管理器。你可以在here 中找到该教程的源码。当你学习本教程的时候,你应该逐步得添加代码到你自己创建的工程中并且观看执行结果。先决条件在你选定的 IDE中创建一个 cpp 文件,然后
3、添加如下代码:#include ExampleApplication.h #define CAMERA_NAME SceneCamera void setupViewport(RenderWindow *win, SceneManager *curr) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 10 页 - - - - - - - - - void dualViewport(RenderWindow *win, SceneManager *primary, Scen
4、eManager *secondary) class SMTutorialListener : public ExampleFrameListener, public OIS:KeyListener public: SMTutorialListener(RenderWindow* win, SceneManager *primary, SceneManager *secondary) : ExampleFrameListener(win, primary-getCamera(CAMERA_NAME), true, false), mPrimary(primary), mSecondary(se
5、condary), mDual(false), mContinue(true) mKeyboard-setEventCallback(this); bool frameStarted(const FrameEvent& evt) mKeyboard-capture(); return mContinue; bool keyPressed(const OIS:KeyEvent &arg) switch (arg.key) case OIS:KC_ESCAPE: mContinue = false; break; default: break; return true; bool keyRelea
6、sed(const OIS:KeyEvent &) return true; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 10 页 - - - - - - - - - private: SceneManager *mPrimary, *mSecondary; bool mDual, mContinue; static void swap(SceneManager *&first, SceneManager *&second) SceneManager *tmp = f
7、irst; first = second; second = tmp; ; class SMTutorialApplication : public ExampleApplication public: SMTutorialApplication() SMTutorialApplication() protected: SceneManager *mPrimary, *mSecondary; void chooseSceneManager(void) void createCamera() void createViewports() void createScene(void) void c
8、reateFrameListener(void) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 10 页 - - - - - - - - - mFrameListener = new SMTutorialListener(mWindow, mPrimary, mSecondary); mFrameListener-showDebugOverlay(true); mRoot-addFrameListener(mFrameListener); ; #if OGRE_PLAT
9、FORM = OGRE_PLATFORM_WIN32 #define WIN32_LEAN_AND_MEAN #include windows.h INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT) #else int main(int argc, char *argv) #endif / Create application object SMTutorialApplication app; try app.go(); catch(Exception& e) #if OGRE_PLATFORM = OGR
10、E_PLATFORM_WIN32 MessageBoxA(NULL, e.getFullDescription().c_str(), An exception has occurred!, MB_OK | MB_ICONERROR | MB_TASKMODAL); #else fprintf(stderr, An exception has occurred: %sn, e.getFullDescription().c_str(); #endif return 0; 在你继续之前,请保证你可以成功编译这段代码。但是不要在这里就执行该程序。Setting up the Application C
11、reating the SceneManagers We have previously covered how to select your SceneManager, so I will not go into detail about this function. The only thing we have 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 10 页 - - - - - - - - - changed is that we are creating
12、two of them. Find the chooseSceneManager function and add the following code: mPrimary = mRoot-createSceneManager(ST_GENERIC, primary); mSecondary = mRoot-createSceneManager(ST_GENERIC, secondary); Creating the Cameras The next thing we need to do is create a Camera object for each of the two SceneM
13、anagers. The only difference from previous tutorials is that we are creating two of them, with the same name. Find the createCamera function and add the following code: mPrimary-createCamera(CAMERA_NAME); mSecondary-createCamera(CAMERA_NAME); Creating the Viewports In creating the Viewport for this
14、application, we will be taking a small departure from previous tutorials. When you create a Viewport, you must do two things: setup the Viewport itself and then set the aspect ratio of the camera you are using. To begin with, add the following code to the createViewports function: setupViewport(mWin
15、dow, mPrimary); The actual code for setting up the Viewport resides in this setupViewport function since we will use this code again elsewhere. The first thing we need to do is remove all the previously created Viewports. None have been created yet, but when we call this function again later, we wil
16、l need to make sure that they are all removed before creating new ones. After that we will setup the Viewports just like we have in previous tutorials. Add the following code to the setupViewport function at the top of the file: win-removeAllViewports(); Camera *cam = curr-getCamera(CAMERA_NAME); Vi
17、ewport *vp = win-addViewport(cam); vp-setBackgroundColour(ColourValue(0,0,0); cam-setAspectRatio(Real(vp-getActualWidth() / Real(vp-getActualHeight(); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 10 页 - - - - - - - - - Creating the Scene Lastly, we need to cr
18、eate a scene for each SceneManager to contain. We wont make anything complex, just something different so that we know when we have swapped between the two. Find the createScene function and add the following code: / Setup the TerrainSceneManager mPrimary-setSkyBox(true, Examples/SpaceSkyBox); / Set
19、up the Generic SceneManager mSecondary-setSkyDome(true, Examples/CloudySky, 5, 8); Be sure your code compiles before continuing. You may run the program at this point, but it currently has no functionality other than exiting when you press Escape. Adding Functionality Dual SceneManagers The first pi
20、ece of functionality we want to add to the program is to allow the user to render both SceneManagers side by side. When the V key is pressed we will toggle dual Viewport mode. The basic plan for this is simple. To turn off dual Viewport mode, we simply call setupViewport (which we created in the pre
21、vious section) with the primary SceneManager to recreate the Viewport in single mode. When we want to turn it on, we will call a new function called dualViewport. We will keep track of the state of the Viewport with the mDual variable. Add the following code to the switch in the keyPressed function:
22、 case OIS:KC_V: mDual = !mDual; if (mDual) dualViewport(mWindow, mPrimary, mSecondary); else setupViewport(mWindow, mPrimary); break; Now we have swapped the mDual variable and called the appropriate function based on which mode we are in. Now we will define the 名师资料总结 - - -精品资料欢迎下载 - - - - - - - -
23、- - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 10 页 - - - - - - - - - dualViewport function which actually contains the code to show two Viewports at once. In order to display two SceneManagers side by side, we basically do the same thing we have already done in the setupViewport function. The onl
24、y difference is that we will create two Viewports, one for each Camera in our SceneManagers. Add the following code to the dualViewport function: win-removeAllViewports(); Viewport *vp = 0; Camera *cam = primary-getCamera(CAMERA_NAME); vp = win-addViewport(cam, 0, 0, 0, 0.5, 1); vp-setBackgroundColo
25、ur(ColourValue(0,0,0); cam-setAspectRatio(Real(vp-getActualWidth() / Real(vp-getActualHeight(); cam = secondary-getCamera(CAMERA_NAME); vp = win-addViewport(cam, 1, 0.5, 0, 0.5, 1); vp-setBackgroundColour(ColourValue(0,0,0); cam-setAspectRatio(Real(vp-getActualWidth() / Real(vp-getActualHeight(); Al
26、l of this should be familiar except for the extra parameters we have added to the addViewport function call. The first parameter to this function is still the Camera we are using. The second parameter is the z order of the Viewport. A higher z order sits on top of the lower z orders. Note that you c
27、annot have two Viewports with the same z order, even if they do not overlap. The next two parameters are the left and top positions of the Viewport, which must be between 0 and 1. Finally, the last two parameters are the width and the height of the Viewport as a percentage of the screen (again, they
28、 must be between 0 and 1). So in this case, the first Viewport we create will be at the position (0, 0) and will take up half of the screen horizontally and all of the screen vertically. The second Viewport will be at position (0.5, 0) and also take up half the horizontal space and all of the vertic
29、al space. Compile and run the application. By pressing V you can now display two SceneManagers at the same time. Swapping SceneManagers 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 7 页,共 10 页 - - - - - - - - - The last piece of functionality we want to add to our p
30、rogram is to swap the SceneManagers whenever the C key is pressed. To do that, we will first swap the mPrimary and mSecondary variables so that when the setupViewport or dualViewport functions are called, we never need to worry about which SceneManager is in which variable. The primary SceneManager
31、will always be displayed in single mode, and the primary will always be on the left side in dualViewport mode. Add the following code to the switch in the keyPressed function: case OIS:KC_C: swap(mPrimary, mSecondary); After we swap the variables, we need to actually perform the change. All we have
32、to do is call the appropriate Viewport setup function depending on whether we are in dual or single mode: if (mDual) dualViewport(mWindow, mPrimary, mSecondary); else setupViewport(mWindow, mPrimary); break; Thats it! Compile and run the application. We can now swap the SceneManagers with the C key,
33、 and swap single and dual mode with the V key. Conclusion Overlays Im sure you have noticed in your program that when you run in dual Viewport mode, the Ogre debug Overlay shows up on both sides. You may turn off Overlay rendering on a per-Viewport basis. Use the Viewport:setOverlaysEnabled function
34、 to turn them on and off. I have made this relatively simple change to the full source of this tutorial, so if you are confused as to how this is done, see that page for the details. One Last Note Always keep in mind that the Viewport class, while not having a lot of functionality itself, is the key
35、 to all Ogre rendering. It doesnt matter how many SceneManagers you create or how many Cameras in each SceneManager, none of them will be rendered to the window unless you 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 8 页,共 10 页 - - - - - - - - - setup a Viewport fo
36、r each Camera you are showing. Also dont forget to clear out Viewports you are not using before creating more. Template:Ogre Tutorial取自 http:/ 1 个分类: Tutorials查看?页面?讨论?源码?历史个人工具?登录 创建账户导航?首页?社区?当前事件?最近更改?随机页面?帮助搜索进入搜索Google 搜索名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - -
37、- - 第 9 页,共 10 页 - - - - - - - - - your search terms t search form earch工具箱?链入页面?链出更改?特殊页面?可打印版?永久链接Google Adsense ?这页的最后修订在 2009 年 6 月 13日 ( 星期六 ) 14:16 。?本页面已经被浏览 1,056 次。?隐私政策?关于 Ogre3D开放资 源地带?沪 ICP备 09049564号名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 10 页,共 10 页 - - - - - - - - -