DisplayListObject.cpp

Demonstration of a 3D render object that uses display lists to cache its result. Note that with the next big release of OpenGL display lists won't be supported anymore. Thus, it might be good to use vertexbuffers instead.

Start e.g.:

../../../bin/vish DisplayListObject.vis

See also:
Required tutorial: - Simple3DObject.cpp

Further tutorial: - VertexBufferObject.cpp - SimpleCoords.cpp - VectorFieldLines.cpp

00001 
00002 #include <stdio.h>
00003 
00004 #include <ocean/GLvish/VRenderObject.hpp>
00005 #include <ocean/plankton/VCreator.hpp>
00006 #include <ocean/plankton/VTime.hpp>
00007 #include <ocean/GLvish/BoundingBox.hpp>
00008 
00009 #include <stdio.h>
00010 
00011 using namespace Wizt;
00012 
00013 
00033 /*
00034  Render object.
00035  */
00036 class   DisplayListObject : public VRenderObject
00037 {
00038         struct  MyState : State
00039         {
00040                 double  complexity;
00041 
00042                 MyState()
00043                 : complexity(0.5)
00044                 {}
00045         };
00046 
00047         override RefPtr<State> newState() const
00048         {
00049                 return new MyState();
00050         }
00051 
00052         virtual void render(VRenderContext&Context) const;
00053 
00054 public:
00055         TypedSlot<double>       Complexity;
00056 
00057         DisplayListObject(const string&name, int p, const RefPtr<VCreationPreferences>&VP)
00058         : VRenderObject(name, p, VP)
00059         , Complexity(this, "complexity", 0.5)
00060         {}
00061 
00062         ~DisplayListObject()
00063         {}
00064 };
00065 
00066 void DisplayListObject::render(VRenderContext&Context) const
00067 {
00068 static  GLfloat Red[4]   = {0.9, 0.1, 0.1, 1.0 }; 
00069 
00070 int     N = 300;
00071 double  r = 1.0;
00072 
00073 double  complexity = 0.5; 
00074         Complexity << Context >> complexity; 
00075 
00076 double  dphi = 3.1415/ 10,
00077         zs   = 0.1 * complexity;
00078 
00079 
00080         glEnable( GL_COLOR_MATERIAL );
00081         glColorMaterial( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE ); 
00082 
00083         // mark the beginning of an object that can be picked by the mouse
00084         SubObjectBegin(Context); 
00085         
00086         // define the highlight color of an picked object
00087         vglColor4fv(Context, Red, 0.4); 
00088 
00089         glEnable(GL_DEPTH_TEST); 
00090 
00091 // create and save state of this object (created only when called the first time) 
00092 RefPtr<MyState>  state = myState(Context);
00093 
00094 // create and save ParameterSpace (created only when called the first time) 
00095 RefPtr<ValueSet> RenderParameterSpace = new ValueSet(); 
00096 
00097 // Test if a displaylist of given state of this object and parameterspace is already there. 
00098 RefPtr<DisplayList> DL; 
00099         try
00100         {
00101                 DL = Context(*state)(this)( RenderParameterSpace ); 
00102         } 
00103         catch(...)
00104         {}
00105 
00106         // If it is and if local parameter complexity has not changed from complexity saved in the state, just draw existing display list 
00107         if (DL && state->complexity == complexity)
00108         {
00109                 if (DL->call() )
00110                         return; 
00111         } 
00112 
00113         // create displaylist for contex
00114         DL = Context[*state][this][ RenderParameterSpace ];
00115         if (!DL) throw "No Display List!?"; 
00116 
00117         // save local parameter complexity to complexity in the state
00118         state->complexity = complexity; 
00119 
00120         // reset BoundingBox
00121         resetBBox(Context); 
00122 
00123         // start displaylist, draw obeject
00124         glCompile( *DL )
00125         {
00126                 glBegin( GL_QUAD_STRIP ); 
00127                 for (int i=0;i<N;i++) 
00128                 {
00129                 double  phi0 = i*dphi,
00130                         phi1 = (i+1/complexity)*dphi; 
00131 
00132                 point   A, B; 
00133                         A = r*cos(phi0), r*sin(phi0), phi0*zs; 
00134                         B = r*cos(phi0), r*sin(phi0), phi1*zs; 
00135 
00136                 bivector N; 
00137                         N = cos(phi0), sin(phi0), 0; 
00138 
00139                         // Enlarge BouningBox on demand
00140                         embrace( Context, A ); 
00141                         embrace( Context, B ); 
00142 
00143                         glNormal( N ); 
00144                         glVertex( A ); 
00145                         glVertex( B );
00146                 } 
00147                 glEnd();
00148         } 
00149 
00150         // BoundingBox has to be closed after calls of embrace(), new BoundingSphere is calculated
00151         closeBBox(Context);
00152 }
00153 
00154 static Ref<VCreator<DisplayListObject> > MyCreator("Tutorial/DisplayListObject", 0);
00155 
00156 /*
00157  This line needs to be contained in ONE source file of each
00158  VISH plugin module. It's not important which one.
00159  */
00160 VISH_DEFAULT_INIT
00161 
00162 

Generated on Thu Apr 2 18:58:47 2009 for VISHTutorial by  doxygen 1.4.7