Gizmo3D

shaders.cpp

Shader example application. This example illustrates how to use a gzGFXProgram.
The source code can also be found in the examples directory of the Gizmo3D distribution.

00001 // *****************************************************************************
00002 // File         : shaders.cpp
00003 // Module       : 
00004 // Description  : Test app to show shader usage
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  030707  Created file    
00020 //
00021 // ******************************************************************************
00022 
00023 // The general include
00024 #include "gzGizmo3DLibrary.h"
00025 
00026 gzNode *createModel()
00027 {
00028     gzGeometry *geom=new gzGeometrySphere(10);
00029 
00030     gzArray<gzVec3> &normals=geom->getNormalArray(FALSE);
00031 
00032     gzArray<gzVec3> tangents(normals.getSize());
00033 
00034     for(gzULong i=0;i<tangents.getSize();i++)
00035     {
00036         // create sample tangent
00037         tangents[i]=gzVec3(normals[i].v3,0,-normals[i].v1);
00038     }
00039 
00040     gzVertexAttributeListTemplate<gzVec3> *vaList=new gzVertexAttributeListTemplate<gzVec3>(tangents,10);
00041 
00042     geom->addVertexAttributeList(vaList);
00043 
00044     return geom;
00045 }
00046 
00047 // And here comes the Java look alike app. It is a matter of taste.
00048 
00049 class MyWindow : public gzWindow
00050 {
00051 public:
00052     MyWindow(gzGraphicsFormat *format , gzString filename ):gzWindow(gzString("Gizmo3D : ")+gzTime::now(TRUE).asString(),NULL,format,FALSE)
00053     {
00054             // Create a scene to render -------------------------------------
00055             // We must have a scene that the camera can "look at"
00056             m_scene=new gzScene("Database Loader Example Scene");
00057             
00058             // Create some light environment group. This way we get lighting into the model
00059             // All stuff under this is lit with material s etc.
00060             gzEnvironment *group=new gzEnvironment;
00061 
00062             // Create a backface culling state
00063             // We don't want to render backfaces. this way we can "force" the geoms to be rendered without 
00064             // backfaces. It also enable the use of the shaders
00065 
00066             m_state=new gzState;
00067 
00068             m_state->setMode(GZ_STATE_POLYGON_MODE,GZ_STATE_ON);
00069             m_state->setBackPolygonMode(GZ_POLYGON_MODE_CULL);
00070             m_state->setFrontPolygonMode(GZ_POLYGON_MODE_FILL);
00071             m_state->disableAllNonEnabledModes();   // We need this for the top global state
00072 
00073             gzState::setGlobalState(getContext(),m_state);
00074             
00075 
00076             // ---------------------- DB loading ------------------------------
00077 
00078             gzString vertexProgram          ="glsl_PFLight.vsh";
00079             gzString fragmentProgram        ="glsl_PFLight.fsh";
00080 
00081             gzString alt_vertexProgram      ="PFLight.vsh";
00082             gzString alt_fragmentProgram    ="PFLight.fsh";
00083 
00084 
00085 
00086             // Create a object node from a generic db file
00087             
00088             //model=gzDbManager::loadDB(filename);
00089 
00090             model=createModel();
00091 
00092             if(model)   // We got a model
00093             {
00094             
00095                     gzState *state;
00096 
00097                     state=model->getState();
00098 
00099                     if(!state)
00100                         state=new gzState;
00101 
00102 
00103                     // Set the vertex program
00104 
00105                     gzGFXProgram *program_vp=new gzGFXProgram;
00106 
00107                     program_vp->loadProgramScript(vertexProgram);
00108 
00109                     program_vp->bindVertexAttribute("tangent",10);
00110 
00111                     // Create fallback program chain
00112 
00113                     gzGFXProgram *program_vp_alt=new gzGFXProgram;
00114 
00115                     program_vp_alt->loadProgramScript(alt_vertexProgram);
00116 
00117                     program_vp->setFallbackProgram(program_vp_alt);
00118 
00119 
00120                     state->setGFXProgram(GZ_GFX_VERTEX_PROGRAM,program_vp);
00121                     
00122 
00123                     // Set the fragment program
00124 
00125                     gzGFXProgram *program_fp=new gzGFXProgram;
00126 
00127                     program_fp->loadProgramScript(fragmentProgram);
00128 
00129                     // Create fallback program
00130 
00131                     gzGFXProgram *program_fp_alt=new gzGFXProgram;
00132 
00133                     program_fp_alt->loadProgramScript(alt_fragmentProgram);
00134 
00135                     program_fp->setFallbackProgram(program_fp_alt);
00136 
00137                     state->setGFXProgram(GZ_GFX_FRAGMENT_PROGRAM,program_fp);
00138                     
00139                     program_fp->setLocalParameter("color",gzVec4(1.0f, 0.5f, 0.3f, 1.0f));
00140 
00141 
00142                     // Enable GFX programs
00143 
00144                     state->setMode(GZ_STATE_GFX_PROGRAM,GZ_STATE_ON);
00145 
00146                     model->setState(state);
00147 
00148 
00149             }
00150 
00151             gzNodeOptimizer opt;        // We let the optimizer take away redundant geometry etc.
00152 
00153             opt.setMaxRecursionDepth(0);
00154 
00155             model=opt.optimize(model,(gzNodeOptimizeLevel)(GZ_NODE_OPTIMIZE_DONT_COMBINE_NAME_ENCODED/*|GZ_NODE_OPTIMIZE_DONT_CONCAT_GEOMETRY*/));
00156 
00157             // -------------------- lights --------------------------------
00158 
00159             // Add some dynamic light
00160             m_spin=new gzLight;                     // This is the lamp that we move around
00161             m_spin->setSpecularColor(0.2f,0.2f,0.2f);
00162             m_spin->setDiffuseColor(0.5f,0.5f,0.5f);
00163             m_spin->setAmbientColor(0.3f,0.3f,0.3f);
00164             m_spin->setPosition(26,10,90);
00165 
00166             group->addLight(m_spin);
00167 
00168             // ------------------------ some global settings --------------
00169 
00170             gzEnvironment::setTwoSideLighting(getContext(),TRUE);
00171 
00172             gzEnvironment::setLocalViewer(getContext(),TRUE);
00173 
00174             // Translate the model and scale it
00175 
00176             gzTransform *trans=new gzTransform;
00177 
00178             trans->addNode(model);
00179                 
00180             trans->unitScale();
00181 
00182             trans->scale(20.0f,20.0f,20.0f);
00183 
00184             group->addNode(trans);
00185             
00186             // Add a geometry alike lamp to show position of light
00187 
00188             m_lamp=new gzTransform;
00189 
00190             gzGeometry *sphere=new gzGeometrySphere(1 , 20 ,gzVec4(1.0,1.0,1.0,1.0) );
00191 
00192             m_lamp->addNode( sphere );
00193 
00194             // Set mouse press state
00195             m_inMousePress=FALSE;
00196 
00197 
00198 
00199             // And add the group to the scene as well
00200             m_scene->addNode(group);
00201 
00202             // And add the lamp
00203             m_scene->addNode(m_lamp);
00204 
00205                         
00206             // Now we want to look at the scene. Grab the default perspective camera from the window and set the scen as the
00207             // active one.
00208             getCamera()->setScene(m_scene);
00209             getCamera()->setNearClipPlane(1);
00210             getCamera()->setFarClipPlane(2000);
00211 
00212             // Lets add some movement to the scene
00213 
00214             m_input=new gzSimpleMouseViewControl(getCamera());
00215 
00216             addInputInterface(m_input);
00217 
00218             // Hmm. trivial
00219             setBackground(0.0f,0.0f,1.0f,1.0f);
00220 
00221             // and show us ...
00222             show();
00223 
00224             // hm. wonder why I put this one here ?
00225             angle=2.22f;
00226     };
00227 
00228 
00229     /*
00230         The following code snippet is a virtual function to catch the window messages
00231         for pressed, repeated and released keys
00232     */
00233 
00234     gzBool onKey(gzKeyValue key , gzKeyState keystate , gzLong mouse_x , gzLong mouse_y)
00235     {
00236         switch(key)
00237         {
00238             case ' ':   // Space pressed // default to non shader view
00239 
00240                 if(keystate == GZ_KEY_STATE_PRESSED)
00241                 {
00242                     gzState *state=new gzState;
00243 
00244                     state->setOverride(GZ_STATE_GFX_PROGRAM,GZ_STATE_ON);
00245                     state->disableAllNonEnabledModes();
00246 
00247                     gzState::setGlobalState(getContext(),state);
00248                 }
00249                 if(keystate == GZ_KEY_STATE_RELEASED)
00250                 {
00251                     gzState::setGlobalState(getContext(),m_state);
00252                 }
00253                 break;
00254 
00255             case GZ_KEY_LBUTTON :
00256 
00257                 if((keystate == GZ_KEY_STATE_PRESSED) && !m_inMousePress)
00258                 {
00259                     setCaptureMouse(TRUE);
00260                     setHideMouse(TRUE);
00261                     m_inMousePress=TRUE;
00262                 }
00263                 else if((keystate == GZ_KEY_STATE_RELEASED) && m_inMousePress)
00264                 {
00265                     setHideMouse(FALSE);
00266                     setCaptureMouse(FALSE);
00267                     m_inMousePress=FALSE;
00268                 }
00269                 break;
00270 
00271         }
00272 
00273         return gzWindow::onKey((gzKeyValue)key,(gzKeyState)keystate,mouse_x,mouse_y);
00274     }
00275 
00276 
00277     gzVoid onIdle()
00278     {
00279         // Hmm. Now i remember. The spinning lamp..
00280 
00281         angle+=0.003f;
00282 
00283         if(angle>2*GZ_PI)
00284             angle-=(gzReal)(2*GZ_PI);
00285 
00286         m_spin->setPosition((gzReal)(50*cos(5*angle)),5.0f,(gzReal)(50*sin(angle/2)+20));
00287 
00288         m_lamp->setTranslation((gzReal)(50*cos(5*angle)),5.0f,(gzReal)(50*sin(angle/2)+20));
00289 
00290     }
00291 
00292     virtual ~MyWindow()
00293     {
00294         if(m_input)
00295             delete m_input;
00296 
00297     }
00298 
00299     gzRefPointer<gzState>       m_state;
00300 
00301     gzLight *                   m_spin;
00302 
00303     gzReal                      angle;
00304 
00305     gzTransform *               m_lamp;
00306     
00307     gzBool                      m_inMousePress;
00308 
00309     gzNode                      *model;
00310 
00311     gzScene                     *m_scene;
00312 
00313     gzSimpleMouseViewControl    *m_input;
00314 
00315 };
00316 
00317 // Definition of a sample application
00318 // The application provides an initialisation and an onIdle loop manager
00319 // to do the refresh of the window
00320 
00321 class WindowApp : public gzApplication
00322 {
00323 public:
00324         
00325     WindowApp():m_win(NULL),m_format(NULL)
00326     {
00327     }
00328 
00329     ~WindowApp()
00330     {
00331         if(m_win)
00332         {
00333             delete m_win;
00334         }
00335 
00336     }
00337 
00338     void Create(gzString filename)
00339     {
00340         m_format = new gzGraphicsFormat;
00341 
00342         m_format->useStencil(TRUE);
00343 
00344         m_win = new MyWindow(m_format,filename);
00345 
00346         gzPerspCamera *camera=gzDynamic_Cast<gzPerspCamera>(m_win->getCamera());
00347 
00348         camera->setPosition(30,30,150);
00349         camera->lookAt(0,0,0);
00350         camera->useInfiniteFarPlane(TRUE);
00351 
00352     }
00353 
00354     void onIdle()
00355     {
00356         if(m_win)
00357         {
00358             m_win->onIdle();
00359 
00360             m_win->triggerKeyEvaluation();
00361 
00362             if(!m_win->refreshWindow()) // Yield some time if no rendering
00363                 gzSleep(30);
00364 
00365         }
00366 
00367     }
00368 
00369 private:
00370 
00371     friend class MyWindow;
00372     
00373     MyWindow                *m_win;
00374     gzGraphicsFormat        *m_format;
00375 };
00376 
00377 
00378 int main(int argc, char *argv[])
00379 {
00380 
00381     gzStartUpGizmo();   // Needed for some systems to install external graphics engines
00382 
00383     gzMessage::setMessageLevel(GZ_MESSAGE_MEM_DEBUG);
00384 
00385     gzCheckLibraryVersion();
00386 
00387 
00388     try
00389     { 
00390         gzString    filename="script.3ds";
00391 
00392         gzLicense::notifyDefaultLicense();
00393     
00394         gzGraphicsEngine::useEngine(GZ_ENGINE_OPENGL);
00395     
00396         gzInitializeDbManagers();
00397 
00398         if(argc > 1)
00399         {
00400             argv++;
00401             argc--;
00402             while(argc > 0)
00403             {
00404                 if(argv[0][0] == '-')
00405                 {
00406                     switch(toupper(argv[0][1]))
00407                     {
00408 
00409                         case 'M' :
00410                             filename=gzString(*(++argv));
00411                             argc--;
00412                             break;
00413 
00414                     }
00415 
00416                 }
00417                 argv++;
00418                 argc--;
00419             }
00420         }
00421 
00422         // Make the application
00423         WindowApp app;
00424           
00425         // Create the scene and the window
00426         app.Create( filename);
00427         
00428         // run the application
00429         app.run();
00430 
00431     }
00432     catch(gzBaseError &error)       // In case of exceptions thrown we want to print the message
00433     {
00434         error.reportError();
00435     }
00436 
00437     gzShutDownGizmo();
00438 
00439     return 0;
00440 }
00441 
00442 
00443 
00444 

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