1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield 2 * 3 * This library is open source and may be redistributed and/or modified under 4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or 5 * (at your option) any later version. The full license is in LICENSE file 6 * included with this distribution, and on the openscenegraph.org website. 7 * 8 * This library is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * OpenSceneGraph Public License for more details. 12*/ 13 14#ifndef OSGUTIL_RENDERBIN 15#define OSGUTIL_RENDERBIN 1 16 17#include <osgUtil/StateGraph> 18 19#include <map> 20#include <vector> 21#include <string> 22 23namespace osgUtil { 24 25class RenderStage; 26class Statistics; 27/** 28 * RenderBin base class. Renderbin contains geometries to be rendered as a group, 29 * renderbins are rendered once each. They can improve efficiency or 30 * use different rendering algorithms. 31 * A renderBin can contain further renderBins producing a tree hierarchy of renderBins. 32 */ 33class OSGUTIL_EXPORT RenderBin : public osg::Object 34{ 35 public: 36 37 typedef std::vector<RenderLeaf*> RenderLeafList; 38 typedef std::vector<StateGraph*> StateGraphList; 39 typedef std::map< int, osg::ref_ptr<RenderBin> > RenderBinList; 40 41 enum SortMode 42 { 43 SORT_BY_STATE, 44 SORT_BY_STATE_THEN_FRONT_TO_BACK, 45 SORT_FRONT_TO_BACK, 46 SORT_BACK_TO_FRONT, 47 TRAVERSAL_ORDER 48 }; 49 50 // static methods. 51 static RenderBin* createRenderBin(const std::string& binName); 52 static RenderBin* getRenderBinPrototype(const std::string& binName); 53 static void addRenderBinPrototype(const std::string& binName,RenderBin* proto); 54 static void removeRenderBinPrototype(RenderBin* proto); 55 56 static void setDefaultRenderBinSortMode(SortMode mode); 57 static SortMode getDefaultRenderBinSortMode(); 58 59 60 61 RenderBin(); 62 63 RenderBin(SortMode mode); 64 65 /** Copy constructor using CopyOp to manage deep vs shallow copy.*/ 66 RenderBin(const RenderBin& rhs,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY); 67 68 virtual osg::Object* cloneType() const { return new RenderBin(); } 69 virtual osg::Object* clone(const osg::CopyOp& copyop) const { return new RenderBin(*this,copyop); } // note only implements a clone of type. 70 virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const RenderBin*>(obj)!=0L; } 71 virtual const char* libraryName() const { return "osgUtil"; } 72 virtual const char* className() const { return "RenderBin"; } 73 74 virtual void reset(); 75 76 void setStateSet(osg::StateSet* stateset) { _stateset = stateset; } 77 osg::StateSet* getStateSet() { return _stateset.get(); } 78 const osg::StateSet* getStateSet() const { return _stateset.get(); } 79 80 81 RenderBin* getParent() { return _parent; } 82 const RenderBin* getParent() const { return _parent; } 83 84 RenderStage* getStage() { return _stage; } 85 const RenderStage* getStage() const { return _stage; } 86 87 int getBinNum() const { return _binNum; } 88 89 StateGraphList& getStateGraphList() { return _stateGraphList; } 90 const StateGraphList& getStateGraphList() const { return _stateGraphList; } 91 92 RenderBinList& getRenderBinList() { return _bins; } 93 const RenderBinList& getRenderBinList() const { return _bins; } 94 95 RenderLeafList& getRenderLeafList() { return _renderLeafList; } 96 const RenderLeafList& getRenderLeafList() const { return _renderLeafList; } 97 98 99 RenderBin* find_or_insert(int binNum,const std::string& binName); 100 101 void addStateGraph(StateGraph* rg) 102 { 103 _stateGraphList.push_back(rg); 104 } 105 106 virtual void sort(); 107 108 virtual void sortImplementation(); 109 110 void setSortMode(SortMode mode); 111 SortMode getSortMode() const { return _sortMode; } 112 113 virtual void sortByState(); 114 virtual void sortByStateThenFrontToBack(); 115 virtual void sortFrontToBack(); 116 virtual void sortBackToFront(); 117 virtual void sortTraversalOrder(); 118 119 struct SortCallback : public osg::Referenced 120 { 121 virtual void sortImplementation(RenderBin*) = 0; 122 }; 123 124 void setSortCallback(SortCallback* sortCallback) { _sortCallback = sortCallback; } 125 SortCallback* getSortCallback() { return _sortCallback.get(); } 126 const SortCallback* getSortCallback() const { return _sortCallback.get(); } 127 128 129 130 virtual void draw(osg::RenderInfo& renderInfo,RenderLeaf*& previous); 131 132 virtual void drawImplementation(osg::RenderInfo& renderInfo,RenderLeaf*& previous); 133 134 struct DrawCallback : public osg::Referenced 135 { 136 virtual void drawImplementation(RenderBin* bin,osg::RenderInfo& renderInfo,RenderLeaf*& previous) = 0; 137 }; 138 139 void setDrawCallback(DrawCallback* drawCallback) { _drawCallback = drawCallback; } 140 DrawCallback* getDrawCallback() { return _drawCallback.get(); } 141 const DrawCallback* getDrawCallback() const { return _drawCallback.get(); } 142 143 /** Extract stats for current draw list. */ 144 bool getStats(Statistics& primStats) const; 145 146 /** Compute the number of dynamic RenderLeaves.*/ 147 virtual unsigned int computeNumberOfDynamicRenderLeaves() const; 148 149 void copyLeavesFromStateGraphListToRenderLeafList(); 150 151 /** If State is non-zero, this function releases any associated OpenGL objects for 152 * the specified graphics context. Otherwise, releases OpenGL objexts 153 * for all graphics contexts. */ 154 virtual void releaseGLObjects(osg::State* state= 0) const; 155 156 protected: 157 158 virtual ~RenderBin(); 159 160 int _binNum; 161 RenderBin* _parent; 162 RenderStage* _stage; 163 RenderBinList _bins; 164 StateGraphList _stateGraphList; 165 RenderLeafList _renderLeafList; 166 167 bool _sorted; 168 SortMode _sortMode; 169 osg::ref_ptr<SortCallback> _sortCallback; 170 171 osg::ref_ptr<DrawCallback> _drawCallback; 172 173 osg::ref_ptr<osg::StateSet> _stateset; 174 175}; 176 177} 178 179#endif 180 181 182