Gizmo3D

mirror.cpp

Mirror example application.
The source code can also be found in the examples directory of the Gizmo3D distribution.

00001 // *****************************************************************************
00002 // File         : mirror.cpp
00003 // Module       : 
00004 // Description  : Test app for a mirror sample
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 #include <iostream>
00027 
00028 
00029 class MyWindow : public gzWindow
00030 {
00031 public:
00032     MyWindow(gzString filename , gzGraphicsFormat *format):gzWindow("MyWindow",NULL,format)
00033     {
00034         // Create top node as an environment with lights
00035         gzEnvironment *group=new gzEnvironment;
00036 
00037         // Create a light and add it to environment
00038         rotlight=new gzLight;
00039         group->addLight(rotlight);
00040 
00041         // Create a node with a 3D model that has a size of 6x6x6 units at 10,0,10
00042         gzTransform *unit=new gzTransform;
00043         unit->addNode(gzDbManager::loadDB(filename,GZ_EVALUATE_EXTENSION));
00044 
00045         unit->unitScale();
00046         unit->scale(6,6,6);
00047         unit->translate(10,0,10);
00048 
00049         group->addNode(unit);
00050 
00051         // Now lets create a scene with two mirrors
00052 
00053         gzScene *scene=new gzScene;
00054 
00055         gzMirror *mirror=new gzMirror();
00056         gzMirror *mirror2=new gzMirror();
00057 
00058         // A mask to create mirror effect
00059         gzTriGeometry *tri=new gzTriGeometry;
00060 
00061         tri->setSize(1);
00062         tri->setTri(0,gzVec3(-5,-5,1),gzVec3(5,-5,1),gzVec3(0,5,1));
00063         tri->setColor(0,gzVec4(0,1,1,1));
00064 
00065         // A frame around the mirror
00066         gzTriGeometry *border=new gzTriGeometry;
00067 
00068         border->setSize(6);
00069         border->setColor(gzVec4(0.8f,0.7f,0.9f,1.0f));
00070         border->setTri(0,gzVec3(-6,-6,1),gzVec3(5,-5,1),gzVec3(-5,-5,1));
00071         border->setTri(1,gzVec3(-6,-6,1),gzVec3(6,-6,1),gzVec3(5,-5,1));
00072         border->setTri(2,gzVec3(6,-6,1),gzVec3(0,6,1),gzVec3(5,-5,1));
00073         border->setTri(3,gzVec3(5,-5,1),gzVec3(0,6,1),gzVec3(0,5,1));
00074         border->setTri(4,gzVec3(0,5,1),gzVec3(0,6,1),gzVec3(-5,-5,1));
00075         border->setTri(5,gzVec3(0,6,1),gzVec3(-6,-6,1),gzVec3(-5,-5,1));
00076 
00077         // A frame for mirror2 with another color
00078         gzTriGeometry *border2=(gzTriGeometry *)border->clone();
00079         border2->setColor(gzVec4(0.2f,0.2f,0.9f,1.0f));
00080 
00081 
00082         // We want the mirrors to clip objects behind the mirrors
00083         // So we do not get fake mirror images
00084 
00085         gzEnvironment *clipper=new gzEnvironment;
00086         gzEnvironment *clipper2=new gzEnvironment;
00087 
00088         gzClip *clip=new gzClip;
00089         clip->setClipPlane(gzVec3(0,0,-1),gzVec3(0,0,1.1f));
00090 
00091         // And lets se some mirror params
00092         mirror->setMaskGeometry(tri);
00093         mirror->setNormalBase(gzVec3(0,0,1),gzVec3(0,0,1));
00094         mirror->setMaxDepth(5);
00095         mirror->addNode(group);
00096 
00097         mirror2->setMaskGeometry(tri);
00098         mirror2->setNormalBase(gzVec3(0,0,1),gzVec3(0,0,1));
00099         mirror2->setMaxDepth(5);
00100         mirror2->addNode(group);
00101 
00102         
00103         clipper->addClipPlane(clip);
00104         clipper->addNode(mirror);
00105         clipper->addNode(border);
00106         clipper->addNode(new gzGeometryAxis(10,1));
00107 
00108         clipper2->addClipPlane(clip);
00109         clipper2->addNode(mirror2);
00110         clipper2->addNode(border2);
00111 
00112         // Locate the first mirror with a transform
00113         trans=new gzTransform();
00114         trans->addNode(clipper);
00115 
00116         // Locate the second mirror
00117         gzTransform *trans2=new gzTransform;
00118 
00119         trans2->setTranslation(20,0,0);
00120         trans2->setHPR(-50,0,0);
00121         trans2->addNode(clipper2);
00122 
00123         // Add mirrors and objects to scene
00124         scene->addNode(trans);
00125         scene->addNode(group);
00126         scene->addNode(trans2);
00127 
00128         // Add mirrors to each other
00129         mirror->addNode(trans2);
00130         mirror2->addNode(trans);
00131 
00132         // Lets print out the scene
00133         scene->debug(GZ_DEBUG_RECURSIVE);
00134                 
00135         angle=0;
00136 
00137         gzCamera *cam=getCamera();
00138 
00139         cam->setScene(scene);
00140 
00141         cam->setPosition(5,0,50);
00142         cam->setHPR(0,0,0);
00143         cam->setFarClipPlane(5000);
00144 
00145         addInputInterface(new gzSimpleMouseViewControl(getCamera()));
00146 
00147     };
00148 
00149 
00150     /*
00151         The following code snippet is a virtual function to catch the window messages
00152         for pressed, repeated and released keys
00153     */
00154 
00155     gzBool onKey(gzKeyValue key , gzKeyState keystate , gzLong mouse_x , gzLong mouse_y)
00156     {
00157         gzBool key_is_down;
00158         
00159         if( (keystate == GZ_KEY_STATE_PRESSED )|| (keystate ==GZ_KEY_STATE_REPEATED))
00160             key_is_down=TRUE;
00161         else
00162             key_is_down=FALSE;
00163 
00164         switch(key)
00165         {
00166             case 'f':
00167 
00168                 if(keystate == GZ_KEY_STATE_PRESSED)
00169                     std::cout<<"Framerate: "<<getFrameRate()<<std::endl;
00170                 break;
00171 
00172             case 't':
00173                 angle=(gzReal)fmod(angle+1,360);
00174                 trans->setHPR(angle,0,0);
00175                 break;
00176 
00177             case 'r':
00178                 angle=(gzReal)fmod(angle-1,360);
00179                 trans->setHPR(angle,0,0);
00180                 break;
00181 
00182 
00183         }
00184 
00185         return gzWindow::onKey(key,keystate,mouse_x,mouse_y);
00186     }
00187     
00188 
00189     gzTransform *trans;
00190     gzReal angle;
00191     gzLight *rotlight;
00192 };
00193 
00194 // Definition of a sample application
00195 // The application provides an initialisation and an onIdle loop manager
00196 // to do the refresh of the window
00197 
00198 class WindowApp : public gzApplication
00199 {
00200 public:
00201         
00202     WindowApp():m_win(NULL),m_format(NULL)
00203     {
00204         m_angle=0;
00205     };
00206 
00207     ~WindowApp()
00208     {
00209         if(m_win)
00210             delete m_win;
00211     }
00212 
00213         void Create(gzBool bFullScreen,gzString filename)
00214     {
00215         if(bFullScreen)
00216         {
00217             m_format = new gzGraphicsFormat;
00218 
00219             m_format->setFullScreenWidth(800);
00220             m_format->useFullScreen(TRUE);
00221             m_format->useStencil(TRUE);
00222         }
00223         else
00224         {
00225             m_format = new gzGraphicsFormat;
00226             m_format->useStencil(TRUE);
00227         }
00228 
00229 
00230         m_win = new MyWindow(filename ,m_format);
00231                 m_win->setBackground(0,0.8f,0.5f,0);
00232     }
00233 
00234     void onIdle()
00235     {
00236         
00237         //  The following code snippet is used to control the movements of the camera
00238 
00239         if(m_win)
00240         {
00241             m_win->triggerKeyEvaluation();
00242             m_win->refreshWindow();
00243 
00244             m_angle=fmod(m_angle+2,360);
00245             m_win->rotlight->setPosition((gzReal)(40*cos(m_angle/180.0*GZ_PI)),0,(gzReal)(30*sin(m_angle/180.0*GZ_PI)));
00246 
00247         }
00248     }
00249 
00250 private:
00251     
00252     MyWindow            *m_win;
00253     gzGraphicsFormat    *m_format;
00254 
00255     gzDouble            m_angle;
00256 };
00257 
00258 
00259 
00260 int main(int argc, char *argv[])
00261 {
00262     gzStartUpGizmo();   // Needed for some systems to install external graphics engines
00263 
00264     gzBool bFullScreen = FALSE;
00265     
00266     gzString filename="model.3ds";
00267 
00268     gzMessage::setMessageLevel(GZ_MESSAGE_DEBUG);
00269     
00270     gzGraphicsEngine::useEngine(GZ_ENGINE_OPENGL);
00271 
00272     gzInitializeDbManagers();
00273 
00274     if(argc > 1)
00275     {
00276         argv++;
00277         argc--;
00278         while(argc > 0)
00279         {
00280             if(argv[0][0] == '-')
00281             {
00282                 switch(toupper(argv[0][1]))
00283                 {
00284                     case 'F':
00285                         bFullScreen = TRUE;
00286                         break;
00287 
00288                     case 'M':
00289                         argv++;
00290                         argc--;
00291                         filename = argv[0];
00292                         break;
00293 
00294 
00295                 }
00296 
00297             }
00298             argv++;
00299             argc--;
00300         }
00301     }
00302 
00303     // Make the application
00304     WindowApp app;
00305     try
00306     {       
00307         // Create the scene and the window
00308         app.Create(bFullScreen,filename);
00309         
00310         // run the application
00311         app.run();
00312 
00313     }
00314     catch(gzBaseError &error)       // In case of exceptions thrown we want to print the message
00315     {
00316                 error.reportError();
00317     }
00318 
00319     gzShutDownGizmo();
00320 
00321     return 0;
00322 }
00323 

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