root/simulator/trunk/src/simulator/graphics.cpp

Revision 1467, 13.8 kB (checked in by sehenley, 6 months ago)

Ogre 1.7.0 update

  • Property WBS set to 1.1.4.1
  • Property svn:keywords set to
    Url
    Rev
    Author
    Date
    Id
Line 
1 //    OSRail -- a network enabled railroad operations simulator and utilities
2 //    Copyright (C) 2007,2008 Samuel E. Henley sehenley@comcast.net
3 //
4 //    This program is free software; you can redistribute it and/or modify
5 //    it under the terms of the GNU General Public License as published by
6 //    the Free Software Foundation; either version 2 of the License, or
7 //    (at your option) any later version.
8 //
9 //    This program is distributed in the hope that it will be useful,
10 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //    GNU General Public License for more details.
13 //
14 //    You should have received a copy of the GNU General Public License along
15 //    with this program; if not, write to the Free Software Foundation, Inc.,
16 //    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 //
18 /// \file
19 /// Implementation file for Graphics manager of the simulator and the IGraphics factory.
20
21 // Pre-Compiled Header for Simulator
22 #include "presimulator.h"
23
24 #include "graphics.h"
25
26 /// \addtogroup simulator "Simulator"
27 /// @{
28     /// \addtogroup simulatorcomponent "Simulator Components"
29     /// @{
30         /// \addtogroup graphics "Graphics"
31         /// @{
32
33 extern IGraphics* createGraphics( ISystem& sys )
34 {
35     return new Graphics( sys );
36 }
37
38 Graphics::Graphics( ISystem& sys )
39     : system( sys ), scenemanager(0), viewport(0), camera(0), renderwindow(0)
40 {
41 }
42
43 Graphics::~Graphics()
44 {
45     if( viewport )
46     {
47         renderwindow->removeViewport( viewport->getZOrder() );
48         viewport = 0;
49     }
50
51     if( renderwindow )
52     {
53         root->detachRenderTarget( renderwindow );
54         delete renderwindow;
55         renderwindow = 0;
56     }
57 }
58
59 // ISimulatorComponent - interfaces
60 //virtual
61 void Graphics::startup()
62 {
63     /// \todo try all Ogre device before admitting failure.
64     MESSAGE( "Simulator", "Graphics", "Startup" )
65     try
66     {
67         Ogre::String log_file = paths::getLogsPath() + "simulatorogre.log";
68         Ogre::String resources_path = paths::getMediaPath();
69
70
71         // defaults to pluginFileName="plugins.cfg" configFileName="ogre.cfg" logFileName="Ogre.log")
72         root = boost::shared_ptr< Ogre::Root >( new Ogre::Root( "", "", log_file ) );
73
74
75         // Plug-ins
76         #ifdef WIN32
77             #ifdef _DEBUG
78                 root->loadPlugin( "Plugin_BSPSceneManager_d" );
79                 root->loadPlugin( "Plugin_CgProgramManager_d" );
80                 root->loadPlugin( "Plugin_OctreeSceneManager_d" );
81                 root->loadPlugin( "Plugin_ParticleFX_d" );
82                 root->loadPlugin( "RenderSystem_Direct3D9_d" );
83                 root->loadPlugin( "RenderSystem_GL_d" );
84             #else
85                 root->loadPlugin( "Plugin_BSPSceneManager" );
86                 root->loadPlugin( "Plugin_CgProgramManager" );
87                 root->loadPlugin( "Plugin_OctreeSceneManager" );
88                 root->loadPlugin( "Plugin_ParticleFX" );
89                 root->loadPlugin( "RenderSystem_Direct3D9" );
90                 root->loadPlugin( "RenderSystem_GL" );
91            #endif // _DEBUG
92         #else //Linux
93             root->loadPlugin( "/usr/local/lib/OGRE/Plugin_BSPSceneManager" );
94             root->loadPlugin( "/usr/local/lib/OGRE/Plugin_CgProgramManager" );
95             root->loadPlugin( "/usr/local/lib/OGRE/Plugin_OctreeSceneManager" );
96             root->loadPlugin( "/usr/local/lib/OGRE/Plugin_ParticleFX" );
97             root->loadPlugin( "/usr/local/lib/OGRE/RenderSystem_GL" );
98         #endif //WIN32
99
100         /// resources
101         Ogre::ResourceGroupManager* resource_group_manager = Ogre::ResourceGroupManager::getSingletonPtr();
102         //[Bootstrap]
103         //Zip=../../Media/packs/OgreCore.zip
104
105         //# Resource locations to be added to the default path
106         //[General]
107         //FileSystem=../../Media
108         //FileSystem=../../Media/fonts
109         //FileSystem=../../Media/materials/programs
110         //FileSystem=../../Media/materials/scripts
111         //FileSystem=../../Media/materials/textures
112         //FileSystem=../../Media/models
113         //FileSystem=../../Media/overlays
114         //FileSystem=../../Media/particle
115         //FileSystem=../../Media/gui
116         //FileSystem=../../Media/DeferredShadingMedia
117         //Zip=../../Media/packs/cubemap.zip
118         //Zip=../../Media/packs/cubemapsJS.zip
119         //Zip=../../Media/packs/dragon.zip
120         //Zip=../../Media/packs/fresneldemo.zip
121         //Zip=../../Media/packs/ogretestmap.zip
122         //Zip=../../Media/packs/skybox.zip
123
124         Ogre::String sz = resources_path + "packs/traingearmedia.zip";
125         resource_group_manager->addResourceLocation( sz, "Zip", "General" );
126 //        Ogre::String sz = resources_path + _T("packs\\OgreCore.zip");
127 //        resource_group_manager->addResourceLocation( sz, "Zip", "Bootstrap" );
128 //
129 //        sz = resources_path;
130 //        resource_group_manager->addResourceLocation( sz, "FileSystem", "General" );
131 //
132 //        sz = resources_path + _T("fonts");
133 //        resource_group_manager->addResourceLocation( sz, "FileSystem", "General" );
134 //
135 //        sz = resources_path + _T("materials\\programs");
136 //        resource_group_manager->addResourceLocation( sz, "FileSystem", "General" );
137 //
138 //        sz = resources_path + _T("materials\\scripts");
139 //        resource_group_manager->addResourceLocation( sz, "FileSystem", "General" );
140 //
141 //        sz = resources_path + _T("materials\\textures");
142 //        resource_group_manager->addResourceLocation( sz, "FileSystem", "General" );
143 //
144 //        sz = resources_path + _T("models");
145 //        resource_group_manager->addResourceLocation( sz, "FileSystem", "General" );
146 //
147 //        sz = resources_path + _T("overlays");
148 //        resource_group_manager->addResourceLocation( sz, "FileSystem", "General" );
149 //
150 //        sz = resources_path + _T("particals");
151 //        resource_group_manager->addResourceLocation( sz, "FileSystem", "General" );
152 //
153 //        sz = resources_path + _T("gui");
154 //        resource_group_manager->addResourceLocation( sz, "FileSystem", "General" );
155 //
156 //        sz = resources_path + _T("packs\\cubemap.zip");
157 //        resource_group_manager->addResourceLocation( sz, "Zip", "General" );
158 //
159 //        sz = resources_path + _T("packs\\cubemapsJS.zip");
160 //        resource_group_manager->addResourceLocation( sz, "Zip", "General" );
161 //
162 //        sz = resources_path + _T("packs\\dragon.zip");
163 //        resource_group_manager->addResourceLocation( sz, "Zip", "General" );
164 //
165 //        sz = resources_path + _T("packs\\fresneldemo.zip");
166 //        resource_group_manager->addResourceLocation( sz, "Zip", "General" );
167 //
168 //        sz = resources_path + _T("packs\\ogretestmap.zip");
169 //        resource_group_manager->addResourceLocation( sz, "Zip", "General" );
170 //
171 //        sz = resources_path + _T("packs\\skybox.zip");
172 //        resource_group_manager->addResourceLocation( sz, "Zip", "General" );
173 //
174         const Ogre::RenderSystemList& list = root->getAvailableRenderers();
175         if( list.empty() )throw Ogre::Exception( Ogre::Exception::ERR_NOT_IMPLEMENTED,
176                                                   _("No rendering devices found"),
177                                                   _("Creating Graphics System")  );
178
179         size_t size = list.size();
180         /// \todo Pick the best on the list or the best on the default monitor
181         /// \todo Select opengl when using a remode desktop
182         //Check for remote desktop connection
183         bool remote = false;
184     #if _WIN32
185         remote = ::GetSystemMetrics( SM_REMOTESESSION ) != 0;
186     #endif //_WIN32
187
188         Ogre::RenderSystem* select = list.at(0); //First on the list? - default -
189         /// \note Must use OpenGL in windows remote desktop - Sometimes this works
190         if( remote )
191         {
192             for( size_t j = 0; j<size; j++ )
193             {
194                 Ogre::RenderSystem* p = list.at(j);    //default to first system listed
195                 Ogre::String sz = p->getName();
196                 if( sz.compare( "OpenGL Rendering Subsystem" ) == 0 )
197                 {
198                     select = list.at(j);
199                     break;
200                 }
201             }
202         }
203         root->setRenderSystem( select );
204         root->initialise( false );
205
206
207
208         Ogre::NameValuePairList options;
209         options["fullscreen"] = "false";
210         options["vsync"] = "false";
211
212         // directx, and opengl device created here
213         renderwindow = root->createRenderWindow( _("OSRail Simulation"), 1024, 768, false, &options );
214
215         Ogre::WindowEventUtilities::addWindowEventListener( renderwindow, this );
216
217
218         scenemanager = root->createSceneManager( Ogre::ST_GENERIC );
219
220         camera = scenemanager->createCamera( _("Player") );
221         camera->setPosition( Ogre::Vector3( 0.0f, 0.0f, 500.0f ));
222         camera->lookAt( Ogre::Vector3( 0.0f, 0.0f, -300.0f ) );
223         camera->setNearClipDistance(5.0f);
224         viewport = renderwindow->addViewport( camera );
225         viewport->setBackgroundColour( Ogre::ColourValue( 0.0f, 0.0f, 0.0f ));
226
227         Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5);
228
229         scenemanager->setAmbientLight( Ogre::ColourValue( 0.2f, 0.2f, 0.2f ));
230
231         /// Must be done after window is created
232         resource_group_manager->initialiseAllResourceGroups();
233
234
235         createScene();
236         frame_timer.reset();
237
238     }
239     catch(Ogre::Exception& e)
240     {
241         /// \todo Hook exceptions up to console utility.
242         std::string data = "Startup Ogre Exception: ";
243         data += e.getDescription();
244         data += ", Class: ";
245         data += typeid( e ).name();
246         EXCEPTION(  "Simulator", "Graphics", data.c_str() );
247
248         shutdown();
249     }
250     catch( ... )
251     {
252         std::string data = "Unknown Exception";
253         EXCEPTION(  "Simulator", "Graphics", data.c_str() );
254         shutdown();
255     }
256
257
258 }
259
260 //virtual
261 unsigned long Graphics::getFrameMilliseconds()
262 {
263     return frame_timer.getMicroseconds();
264 }
265
266
267 // private
268 void Graphics::createScene( void )
269 {
270     scenemanager->setAmbientLight( Ogre::ColourValue( 0.2f, 0.2f, 0.2f ));
271     scenemanager->setSkyDome( true, "Examples/CloudySky", 5, 8 );
272     Ogre::Light* light = scenemanager->createLight("MainLight");
273     light->setPosition( 20, 80, 50 );
274
275     Ogre::Plane plane;
276     plane.normal = Ogre::Vector3::UNIT_Y;
277     plane.d = 200;
278     Ogre::MeshManager::getSingleton().createPlane( "FloorPlane",
279         Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
280         plane,
281         200000, 200000, 20, 20, true, 1, 50, 50, Ogre::Vector3::UNIT_Z );
282
283     Ogre::Entity* entity = scenemanager->createEntity("floor", "FloorPlane");
284     entity->setMaterialName("Examples/RustySteel");
285     scenemanager->getRootSceneNode()->createChildSceneNode()->attachObject( entity );
286
287     Ogre::SceneNode* node = scenemanager->getRootSceneNode()->createChildSceneNode();
288     Ogre::Entity* head = scenemanager->createEntity( "Head", "ogrehead.mesh" );
289     node->attachObject( head );
290
291     camera->setAutoTracking( true, node );
292     node = scenemanager->getRootSceneNode()->createChildSceneNode();
293     node->attachObject( camera );
294
295     Ogre::Animation* animation = scenemanager->createAnimation( "CameraTrack", 10 );
296     animation->setInterpolationMode( Ogre::Animation::IM_SPLINE );
297     Ogre::NodeAnimationTrack* track = animation->createNodeTrack( 0, node );
298
299     Ogre::TransformKeyFrame* key = track->createNodeKeyFrame(0.0f);
300
301     key = track->createNodeKeyFrame( 2.5f );
302     key->setTranslate( Ogre::Vector3( 500, 500, -1000 ));
303
304     key = track->createNodeKeyFrame( 5.0f );
305     key->setTranslate( Ogre::Vector3( 1500, 1000, -600 ));
306
307     key = track->createNodeKeyFrame( 7.5f );
308     key->setTranslate( Ogre::Vector3( 0, -100, 0 ));
309
310     key = track->createNodeKeyFrame( 10.0f );
311     key->setTranslate( Ogre::Vector3( 0, 0, 0 ));
312
313     animation_state = scenemanager->createAnimationState( "CameraTrack" );
314
315     /// initial animation speed
316     animation_speed = 0.01f;
317
318     animation_state->setEnabled( true );
319
320 }
321
322
323 //virtual
324 void Graphics::operator()()
325 {
326     if( viewport )
327     {
328 //      static unsigned char tone[3] = { 0, 0, 0 }; //range 0 - xffffff
329 //      static int c = 0;
330 //      static int d = 1;
331 //      if( tone[c] == 0xff )
332 //      {
333 //          d = -1;
334 //      }
335 //
336 //      if( tone[c] == 0x0 )
337 //      {
338 //          c++;
339 //          c %= 3;
340 //          d = 1;
341 //      }
342 //
343 //      tone[c] += d;
344 //
345 //      viewport->setBackgroundColour( Ogre::ColourValue( float( ( tone[0] & 0xff )/255.0f ),
346 //                                                        float( ( tone[1] & 0xff )/255.0f ),
347 //                                                        float( ( tone[2] & 0xff )/255.0f ),
348 //                                                        1.0f ));
349 //
350         TWEAKER( "Simulator", "Tweeker", float, speed, 0.01f, "Animation Speed" )
351         animation_state->addTime( *speed );
352
353         Ogre::WindowEventUtilities::messagePump();
354
355         if( root )root->renderOneFrame();
356
357         if( renderwindow )renderwindow->update();
358
359         frame_timer.reset();
360     }
361 }
362
363 //virtual
364 void Graphics::shutdown()
365 {
366     MESSAGE( "Simulator", "Graphics", "Shutdown" )
367     if( viewport && renderwindow )
368     {
369         renderwindow->removeViewport( viewport->getZOrder() );
370         viewport = 0;
371     }
372
373     if( renderwindow && root )
374     {
375         root->detachRenderTarget( renderwindow );
376         delete renderwindow;
377         renderwindow = 0;
378     }
379
380     root.reset();
381 }
382
383 // IGraphics - interfaces
384
385 //virtual
386 bool Graphics::isClosed() const
387 {
388     if( renderwindow )return renderwindow->isClosed();
389
390     return true;
391 }
392
393
394 //virtual
395 bool Graphics::isVisible() const
396 {
397     if( renderwindow )return renderwindow->isVisible();
398     return false;
399 }
400
401 //WindowEventListener
402 //virtual
403 /// \note Under DirectX this routine prevents a surface rendering error, after window closed.
404 bool Graphics::windowClosing( Ogre::RenderWindow* rw )
405 {
406     #ifdef WIN32
407         shutdown();
408         return false;
409     #else
410         return true;
411     #endif
412 }
413
414
415         /// @}   group graphics
416     /// @}   group simulatorcomponent
417 /// @} group simulator
Note: See TracBrowser for help on using the browser.