Gizmo3D

lod.cpp

Lod example application. This example illustrates how to use gzLod nodes.
The source code can also be found in the examples directory of the Gizmo3D distribution.

00001 // *****************************************************************************
00002 // File         : lod.cpp
00003 // Module       : 
00004 // Description  : Test app for a sample lod
00005 // Author       : Lisa Johansson          
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 // LJH  040322  Created file    
00020 //
00021 // ******************************************************************************
00022 
00023 #include "gzGizmo3DLibrary.h"
00024 
00025 // *******************************************************************************
00026 // Defines the window class
00027 
00028 class MyWin : public gzWindow
00029 {
00030 public:
00031         
00032     MyWin();
00033 
00034     virtual ~MyWin(){};
00035 
00036     void onIdle();
00037 
00038     gzRefPointer<gzText>        screenText;
00039 
00040     gzRefPointer<gzPerspCamera> cam;
00041 
00042     gzRefPointer<gzLod>         lod;
00043 };
00044 
00045 
00046 MyWin::MyWin():gzWindow("Lod Test")
00047 {
00048 
00049     // Create a scene to render
00050     gzScene * scene=new gzScene();
00051 
00052     scene -> addNode(new gzGeometryAxis(10,1));
00053 
00054     gzEnvironment * env = new gzEnvironment();
00055 
00056     gzState * state=new gzState;
00057     
00058     // the state removes the backfaces in the rendering
00059     state -> setMode(GZ_STATE_POLYGON_MODE,GZ_STATE_ON);
00060     
00061     state -> setBackPolygonMode(GZ_POLYGON_MODE_CULL);
00062     
00063     state -> setFrontPolygonMode(GZ_POLYGON_MODE_FILL);
00064 
00065     env -> setState(state);
00066 
00067     gzNode * gizmo1 = gzDbManager::loadDB("lodmodel1.3DS",GZ_EVALUATE_EXTENSION,GZ_DB_FLAGS_USE_MIPMAPS);
00068 
00069     gzTransform * gt1 = gzDynamic_Cast<gzTransform>(gizmo1);
00070 
00071     if(gt1)
00072     {
00073         gt1 -> unitScale();
00074 
00075         gt1 -> scale((gzReal)(2),(gzReal)(2),(gzReal)(2));
00076 
00077     }
00078 
00079     gzNode * gizmo2 = gzDbManager::loadDB("lodmodel2.3DS",GZ_EVALUATE_EXTENSION,GZ_DB_FLAGS_USE_MIPMAPS);
00080 
00081     gzTransform * gt2 = gzDynamic_Cast<gzTransform>(gizmo2);
00082 
00083     if(gt2)
00084     {
00085         gt2 -> unitScale();
00086 
00087         gt2 -> scale((gzReal)(2),(gzReal)(2),(gzReal)(2));
00088 
00089     }
00090 
00091     gzNode * gizmo3 = gzDbManager::loadDB("lodmodel3.3DS",GZ_EVALUATE_EXTENSION,GZ_DB_FLAGS_USE_MIPMAPS);
00092 
00093     gzTransform * gt3 = gzDynamic_Cast<gzTransform>(gizmo3);
00094 
00095     if(gt3)
00096     {
00097         gt3 -> unitScale();
00098 
00099         gt3 -> scale((gzReal)(2),(gzReal)(2),(gzReal)(2));
00100 
00101     }
00102 
00103     lod = new gzLod;
00104 
00105     lod -> addNode(gizmo1);
00106 
00107     lod -> addNode(gizmo2);
00108 
00109     lod -> addNode(gizmo3);
00110 
00111     //lod->setLOD(gzULong index , gzBool enable , gzReal min = 0 , gzReal max = 1, gzReal fade = 0);
00112 
00113     lod->setLOD(0, TRUE , 0, 20, 0,30);
00114 
00115     lod->setLOD(1, TRUE, 20, 40, 10,40);
00116 
00117     lod->setLOD(2, TRUE, 40, 100, 20,0);
00118 
00119     env -> addNode(lod);
00120     
00121     scene -> addNode (env);
00122 
00123     //
00124     // Add some text to the scene
00125     //
00126 
00127     screenText = new gzText("Screen Text");
00128 
00129     screenText -> setColor(gzVec4(1,1,0,0.5));
00130     
00131     screenText -> use2DPosition(TRUE);
00132     
00133     screenText -> set2DPosition(10, -10);
00134             
00135     scene -> addNode(screenText);
00136 
00137     cam = new gzPerspCamera;
00138 
00139     cam -> setPosition(0,0,20);
00140 
00141     cam -> setScene(scene);
00142 
00143     cam -> setNearClipPlane(1);
00144 
00145     cam -> setFarClipPlane(100000);
00146 
00147     setCamera(cam);
00148 
00149     gzSimpleMouseViewControl * myInput = new gzSimpleMouseViewControl( cam );
00150     
00151     addInputInterface( myInput );
00152 
00153     refreshWindow();
00154 }
00155 
00156 
00157 void MyWin::onIdle()
00158 {
00159     triggerKeyEvaluation();
00160 
00161     refreshWindow();
00162     
00163 };
00164 
00165 
00166 // ******************************************************************************
00167 // Definition of a lod application
00168 // The application provides an initialisation and an onIdle loop manager
00169 // to do the refresh of the window
00170 
00171 
00172 class LodApp : public gzApplication
00173 {
00174 public:
00175         
00176     LodApp();
00177 
00178     ~LodApp();
00179 
00180 private:
00181 
00182     friend class MyWin;
00183 
00184     gzRefPointer<MyWin> m_win;         // the window   
00185     
00186     void onIdle();
00187 
00188 };
00189 
00190 LodApp::LodApp()
00191 {
00192         m_win = new MyWin();
00193 }
00194 
00195 LodApp::~LodApp()
00196 {
00197     // When the lod application is deleted the smart pointer for the window is
00198     // automatically removed
00199 }
00200 
00201 void LodApp::onIdle()
00202 {
00203     if(m_win)
00204     {
00205         m_win -> onIdle();
00206 
00207         m_win -> triggerKeyEvaluation();
00208 
00209         gzString outputString;
00210 
00211         outputString = gzString("FrameRate:")+gzString(m_win->getFrameRate());
00212 
00213         outputString = outputString + gzString("\n\nCamera position: ") + gzString(m_win->cam->getPosition().v1) + gzString(", ") + gzString(m_win->cam->getPosition().v2) + gzString(", ") + gzString(m_win->cam->getPosition().v3);
00214     
00215         outputString = outputString + gzString("\n\nHeading: ") + gzString(m_win->cam->getHPR().v1) + gzString("\nPitch: ") + gzString(m_win->cam->getHPR().v2) + gzString("\nRoll: ") + gzString(m_win->cam->getHPR().v3);
00216     
00217         gzVec3 lodCenter = m_win->lod->getCenter();
00218 
00219         gzDouble lodDistance = sqrt((m_win->cam->getPosition().v1 - lodCenter.v1)*(m_win->cam->getPosition().v1 - lodCenter.v1) + (m_win->cam->getPosition().v2 - lodCenter.v2)*(m_win->cam->getPosition().v2 - lodCenter.v2) + (m_win->cam->getPosition().v3 - lodCenter.v3)*(m_win->cam->getPosition().v3 - lodCenter.v3));
00220         
00221         outputString = outputString + gzString("\n\nLodDistance: ") + gzString(lodDistance);
00222 
00223         m_win->screenText->setText(outputString);
00224 
00225     }
00226 
00227 }
00228 
00229 
00230 // ******************************************************************************
00231 // The main method
00232 
00233 int main(int argc, char *argv[])
00234 {
00235     gzStartUpGizmo();   // Needed for some systems to install external graphics engines
00236     
00237     try
00238     {       
00239         // Set the appropriate graphics engine API
00240         gzGraphicsEngine::useEngine(GZ_ENGINE_OPENGL);
00241 
00242         gzInitializeDbManagers();
00243 
00244         // Make the application
00245         LodApp app;
00246 
00247         // run the application
00248         app.run();
00249 
00250     }
00251     catch(gzBaseError &error)       // In case of exceptions thrown we want to print the message
00252     {
00253                 error.reportError();
00254     }
00255     
00256     gzShutDownGizmo();
00257     
00258     return 0;
00259 }

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