Basic tutorial 8 (use of multi-scenario Manager)

Source: Internet
Author: User
Summary

In this short tutorial, we will introduce how to convert the multi-scenario manager. You can find the source code of this tutorial in here. When you are studying this tutorial, you should gradually add code to your own project and view the execution results.

Prerequisites

Create a CPP file in the IDE you selected and add the following code:

#include "ExampleApplication.h"#define CAMERA_NAME "SceneCamera"void setupViewport(RenderWindow *win, SceneManager *curr){}void dualViewport(RenderWindow *win, SceneManager *primary, SceneManager *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(secondary), 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 keyReleased(const OIS::KeyEvent &) {return true;}private:    SceneManager *mPrimary, *mSecondary;    bool mDual, mContinue;    static void swap(SceneManager *&first, SceneManager *&second)    {        SceneManager *tmp = first;        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 createFrameListener(void)    {        mFrameListener = new SMTutorialListener(mWindow, mPrimary, mSecondary);        mFrameListener->showDebugOverlay(true);        mRoot->addFrameListener(mFrameListener);    }};#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32 #define WIN32_LEAN_AND_MEAN#include "windows.h"INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT)#elseint main(int argc, char **argv)#endif{    // Create application object    SMTutorialApplication app;    try {        app.go();    } catch(Exception& e) {#if OGRE_PLATFORM == OGRE_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: %s/n",            e.getFullDescription().c_str());#endif    }    return 0;}

Before you proceed, make sure that you can successfully compile this code. But do not execute this program here.

Create a project

Create a scenario Manager

In the previous tutorial, we have learned the scenario manager. Therefore, we will not describe this function in detail. We just want to exchange the scenario we created. Locate choosescenemanager () and add the following code to it:

       mPrimary = mRoot->createSceneManager(ST_GENERIC, "primary");       mSecondary = mRoot->createSceneManager(ST_GENERIC, "secondary");

Create a camera

Next, we need to create a camera for each scenario. Unlike the previous tutorial, we need to create two cameras with the same name. Find createcamera () and add the following code to it:

       mPrimary->createCamera(CAMERA_NAME);       mSecondary->createCamera(CAMERA_NAME);

Creating the viewports

Creating a viewport in this project is somewhat different from the previous tutorial. When creating a view, you must do the following: Create a view by yourself and set the aspect ratio for the camera you are using. First, add the following code to createviewports ()

       setupViewport(mWindow, mPrimary);

Add the following code to setupviewport ()

   win->removeAllViewports();   Camera *cam = curr->getCamera(CAMERA_NAME);   Viewport *vp = win->addViewport(cam);   vp->setBackgroundColour(ColourValue(0,0,0));   cam->setAspectRatio(Real(vp->getActualWidth()) / Real(vp->getActualHeight()));

Scenario

Finally, we need to create a scenario for each scenario manager. We will not add some complicated things. We only need to make a few changes to the two scenarios and add the following code to createscene ():

       // Setup the TerrainSceneManager       mPrimary->setSkyBox(true, "Examples/SpaceSkyBox");       // Setup the Generic SceneManager       mSecondary->setSkyDome(true, "Examples/CloudySky", 5, 8);

Before continuing, you must ensure that your program can be correctly compiled. You can compile your program now, but there is no other function in the program except to exit!

Add feature

Add feature

In doing the first feature, we want to add it to the program so that it allows users to side two scenemanagers. When the V Key is pressed, we switch the two-mode of the view. This is a simple basic plan. To disable the dual-view mode, call setupviewport (the main scenario manager we created in the previous section) to recreate the single-mode view. When we want to open it, we will call a new function to call dualviewport. We will continue to check the status variable and the mdual orbit. Add the following code to keypressed ():

       case OIS::KC_V:           mDual = !mDual;           if (mDual)               dualViewport(mWindow, mPrimary, mSecondary);           else               setupViewport(mWindow, mPrimary);           break;

On the basis of our current situation, we have exchanged mdual variables and function calls in an appropriate way. We will determine the Code actually contained in dualviewport () to display it in two views at a time.

To display the two scenemanagers side by side, we basically do the same thing, and we have done it in the setupviewport function. The only difference is that we will create two sdks and one of the cameras in scenemanagers. Add the following code to dualviewport ():

   win->removeAllViewports();   Viewport *vp = 0;   Camera *cam = primary->getCamera(CAMERA_NAME);   vp = win->addViewport(cam, 0, 0, 0, 0.5, 1);   vp->setBackgroundColour(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()));

All of this should be familiar, except for the additional parameters we have added to the addviewport function call. The first parameter is still the camera we use. The second parameter is the Z-order (equivalent to the depth buffer) of the view ). The value of Z is displayed before the value of Z. Please note that you cannot have the same Z-order two views even if they do not overlap. The following two parameters must be in the left-side and top-side positions of the viewport, which must be between 0 and 1. The last two parameters are the width and the height of the screen aspect ratio (similarly, they must be 0 and 1 ). Therefore, in this case, we create the first viewport that will be at the position (0, 0) and will be at half the screen level, and all the screens will be vertical. The second will be in the position of the viewport (0.5, 0), and take a half of the horizontal space and vertical space.

Compile and run the application. Press V and you can now display two scenemanagers at the same time.

Exchange scenario

In terms of functionality, we want to add to our plan is to exchange scenemanagers whenever the C key is pressed. To do this, we will replace the mprimary and msecondary variables first, so that when the setupviewport or dualviewport function is called, we never have to worry about the variables in the scenario manager. The main scenario manager will always be displayed in single mode, and the other will always be on the left side of dualviewport mode. Add the following code to keypressed ():

       case OIS::KC_C:           swap(mPrimary, mSecondary);

Finally, we need to change the variables. All we need to do is to call the appropriate viewport setting function, whether we are in a different single or dual mode:

           if (mDual)               dualViewport(mWindow, mPrimary, mSecondary);           else               setupViewport(mWindow, mPrimary);           break;

That's it! Compile and run the application. Now we can switch to a C-Key scenemanagers and switch to a V-key single-mode or dual-mode

Suggestions

Overlays

I make sure that you will find in the program that when you are running in dual-view mode, the ogre coverage in debug mode shows both sides. You may close the display that is overwritten in the viewport. Use the: setoverlaysenabled function to open and close them. I have this relatively simple change to the full source code of this tutorial, so if you do not know how to do this, please view the relevant details and web pages.

Last

Even though there are not many features, it is the key to rendering all diners. It doesn't matter how many scenemanagers you have created or how many cameras are displayed to the window in each scenario manager unless you set each camera to your rendering port. Also, do not forget to clear the previously created views that you did not use!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.