source: trunk/VUT/work/TestCullingTerrain/TestCullingTerrainApplication.cpp @ 121

Revision 121, 8.8 KB checked in by mattausch, 19 years ago (diff)
RevLine 
[61]1#include <OgreNoMemoryMacros.h>
2#include <CEGUI/CEGUI.h>
3#include <../CEGUIRenderer/include/OgreCEGUIRenderer.h>
4#include <../CEGUIRenderer/include/OgreCEGUIResourceProvider.h>
5#include <../CEGUIRenderer/include/OgreCEGUITexture.h>
6#include <OgreMemoryMacros.h>
7
[100]8#include <Ogre.h>
[115]9//#include "OgreReferenceAppLayer.h"
[100]10//#include "OgreRefAppWorld.h"
[61]11#include "TestCullingTerrainApplication.h"
12
[115]13
[61]14#define WIN32_LEAN_AND_MEAN
[94]15#include <windows.h>
[61]16
[80]17/*******************************************************/
18/*     TestCullingTerrainApplication implementation    */
19/*******************************************************/
[100]20
21//-----------------------------------------------------------------------
[61]22TestCullingTerrainApplication::~TestCullingTerrainApplication()
23{
[93]24        if(mTerrainContentGenerator)
25                delete mTerrainContentGenerator;
[99]26        //if(mRenderTargetListener)     delete mRenderTargetListener;
[61]27}
28//-----------------------------------------------------------------------
[93]29void TestCullingTerrainApplication::createCamera()
[61]30{
[99]31        // create the camera
[103]32        mCamera = mSceneMgr->createCamera("PlayerCam");
[93]33       
[100]34        /** set a nice viewpoint
35        *       we use a camera node here and apply all transformations on it instead
36        *       of applying all transformations directly to the camera
37        *       because then the camera is displayed correctly in the visualization
38        */
39        mCamNode = mSceneMgr->getRootSceneNode()->createChildSceneNode(
40                "CamNode1", Vector3(707, 5000, 528));
41        //mCamera->setPosition(707, 5000, 528);
42        mCamNode->setOrientation(Quaternion(-0.3486, 0.0122, 0.9365, 0.0329));
43        mCamNode->attachObject(mCamera);
[61]44
[100]45        //-- create visualization camera
46        mVizCamera = mSceneMgr->createCamera("VizCam");
47        mVizCamera->setPosition(mCamNode->getPosition());
48
49        mVizCamera->setNearClipDistance(1);
[93]50        mCamera->setNearClipDistance(1);
[61]51
[99]52        // infinite far plane?
[93]53        if (mRoot->getRenderSystem()->getCapabilities()->hasCapability(RSC_INFINITE_FAR_PLANE))
54        {
[100]55                mVizCamera->setFarClipDistance(0);
[93]56                mCamera->setFarClipDistance(0);
57        }
58        else
59        {
[104]60                 mVizCamera->setFarClipDistance(20000);
61                 mCamera->setFarClipDistance(20000);
[99]62        }       
[61]63}
[99]64
[61]65//-----------------------------------------------------------------------
[99]66bool TestCullingTerrainApplication::setup()
67{
68        bool result = ExampleApplication::setup();
69
70        createRenderTargetListener();
71
72        return result;
73}
74//-----------------------------------------------------------------------
75void TestCullingTerrainApplication::createRenderTargetListener()
76{
77        mWindow->addListener(new VisualizationRenderTargetListener(mSceneMgr));
78}
79//-----------------------------------------------------------------------
[75]80void TestCullingTerrainApplication::createScene()
[61]81{
82        // Set ambient light
[112]83        mSceneMgr->setAmbientLight(ColourValue(0.5, 0.5, 0.5));
[113]84       
[110]85        //-- create light
[111]86        mSunLight = mSceneMgr->createLight("SunLight");
87        mSunLight->setType(Light::LT_DIRECTIONAL);
88        //mSunLight->setType(Light::LT_SPOTLIGHT);
[112]89        //mSunLight->setSpotlightRange(Degree(30), Degree(50));
90
[111]91    mSunLight->setPosition(707, 2000, 500);
92        mSunLight->setCastShadows(true);
93
[113]94        Vector3 dir(0.5, 1, 0.5);
[110]95        dir.normalise();
[112]96        mSunLight->setDirection(dir);
97        //mSunLight->setDirection(Vector3::NEGATIVE_UNIT_Y);
[110]98
[111]99        mSunLight->setDiffuseColour(1, 1, 1);
100        mSunLight->setSpecularColour(1, 1, 1);
[110]101
[61]102        // --Fog
103        // NB it's VERY important to set this before calling setWorldGeometry
104        // because the vertex program picked will be different
105        ColourValue fadeColour(0.93, 0.86, 0.76);
106        mWindow->getViewport(0)->setBackgroundColour(fadeColour);
107        //mSceneMgr->setFog( FOG_LINEAR, fadeColour, .001, 500, 1000);
[94]108       
[61]109        // Create a skybox
[103]110        mSceneMgr->setSkyBox(true, "Examples/SpaceSkyBox", 5000, false);
111       
[61]112        std::string terrain_cfg("terrain.cfg");
113#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
114        terrain_cfg = mResourcePath + terrain_cfg;
115#endif
[74]116        mSceneMgr->setWorldGeometry(terrain_cfg);
[75]117       
[100]118        //-- CEGUI setup
[61]119        setupGui();
[100]120
[111]121        // Floor plane
[121]122        /*
123        Plane plane;
[111]124        plane.normal = Vector3::UNIT_Y;
125        plane.d = -60;
126        MeshManager::getSingleton().createPlane("Myplane",
127                ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, plane,
128                5000,5000,100,100,true,1,5,5,Vector3::UNIT_Z);
129        Entity* pPlaneEnt = mSceneMgr->createEntity( "plane", "Myplane" );
130        pPlaneEnt->setMaterialName("Examples/Rockwall");
131        pPlaneEnt->setCastShadows(true);
[121]132        mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(pPlaneEnt);
133        */
[111]134
135        if (mRoot->getRenderSystem()->getCapabilities()->hasCapability(RSC_HWRENDER_TO_TEXTURE))
136    {
137                // In D3D, use a 1024x1024 shadow texture
138                mSceneMgr->setShadowTextureSettings(1024, 2);
139        }
140        else
141        {
142                // Use 512x512 texture in GL since we can't go higher than the window res
143                mSceneMgr->setShadowTextureSettings(512, 2);
144        }
[112]145        //mSceneMgr->setShadowColour(ColourValue(0, 0, 0));
146        mSceneMgr->setShadowColour(ColourValue(0.5, 0.5, 0.5));
147    // mSceneMgr->setShowDebugShadows(true);
[111]148
149
[100]150        //-- terrain content setup
151
[80]152        // HACK: necessary to call once before the content creation for
153        // terrain initialisation
154        mSceneMgr->_renderScene(mCamera, mWindow->getViewport(0), true);
[74]155
[82]156        mTerrainContentGenerator = new TerrainContentGenerator(mSceneMgr);
[110]157       
[107]158        // if no objects file generate yourself
[110]159        if (!mTerrainContentGenerator->LoadObjects("objects.out"))
160        {
[107]161                // height is restricted to 50, so no objects appear on peaks
162                // => there is much occlusion
163                mTerrainContentGenerator->SetMaxPos(Vector3(3000.0f, 50.0f, 3000.0f));
[111]164                mTerrainContentGenerator->SetOffset(0);
[110]165
166                // the objects are generated on the whole terrain
[120]167                mTerrainContentGenerator->GenerateScene(1500, "robot");
[112]168                //mTerrainContentGenerator->GenerateScene(1500, "athene");
[120]169                //mTerrainContentGenerator->GenerateScene(500, "ninja");
[110]170        }
[86]171
[84]172        // no limitations needed anymore: the user can set
173        // objects also on peaks of terrain
[107]174        mTerrainContentGenerator->SetMaxPos(Vector3(3000.0f, 5000.0f, 3000.0f));
[61]175}
176//-----------------------------------------------------------------------
[80]177void TestCullingTerrainApplication::setupGui()
[61]178{
[75]179         mGUIRenderer = new CEGUI::OgreCEGUIRenderer(mWindow, Ogre::RENDER_QUEUE_OVERLAY,
180                 false, 3000, ST_EXTERIOR_CLOSE);
[61]181     mGUISystem = new CEGUI::System(mGUIRenderer);
182
183         // Mouse
184     CEGUI::SchemeManager::getSingleton().loadScheme((CEGUI::utf8*)"TaharezLook.scheme");
185     CEGUI::MouseCursor::getSingleton().setImage("TaharezLook", "MouseArrow");
186         mGUISystem->setDefaultMouseCursor(
187                (CEGUI::utf8*)"TaharezLook", (CEGUI::utf8*)"MouseArrow");
188
[75]189         CEGUI::MouseCursor::getSingleton().show();
[61]190}
191//-----------------------------------------------------------------------
[80]192void TestCullingTerrainApplication::createFrameListener()
[61]193{
[115]194        mTerrainFrameListener = new TerrainMouseQueryListener(mWindow, mCamera, mSceneMgr,
[121]195                mGUIRenderer, mTerrainContentGenerator, mVizCamera, mCamNode, mSunLight);
[120]196       
[115]197        mRoot->addFrameListener(mTerrainFrameListener);
[61]198}
199//-----------------------------------------------------------------------
[80]200void TestCullingTerrainApplication::chooseSceneManager()
[61]201{
202        mSceneMgr = mRoot->getSceneManager(ST_EXTERIOR_CLOSE);
203}
204
205
[99]206/**************************************************************/
207/*      VisualizationRenderTargetListener implementation      */
208/**************************************************************/
[107]209//-----------------------------------------------------------------------
[99]210VisualizationRenderTargetListener::VisualizationRenderTargetListener(SceneManager *sceneMgr)
211:RenderTargetListener(), mSceneMgr(sceneMgr)
212{
213}
[61]214//-----------------------------------------------------------------------
[99]215void VisualizationRenderTargetListener::preViewportUpdate(const RenderTargetViewportEvent &evt)
216{
[100]217        const bool showViz = evt.source->getZOrder() == VIZ_VIEWPORT_Z_ORDER; // visualization viewport
218        const bool nShowViz = !showViz;
219
[113]220        // ambient light must be full for visualization
221    if (showViz)
222        {
223                mSceneMgr->setAmbientLight(ColourValue(1, 1, 1));
224        }
225        else
226        {
227                mSceneMgr->setAmbientLight(ColourValue(0.5, 0.5, 0.5));
228        }
229
[100]230    mSceneMgr->setOption("ShowVisualization", &showViz);
231        mSceneMgr->setOption("SkyBoxEnabled", &nShowViz);
232        //mSceneMgr->setOption("SkyPlaneEnabled", &showViz);
[99]233       
234        RenderTargetListener::preViewportUpdate(evt);
235}
236//-----------------------------------------------------------------------
237void VisualizationRenderTargetListener::postRenderTargetUpdate(const RenderTargetEvent &evt)
238{
239        RenderTargetListener::postRenderTargetUpdate(evt);
240}
241//-----------------------------------------------------------------------
[61]242INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
243{
244    // Create application object
245    TestCullingTerrainApplication app;
246
247        try
248        {
249        app.go();
250    }
251        catch( Ogre::Exception& e )
252        {
[75]253        MessageBox( NULL, e.getFullDescription().c_str(),
254                        "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
[61]255    }   
256
257    return 0;
258}
Note: See TracBrowser for help on using the repository browser.