Gizmo3D

dbviewer.cpp

Database Viewer This example illustrates how a generic databaseviewer can be realised.
The source code can also be found in the examples directory of the Gizmo3D distribution.

00001 // *****************************************************************************
00002 // File         : viewer.cpp
00003 // Module       : 
00004 // Description  : Test app shadow database viewer
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 #define USE_STATS
00026 
00027 // The general include
00028 #include "gzGizmo3DLibrary.h"
00029 
00030 // And here comes the Java look alike app. It is a matter of taste.
00031 gzBaseGeometry *createPlane(gzULong offset)
00032 {
00033                 gzViewPlane *plane=new gzViewPlane();
00034 
00035                 plane->setTexCoordinates(gzVec2(0+offset*0.28,0+offset*0.28), gzVec2(3+offset*0.28,3+offset*0.28));
00036 
00037                 gzState *transp=new gzState;
00038 
00039                 plane->setState(transp);
00040 
00041                 transp->setTransparency(0.2f);
00042 
00043                 if(offset)
00044                 {
00045 
00046                     gzImage *image=gzImageManager::loadImage("brus.dds");
00047 
00048                     gzTexture *tex=new gzTexture;
00049 
00050                     tex->setImage(image);
00051                     tex->setWrapS(GZ_TEXTURE_WRAP_REPEAT);
00052                     tex->setWrapT(GZ_TEXTURE_WRAP_REPEAT);
00053 
00054 
00055                     transp->setTexture(tex);
00056                     transp->setMode(GZ_STATE_TEXTURE,GZ_STATE_ON);
00057 
00058                     gzTexEnv *env=new gzTexEnv;
00059 
00060                     env->setMode(GZ_TEXENV_MODE_BLEND);
00061 
00062                     transp->setTexEnv(env);
00063                     transp->setMode(GZ_STATE_TEXENV,GZ_STATE_ON);
00064                 }
00065 
00066                 return plane;
00067 
00068 }
00069 
00070 class MyWindow : public gzWindow
00071 {
00072 public:
00073     MyWindow(gzInt swap,gzBool useLodSmooth,gzDouble fadeDist,gzBool useFog,gzBool useRoi,gzBool softShadow,gzBool debug, gzGraphicsFormat *format , gzBool useShadow, gzSerializeAdapterFlags flags , gzString filename , gzReal scale,gzBool skyLayer):gzWindow(gzString("Gizmo3D : ")+gzTime::now(TRUE).asString(),NULL,format,FALSE)
00074     {
00075             // Create a scene to render -------------------------------------
00076             // We must have a scene that the camera can "look at"
00077             m_scene=new gzScene("Database Loader Example Scene");
00078 
00079         
00080             // Create some light environment group. This way we get lighting into the model
00081             // All stuff under this is lit with material s etc.
00082             gzEnvironment *group=new gzEnvironment;
00083 
00084             // Create a backface culling state
00085             // We don't want to render backfaces. this way we can "force" the geoms to be rendered without 
00086             // backfaces
00087             m_state=new gzState;
00088 
00089             m_state->setMode(GZ_STATE_POLYGON_MODE,GZ_STATE_ON);
00090             m_state->setBackPolygonMode(GZ_POLYGON_MODE_CULL);
00091             m_state->setFrontPolygonMode(GZ_POLYGON_MODE_FILL);
00092 
00093 #ifdef USE_STATS            
00094             m_state->setMode(GZ_STATE_GENERATE_DEBUG_INFO,GZ_STATE_ON);
00095             m_state->setDebugMode((gzStateDebugMode)(GZ_STATE_DEBUG_COLLECT_STATS /*|GZ_STATE_DEBUG_SHOW_BSPHERE*/));
00096 #endif                             
00097             
00098             
00099 
00100             m_state->disableAllNonEnabledModes();   // We need this for the top global state
00101 
00102             gzState::setGlobalState(getContext(),m_state);
00103             
00104 
00105             // ---------------------- DB loading ------------------------------
00106 
00107             // Create a object node from a generic db file
00108             
00109             gzDouble secs=gzTime::systemSeconds();  // Some timing
00110 
00111             // Load the DB
00112             
00113             gizmo=gzDbManager::loadDB(filename,GZ_EVALUATE_EXTENSION,flags 
00114                                                                         //  | GZ_DB_FLAGS_FLIP_DDS_IMAGES
00115                                                                         //  | GZ_DB_FLAGS_USE_MIPMAPS
00116                                                                         //  | GZ_DB_FLAGS_SORT_BINARY_ALPHA
00117                                                                         //  | GZ_DB_FLAGS_GENERATE_LIGHT_MATERIAL
00118                                                                         //  | GZ_DB_FLAGS_USE_INDEXED_GEOMETRY
00119                                                                         //  | GZ_DB_FLAGS_USE_DISPLAYLIST_GEOMETRY
00120                                                                         //  | GZ_DB_FLAGS_USE_ANIMATION
00121                                                                         //  | GZ_DB_FLAGS_COMPRESS_ON_GPU 
00122                                                                         //  | GZ_DB_FLAGS_USE_BUFFERED_GEOMETRY 
00123                                                                         //  | GZ_DB_FLAGS_DISPOSE_BUFFERED_GEOMETRY 
00124                                                                         );
00125 
00126             // How long
00127             gzDouble duration=gzTime::systemSeconds()-secs;
00128 
00129             gzNodeOptimizer opt;        // We let the optimizer take away redundant geometry etc.
00130 
00131             gizmo=opt.optimize(gizmo);
00132 
00133             // --------------------- animation ------------------------------
00134 
00135             if(gizmo)
00136             {
00137                 if(gizmo->isOfType(gzFrame::getClassType()))    // Check if it is animated
00138                 {
00139                     gzFrame *frame=(gzFrame *)gizmo;
00140 
00141                     frame->setFrameDuration(0.05);
00142                     frame->startFrameCounter();
00143                 }
00144             }
00145 
00146             // -------------------- lights --------------------------------
00147 
00148             // Add some dynamic light
00149             m_spin=new gzLight;                     // This is the lamp that we move around
00150             m_spin->setSpecularColor(0.5f,0.5f,0.5f);
00151             m_spin->setDiffuseColor(0.7f,0.7f,0.7f);
00152             m_spin->setAmbientColor(0.2f,0.2f,0.2f);
00153             m_spin->setPosition(26,10,90);
00154 
00155             group->addLight(m_spin);
00156 
00157             // ------------------------ some global settings --------------
00158 
00159             gzEnvironment::setTwoSideLighting(getContext(),TRUE);
00160 
00161             gzEnvironment::setLocalViewer(getContext(),TRUE);
00162 
00163 
00164             m_lodFactor=1;
00165             // How much do we want to scale LOD details
00166             gzLod::setLodFactor(getContext(),m_lodFactor);
00167 
00168             // We want to enable an automatic calc of smoothness in lods
00169             if(useLodSmooth)
00170                 gzLod::setGenericFadeDistance(getContext(),(gzFloat)fadeDist);
00171 
00172     
00173             if(skyLayer)
00174             {
00175                 gzSkyBox *box=new gzSkyBox;
00176 
00177     //          box->setImageSide(GZ_SKYBOX_EAST,gzImageManager::loadImage("east.bmp"));
00178     //          box->setImageSide(GZ_SKYBOX_SOUTH,gzImageManager::loadImage("south.bmp"));
00179     //          box->setImageSide(GZ_SKYBOX_WEST,gzImageManager::loadImage("west.bmp"));
00180     //          box->setImageSide(GZ_SKYBOX_NORTH,gzImageManager::loadImage("north.bmp"));
00181     //          box->setImageSide(GZ_SKYBOX_SKY,gzImageManager::loadImage("sky.bmp"));
00182     //          box->setImageSide(GZ_SKYBOX_GROUND,gzImageManager::loadImage("ground.bmp"));
00183 
00184                 //box->setImageSide(GZ_SKYBOX_SKY,gzImageManager::loadImage("clouds.png"));
00185 
00186                 //box->setHorizonAngle(20);
00187 
00188                 //box->setGroundPoint(0,-1000,0);
00189 
00190                 //box->setStandardHeight(2000);
00191 
00192                 m_scene->addNode(box);
00193 
00194 
00195                 gzSkyLayer *layer;
00196 
00197                 layer=new gzSkyLayer;
00198 
00199                 gzImage *clouds=gzImageManager::loadImage("light_cloud.dds",GZ_EVALUATE_EXTENSION,GZ_IMAGE_FLAGS_NO_ALTERNATE_IMAGE_EXT);
00200 
00201                 layer->setLayerImage(clouds,100000,100000,5000,600,300);
00202 
00203                 m_scene->addNode(layer);
00204 
00205                 
00206                 layer=new gzSkyLayer;
00207 
00208                 layer->setLayerImage(clouds,70000,50000,6000,0,500);
00209 
00210                 m_scene->addNode(layer);
00211 
00212             }
00213 
00214 
00215             // -------- Ajustments for viewer ----------------------------
00216 
00217             // Lets make a node that scales down the loaded object to a unit size
00218 
00219             if(gizmo && gizmo->isOfType(gzRoi::getClassType()))
00220             {
00221                 // We use ROI. model is scaled in metres
00222 
00223                 gzTransform *trans=new gzTransform;
00224 
00225                 trans->addNode(gizmo);
00226 
00227                 gzDouble x=0,y=0,z=0;
00228 
00229                 if(gzHasNodeDbInfo(gizmo,GZ_DB_INFO_DB_SIZE))
00230                 {
00231                     gzAttribute_DBSize dbinfo=gzDynamic_Cast<gzAttribute_DBSize>(gzGetNodeDbInfo(gizmo,GZ_DB_INFO_DB_SIZE));
00232 
00233                     x=(dbinfo.minX+dbinfo.maxX)/2;
00234 
00235                     y=(dbinfo.minY+dbinfo.maxY)/2;
00236 
00237                     z=(dbinfo.minZ+dbinfo.maxZ)/2;
00238 
00239                     trans->setTranslation(-(gzReal)x,-(gzReal)y,-(gzReal)z);
00240                 }
00241 
00242                 
00243                 // Lets scale it a bit
00244 
00245                 if(gzHasNodeDbInfo(gizmo,GZ_DB_INFO_DB_MAX_LOD_RANGE))
00246                 {
00247                     gzFloat range=1/(gzFloat)gzGetNodeDbInfo(gizmo,GZ_DB_INFO_DB_MAX_LOD_RANGE).getNumber();
00248 
00249                     trans->scale(range);
00250                 }
00251 
00252                 trans->scale((gzReal)(30*scale),(gzReal)(30*scale),(gzReal)(30*scale));
00253 
00254                 group->addNode(trans);
00255 
00256                 //((gzRoi *)gizmo)->loadAtPosition(NULL,gzRoiPosition(x,y,z),TRUE);
00257 
00258             }
00259             else
00260             {
00261                 // We don't use Region of Interest. lets autoscale it
00262 
00263                 gzTransform *trans=new gzTransform;
00264 
00265                 trans->addNode(gizmo);
00266                 
00267                 trans->unitScale();
00268 
00269                 // Lets scale it a bit
00270 
00271                 trans->scale((gzReal)(30*scale),(gzReal)(30*scale),(gzReal)(30*scale));
00272 
00273                 group->addNode(trans);
00274             }
00275 
00276 
00277             
00278             // Add a lamp to show position of light
00279 
00280             m_lamp=new gzTransform;
00281 
00282             gzGeometry *sphere=new gzGeometrySphere(1 , 20 ,gzVec4(1.0,1.0,1.0,1.0) );
00283 
00284             sphere->setBind(TRUE,GZ_VBO_STATIC_DROP_LOCAL);
00285 
00286             m_lamp->addNode( sphere );
00287 
00288             m_scene->addNode(m_lamp);
00289 
00290             // Set mouse press state
00291             m_inMousePress=FALSE;
00292 
00293             // Add a line of sight object
00294 
00295             m_los=new gzLineOfSight;
00296 
00297             m_los->setNodeTree(m_scene);
00298 
00299             m_scene->addNode(m_los);
00300 
00301             // Add a visual coverage object
00302             m_cov = new gzVisualCoverage;
00303 
00304             m_cov->setNodeTree(group);
00305 
00306             m_scene->addNode(m_cov);
00307 
00308             // Add a visual coverage sampling object
00309 
00310             gzImageRender *renderer=new gzImageRender(this);
00311 
00312             //gzImage_DepthMap *image=new gzImage_DepthMap;
00313             gzImage *image=gzImage::createImage(GZ_IMAGE_TYPE_DEPTHMAP);
00314 
00315             renderer->setImage(image);
00316 
00317             m_covSample = new gzVisualCovSample(renderer,image);
00318 
00319             m_covSample->setNodeTree(group);
00320 
00321             m_scene->addNode(m_covSample);
00322 
00323             // Test a virtual pip display
00324 
00325             gzViewPlane *viewPlane=new gzViewPlane;
00326 
00327             viewPlane->setCoordinates(gzVec2(0,0),gzVec2(100,100));
00328 
00329             viewPlane->usePixelPosition(TRUE);
00330 
00331             gzState *viewPlaneState=new gzState;
00332 
00333             gzTexture *tex=new gzTexture;
00334 
00335             tex->setImage(image);
00336 
00337             viewPlaneState->setTexture(tex);
00338 
00339             viewPlaneState->setMode(GZ_STATE_TEXTURE,GZ_STATE_ON);
00340 
00341             viewPlaneState->setActionStage(GZ_ACTION_STAGE_OVERLAY);
00342 
00343             viewPlaneState->setMode(GZ_STATE_ACTION_STAGE,GZ_STATE_ON);
00344 
00345             viewPlane->setState(viewPlaneState);
00346 
00347             viewPlane->setAlignment(GZ_VIEWPLANE_ALIGN_BOTTOM_RIGHT);
00348 
00349             gzTexEnv *env=new gzTexEnv;
00350 
00351             env->setMode(GZ_TEXENV_MODE_DECAL);
00352 
00353             viewPlaneState->setMode(GZ_STATE_TEXENV,GZ_STATE_ON);
00354 
00355             viewPlaneState->setTexEnv(env);
00356 
00357             m_switch = new gzSwitch;
00358 
00359             m_switch->addNode(viewPlane);
00360 
00361             m_scene->addNode(m_switch);
00362 
00363             m_switch->setSwitchVal(GZ_SWITCH_NONE);
00364 
00365 
00366             // Add the logo PIP
00367 
00368             gzImage *logo=gzImageManager::loadImage("gizmo3d.png",GZ_EVALUATE_EXTENSION,GZ_IMAGE_FLAGS_NO_ALTERNATE_IMAGE_EXT);
00369 
00370 
00371             if(logo)
00372             {
00373 
00374                 gzViewPlane *viewPlane2=new gzViewPlane;
00375 
00376                 viewPlane2->setCoordinates(gzVec2(10,10),gzVec2(256+10,64+10));
00377 
00378                 viewPlane2->usePixelPosition(TRUE);
00379 
00380                 gzState *viewPlaneState2=new gzState;
00381 
00382                 gzTexture *tex2=new gzTexture;
00383 
00384                 tex2->setImage(logo);
00385 
00386                 tex2->setComponents(4);
00387 
00388                 viewPlaneState2->setTexture(tex2);
00389 
00390                 viewPlaneState2->setMode(GZ_STATE_TEXTURE,GZ_STATE_ON);
00391 
00392                 viewPlaneState2->setActionStage(GZ_ACTION_STAGE_OVERLAY);
00393 
00394                 viewPlaneState2->setMode(GZ_STATE_ACTION_STAGE,GZ_STATE_ON);
00395 
00396                 //viewPlaneState2->setMode(GZ_STATE_ALPHA_FUNC,GZ_STATE_ON);
00397 
00398                 //viewPlaneState2->setAlphaFunc(GZ_ALPHA_FUNC_GREATER,0.5f);
00399 
00400                 viewPlaneState2->setMode(GZ_STATE_BLENDING,GZ_STATE_ON);
00401                 viewPlaneState2->setBlendFunction(GZ_BLEND_SRC_ALPHA,GZ_BLEND_ONE_MINUS_SRC_ALPHA);
00402 
00403 
00404                 viewPlane2->setState(viewPlaneState2);
00405 
00406                 viewPlane2->setAlignment(GZ_VIEWPLANE_ALIGN_BOTTOM_LEFT);
00407 
00408                 viewPlaneState2->setMode(GZ_STATE_TEXENV,GZ_STATE_ON);
00409 
00410                 gzTexEnv *env2=new gzTexEnv;
00411 
00412                 env2->setMode(GZ_TEXENV_MODE_BLEND);
00413 
00414                 viewPlaneState2->setTexEnv(env2);
00415 
00416                 m_scene->addNode(viewPlane2);
00417             }
00418 
00419         
00420             // Shall we shadow
00421             if(useShadow)
00422             {
00423 
00424                 // ------------------- SHADOWS !!! --------------------------
00425 
00426                 // Add the shadow renderer. This is responisble for the volume rending
00427 
00428                 gzShadowRender *vol=new gzShadowRender;
00429 
00430                 //vol->showVolumes(TRUE);   // Try this ;-)
00431 
00432                 // Our shadow renderer can make various shadows. Now lets make a "simple" shadow that just
00433                 // darkens a region and take away the specular component of the light
00434 
00435                     
00436                 // mask that adds the trasnparend black color to the shadow
00437 
00438                 if(softShadow)
00439                 {
00440 
00441                     vol->addGeometryMask(createPlane(0));           // Now the plane that cover the entire display is a 
00442                     vol->addGeometryMask(createPlane(0),1);
00443                     vol->addGeometryMask(createPlane(0),2);
00444                     vol->addGeometryMask(createPlane(0),3);
00445 
00446                     //vol->setPassOffset(1);    //Standard way to use smooth shadow corners
00447 
00448                 }
00449                 else
00450                 {
00451                     m_spin->excludeDiffuseInShadow(TRUE);
00452                     //vol->setPassOffset(1);    //Standard way to use smooth shadow corners
00453                 }
00454 
00455                 vol->useClearing(FALSE);                // Don't bother to clean up stencil values  
00456 
00457                 // What is in the shadow..
00458 
00459                 // Lets add all the scene to the possible shadow receivers for occluded specular light. All nodes under the shadow renderer
00460                 // receives the shadow with specular component disabled.
00461 
00462                 vol->addNode(group);
00463 
00464                 // and add the shadow render to the scene 
00465                 m_scene->addNode(vol);
00466         
00467                 // Oahh ! Lets add shadows to all objects in the scene.
00468                 // It adds shadow generators to all geomtery under the scene node
00469                 // The length of the shadow is 50 times the distance between the light and the occluder
00470 
00471                 if(softShadow)
00472                     gzShadowGenerator::addShadowGenerators(group,vol,m_spin,FALSE,50,3,0.98f);
00473                 else
00474                     gzShadowGenerator::addShadowGenerators(group,vol,m_spin,FALSE,50);
00475 
00476             }
00477 
00478 
00479 
00480             // And add the group to the scene as well
00481             m_scene->addNode(group);
00482 
00483                                     
00484             // Add some textual info about frame rates
00485             m_text=new gzText("Screen Text");
00486 
00487             //m_text->setColor(gzVec4(1,1,0,0.5));
00488             m_text->use2DPosition(TRUE);
00489             m_text->set2DPosition(10, -10);
00490             //m_text->useMaskColor(TRUE);
00491             
00492             m_scene->addNode(m_text);
00493 
00494             // Ok. How long did this take...
00495 
00496             GZMESSAGE(GZ_MESSAGE_NOTICE,"Loading time for DB = %d secs",duration);
00497 
00498 
00499             // Now we want to look at the scene. Grab the default perspective camera from the window and set the scen as the
00500             // active one.
00501             getCamera()->setScene(m_scene);
00502             getCamera()->setNearClipPlane(1);
00503             getCamera()->setFarClipPlane(2000);
00504             //((gzPerspCamera *)getCamera())->setVerticalFOV(3);
00505             //((gzPerspCamera *)getCamera())->setHorizontalFOV(3);
00506 
00507 
00508             // Lets add some movement to the scene
00509 
00510             m_input=new gzSimpleMouseViewControl(getCamera());
00511 
00512             addInputInterface(m_input);
00513 
00514             if(useFog)
00515             {
00516                 // And hey. Why not toss in some fog as well...
00517                 group->useFog(TRUE);
00518                 group->setFogStart(0);
00519                 group->setFogEnd(3000);
00520                 group->setFogMode(GZ_FOG_MODE_LINEAR);
00521                 group->setFogDensity(0.7f);
00522                 group->setFogColor(gzRGBA(0.5f,0.5f,0.5f,1.0f));
00523             }
00524 
00525             // Hmm. trivial
00526             setBackground(0.0f,0.0f,1.0f,1.0f);
00527 
00528             // Let us try to sync the refresh to the default monitor rate. Set zero value to go bezerk
00529             setSwapInterval(swap);
00530 
00531             // and show us ...
00532             show();
00533 
00534             // hm. wonder why I put this one here ?
00535             angle=2.22f;
00536 
00537             if(debug)
00538                 m_scene->debug(GZ_DEBUG_SHOW_ALL);
00539 
00540             gzDynamicLoaderManager::startManager();
00541 
00542             gzOrigin::addOriginAxis();
00543     };
00544 
00545 
00546     /*
00547         The following code snippet is a virtual function to catch the window messages
00548         for pressed, repeated and released keys
00549     */
00550 
00551     gzBool onKey(gzKeyValue key , gzKeyState keystate , gzLong mouse_x , gzLong mouse_y)
00552     {
00553         switch(key)
00554         {
00555             case ' ':   // Space pressed
00556 
00557                 if(keystate == GZ_KEY_STATE_PRESSED)
00558                 {
00559                     gzState *state=new gzState;
00560 
00561                     
00562                     state->setFrontPolygonMode(GZ_POLYGON_MODE_LINE);
00563                     state->setBackPolygonMode(GZ_POLYGON_MODE_LINE);
00564                     state->setMode(GZ_STATE_POLYGON_MODE,GZ_STATE_ON);
00565                     state->setOverride(GZ_STATE_POLYGON_MODE,GZ_STATE_ON);
00566                     state->setOverride(GZ_STATE_TEXTURE,GZ_STATE_OFF);
00567 
00568                     state->setMode(GZ_STATE_GENERATE_DEBUG_INFO,GZ_STATE_ON);
00569                     state->setDebugMode((gzStateDebugMode)(GZ_STATE_DEBUG_COLLECT_STATS));
00570 
00571                     
00572                     state->disableAllNonEnabledModes();
00573 
00574                     gzState::setGlobalState(getContext(),state);
00575                 }
00576                 if(keystate == GZ_KEY_STATE_RELEASED)
00577                 {
00578                     gzState::setGlobalState(getContext(),m_state);
00579                 }
00580                 break;
00581 
00582             case GZ_KEY_LBUTTON :
00583 
00584                 if((keystate == GZ_KEY_STATE_PRESSED) && !m_inMousePress)
00585                 {
00586                     setCaptureMouse(TRUE);
00587                     setHideMouse(TRUE);
00588                     m_inMousePress=TRUE;
00589                 }
00590                 else if((keystate == GZ_KEY_STATE_RELEASED) && m_inMousePress)
00591                 {
00592                     setHideMouse(FALSE);
00593                     setCaptureMouse(FALSE);
00594                     m_inMousePress=FALSE;
00595                 }
00596                 break;
00597 
00598             case 'd' :
00599                 if(keystate == GZ_KEY_STATE_PRESSED)
00600                 {
00601                     gzLogger logger;
00602                     getCamera()->getScene()->debug(GZ_DEBUG_SHOW_ALL);
00603                 }
00604                 break;
00605 
00606             case 'f' :
00607                 if(keystate == GZ_KEY_STATE_PRESSED)
00608                 {
00609                     GZMESSAGE(GZ_MESSAGE_NOTICE,"FrameRate %d",getFrameRate());
00610                 }
00611                 break;
00612 
00613             case 'p' :
00614                 if(keystate == GZ_KEY_STATE_PRESSED)
00615                 {
00616                     GZMESSAGE(GZ_MESSAGE_NOTICE,"Position %s HPR %s",(const char *)getCamera()->getPosition().asString(),(const char *)getCamera()->getHPR().asString());
00617                 }
00618                 break;
00619 
00620             case '1' :
00621             case '2' :
00622                 {
00623                     gzVec3 startPosition;
00624                     gzVec3 direction;
00625 
00626 
00627                     gzGetMouseXYIntersector(startPosition,direction,getCamera(),mouse_x,mouse_y,getSize());
00628 
00629                     gzIntersector isector;
00630 
00631                     isector.setBaseTransform(getContext()->mainTransform);
00632                     isector.setProjectionTransform(getContext()->mainProjTransform);
00633 
00634                     isector.setStartPosition(startPosition);
00635                     isector.setDirection(direction);
00636 
00637                     if(isector.intersect(getCamera()->getScene(),(GZ_INTERSECTOR_NEAREST_POINT)))
00638                     {
00639                         gzVec3 pos=isector.getResult()[0].coordinate;
00640 
00641                         if(key=='1')
00642                             m_los->setStartPosition(pos);
00643                         else
00644                             m_los->setStopPosition(pos);
00645                     }
00646                 }
00647                 break;
00648 
00649             case '3' :
00650                 {
00651                     gzVec3 startPosition;
00652                     gzVec3 direction;
00653 
00654                     gzGetMouseXYIntersector(startPosition,direction,getCamera(),mouse_x,mouse_y,getSize());
00655 
00656                     gzIntersector isector;
00657 
00658                     isector.setBaseTransform(getContext()->mainTransform);
00659                     isector.setProjectionTransform(getContext()->mainProjTransform);
00660 
00661                     isector.setStartPosition(startPosition);
00662                     isector.setDirection(direction);
00663 
00664                     if(isector.intersect(getCamera()->getScene(),(GZ_INTERSECTOR_NEAREST_POINT)))
00665                     {
00666                         gzVec3 pos=isector.getResult()[0].coordinate;
00667 
00668                         m_cov->setPosition(pos+gzVec3(0,2,0));
00669                     }
00670                 }
00671                 break;
00672 
00673             case '4' :
00674             case '5' :
00675                 {
00676                     gzVec3 startPosition;
00677                     gzVec3 direction;
00678 
00679                     gzGetMouseXYIntersector(startPosition,direction,getCamera(),mouse_x,mouse_y,getSize());
00680                     gzIntersector isector;
00681 
00682                     isector.setBaseTransform(getContext()->mainTransform);
00683                     isector.setProjectionTransform(getContext()->mainProjTransform);
00684 
00685                     isector.setStartPosition(startPosition);
00686                     isector.setDirection(direction);
00687 
00688                     if(isector.intersect(getCamera()->getScene(),(GZ_INTERSECTOR_NEAREST_POINT)))
00689                     {
00690                         gzVec3 pos=isector.getResult()[0].coordinate;
00691 
00692                         if(key=='4')
00693                             m_covSample->setPosition(pos+gzVec3(0,2,0));
00694                         else
00695                             m_covSample->lookAt(pos);
00696 
00697                         m_switch->setSwitchVal(GZ_SWITCH_ALL);
00698                     }
00699                 }
00700                 break;
00701 
00702             case GZ_KEY_ADD :
00703                 {
00704                     m_lodFactor+=0.1f;
00705                     gzLod::setLodFactor(getContext(),m_lodFactor);
00706                     GZMESSAGE(GZ_MESSAGE_DEBUG,"Lod Factor:%f",m_lodFactor);
00707                 }
00708                 break;
00709 
00710             case GZ_KEY_SUBTRACT :
00711                 {
00712                     if(m_lodFactor>0)
00713                         m_lodFactor-=0.1f;
00714                     gzLod::setLodFactor(getContext(),m_lodFactor);
00715                     GZMESSAGE(GZ_MESSAGE_DEBUG,"Lod Factor:%f",m_lodFactor);
00716                 }
00717                 break;
00718 
00719             case 'h' :
00720                 {
00721                     if(keystate == GZ_KEY_STATE_PRESSED)
00722                     {
00723                 
00724                         gzVec3 startPosition;
00725                         gzVec3 direction;
00726 
00727                         gzGetMouseXYIntersector(startPosition,direction,getCamera(),mouse_x,mouse_y,getSize());
00728                         gzIntersector isector;
00729 
00730                     isector.setBaseTransform(getContext()->mainTransform);
00731                     isector.setProjectionTransform(getContext()->mainProjTransform);
00732 
00733                     isector.setStartPosition(startPosition);
00734                     isector.setDirection(direction);
00735 
00736                         if(isector.intersect(getCamera()->getScene(),(GZ_INTERSECTOR_NEAREST_POINT|GZ_INTERSECTOR_HISTORY|GZ_INTERSECTOR_NODE)))
00737                         {
00738                             gzVec3 pos=isector.getResult()[0].coordinate;
00739 
00740                             GZMESSAGE(GZ_MESSAGE_DEBUG,"Node %s",(const char *)isector.getResult()[0].node->getName());
00741                         }
00742                     }
00743                 }
00744                 break;
00745 
00746         }
00747 
00748         return gzWindow::onKey((gzKeyValue)key,(gzKeyState)keystate,mouse_x,mouse_y);
00749     }
00750 
00751 
00752     gzVoid onIdle()
00753     {
00754         // Hmm. Now i remember. The spinning lamp..
00755 
00756         angle+=0.003f;
00757 
00758         if(angle>2*GZ_PI)
00759             angle-=(gzReal)(2*GZ_PI);
00760 
00761         m_spin->setPosition((gzReal)(30*cos(5*angle)),(gzReal)(10*cos(angle))+30,(gzReal)(30*sin(angle/2)+20));
00762 
00763         m_lamp->setTranslation((gzReal)(30*cos(5*angle)),(gzReal)(10*cos(angle))+30,(gzReal)(30*sin(angle/2)+20));
00764 
00765         gzSeedTransform::setGlobalStriveStrength((1+(0.3*cos(7*angle)*sin(3*angle)+0.4*cos(5*angle)+0.2*sin(8*angle)+0.1*sin(21*angle) ))*0.5);
00766 
00767     }
00768 
00769     virtual ~MyWindow()
00770     {
00771         if(m_input)
00772             delete m_input;
00773 
00774         if(m_los)
00775             m_los->setNodeTree(NULL);   // Recursive deadlock of scene
00776     }
00777 
00778     gzRefPointer<gzState> m_state;
00779 
00780     gzLight *           m_spin;
00781 
00782     gzText *            m_text;
00783 
00784     gzReal              angle;
00785 
00786     gzTransform *       m_lamp;
00787     
00788     gzBool              m_inMousePress;
00789 
00790     gzNode              *gizmo;
00791 
00792     gzLineOfSight       *m_los;
00793 
00794     gzVisualCoverage    *m_cov;
00795 
00796     gzVisualCovSample   *m_covSample;
00797 
00798     gzScene             *m_scene;
00799 
00800     gzSwitch            *m_switch;
00801 
00802     gzSimpleMouseViewControl    *m_input;
00803 
00804 
00805     gzFloat             m_lodFactor;
00806 };
00807 
00808 // Definition of a sample application
00809 // The application provides an initialisation and an onIdle loop manager
00810 // to do the refresh of the window
00811 
00812 class WindowApp : public gzApplication
00813 {
00814 public:
00815         
00816     WindowApp(){}
00817 
00818     ~WindowApp(){}
00819 
00820     void Create(gzInt swap,gzBool useLodSmooth,gzDouble fadeDist,gzBool useFog,gzBool softShadow,gzBool useRoi,gzBool debug,gzBool bFullScreen,gzBool useShadow, gzSerializeAdapterFlags flags,gzString filename,gzReal scale,gzBool skyLayer, gzVec3 eyePos,gzVec3 hpr)
00821     {
00822         gzGraphicsFormat *format = new gzGraphicsFormat;
00823 
00824         if(bFullScreen)
00825         {
00826             format->setFullScreenWidth(1280);
00827             format->setFullScreenFrequency(75);
00828             format->useFullScreen(TRUE);
00829             format->setDepthBits(32);
00830         }
00831 
00832         format->useStencil(TRUE);
00833 
00834         m_win = new MyWindow(   swap,
00835                                 useLodSmooth,
00836                                 fadeDist,
00837                                 useFog,
00838                                 useRoi,
00839                                 softShadow,
00840                                 debug,
00841                                 format,useShadow,flags,filename,scale,skyLayer);
00842 
00843         gzPerspCamera *camera=gzDynamic_Cast<gzPerspCamera>(m_win->getCamera());
00844 
00845         camera->setHPR(hpr);
00846         camera->setPosition(eyePos);
00847 
00848         if(hpr==gzVec3(0,0,0))
00849             camera->lookAt(0,0,0);
00850 
00851         camera->useInfiniteFarPlane(TRUE);
00852 
00853         //camera->flipVerticalView(TRUE);
00854 
00855         //camera->flipHorizontalView(TRUE);
00856 
00857         if(useRoi)
00858             m_win->getCamera()->useRoiPosition(TRUE);
00859 
00860 
00861         gzULongXY size=m_win->getSize();
00862 
00863 
00864     }
00865 
00866     void onIdle()
00867     {
00868         if(m_win)
00869         {
00870             m_win->onIdle();
00871 
00872             m_win->triggerKeyEvaluation();
00873 
00874             if(!m_win->refreshWindow()) // Yield some time if no rendering
00875                 gzSleep(100);
00876             else
00877                 gzSleep(1);             // Yield so we can use some dynamic loading in the background as well.
00878 
00879             gzDouble delay=gzDynamicLoader::getWorkDelay();
00880 
00881             gzULong pending=gzDynamicLoader::getPendingWork();
00882 
00883             gzString outputString;
00884 
00885             if(!gzDynamicLoader::isWorking())
00886             {
00887                 outputString=gzString("FrameRate:")+gzString(m_win->getFrameRate());
00888                 delay=0;
00889             }
00890             else
00891                 outputString=gzString("FrameRate:")+gzString(m_win->getFrameRate())+"\n\nDynamic Loading -- Delay("+gzString(delay,"%.2f")+gzString("s) Pending(") + gzString((gzLongLong)gzDynamicLoader::getPendingWork())+ gzString(")");
00892 
00893 #ifdef USE_STATS
00894             outputString+=(gzString("\n\n\n")+gzGraphicsStats(m_win->getContext()));
00895 #endif
00896             
00897             if((delay>0.8) || (pending>100))
00898             {
00899                 gzDynamicLoader::boostLoaderWork();
00900                 outputString+="\n\n BOOSTED LOADING !!";
00901             }
00902             else
00903                 gzDynamicLoader::unBoostLoaderWork();
00904 
00905             m_win->m_text->setText(outputString);
00906 
00907         }
00908 
00909     }
00910 
00911 private:
00912 
00913     friend class MyWindow;
00914     
00915     gzRefPointer<MyWindow> m_win;
00916 
00917 };
00918 
00919 #include "gzDebug.h"
00920 
00921 
00922 
00923 
00924 //---------------------------------------------------------------
00925 
00926 gzVoid outputHelp()
00927 {
00928     GZMESSAGE(GZ_MESSAGE_NOTICE,"---------------------- Help -----------------------");
00929     GZMESSAGE(GZ_MESSAGE_NOTICE,"- Use keys 'a' and 'z' to move forward/backwards");
00930     GZMESSAGE(GZ_MESSAGE_NOTICE,"- Use keys 'q' and 'w' to stride left/right");
00931     GZMESSAGE(GZ_MESSAGE_NOTICE,"- Use keys 's' and 'x' to move up/down");
00932     GZMESSAGE(GZ_MESSAGE_NOTICE,"- Use arrow keys to rotate viewer");
00933     GZMESSAGE(GZ_MESSAGE_NOTICE,"- or press left mouse button to fly with mouse");
00934     GZMESSAGE(GZ_MESSAGE_NOTICE,"-");
00935     GZMESSAGE(GZ_MESSAGE_NOTICE,"- Use argument -x xxx to enable xxx func.. see below");
00936     GZMESSAGE(GZ_MESSAGE_NOTICE,"-");
00937     GZMESSAGE(GZ_MESSAGE_NOTICE,"- Use '-a' to enable animation in files");
00938     GZMESSAGE(GZ_MESSAGE_NOTICE,"- Use '-b' to smooth alpha mask");
00939     GZMESSAGE(GZ_MESSAGE_NOTICE,"- Use '-c' to enable fog");
00940     GZMESSAGE(GZ_MESSAGE_NOTICE,"- Use '-d' to get debug tree output");
00941     GZMESSAGE(GZ_MESSAGE_NOTICE,"- Use '-e' to enable smooth lod ranges (200 m)");
00942     GZMESSAGE(GZ_MESSAGE_NOTICE,"- Use '-f' to enable full screen");
00943     GZMESSAGE(GZ_MESSAGE_NOTICE,"- Use '-g xxx' to set swap sync to xxx");
00944     GZMESSAGE(GZ_MESSAGE_NOTICE,"- Use '-h' to print this help");
00945     GZMESSAGE(GZ_MESSAGE_NOTICE,"- Use '-i xxx' to set lod fade distance to xxx");
00946     GZMESSAGE(GZ_MESSAGE_NOTICE,"- Use '-j' to convert to grayscale images");
00947     GZMESSAGE(GZ_MESSAGE_NOTICE,"- Use '-k' to generate indexed geometry");
00948     GZMESSAGE(GZ_MESSAGE_NOTICE,"- Use '-l' to enable generation of light materials");
00949     GZMESSAGE(GZ_MESSAGE_NOTICE,"- Use '-m xxxx' to load model xxxx");
00950     GZMESSAGE(GZ_MESSAGE_NOTICE,"- Use '-p' to enable mipmap usage");
00951     GZMESSAGE(GZ_MESSAGE_NOTICE,"- Use '-q' to enable shadows for scene");
00952     GZMESSAGE(GZ_MESSAGE_NOTICE,"- Use '-r' to enable ROI (Region Of Interest) position");
00953     GZMESSAGE(GZ_MESSAGE_NOTICE,"- Use '-s xxx' to set scale xxx");
00954     GZMESSAGE(GZ_MESSAGE_NOTICE,"- Use '-t' to enable soft shadows on some HW");
00955     GZMESSAGE(GZ_MESSAGE_NOTICE,"- Use '-u' to enable sky Layers");
00956     GZMESSAGE(GZ_MESSAGE_NOTICE,"- Use '-v x,y,z' to set camera position");
00957     GZMESSAGE(GZ_MESSAGE_NOTICE,"- Use '-w' to enable prefix encoding to enhance some DB formats");
00958     GZMESSAGE(GZ_MESSAGE_NOTICE,"- Use '-x h,p,r' to set camera heading");
00959     GZMESSAGE(GZ_MESSAGE_NOTICE,"- Use '-y' to enable VBO");
00960     GZMESSAGE(GZ_MESSAGE_NOTICE,"- Use '-z' to enable DisplayLists");
00961     
00962 }
00963 
00964 
00965 int main(int argc, char *argv[])
00966 {
00967 
00968     gzStartUpGizmo();   // Needed for some systems to install external graphics engines
00969 
00970     gzMessage::setMessageLevel(GZ_MESSAGE_MEM_DEBUG);
00971 
00972 
00973     try
00974     { 
00975         gzArgumentParser args(argc,argv);
00976 
00977         args.exitOnError(FALSE);    // We want to print the help
00978 
00979         gzBool      fullscreen  = args.hasOption("f") | args.hasOption("fullscreen");
00980 
00981         gzString    filename    = args.getOptionValue("m","gizmo.3ds");
00982 
00983         gzFloat     scale       = args.getOptionValue("s",1.0);
00984 
00985         gzBool      useShadow   = args.hasOption("q");
00986 
00987         gzBool      debug       = args.hasOption("d");
00988 
00989         gzBool      useRoi      = args.hasOption("r");
00990 
00991         gzBool      softShadow  = args.hasOption("q");
00992 
00993         gzBool      useLodSmooth= args.hasOption("e");
00994 
00995         gzBool      useFog      = args.hasOption("c");
00996 
00997         gzDouble    fadeDist    = args.getOptionValue("i",500);
00998 
00999         gzInt       swap        = args.getOptionValue("g",1);
01000 
01001         gzBool      skyLayer    = args.hasOption("u");
01002 
01003         gzSerializeAdapterFlags     flags=GZ_DB_FLAGS_DEFAULT;
01004 
01005         gzVec3      eyePos      = gzVec3(args.getOptionValue("v",50.0,0),args.getOptionValue("v",50.0,1),args.getOptionValue("v",120.0,2));
01006 
01007         gzVec3      hpr         = gzVec3(args.getOptionValue("x",0.0f,0),args.getOptionValue("x",0.0f,1),args.getOptionValue("v",0.0f,2));
01008 
01009 
01010         gzLicense::notifyDefaultLicense();
01011     
01012         gzGraphicsEngine::useEngine(GZ_ENGINE_OPENGL);
01013 
01014         gzInitializeDbManagers();
01015 
01016         flags=flags|(args.hasOption("a") ? GZ_DB_FLAGS_USE_ANIMATION : GZ_DB_FLAGS_DEFAULT);
01017 
01018         flags=flags|(args.hasOption("b") ? GZ_DB_FLAGS_USE_TWO_PASS_ALPHA : GZ_DB_FLAGS_DEFAULT);
01019 
01020         flags=flags|(args.hasOption("j") ? GZ_DB_FLAGS_CONVERT_IMAGES_TO_GRAYSCALE : GZ_DB_FLAGS_DEFAULT);
01021 
01022         flags=flags|(args.hasOption("k") ? GZ_DB_FLAGS_USE_INDEXED_GEOMETRY : GZ_DB_FLAGS_DEFAULT);
01023 
01024         flags=flags|(args.hasOption("l") ? GZ_DB_FLAGS_GENERATE_LIGHT_MATERIAL : GZ_DB_FLAGS_DEFAULT);
01025 
01026         flags=flags|(args.hasOption("p") ? GZ_DB_FLAGS_USE_MIPMAPS : GZ_DB_FLAGS_DEFAULT);
01027 
01028         flags=flags|(args.hasOption("w") ? GZ_DB_FLAGS_USE_NAME_ENCODED_NODES : GZ_DB_FLAGS_DEFAULT);
01029 
01030         flags=flags|(args.hasOption("y") ? GZ_DB_FLAGS_USE_BUFFERED_GEOMETRY : GZ_DB_FLAGS_DEFAULT);
01031 
01032         flags=flags|(args.hasOption("z") ? GZ_DB_FLAGS_USE_DISPLAYLIST_GEOMETRY : GZ_DB_FLAGS_DEFAULT);
01033 
01034         if(args.hasOption("h") || args.hasOption("help") || args.hasError())
01035         {
01036             if(args.hasError())
01037                 GZMESSAGE(GZ_MESSAGE_WARNING,"Syntax Error:%s",(const char *)args.getError());
01038             outputHelp();
01039 
01040             exit(99);
01041         }
01042                 
01043         gzModule *module=gzModule::getModule("gzFFMPeg");
01044 
01045         // Make the application
01046         WindowApp app;
01047           
01048         // Create the scene and the window
01049         app.Create( swap,
01050                     useLodSmooth,
01051                     fadeDist,
01052                     useFog,
01053                     softShadow,
01054                     useRoi,
01055                     debug,
01056                     fullscreen,
01057                     useShadow,
01058                     flags,
01059                     filename,
01060                     scale,
01061                     skyLayer,
01062                     eyePos,
01063                     hpr);
01064         
01065         // run the application
01066         app.run();
01067 
01068     }
01069     catch(gzBaseError &error)       // In case of exceptions thrown we want to print the message
01070     {
01071         error.reportError();
01072     }
01073 
01074     gzModule::unLoadModules();
01075     
01076     gzShutDownGizmo();
01077 
01078     return 0;
01079 }
01080 
01081 
01082 

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