OSGRenderAction.h

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002  *                                OpenSG                                     *
00003  *                                                                           *
00004  *                                                                           *
00005  *             Copyright (C) 2000-2002 by the OpenSG Forum                   *
00006  *                                                                           *
00007  *                            www.opensg.org                                 *
00008  *                                                                           *
00009  *   contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de          *
00010  *                                                                           *
00011 \*---------------------------------------------------------------------------*/
00012 /*---------------------------------------------------------------------------*\
00013  *                                License                                    *
00014  *                                                                           *
00015  * This library is free software; you can redistribute it and/or modify it   *
00016  * under the terms of the GNU Library General Public License as published    *
00017  * by the Free Software Foundation, version 2.                               *
00018  *                                                                           *
00019  * This library is distributed in the hope that it will be useful, but       *
00020  * WITHOUT ANY WARRANTY; without even the implied warranty of                *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
00022  * Library General Public License for more details.                          *
00023  *                                                                           *
00024  * You should have received a copy of the GNU Library General Public         *
00025  * License along with this library; if not, write to the Free Software       *
00026  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 *
00027  *                                                                           *
00028 \*---------------------------------------------------------------------------*/
00029 /*---------------------------------------------------------------------------*\
00030  *                                Changes                                    *
00031  *                                                                           *
00032  *                                                                           *
00033  *                                                                           *
00034  *                                                                           *
00035  *                                                                           *
00036  *                                                                           *
00037 \*---------------------------------------------------------------------------*/
00038 
00039 #ifndef _OSGRENDERACTION_H_
00040 #define _OSGRENDERACTION_H_
00041 #ifdef __sgi
00042 #pragma once
00043 #endif
00044 
00045 //---------------------------------------------------------------------------
00046 //  Includes
00047 //---------------------------------------------------------------------------
00048 
00049 #include <vector>
00050 #include <stack>
00051 #include <map>
00052 #include <set>
00053 
00054 #include "OSGSystemDef.h"
00055 #include "OSGBaseTypes.h"
00056 #include "OSGDrawActionBase.h"
00057 #include "OSGMatrix.h"
00058 #include "OSGMaterial.h"
00059 #include "OSGStatElemTypes.h"
00060 
00061 OSG_BEGIN_NAMESPACE
00062 
00063 //---------------------------------------------------------------------------
00064 //  Forward References
00065 //---------------------------------------------------------------------------
00066 
00067 class Material;
00068 class DrawTreeNode;
00069 class MaterialDrawable;
00070 class State;
00071 class Light;
00072 class LightEnv;
00073 class LightChunk;
00074 class ClipPlane;
00075 class SClipPlaneChunk;
00076 
00077 class DrawTreeNodeFactory;
00078 
00079 //---------------------------------------------------------------------------
00080 //   Types
00081 //---------------------------------------------------------------------------
00082 
00083 //---------------------------------------------------------------------------
00084 //  Class
00085 //---------------------------------------------------------------------------
00086 
00090 class OSG_SYSTEM_DLLMAPPING RenderAction : public DrawActionBase
00091 {
00092   public:
00093 
00094     typedef struct
00095     {
00096         UInt32  first;
00097         Matrixr second;
00098         Matrixr acc;
00099     }
00100     MatrixStore;
00101 
00102     typedef std::map <Material   *,      DrawTreeNode *> MaterialMap;
00103     typedef std::pair<LightChunk *,      Matrixr       > LightStore;
00104     typedef std::pair<SClipPlaneChunk *, Matrix        > ClipPlaneStore;
00105 
00106     //-----------------------------------------------------------------------
00107     //   constants
00108     //-----------------------------------------------------------------------
00109 
00110     static StatElemDesc<StatTimeElem   > statDrawTime;
00111     static StatElemDesc<StatIntElem    > statNMaterials;
00112     static StatElemDesc<StatIntElem    > statNMatrices;
00113     static StatElemDesc<StatIntElem    > statNGeometries;
00114     static StatElemDesc<StatIntElem    > statNTransGeometries;
00115     static StatElemDesc<StatStringElem > statNOcclusionMode;
00116     static StatElemDesc<StatIntElem    > statNOcclusionTests;
00117     static StatElemDesc<StatIntElem    > statNOcclusionCulled;
00118 
00119     static const Int32 OcclusionStopAndWait;
00120     static const Int32 OcclusionMultiFrame;
00121     static const Int32 OcclusionHierarchicalMultiFrame;
00122 
00123     //-----------------------------------------------------------------------
00124     //   enums
00125     //-----------------------------------------------------------------------
00126 
00127     //-----------------------------------------------------------------------
00128     //   types
00129     //-----------------------------------------------------------------------
00130 
00131     //-----------------------------------------------------------------------
00132     //   class functions
00133     //-----------------------------------------------------------------------
00134 
00135     static RenderAction *create      (void                    );
00136 
00137     static void          setPrototype(RenderAction *pPrototype);
00138     static RenderAction *getPrototype(void                    );
00139 
00140 
00141     static void registerEnterDefault (const FieldContainerType &type,
00142                                       const Action::Functor    &func);
00143 
00144     static void registerLeaveDefault (const FieldContainerType &type,
00145                                       const Action::Functor    &func);
00146 
00147     //-----------------------------------------------------------------------
00148     //   instance functions
00149     //-----------------------------------------------------------------------
00150 
00151     virtual ~RenderAction(void);
00152 
00153     /*------------------------- your_category -------------------------------*/
00154 
00155     virtual Action::ResultE start(void       );
00156     virtual Action::ResultE stop (ResultE res);
00157 
00158     /*------------------------- your_operators ------------------------------*/
00159 
00160            void           push_matrix(const Matrixr &matrix);
00161            void           pop_matrix (      void           );
00162 
00163     inline const Matrixr &top_matrix (      void           );
00164 
00165     /*------------------------- assignment ----------------------------------*/
00166 
00167     void dropGeometry(MaterialDrawable      *pGeo);
00168     void dropFunctor (Material::DrawFunctor &func, Material *mat);
00169 
00170     void dropLight     (Light     *pLight);
00171     void undropLight   (Light     *pLight);
00172 
00173     void dropLightEnv  (LightEnv  *pLightEnv);
00174     void undropLightEnv(LightEnv  *pLightEnv);
00175 
00176     void dropClipPlane  (ClipPlane     *pClipPlane);
00177     void undropClipPlane(ClipPlane     *pClipPlane);
00178 
00179 
00180     void setStateSorting(bool s);
00181     bool getStateSorting(void);
00182 
00183     std::vector<Light *> getActiveLights(void);
00184     UInt32 getActiveLightsMask(void);
00185     UInt32 getActiveLightsCount(void);
00186 
00187     const std::vector<UInt32> &getLightEnvsLightsState(void);
00188 
00189     inline State *getCurrentState(void);
00190 
00191     /*------------------------- comparison ----------------------------------*/
00192 
00193     void setSortTrans(bool bVal);
00194     bool getSortTrans(void) const;
00195 
00196     void setZWriteTrans(bool bVal);
00197     bool getZWriteTrans(void) const;
00198 
00199     void setLocalLights(bool bVal);
00200     bool getLocalLights(void) const;
00201 
00202     void setCorrectTwoSidedLighting(bool bVal);
00203     bool getCorrectTwoSidedLighting(void) const;
00204 
00205     void setOcclusionCulling(bool bVal);
00206     bool getOcclusionCulling(void) const;
00207 
00208     void setOcclusionCullingMode(Int32 mode);
00209     Int32 getOcclusionCullingMode(void) const;
00210     void setOcclusionCullingPixels(UInt32 pixels);
00211     UInt32 getOcclusionCullingPixels(void) const;
00212     void setOcclusionCullingThreshold(UInt32 threshold);
00213     UInt32 getOcclusionCullingThreshold(void) const;
00214 
00215     void setSmallFeatureCulling(bool bVal);
00216     bool getSmallFeatureCulling(void) const;
00217     void setSmallFeaturePixels(Real32 pixels);
00218     Real32 getSmallFeaturePixels(void) const;
00219     void setSmallFeatureThreshold(UInt32 threshold);
00220     UInt32 getSmallFeatureThreshold(void) const;
00221     void setUseGLFinish(bool s);
00222     bool getUseGLFinish(void) const;
00223 
00224     /*------------------------- comparison ----------------------------------*/
00225 
00226     bool isSmallFeature(const NodePtr &node);
00227     bool isOccluded(DrawTreeNode *pRoot);
00228     void deleteOcclusionQueriesPool(void);
00229     GLuint getOcclusionQuery(void);
00230     GLuint getOcclusionQuery(NodePtr node);
00231     void setOcclusionQuery(NodePtr node, GLuint occlusionQuery);
00232     void resetOcclusionQueryIndex(void);
00233     void setOcclusionMask(NodePtr node, UInt8 mask);
00234     bool hasGeometryChild(NodePtr node);
00235 
00236     void drawOcclusionBB(const Pnt3r &bbmin, const Pnt3r &bbmax);
00237     void drawMultiFrameOcclusionBB(DrawTreeNode *pRoot);
00238     void drawHierarchicalMultiFrameOcclusionBB(const Matrixr &view, NodePtr node);
00239     // test a single node
00240     bool            isVisible( Node* node );
00241 
00242     // visibility levels
00243     bool  pushVisibility(void);
00244     void  popVisibility(void);
00245 
00246     void (OSG_APIENTRY* _glGenQueriesARB)(GLsizei, GLuint*);
00247     void (OSG_APIENTRY* _glDeleteQueriesARB)(GLsizei, GLuint*);
00248     void (OSG_APIENTRY* _glBeginQueryARB)(GLenum, GLuint);
00249     void (OSG_APIENTRY* _glEndQueryARB)(GLenum);
00250     void (OSG_APIENTRY* _glGetQueryObjectuivARB)(GLuint, GLenum, GLuint*);
00251 
00252   protected:
00253 
00254     //-----------------------------------------------------------------------
00255     //   enums
00256     //-----------------------------------------------------------------------
00257 
00258     //-----------------------------------------------------------------------
00259     //   types
00260     //-----------------------------------------------------------------------
00261 
00262     typedef DrawActionBase Inherited;
00263 
00264     //-----------------------------------------------------------------------
00265     //   class variables
00266     //-----------------------------------------------------------------------
00267 
00268     static RenderAction    *_pPrototype;
00269 
00270     static std::vector<Functor> *_vDefaultEnterFunctors;
00271     static std::vector<Functor> *_vDefaultLeaveFunctors;
00272 
00273     //-----------------------------------------------------------------------
00274     //   class functions
00275     //-----------------------------------------------------------------------
00276 
00277     static bool terminateEnter(void);
00278     static bool terminateLeave(void);
00279 
00280     //-----------------------------------------------------------------------
00281     //   instance variables
00282     //-----------------------------------------------------------------------
00283 
00284     DrawTreeNodeFactory      *_pNodeFactory;
00285 
00286     UInt32                    _uiMatrixId;
00287 
00288     MatrixStore               _currMatrix;
00289     Matrixr                   _camInverse;
00290 
00291     std::vector<MatrixStore>  _vMatrixStack;
00292 
00293     MaterialMap               _mMatMap;
00294 
00295     //DrawTreeNode             *_pRoot;
00296     typedef std::map<Int32, DrawTreeNode *> SortKeyMap;
00297     SortKeyMap                  _pMatRoots;
00298 
00299     typedef std::map<Real,  DrawTreeNode *> TransSortMap;
00300     typedef std::map<Int32, TransSortMap  > TransSortKeyMap;
00301     TransSortKeyMap             _pTransMatRoots;
00302 
00303     DrawTreeNode               *_pNoStateSortRoot;
00304     DrawTreeNode               *_pNoStateSortTransRoot;
00305 
00306     typedef std::map<Real, DrawTreeNode  *> OCMap;
00307     OCMap                       _ocRoot;
00308 
00309     UInt32                    _uiActiveMatrix;
00310     State                    *_pActiveState;
00311 
00312     UInt32                    _uiNumMaterialChanges;
00313     UInt32                    _uiNumMatrixChanges;
00314     UInt32                    _uiNumGeometries;
00315     UInt32                    _uiNumTransGeometries;
00316     UInt32                    _uiNumOcclusionTests;
00317     UInt32                    _uiNumOcclusionCulled;
00318 
00319     bool                      _bSortTrans;
00320     bool                      _bZWriteTrans;
00321     bool                      _bLocalLights;
00322     bool                      _bCorrectTwoSidedLighting;
00323     bool                      _bOcclusionCulling;
00324     Int32                     _occlusionCullingMode;
00325     UInt32                    _occlusionCullingPixels;
00326     UInt32                    _occlusionCullingThreshold;
00327     UInt32                    _currentOcclusionQueryIndex;
00328     std::vector<NodePtr>      _occluded_nodes;
00329     std::set<UInt32>          _hier_occlusions;
00330     UInt32                    _occ_bb_dl;
00331 
00332     bool                      _bSmallFeatureCulling;
00333     Real32                    _smallFeaturesPixels;
00334     UInt32                    _smallFeaturesThreshold;
00335     Matrixr                   _worldToScreenMatrix;
00336     bool                      _useGLFinish;
00337 
00338     std::vector<LightStore>   _vLights;
00339     std::vector<Light *>      _lightsMap;
00340     UInt32                    _lightsState;
00341     UInt32                    _activeLightsState;
00342     UInt32                    _activeLightsCount;
00343     UInt32                    _activeLightsMask;
00344 
00345     std::vector<std::vector<UInt32> > _lightsTable;
00346     std::vector<UInt32>               _lightsPath;
00347     std::vector<UInt32>               _lightEnvsLightsState;
00348 
00349     std::vector<ClipPlaneStore> _vClipPlanes;
00350     std::vector<ClipPlane *>    _clipPlanesMap;
00351     UInt32                      _clipPlanesState;
00352     UInt32                      _activeClipPlanesState;
00353     UInt32                      _activeClipPlanesCount;
00354     UInt32                      _activeClipPlanesMask;
00355 
00356     std::vector<std::vector<UInt32> > _clipPlanesTable;
00357     std::vector<UInt32>               _clipPlanesPath;
00358 
00359     bool                      _stateSorting;
00360 
00361     std::vector<FrustumVolume::PlaneSet>  _visibilityStack;
00362 
00363     GLuint _occlusionQuery;
00364     std::vector<GLuint> _occlusionQueriesPool;
00365     std::map<UInt32, GLuint> _occlusionQueries;
00366 
00367 
00368     Int32 _cgChunkId;
00369     Int32 _cgfxChunkId;
00370     Int32 _shlChunkId;
00371 
00372     static UInt32 _arbOcclusionQuery;
00373     static UInt32 _funcGenQueriesARB;
00374     static UInt32 _funcDeleteQueriesARB;
00375     static UInt32 _funcBeginQueryARB;
00376     static UInt32 _funcEndQueryARB;
00377     static UInt32 _funcGetQueryObjectuivARB;
00378 
00379 //    Time                 _tMatSlot
00380 
00381     //-----------------------------------------------------------------------
00382     //   instance functions
00383     //-----------------------------------------------------------------------
00384 
00385     // prohibit default functions (move to 'public' if you need one)
00386 
00387     RenderAction(void);
00388     RenderAction(const RenderAction &source);
00389 
00390     void operator =(const RenderAction &source);
00391 
00392     // access default functors
00393 
00394     virtual std::vector<Functor> *getDefaultEnterFunctors(void);
00395     virtual std::vector<Functor> *getDefaultLeaveFunctors(void);
00396 
00397             void dump(DrawTreeNode *pRoot, UInt32 uiIndent);
00398             void updateShader(State *state);
00399    virtual void draw(DrawTreeNode *pRoot);
00400 
00401     inline  void updateTopMatrix(void);
00402             void activateLocalLights(DrawTreeNode *pRoot);
00403             void activateLocalClipPlanes(DrawTreeNode *pRoot);
00404 
00405     void getMaterialStates(Material *mat, std::vector<State *> &states);
00406 
00407   private:
00408 
00409     //-----------------------------------------------------------------------
00410     //   enums
00411     //-----------------------------------------------------------------------
00412 
00413     //-----------------------------------------------------------------------
00414     //   types
00415     //-----------------------------------------------------------------------
00416 
00417     //-----------------------------------------------------------------------
00418     //   friend classes
00419     //-----------------------------------------------------------------------
00420 
00421     friend class ShadowViewport;
00422 
00423     //-----------------------------------------------------------------------
00424     //   friend functions
00425     //-----------------------------------------------------------------------
00426 
00427     //-----------------------------------------------------------------------
00428     //   class variables
00429     //-----------------------------------------------------------------------
00430 
00431     //-----------------------------------------------------------------------
00432     //   class functions
00433     //-----------------------------------------------------------------------
00434 
00435     //-----------------------------------------------------------------------
00436     //   instance variables
00437     //-----------------------------------------------------------------------
00438 
00439     //-----------------------------------------------------------------------
00440     //   instance functions
00441     //-----------------------------------------------------------------------
00442 };
00443 
00444 //---------------------------------------------------------------------------
00445 //   Exported Types
00446 //---------------------------------------------------------------------------
00447 
00448 OSG_END_NAMESPACE
00449 
00450 #include "OSGRenderAction.inl"
00451 
00452 #endif /* _OSGRENDERACTION_H_ */
00453 
00454