Gizmo3D

skybox.cpp

Skybox example application. This example illustrates how a skybox is created.
The source code can also be found in the examples directory of the Gizmo3D distribution.

00001 // *****************************************************************************
00002 // File         : SkyBox.cpp
00003 // Module       : 
00004 // Description  : Test app shows skybox feature
00005 // Author       : Anders Modén          
00006 // Product      : Gizmo3D 2.1.1
00007 //              
00008 // Copyright © 2003- Saab Training Systems AB, Sweden
00009 //                      
00010 // NOTE:    Gizmo3D is a high performance 3D Scene Graph and effect visualisation 
00011 //          C++ toolkit for Linux, Mac OS X, Windows (Win32) and IRIX® for  
00012 //          usage in Game or VisSim development.
00013 //
00014 //
00015 // Revision History...                                                  
00016 //                                                                      
00017 // Who  Date    Description                                             
00018 //                                                                      
00019 // AMO  020901  Created file    
00020 //
00021 // ******************************************************************************
00022 
00023 //#define GZ_MEM_DEBUG  //to enable mem debug
00024 
00025 
00026 // Enable this line to get a user defined skybox
00027 //#define USE_USER_SKYBOX
00028 
00029 //#define USE_USER_SKYBOX_ALT
00030 
00031 #include "gzGizmo3DLibrary.h"
00032 
00033 
00034 
00035 // And here comes the Java look alike app. It is a matter of taste.
00036 
00037 class MyWindow : public gzWindow
00038 {
00039 public:
00040     MyWindow(gzGraphicsFormat *format):gzWindow(gzString("Gizmo3D : ")+gzTime::now(TRUE).asString(),NULL,format)
00041     {
00042             // Create a scene to render -------------------------------------
00043             // We must have a scene that the camera can "look at"
00044             m_scene=new gzScene("Database Loader Example Scene");
00045             
00046             // Create a backface culling state
00047             // We don't want to render backfaces. this way we can "force" the geoms to be rendered without 
00048             // backfaces
00049             m_state=new gzState;
00050 
00051             m_state->setMode(GZ_STATE_POLYGON_MODE,GZ_STATE_ON);
00052             m_state->setBackPolygonMode(GZ_POLYGON_MODE_CULL);
00053             m_state->setFrontPolygonMode(GZ_POLYGON_MODE_FILL);
00054 
00055             m_state->disableAllNonEnabledModes();   // We need this for the top global state
00056 
00057             gzState::setGlobalState(getContext(),m_state);
00058             
00059             gzEnvironment *env=new gzEnvironment;
00060             
00061             //env->useFog(TRUE);
00062             env->setFogStart(0);
00063             env->setFogEnd(5000);
00064             env->setFogMode(GZ_FOG_MODE_LINEAR);
00065             env->setFogDensity(0.7f);
00066             env->setFogColor(gzRGBA(0.5f,0.5f,0.5f,1.0f));
00067                 
00068             m_scene->addNode(env);
00069 
00070 
00071             gzSkyBox *box=new gzSkyBox;
00072             
00073             box->setHorizonAngle(1);
00074             
00075 
00076 #if defined USE_USER_SKYBOX
00077 
00078             box->setImageSide(GZ_SKYBOX_EAST,gzImageManager::loadImage("east.bmp"));
00079             box->setImageSide(GZ_SKYBOX_SOUTH,gzImageManager::loadImage("south.bmp"));
00080             box->setImageSide(GZ_SKYBOX_WEST,gzImageManager::loadImage("west.bmp"));
00081             box->setImageSide(GZ_SKYBOX_NORTH,gzImageManager::loadImage("north.bmp"));
00082             box->setImageSide(GZ_SKYBOX_SKY,gzImageManager::loadImage("sky.bmp"));
00083             box->setImageSide(GZ_SKYBOX_GROUND,gzImageManager::loadImage("ground.bmp"));
00084 
00085             box->setHorizonAngle(20);
00086 
00087 #endif
00088 
00089 
00090 #if defined USE_USER_SKYBOX_ALT
00091 
00092             box->setImageSide(GZ_SKYBOX_SKY,gzImageManager::loadImage("sky.png"));
00093             box->setImageSide(GZ_SKYBOX_NORTH,gzImageManager::loadImage("north.png"));
00094             box->setImageSide(GZ_SKYBOX_EAST,gzImageManager::loadImage("east.png"));
00095             box->setImageSide(GZ_SKYBOX_WEST,gzImageManager::loadImage("west.png"));
00096             box->setImageSide(GZ_SKYBOX_SOUTH,gzImageManager::loadImage("south.png"));
00097             
00098             box->setHorizonAngle(10);
00099 
00100 #endif
00101 
00102             box->setStandardHeight(2000);
00103 
00104             box->setGroundPoint(0,-500,0);
00105             
00106             box->useFixedHeight(TRUE);
00107 
00108             env->addNode(box);
00109 
00110 
00111             // Use this code to add moving sky layer
00112 
00113             
00114 
00115             gzSkyLayer *layer;
00116 
00117             layer=new gzSkyLayer;
00118 
00119             gzImage *clouds=gzImageManager::loadImage("cloud2.png");
00120 
00121             layer->setLayerImage(clouds,100000,100000,2000,600,300);
00122             
00123             layer->setHorizonAngle(10);
00124 
00125             env->addNode(layer);
00126 
00127             /*
00128             
00129             layer=new gzSkyLayer;
00130 
00131             layer->setHorizonAngle(10);
00132             
00133             layer->setLayerImage(clouds,70000,50000,3000,0,500);
00134 
00135             env->addNode(layer);
00136 
00137             */
00138 
00139 
00140             // Now we want to look at the scene. Grab the default perspective camera from the window and set the scen as the
00141             // active one.
00142             getCamera()->setScene(m_scene);
00143             getCamera()->setNearClipPlane(1);
00144             getCamera()->setFarClipPlane(5000);
00145 
00146             // Lets add some movement to the scene
00147 
00148             m_input=new gzSimpleMouseViewControl(getCamera());
00149 
00150             addInputInterface(m_input);
00151 
00152 
00153             // Hmm. trivial
00154             setBackground(0.5f,0.5f,0.5f,1.0f);
00155 
00156             // and show us ...
00157             show();
00158     };
00159 
00160 
00161     /*
00162         The following code snippet is a virtual function to catch the window messages
00163         for pressed, repeated and released keys
00164     */
00165 
00166     gzBool onKey(gzKeyValue key , gzKeyState keystate , gzLong mouse_x , gzLong mouse_y)
00167     {
00168         switch(key)
00169         {
00170             case ' ':   // Space pressed
00171 
00172                 if(keystate == GZ_KEY_STATE_PRESSED)
00173                 {
00174                     gzState *state=new gzState;
00175 
00176                     state->setFrontPolygonMode(GZ_POLYGON_MODE_LINE);
00177                     state->setBackPolygonMode(GZ_POLYGON_MODE_CULL);
00178                     state->setMode(GZ_STATE_POLYGON_MODE,GZ_STATE_ON);
00179                     state->setOverride(GZ_STATE_POLYGON_MODE,GZ_STATE_ON);
00180                     state->setOverride(GZ_STATE_TEXTURE,GZ_STATE_ON);
00181 
00182                     state->setMode(GZ_STATE_GENERATE_DEBUG_INFO,GZ_STATE_ON);
00183                     state->setDebugMode((gzStateDebugMode)(GZ_STATE_DEBUG_COLLECT_STATS));
00184             
00185 
00186                     state->disableAllNonEnabledModes();
00187 
00188                     gzState::setGlobalState(getContext(),state);
00189                 }
00190                 if(keystate == GZ_KEY_STATE_RELEASED)
00191                 {
00192                     gzState::setGlobalState(getContext(),m_state);
00193                 }
00194                 break;
00195 
00196             case GZ_KEY_LBUTTON :
00197 
00198                 if((keystate == GZ_KEY_STATE_PRESSED) && !m_inMousePress)
00199                 {
00200                     setCaptureMouse(TRUE);
00201                     setHideMouse(TRUE);
00202                     m_inMousePress=TRUE;
00203                 }
00204                 else if((keystate == GZ_KEY_STATE_RELEASED) && m_inMousePress)
00205                 {
00206                     setHideMouse(FALSE);
00207                     setCaptureMouse(FALSE);
00208                     m_inMousePress=FALSE;
00209                 }
00210                 break;
00211 
00212             case 'd' :
00213                 if(keystate == GZ_KEY_STATE_PRESSED)
00214                 {
00215                     getCamera()->getScene()->debug(GZ_DEBUG_SHOW_ALL);
00216                 }
00217                 break;
00218 
00219 
00220         }
00221 
00222         return gzWindow::onKey((gzKeyValue)key,(gzKeyState)keystate,mouse_x,mouse_y);
00223     }
00224 
00225 
00226     virtual ~MyWindow()
00227     {
00228         if(m_input)
00229             delete m_input;
00230     }
00231 
00232     gzRefPointer<gzState> m_state;
00233 
00234     gzBool              m_inMousePress;
00235 
00236     gzScene             *m_scene;
00237 
00238     gzSimpleMouseViewControl    *m_input;
00239 
00240 };
00241 
00242 // Definition of a sample application
00243 // The application provides an initialisation and an onIdle loop manager
00244 // to do the refresh of the window
00245 
00246 class WindowApp : public gzApplication
00247 {
00248 public:
00249         
00250     WindowApp():m_win(NULL),m_format(NULL),m_heading(0),m_pitch(0),m_position(30,0,200)
00251     {
00252     };
00253 
00254     ~WindowApp()
00255     {
00256         if(m_win)
00257         {
00258             delete m_win;
00259         }
00260 
00261     }
00262 
00263     void Create()
00264     {
00265         gzGraphicsFormat *format=new gzGraphicsFormat;
00266 
00267         m_win = new MyWindow(format);
00268 
00269         gzPerspCamera *camera=gzDynamic_Cast<gzPerspCamera>(m_win->getCamera());
00270 
00271         camera->setHPR(m_heading,m_pitch,0);
00272         camera->setPosition(m_position.v1,m_position.v2,m_position.v3);
00273         camera->lookAt(0,0,0);
00274         camera->useInfiniteFarPlane(TRUE);
00275 
00276     }
00277 
00278     void onIdle()
00279     {
00280         if(m_win)
00281         {
00282             m_win->triggerKeyEvaluation();
00283 
00284             if(!m_win->refreshWindow()) // Yield some time if no rendering
00285                 gzSleep(30);
00286 
00287         }
00288 
00289     }
00290 
00291 private:
00292 
00293     friend class MyWindow;
00294     
00295     MyWindow                *m_win;
00296     gzGraphicsFormat        *m_format;
00297 
00298     gzFloat             m_heading;
00299     gzFloat             m_pitch;
00300     gzVec3              m_position;
00301 
00302 };
00303 
00304 
00305 
00306 //---------------------------------------------------------------
00307 
00308 
00309 int main(int argc, char *argv[])
00310 {
00311     gzStartUpGizmo();   // Needed for some systems to install external graphics engines
00312     
00313     gzMessage::setMessageLevel(GZ_MESSAGE_MEM_DEBUG);
00314 
00315     try
00316     { 
00317         gzLicense::notifyDefaultLicense();
00318     
00319         gzGraphicsEngine::useEngine(GZ_ENGINE_OPENGL);
00320     
00321         gzInitializeDbManagers();
00322 
00323 
00324         // Make the application
00325         WindowApp app;
00326           
00327         // Create the scene and the window
00328         app.Create();
00329         
00330         // run the application
00331         app.run();
00332 
00333     }
00334     catch(gzBaseError &error)       // In case of exceptions thrown we want to print the message
00335     {
00336         error.reportError();
00337     }
00338     
00339     gzShutDownGizmo();
00340 
00341     return 0;
00342 }
00343 
00344 
00345 
00346 

Documentation for Gizmo3D generated at Wed Feb 20 11:54:02 2008 by   Saab Training Systems AB, ¸ (c) 2003-and beyond