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

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