Gizmo3D

cube.cpp

Cube example application. This example illustrates how to create a cube with properties.
The source code can also be found in the examples directory of the Gizmo3D distribution.

00001 // *****************************************************************************
00002 // File         : cube.cpp
00003 // Module       : 
00004 // Description  : Test app for a sample cube
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 #include "gzGizmo3DLibrary.h"
00024 
00025 #include <ctype.h>
00026 
00027 // Definition of a cube application
00028 // The application provides an initialisation and an onIdle loop manager
00029 // to do the refresh of the window
00030 
00031 class CubeApp : public gzApplication
00032 {
00033 public:
00034         
00035     CubeApp();
00036     ~CubeApp();
00037     void Create(gzBool bOrthoCamera, gzBool bFullScreen, gzBool bTwoSideLighting);
00038     void onIdle();
00039 
00040 private:
00041     gzNode *MakeCube();
00042     
00043     gzWindow    *m_win;         // the window   
00044     gzReal      m_angle;
00045     gzTransform *m_redTransform;
00046     gzTransform *m_greenTransform;
00047     int m_frame;
00048 };
00049 
00050 
00051 int main(int argc, char *argv[])
00052 {
00053     gzStartUpGizmo();   // Needed for some systems to install external graphics engines
00054     
00055     gzBool bOrthoCamera = FALSE;
00056     gzBool bFullScreen = FALSE;
00057     gzBool bTwoSideLighting = FALSE;
00058 
00059     if(argc > 1)
00060     {
00061         argv++;
00062         argc--;
00063         while(argc > 0)
00064         {
00065             if(argv[0][0] == '-')
00066             {
00067                 switch(toupper(argv[0][1]))
00068                 {
00069                     case 'F':
00070                         bFullScreen = TRUE;
00071                         break;
00072                     case 'O':
00073                         bOrthoCamera = TRUE;
00074                         break;
00075                     case '2':
00076                         bTwoSideLighting = TRUE;
00077                         break;
00078                 }
00079             }
00080             argv++;
00081             argc--;
00082         }
00083     }
00084 
00085     // Make the application
00086     CubeApp app;
00087     try
00088     {       
00089         // Create the scene and the window
00090         app.Create(bOrthoCamera, bFullScreen, bTwoSideLighting);
00091         
00092         // run the application
00093         app.run();
00094 
00095     }
00096     catch(gzBaseError &error)       // In case of exceptions thrown we want to print the message
00097     {
00098                 error.reportError();
00099     }
00100     
00101     gzShutDownGizmo();
00102     
00103     return 0;
00104 }
00105 
00106 CubeApp::CubeApp()
00107     : m_win(NULL), m_frame(0), m_angle(0)
00108 {
00109 }
00110 
00111 CubeApp::~CubeApp()
00112 {
00113     if(m_win)
00114         delete m_win;
00115 }
00116 
00117 
00118 void CubeApp::Create(gzBool bOrthoCamera, gzBool bFullScreen, gzBool bTwoSideLighting)
00119 {
00120 
00121     // Set the appropriate graphics engine API
00122     gzGraphicsEngine::useEngine(GZ_ENGINE_OPENGL);
00123 
00124     // Create a scene to render
00125     gzScene *scene=new gzScene();
00126 
00127 #ifdef GZ_DEBUG // Add axis in debug mode
00128     scene->addNode(new gzGeometryAxis(10,1));
00129 #endif
00130 
00131     // Create an environment. The environment makes it possible to add lights and fog to the scene.
00132     gzEnvironment * env = new gzEnvironment();
00133     // Create a point light. 
00134     gzLight *pointlight = new gzLight();
00135     // Set the position for the light
00136     pointlight->setPosition(0, 0, 0);
00137     // Add the light to the environment. The light will lit every object which is a child to the scene
00138     env->addLight(pointlight);
00139 
00140     // Add the environment to the scene
00141     scene->addNode(env);
00142 
00143     // Create a state                      
00144     gzState *state=new gzState;
00145     // Enable polygon mode. This makes it possible to change settings for the polygons
00146     state->setMode(GZ_STATE_POLYGON_MODE,GZ_STATE_ON);
00147     // Sets the mode for the back of each polygon. The polygons are filled on the back
00148     state->setBackPolygonMode(GZ_POLYGON_MODE_FILL);
00149     // Set the state to the environment. This state will be applied to env and to all nodes which 
00150     // are children to env.  
00151     env->setState(state);
00152 
00153     //-----------------------------------------------------------------------------
00154     // RED CUBE
00155     //----------------------------------------------------------------------------- 
00156 
00157     // Create a red material with a bit of transparency
00158     gzMaterial *redMaterial = new gzMaterial();
00159     // Set the ambient reflection property for the material. 
00160     redMaterial->setAmbient(0.16f , 0.16f, 0.16f , 0.5f);
00161     // Set the specular reflection property
00162     redMaterial->setSpecular(1.0f, 1.0f, 1.0f,1.0f);
00163     // Specifies how the diffuse light is reflected
00164     redMaterial->setDiffuse(0.8f, 0.1f, 0.1f, 0.5f);
00165     // Sets the color of the light which is emitted from the material
00166     redMaterial->setEmission(0 , 0, 0, 0.5f);
00167     // Set the shininess, which specifies the size and brightness for the specular reflection.
00168     redMaterial->setShininess(16);
00169 
00170     // create a state which uses the red material
00171     gzState* redState=new gzState;
00172     // set the material the state shall use
00173     redState->setMaterial(redMaterial);
00174     // enable material mode for the state. If the material shall be visible a light has to exist in the scene.
00175     redState->setMode(GZ_STATE_MATERIAL,GZ_STATE_ON);
00176     // specify the blending function. 
00177     redState->setBlendFunction( GZ_BLEND_SRC_ALPHA, GZ_BLEND_ONE_MINUS_SRC_ALPHA);
00178     // enable blending mode
00179     redState->setMode(GZ_STATE_BLENDING,GZ_STATE_ON);
00180     redState->setSeparateFrontAndBackRender(TRUE);
00181 
00182     // Create the cube.
00183     gzNode *cubeShape = MakeCube();
00184 
00185     // Create a transform 
00186     m_redTransform=new gzTransform;
00187     // translate -5 in the z direction
00188     m_redTransform->setTranslation(0.0f, 0.0f, -5.0f);
00189     // set the red state to the transform. The state will be applied to all children to the transform
00190     m_redTransform->setState(redState);
00191     // Add the cube as child to the transform
00192     m_redTransform->addNode(cubeShape);
00193     // Add transform to the environment. 
00194     env->addNode(m_redTransform);
00195 
00196     //----------------------------------------------------------------------------- 
00197     // GREEN CUBE
00198     //----------------------------------------------------------------------------- 
00199 
00200     // Create a green material
00201     gzMaterial *greenMaterial = new gzMaterial();
00202     // set ambient reflection
00203     greenMaterial->setAmbient(0.16f , 0.16f, 0.16f , 0.5f);
00204     // set diffuse reflection
00205     greenMaterial->setDiffuse(0.1f, 0.8f, 0.1f, 0.5f);
00206     // set color to light emitted from the material. 
00207     greenMaterial->setEmission(0 , 0, 0, 0.5f);
00208     // Set the shininess, which specifies the size and brightness for the specular reflection.
00209     greenMaterial->setShininess(16);
00210 
00211     // Create a new state 
00212     gzState* greenState=new gzState;
00213     // The state uses the green material
00214     greenState->setMaterial(greenMaterial);
00215     // enable material mode for the state
00216     greenState->setMode(GZ_STATE_MATERIAL,GZ_STATE_ON);
00217 
00218     // create a transform
00219     m_greenTransform=new gzTransform;
00220     // the transform uses the green state
00221     m_greenTransform->setState(greenState);
00222     // Translate 
00223     m_greenTransform->setTranslation(0.5f, 0.1f, -6.0f);
00224     // add the same node as child to this transform. 
00225     // We now have one geometry which is child to two different transforms
00226     m_greenTransform->addNode(cubeShape);
00227     // Add the transform as a child to the environment
00228     env->addNode(m_greenTransform);
00229 
00230     gzGraphicsFormat *format=NULL;
00231     if (bFullScreen)
00232     {
00233         // Setup fulllscreen format
00234         format = new gzGraphicsFormat;
00235         format->useFullScreen(TRUE);
00236         format->setFullScreenWidth(800);
00237     }
00238 
00239 
00240     //----------------------------------------------------------------------------- 
00241     // Create our window
00242     //----------------------------------------------------------------------------- 
00243     m_win=new gzWindow("Cube",NULL, format);
00244 
00245     // Default size
00246     if (!bFullScreen)
00247         m_win->setSize(600, 600);
00248 
00249     if (bTwoSideLighting)
00250        gzEnvironment::setTwoSideLighting(m_win->getContext(),TRUE);
00251 
00252     // create a camera
00253     gzCamera *cam;
00254 
00255     if (bOrthoCamera)
00256     {
00257         // A orthogonal camera is created
00258         gzOrthoCamera *orthoCam = new gzOrthoCamera;
00259         // Width and height is set to 4
00260         orthoCam->setWidth(4.0f);
00261         orthoCam->setHeight(4.0f);
00262         cam = orthoCam;
00263     }
00264     else
00265     {
00266         // Default a perspective camera is created
00267         gzPerspCamera *perspCam = new gzPerspCamera;
00268         cam = perspCam;
00269     }
00270 
00271     // Set position for the camera. the default viewing direction is along the negativ z-axis
00272     cam->setPosition(0,0,8);
00273     // Set the scene to the camera. The camera will observe the scene
00274     cam->setScene(scene);
00275     // Connect the camera to the window. The part of the scene visible to the camera will be drawn in the window
00276     m_win->setCamera(cam);
00277     // Refresh the window. An image is captured with the camera and then drawn in the window
00278     m_win->refreshWindow();
00279 }
00280 
00281 // When the application has started with run() the onIdle() method is called 
00282 void CubeApp::onIdle()
00283 {
00284     // Increase the angle value
00285     m_angle+=(gzReal)0.2;
00286     // If the angle is larger than 360 it is reset to 0
00287     if (m_angle > 360)
00288         m_angle = 0;
00289 
00290     // Rotate around the specified axis in degrees. The m_angle decides the size of rotation
00291     m_redTransform->setRotation(gzVec3(1.0f, 1.0f, 0.0f), m_angle);
00292     m_greenTransform->setRotation(gzVec3(0.5f, 0.1f, 1.0f), m_angle);
00293 
00294     // And refresh window. Can be automated with setRefreshRate();
00295     m_win->refreshWindow();
00296     
00297     if (m_frame++>700)
00298     {
00299         // The framerate is printed in the console each 700th frame
00300         printf("Frame Rate:%d\n",(int)m_win->getFrameRate());
00301         m_frame=0;
00302     }
00303 }
00304 
00305 // The method that creates the cube
00306 gzNode *CubeApp::MakeCube()
00307 {
00308     // Create an array with gzVec3 which is the points in the cube
00309     static gzVec3 cubeCoords[] = 
00310     {
00311         gzVec3(-1,-1,-1),gzVec3(1,-1,-1),gzVec3(1,1,-1),gzVec3(-1,1,-1),
00312         gzVec3(-1,-1,1),gzVec3(1,-1,1),gzVec3(1,1,1),gzVec3(-1,1,1)
00313     };
00314 
00315     // Create a list with indexes. This indexes indicates the position in the array with cubeCoords
00316     // for the point that shall be used
00317     static gzULong indexList[] =
00318     {
00319         1,0,3,  // Z-
00320         3,2,1,  // Z-
00321         4,5,6,  // Z+
00322         6,7,4,  // Z+
00323         5,1,2,  // X+
00324         2,6,5,  // X+
00325         0,4,7,  // X-
00326         7,3,0,  // X-
00327         7,6,2,  // Y+
00328         2,3,7,  // Y+
00329         0,1,5,  // Y-
00330         5,4,0   // Y-
00331     };
00332 
00333     // Compute the number of triangles in the cube
00334     static gzULong numberOfTris=sizeof(indexList)/3/sizeof(gzULong);
00335 
00336     // Create a triangle geometry
00337     gzTriGeometry *shape = new gzTriGeometry();
00338     // Define the number of triangles used in the geometry
00339     shape->setSize(numberOfTris);
00340 
00341     // Create the cube from triangles. Normals are automatically
00342     // calculated. Each point has a corresponding normal.
00343     // This cube can be made in other manners but this is easy enough
00344     // Because of the large number of normals, it is not as fast as it
00345     // can be made.
00346 
00347     for (gzULong i = 0; i < numberOfTris; i++)
00348     {
00349         // Add a triangle to the shape using the coordinate array and determine position with the indexlist.
00350         shape->setTri(i,cubeCoords[indexList[i*3]],cubeCoords[indexList[i*3+1]],cubeCoords[indexList[i*3+2]]);
00351     }
00352     // Optimize the shape
00353     gzGeometryOptimizer::optimize(shape);
00354 
00355     // return the gzTriGeometry as a gzNode. 
00356     return shape;
00357 }

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