root/OpenSceneGraph/trunk/include/osg/Geometry @ 9886

Revision 9886, 19.2 kB (checked in by robert, 6 years ago)

From Roland Smeenk, "While working on the Collada plugin I noticed that all geometry created by the dae reader result in slow path geometry.
Because there already exists the option to convert slow path geometry to the fast path by computing an internal fast path alternative, I added a new optimizer option that automatically does this. To check the results I also made some changes to the statistics gathering and rendering.

Somewhat unrelated, but also part of the optimizer I disabled removal of CameraView? nodes during RemoveRedundantNodes? optimization.
As discussed on the ML, CameraViews? were removed from the scenegraph. This solves that issue.

Summary:
-Geometry::areFastPathsUsed now also looks at internalOptimizedGeometry
-Added Optimize option to make all slow path geometry compute their internal fast path alternative
-Added fast geometry counter to the statistics
-Disabled removel of CameraViews? in optimizer
"

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
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 OSG_GEOMETRY
15#define OSG_GEOMETRY 1
16
17#include <osg/Drawable>
18#include <osg/Vec2>
19#include <osg/Vec3>
20#include <osg/Vec4>
21#include <osg/Array>
22#include <osg/PrimitiveSet>
23
24namespace osg {
25
26
27class OSG_EXPORT Geometry : public Drawable
28{
29    public:
30
31        Geometry();
32
33        /** Copy constructor using CopyOp to manage deep vs shallow copy. */
34        Geometry(const Geometry& geometry,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
35           
36        virtual Object* cloneType() const { return new Geometry(); }
37        virtual Object* clone(const CopyOp& copyop) const { return new Geometry(*this,copyop); }       
38        virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const Geometry*>(obj)!=NULL; }
39        virtual const char* libraryName() const { return "osg"; }
40        virtual const char* className() const { return "Geometry"; }
41
42        virtual Geometry* asGeometry() { return this; }
43        virtual const Geometry* asGeometry() const { return this; }
44
45        bool empty() const;
46
47        enum AttributeBinding
48        {
49            BIND_OFF=0,
50            BIND_OVERALL,
51            BIND_PER_PRIMITIVE_SET,
52            BIND_PER_PRIMITIVE,
53            BIND_PER_VERTEX
54        };
55               
56        struct OSG_EXPORT ArrayData
57        {
58            ArrayData():
59                binding(BIND_OFF),
60                normalize(GL_FALSE) {}
61               
62            ArrayData(const ArrayData& data,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
63
64            ArrayData(Array* a, AttributeBinding b, GLboolean n = GL_FALSE):
65                array(a),
66                indices(0),
67                binding(b),
68                normalize(n) {}
69
70            ArrayData(Array* a, IndexArray* i, AttributeBinding b, GLboolean n = GL_FALSE):
71                array(a),
72                indices(i),
73                binding(b),
74                normalize(n) {}
75
76            ArrayData& operator = (const ArrayData& rhs)
77            {
78                array = rhs.array;
79                indices = rhs.indices;
80                binding = rhs.binding;
81                normalize = rhs.normalize;
82                return *this;
83            }
84           
85            inline bool empty() const { return !array.valid(); }
86
87            ref_ptr<Array>          array;
88            ref_ptr<IndexArray>     indices;
89            AttributeBinding        binding;
90            GLboolean               normalize;
91        };
92       
93        struct OSG_EXPORT Vec3ArrayData
94        {
95            Vec3ArrayData():
96                binding(BIND_OFF),
97                normalize(GL_FALSE) {}
98               
99            Vec3ArrayData(const Vec3ArrayData& data,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
100
101            Vec3ArrayData(Vec3Array* a, AttributeBinding b, GLboolean n = GL_FALSE):
102                array(a),
103                indices(0),
104                binding(b),
105                normalize(n) {}
106
107            Vec3ArrayData(Vec3Array* a, IndexArray* i, AttributeBinding b, GLboolean n = GL_FALSE):
108                array(a),
109                indices(i),
110                binding(b),
111                normalize(n) {}
112
113            Vec3ArrayData& operator = (const Vec3ArrayData& rhs)
114            {
115                array = rhs.array;
116                indices = rhs.indices;
117                binding = rhs.binding;
118                normalize = rhs.normalize;
119                return *this;
120            }
121
122            inline bool empty() const { return !array.valid(); }
123
124            ref_ptr<Vec3Array>          array;
125            ref_ptr<IndexArray>     indices;
126            AttributeBinding        binding;
127            GLboolean               normalize;
128        };
129
130        /** Static ArrayData which is returned from getTexCoordData(i) const and getVertexAttribData(i) const
131          * when i is out of range.
132        */
133        static const ArrayData s_InvalidArrayData;
134       
135        typedef std::vector< ArrayData >  ArrayDataList;
136               
137
138        void setVertexArray(Array* array);
139        Array* getVertexArray() { return _vertexData.array.get(); }
140        const Array* getVertexArray() const  { return _vertexData.array.get(); }
141
142        void setVertexData(const ArrayData& arrayData);
143        ArrayData& getVertexData() { return _vertexData; }
144        const ArrayData& getVertexData() const { return _vertexData; }
145       
146
147        void setNormalBinding(AttributeBinding ab);
148        AttributeBinding getNormalBinding() const { return _normalData.binding; }
149
150        void setNormalArray(Array* array);
151        Array* getNormalArray() { return _normalData.array.get(); }
152        const Array* getNormalArray() const { return _normalData.array.get(); }
153
154        void setNormalData(const ArrayData& arrayData);
155        ArrayData& getNormalData() { return _normalData; }
156        const ArrayData& getNormalData() const { return _normalData; }
157
158        void setColorBinding(AttributeBinding ab);
159        AttributeBinding getColorBinding() const { return _colorData.binding; }
160
161        void setColorArray(Array* array);
162        Array* getColorArray() { return _colorData.array.get(); }
163        const Array* getColorArray() const { return _colorData.array.get(); }
164
165        void setColorData(const ArrayData& arrayData);
166        ArrayData& getColorData() { return _colorData; }
167        const ArrayData& getColorData() const { return _colorData; }
168
169
170        void setSecondaryColorBinding(AttributeBinding ab);
171        AttributeBinding getSecondaryColorBinding() const { return _secondaryColorData.binding; }
172
173        void setSecondaryColorArray(Array* array);
174        Array* getSecondaryColorArray() { return _secondaryColorData.array.get(); }
175        const Array* getSecondaryColorArray() const { return _secondaryColorData.array.get(); }
176
177        void setSecondaryColorData(const ArrayData& arrayData);
178        ArrayData& getSecondaryColorData() { return _secondaryColorData; }
179        const ArrayData& getSecondaryColorData() const { return _secondaryColorData; }
180
181
182        void setFogCoordBinding(AttributeBinding ab);
183        AttributeBinding getFogCoordBinding() const { return _fogCoordData.binding; }
184
185        void setFogCoordArray(Array* array);
186        Array* getFogCoordArray() { return _fogCoordData.array.get(); }
187        const Array* getFogCoordArray() const { return _fogCoordData.array.get(); }
188
189        void setFogCoordData(const ArrayData& arrayData);
190        ArrayData& getFogCoordData() { return _fogCoordData; }
191        const ArrayData& getFogCoordData() const { return _fogCoordData; }
192       
193
194        void setTexCoordArray(unsigned int unit,Array*);
195        Array* getTexCoordArray(unsigned int unit);
196        const Array* getTexCoordArray(unsigned int unit) const;
197
198        void setTexCoordData(unsigned int index,const ArrayData& arrayData);
199        ArrayData& getTexCoordData(unsigned int index);
200        const ArrayData& getTexCoordData(unsigned int index) const;
201
202        unsigned int getNumTexCoordArrays() const {  return static_cast<unsigned int>(_texCoordList.size()); }
203        ArrayDataList& getTexCoordArrayList() { return _texCoordList; }
204        const ArrayDataList& getTexCoordArrayList() const { return _texCoordList; }
205
206
207
208        void setVertexAttribArray(unsigned int index,Array* array);
209        Array *getVertexAttribArray(unsigned int index);
210        const Array *getVertexAttribArray(unsigned int index) const;
211
212        void setVertexAttribBinding(unsigned int index,AttributeBinding ab);
213        AttributeBinding getVertexAttribBinding(unsigned int index) const;
214
215        void setVertexAttribNormalize(unsigned int index,GLboolean norm);
216        GLboolean getVertexAttribNormalize(unsigned int index) const;
217
218        void setVertexAttribData(unsigned int index,const ArrayData& arrayData);
219        ArrayData& getVertexAttribData(unsigned int index);
220        const ArrayData& getVertexAttribData(unsigned int index) const;
221
222        unsigned int getNumVertexAttribArrays() const { return static_cast<unsigned int>(_vertexAttribList.size()); }
223        ArrayDataList& getVertexAttribArrayList() { return _vertexAttribList; }
224        const ArrayDataList& getVertexAttribArrayList() const { return _vertexAttribList; }
225
226
227
228        typedef std::vector< ref_ptr<PrimitiveSet> > PrimitiveSetList;
229
230        void setPrimitiveSetList(const PrimitiveSetList& primitives) { _primitives = primitives; dirtyDisplayList(); dirtyBound(); }
231        PrimitiveSetList& getPrimitiveSetList() { return _primitives; }
232        const PrimitiveSetList& getPrimitiveSetList() const { return _primitives; }
233
234        unsigned int getNumPrimitiveSets() const { return static_cast<unsigned int>(_primitives.size()); }
235        PrimitiveSet* getPrimitiveSet(unsigned int pos) { return _primitives[pos].get(); }
236        const PrimitiveSet* getPrimitiveSet(unsigned int pos) const { return _primitives[pos].get(); }
237       
238        /** Add a primitive set to the geometry. */
239        bool addPrimitiveSet(PrimitiveSet* primitiveset);
240       
241        /** Set a primitive set to the specified position in geometry's primitive set list. */
242        bool setPrimitiveSet(unsigned int i,PrimitiveSet* primitiveset);
243       
244        /** Insert a primitive set to the specified position in geometry's primitive set list. */
245        bool insertPrimitiveSet(unsigned int i,PrimitiveSet* primitiveset);
246       
247        /** Remove primitive set(s) from the specified position in geometry's primitive set list. */
248        bool removePrimitiveSet(unsigned int i,unsigned int numElementsToRemove=1);
249
250        /** Get the index number of a primitive set, return a value between
251          * 0 and getNumPrimitiveSet()-1 if found, if not found then return getNumPrimitiveSet().
252          * When checking for a valid find value use if ((value=geometry->getPrimitiveSetIndex(primitive))!=geometry.getNumPrimitiveSet())
253        */
254        unsigned int getPrimitiveSetIndex(const PrimitiveSet* primitiveset) const;
255
256
257
258        /** deprecated - forces OpenGL slow path, just kept for backwards compatibility.*/
259        void setVertexIndices(IndexArray* array);
260        IndexArray* getVertexIndices() { return _vertexData.indices.get(); }
261        const IndexArray* getVertexIndices() const  { return _vertexData.indices.get(); }
262     
263        /** deprecated - forces OpenGL slow path, just kept for backwards compatibility.*/
264        void setNormalIndices(IndexArray* array);
265        IndexArray* getNormalIndices() { return _normalData.indices.get(); }
266        const IndexArray* getNormalIndices() const { return _normalData.indices.get(); }
267
268        /** deprecated - forces OpenGL slow path, just kept for backwards compatibility.*/
269        void setColorIndices(IndexArray* array);
270        IndexArray* getColorIndices() { return _colorData.indices.get(); }
271        const IndexArray* getColorIndices() const { return _colorData.indices.get(); }
272
273        /** deprecated - forces OpenGL slow path, just kept for backwards compatibility.*/
274        void setSecondaryColorIndices(IndexArray* array);
275        IndexArray* getSecondaryColorIndices() { return _secondaryColorData.indices.get(); }
276        const IndexArray* getSecondaryColorIndices() const { return _secondaryColorData.indices.get(); }
277
278        /** deprecated - forces OpenGL slow path, just kept for backwards compatibility.*/
279        void setFogCoordIndices(IndexArray* array);
280        IndexArray* getFogCoordIndices() { return _fogCoordData.indices.get(); }
281        const IndexArray* getFogCoordIndices() const { return _fogCoordData.indices.get(); }
282
283        /** deprecated - forces OpenGL slow path, just kept for backwards compatibility.*/
284        void setTexCoordIndices(unsigned int unit,IndexArray*);
285        IndexArray* getTexCoordIndices(unsigned int unit);
286        const IndexArray* getTexCoordIndices(unsigned int unit) const;
287
288        /** deprecated - forces OpenGL slow path, just kept for backwards compatibility.*/
289        void setVertexAttribIndices(unsigned int index,IndexArray* array);
290        IndexArray* getVertexAttribIndices(unsigned int index);
291        const IndexArray* getVertexAttribIndices(unsigned int index) const;
292
293
294
295
296        /** When set to true, ignore the setUseDisplayList() settings, and hints to the drawImplementation
297            method to use OpenGL vertex buffer objects for rendering.*/
298        virtual void setUseVertexBufferObjects(bool flag);
299
300        /** Force a recompile on next draw() of any OpenGL display list associated with this geoset.*/
301        virtual void dirtyDisplayList();
302
303
304        /** Resize any per context GLObject buffers to specified size. */
305        virtual void resizeGLObjectBuffers(unsigned int maxSize);
306
307        /** If State is non-zero, this function releases OpenGL objects for
308          * the specified graphics context. Otherwise, releases OpenGL objects
309          * for all graphics contexts. */
310        virtual void releaseGLObjects(State* state=0) const;
311
312        typedef std::vector<osg::Array*>  ArrayList;
313        bool getArrayList(ArrayList& arrayList) const;
314
315        typedef std::vector<osg::DrawElements*>  DrawElementsList;
316        bool getDrawElementsList(DrawElementsList& drawElementsList) const;
317
318        osg::VertexBufferObject* getOrCreateVertexBufferObject();
319
320        osg::ElementBufferObject* getOrCreateElementBufferObject();
321
322
323        /** Set whether fast paths should be used when supported. */
324        void setFastPathHint(bool on) {  _fastPathHint = on; }
325
326        /** Get whether fast paths should be used when supported. */
327        bool getFastPathHint() const { return _fastPathHint; }
328
329        /** Return true if OpenGL fast paths will be used with drawing this Geometry.
330          * Fast paths use vertex arrays, and glDrawArrays/glDrawElements.  Slow paths
331          * use glBegin()/glVertex.../glEnd(). Use of per primitive bindings or per vertex indexed
332          * arrays will drop the rendering path off the fast path.
333        */
334        inline bool areFastPathsUsed() const
335        {
336            if (_internalOptimizedGeometry.valid())
337                return _internalOptimizedGeometry->areFastPathsUsed();
338            else
339                return _fastPath && _fastPathHint;
340        }
341
342        bool computeFastPathsUsed();
343
344        bool verifyBindings() const;
345
346        void computeCorrectBindingsAndArraySizes();
347
348
349        bool suitableForOptimization() const;
350
351        void copyToAndOptimize(Geometry& target);
352
353
354        bool containsSharedArrays() const;
355       
356        void duplicateSharedArrays();
357       
358
359        void computeInternalOptimizedGeometry();
360
361        void removeInternalOptimizedGeometry() { _internalOptimizedGeometry = 0; }
362
363        void setInternalOptimizedGeometry(osg::Geometry* geometry) { _internalOptimizedGeometry = geometry; }
364
365        osg::Geometry* getInternalOptimizedGeometry() { return _internalOptimizedGeometry.get(); }
366
367        const osg::Geometry* getInternalOptimizedGeometry() const { return _internalOptimizedGeometry.get(); }
368
369
370        /** Return the estimated size of GLObjects (display lists/vertex buffer objects) that are associated with this drawable.
371          * This size is used a hint for reuse of deleted display lists/vertex buffer objects. */
372        virtual unsigned int getGLObjectSizeHint() const;
373       
374        /** Draw Geometry directly ignoring an OpenGL display list which could be attached.
375          * This is the internal draw method which does the drawing itself,
376          * and is the method to override when deriving from Geometry for user-drawn objects.
377        */
378        virtual void drawImplementation(RenderInfo& renderInfo) const;
379
380        /** Return true, osg::Geometry does support accept(Drawable::AttributeFunctor&). */
381        virtual bool supports(const Drawable::AttributeFunctor&) const { return true; }
382
383        /** Accept an Drawable::AttributeFunctor and call its methods to tell it about the internal attributes that this Drawable has. */
384        virtual void accept(Drawable::AttributeFunctor& af);
385
386        /** Return true, osg::Geometry does support accept(Drawable::ConstAttributeFunctor&). */
387        virtual bool supports(const Drawable::ConstAttributeFunctor&) const { return true; }
388
389        /** Accept a Drawable::ConstAttributeFunctor and call its methods to tell it about the internal attributes that this Drawable has. */
390        virtual void accept(Drawable::ConstAttributeFunctor& af) const;
391
392        /** Return true, osg::Geometry does support accept(PrimitiveFunctor&). */
393        virtual bool supports(const PrimitiveFunctor&) const { return true; }
394
395        /** Accept a PrimitiveFunctor and call its methods to tell it about the internal primitives that this Drawable has. */
396        virtual void accept(PrimitiveFunctor& pf) const;
397
398        /** Return true, osg::Geometry does support accept(PrimitiveIndexFunctor&). */
399        virtual bool supports(const PrimitiveIndexFunctor&) const { return true; }
400
401        /** Accept a PrimitiveFunctor and call its methods to tell it about the internal primitives that this Drawable has. */
402        virtual void accept(PrimitiveIndexFunctor& pf) const;
403
404
405    protected:
406
407        Geometry& operator = (const Geometry&) { return *this;}
408
409        virtual ~Geometry();
410       
411        bool verifyBindings(const ArrayData& arrayData) const;
412        bool verifyBindings(const Vec3ArrayData& arrayData) const;
413       
414        void computeCorrectBindingsAndArraySizes(ArrayData& arrayData,const char* arrayName);
415        void computeCorrectBindingsAndArraySizes(Vec3ArrayData& arrayData,const char* arrayName);
416       
417        void addVertexBufferObjectIfRequired(osg::Array* array);
418        void addElementBufferObjectIfRequired(osg::PrimitiveSet* primitiveSet);
419       
420
421        PrimitiveSetList                _primitives;
422        ArrayData                       _vertexData;
423        ArrayData                       _normalData;
424        ArrayData                       _colorData;
425        ArrayData                       _secondaryColorData;
426        ArrayData                       _fogCoordData;
427        ArrayDataList                   _texCoordList;
428        ArrayDataList                   _vertexAttribList;
429
430        mutable bool                    _fastPath;
431        bool                            _fastPathHint;
432
433        ref_ptr<Geometry>               _internalOptimizedGeometry;
434};
435
436/** Convenience function to be used for creating quad geometry with texture coords.
437  * Tex coords go from left bottom (l,b) to right top (r,t).
438*/
439extern OSG_EXPORT Geometry* createTexturedQuadGeometry(const Vec3& corner,const Vec3& widthVec,const Vec3& heightVec, float l, float b, float r, float t);
440
441/** Convenience function to be used for creating quad geometry with texture coords.
442  * Tex coords go from bottom left (0,0) to top right (s,t).
443*/
444inline Geometry* createTexturedQuadGeometry(const Vec3& corner,const Vec3& widthVec,const Vec3& heightVec, float s=1.0f, float t=1.0f)
445{
446    return createTexturedQuadGeometry(corner,widthVec,heightVec, 0.0f, 0.0f, s, t);
447}
448
449
450}
451
452#endif
Note: See TracBrowser for help on using the browser.