Gizmo3D

node.cpp

Node example application. This example illustrates how to create a custom node.
The source code can also be found in the examples directory of the Gizmo3D distribution.

00001 // *****************************************************************************
00002 // File         : node.cpp
00003 // Module       : 
00004 // Description  : Test app for custom nodes
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 class MyNodeClass : public gzNode
00026 {
00027 public:
00028 
00029     GZ_DECLARE_TYPE_INTERFACE;      // provide RTTI typed interface
00030 
00031     MyNodeClass(const gzString & name="MyNodeClass"):gzNode(name)
00032     {
00033 
00034     }
00035 
00036     virtual ~MyNodeClass()
00037     {
00038     }
00039 
00040     // ---------- Clone interface ---------------------------------------
00041 
00042     virtual gzReference* clone() const  // Provide a clone method for factories
00043     {
00044         return new MyNodeClass(*this);
00045     }
00046 
00047     // ---------- Action Interface --------------------------------------
00048 
00049     virtual gzVoid preTraverseAction( gzTraverseAction *actionclass , gzContext *context)
00050     {
00051         if(actionclass->isExactType(gzRenderAction::getClassType()))    // Exact a graphic action
00052         {
00053             gzBegin(GZ_QUADS);
00054             gzColor4d(1,1,1,1);
00055             gzVertex3d(0,0,0);
00056             gzColor4d(1,0,1,1);
00057             gzVertex3d(0,1,0);
00058             gzColor4d(1,1,0,1);
00059             gzVertex3d(1,1,0);
00060             gzColor4d(0,1,1,1);
00061             gzVertex3d(1,0,0);
00062             gzEnd();
00063         }
00064     }
00065 
00066     virtual gzVoid postTraverseAction( gzTraverseAction *actionclass , gzContext *context)
00067     {
00068         // Here we could add some cleanup if we were a group etc.
00069     }
00070 
00071     virtual gzActionStage useActionStage( gzTraverseAction *actionclass , gzContext *context) 
00072     { 
00073         return GZ_ACTION_STAGE_SORTED_TEXTURE; 
00074     }
00075 
00076     // Gets called on node data updates
00077     virtual gzVoid updateNode()         // Note new code since Gizmo3D 1.1 Beta 24
00078     {
00079         gzVec3 center(0.5,0.5,0);       // Here is your own code to calculate the boundary sphere
00080 
00081         gzReal radius=2;
00082 
00083         resetBoundary();                // And set it
00084         includeBoundary(center,radius); 
00085 
00086         adjustBoundary();               // We must apply to this rule
00087     }
00088 };
00089 
00090 // Add some type info inheritance
00091 GZ_DECLARE_TYPE_CHILD(gzNode,MyNodeClass,"MyNodeClassName");
00092 
00093 
00094 class MyWindow : public gzWindow
00095 {
00096 public:
00097     MyWindow():gzWindow("MyWindow")
00098     {
00099 
00100         gzScene *scene=new gzScene;
00101 
00102         scene->addNode(new MyNodeClass("A test quad"));
00103 
00104         // Lets print out the scene
00105         scene->debug(GZ_DEBUG_RECURSIVE);
00106                 
00107         gzCamera *cam=getCamera();
00108 
00109         cam->setScene(scene);
00110 
00111         cam->setPosition(0.5,0.5,10);
00112         cam->lookAt(0.5,0.5,0);
00113     }
00114 
00115 
00116 };
00117 
00118 // Definition of a sample application
00119 
00120 class WindowApp : public gzApplication
00121 {
00122 public:
00123         
00124     WindowApp():m_win(NULL)
00125     {
00126     };
00127 
00128     ~WindowApp()
00129     {
00130         if(m_win)
00131             delete m_win;
00132     }
00133 
00134     void Create()
00135     {
00136         m_win = new MyWindow();
00137         m_win->setBackground(0,0.8f,0.5f,0);
00138         m_win->refreshWindow();
00139     }
00140 
00141 
00142 private:
00143     
00144     MyWindow            *m_win;
00145 };
00146 
00147 
00148 
00149 int main(int argc, char *argv[])
00150 {
00151     gzStartUpGizmo();   // Needed for some systems to install external graphics engines
00152     
00153     gzMessage::setMessageLevel(GZ_MESSAGE_DEBUG);
00154 
00155     gzGraphicsEngine::useEngine(GZ_ENGINE_OPENGL);
00156 
00157 
00158     WindowApp app;
00159     try
00160     {
00161         // Create the app + window
00162         app.Create();
00163 
00164         // run the application
00165         app.run();
00166 
00167     }
00168     catch(gzBaseError &error)       // In case of exceptions thrown we want to print the message
00169     {
00170                 error.reportError();
00171     }
00172 
00173     gzShutDownGizmo();
00174 
00175     return 0;
00176 }
00177 

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