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

Revision 13041, 19.6 kB (checked in by robert, 3 years ago)

Ran script to remove trailing spaces and tabs

  • 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);
231
232        PrimitiveSetList& getPrimitiveSetList() { return _primitives; }
233        const PrimitiveSetList& getPrimitiveSetList() const { return _primitives; }
234
235        unsigned int getNumPrimitiveSets() const { return static_cast<unsigned int>(_primitives.size()); }
236        PrimitiveSet* getPrimitiveSet(unsigned int pos) { return _primitives[pos].get(); }
237        const PrimitiveSet* getPrimitiveSet(unsigned int pos) const { return _primitives[pos].get(); }
238
239        /** Add a primitive set to the geometry. */
240        bool addPrimitiveSet(PrimitiveSet* primitiveset);
241
242        /** Set a primitive set to the specified position in geometry's primitive set list. */
243        bool setPrimitiveSet(unsigned int i,PrimitiveSet* primitiveset);
244
245        /** Insert a primitive set to the specified position in geometry's primitive set list. */
246        bool insertPrimitiveSet(unsigned int i,PrimitiveSet* primitiveset);
247
248        /** Remove primitive set(s) from the specified position in geometry's primitive set list. */
249        bool removePrimitiveSet(unsigned int i,unsigned int numElementsToRemove=1);
250
251        /** Get the index number of a primitive set, return a value between
252          * 0 and getNumPrimitiveSet()-1 if found, if not found then return getNumPrimitiveSet().
253          * When checking for a valid find value use if ((value=geometry->getPrimitiveSetIndex(primitive))!=geometry.getNumPrimitiveSet())
254        */
255        unsigned int getPrimitiveSetIndex(const PrimitiveSet* primitiveset) const;
256
257
258
259        /** deprecated - forces OpenGL slow path, just kept for backwards compatibility.*/
260        void setVertexIndices(IndexArray* array);
261        IndexArray* getVertexIndices() { return _vertexData.indices.get(); }
262        const IndexArray* getVertexIndices() const  { return _vertexData.indices.get(); }
263
264        /** deprecated - forces OpenGL slow path, just kept for backwards compatibility.*/
265        void setNormalIndices(IndexArray* array);
266        IndexArray* getNormalIndices() { return _normalData.indices.get(); }
267        const IndexArray* getNormalIndices() const { return _normalData.indices.get(); }
268
269        /** deprecated - forces OpenGL slow path, just kept for backwards compatibility.*/
270        void setColorIndices(IndexArray* array);
271        IndexArray* getColorIndices() { return _colorData.indices.get(); }
272        const IndexArray* getColorIndices() const { return _colorData.indices.get(); }
273
274        /** deprecated - forces OpenGL slow path, just kept for backwards compatibility.*/
275        void setSecondaryColorIndices(IndexArray* array);
276        IndexArray* getSecondaryColorIndices() { return _secondaryColorData.indices.get(); }
277        const IndexArray* getSecondaryColorIndices() const { return _secondaryColorData.indices.get(); }
278
279        /** deprecated - forces OpenGL slow path, just kept for backwards compatibility.*/
280        void setFogCoordIndices(IndexArray* array);
281        IndexArray* getFogCoordIndices() { return _fogCoordData.indices.get(); }
282        const IndexArray* getFogCoordIndices() const { return _fogCoordData.indices.get(); }
283
284        /** deprecated - forces OpenGL slow path, just kept for backwards compatibility.*/
285        void setTexCoordIndices(unsigned int unit,IndexArray*);
286        IndexArray* getTexCoordIndices(unsigned int unit);
287        const IndexArray* getTexCoordIndices(unsigned int unit) const;
288
289        /** deprecated - forces OpenGL slow path, just kept for backwards compatibility.*/
290        void setVertexAttribIndices(unsigned int index,IndexArray* array);
291        IndexArray* getVertexAttribIndices(unsigned int index);
292        const IndexArray* getVertexAttribIndices(unsigned int index) const;
293
294
295
296
297        /** When set to true, ignore the setUseDisplayList() settings, and hints to the drawImplementation
298            method to use OpenGL vertex buffer objects for rendering.*/
299        virtual void setUseVertexBufferObjects(bool flag);
300
301        /** Force a recompile on next draw() of any OpenGL display list associated with this geoset.*/
302        virtual void dirtyDisplayList();
303
304
305        /** Resize any per context GLObject buffers to specified size. */
306        virtual void resizeGLObjectBuffers(unsigned int maxSize);
307
308        /** If State is non-zero, this function releases OpenGL objects for
309          * the specified graphics context. Otherwise, releases OpenGL objects
310          * for all graphics contexts. */
311        virtual void releaseGLObjects(State* state=0) const;
312
313        typedef std::vector<osg::Array*>  ArrayList;
314        bool getArrayList(ArrayList& arrayList) const;
315
316        typedef std::vector<osg::DrawElements*>  DrawElementsList;
317        bool getDrawElementsList(DrawElementsList& drawElementsList) const;
318
319        osg::VertexBufferObject* getOrCreateVertexBufferObject();
320
321        osg::ElementBufferObject* getOrCreateElementBufferObject();
322
323
324        /** Set whether fast paths should be used when supported. */
325        void setFastPathHint(bool on) {  _fastPathHint = on; }
326
327        /** Get whether fast paths should be used when supported. */
328        bool getFastPathHint() const { return _fastPathHint; }
329
330
331        /** Return true if OpenGL fast paths will be used with drawing this Geometry.
332          * Fast paths directly use vertex arrays, and glDrawArrays/glDrawElements so have low CPU overhead.
333          * With Slow paths the osg::Geometry::drawImplementation has to dynamically assemble OpenGL
334          * compatible vertex arrays from the osg::Geometry arrays data and then dispatch these to OpenGL,
335          * so have higher CPU overhead than the Fast paths.
336          * Use of per primitive bindings or per vertex indexed arrays will drop the rendering path off the fast path.
337        */
338        inline bool areFastPathsUsed() const
339        {
340            if (_internalOptimizedGeometry.valid())
341                return _internalOptimizedGeometry->areFastPathsUsed();
342            else
343                return _fastPath && _fastPathHint;
344        }
345
346        bool computeFastPathsUsed();
347
348        bool verifyBindings() const;
349
350        void computeCorrectBindingsAndArraySizes();
351
352        /** check whether the arrays, indices, bindings and primitives all match correctly, return false is .*/
353        bool verifyArrays(std::ostream& out) const;
354
355        bool suitableForOptimization() const;
356
357        void copyToAndOptimize(Geometry& target);
358
359
360        bool containsSharedArrays() const;
361
362        void duplicateSharedArrays();
363
364
365        void computeInternalOptimizedGeometry();
366
367        void removeInternalOptimizedGeometry() { _internalOptimizedGeometry = 0; }
368
369        void setInternalOptimizedGeometry(osg::Geometry* geometry) { _internalOptimizedGeometry = geometry; }
370
371        osg::Geometry* getInternalOptimizedGeometry() { return _internalOptimizedGeometry.get(); }
372
373        const osg::Geometry* getInternalOptimizedGeometry() const { return _internalOptimizedGeometry.get(); }
374
375
376        /** Return the estimated size of GLObjects (display lists/vertex buffer objects) that are associated with this drawable.
377          * This size is used a hint for reuse of deleted display lists/vertex buffer objects. */
378        virtual unsigned int getGLObjectSizeHint() const;
379
380        /** Immediately compile this \c Drawable into an OpenGL Display List/VertexBufferObjects.
381          * @note Operation is ignored if \c _useDisplayList is \c false or VertexBufferObjects are not used.
382        */
383        virtual void compileGLObjects(RenderInfo& renderInfo) const;
384
385        /** Draw Geometry directly ignoring an OpenGL display list which could be attached.
386          * This is the internal draw method which does the drawing itself,
387          * and is the method to override when deriving from Geometry for user-drawn objects.
388        */
389        virtual void drawImplementation(RenderInfo& renderInfo) const;
390
391        /** Return true, osg::Geometry does support accept(Drawable::AttributeFunctor&). */
392        virtual bool supports(const Drawable::AttributeFunctor&) const { return true; }
393
394        /** Accept an Drawable::AttributeFunctor and call its methods to tell it about the internal attributes that this Drawable has. */
395        virtual void accept(Drawable::AttributeFunctor& af);
396
397        /** Return true, osg::Geometry does support accept(Drawable::ConstAttributeFunctor&). */
398        virtual bool supports(const Drawable::ConstAttributeFunctor&) const { return true; }
399
400        /** Accept a Drawable::ConstAttributeFunctor and call its methods to tell it about the internal attributes that this Drawable has. */
401        virtual void accept(Drawable::ConstAttributeFunctor& af) const;
402
403        /** Return true, osg::Geometry does support accept(PrimitiveFunctor&). */
404        virtual bool supports(const PrimitiveFunctor&) const { return true; }
405
406        /** Accept a PrimitiveFunctor and call its methods to tell it about the internal primitives that this Drawable has. */
407        virtual void accept(PrimitiveFunctor& pf) const;
408
409        /** Return true, osg::Geometry does support accept(PrimitiveIndexFunctor&). */
410        virtual bool supports(const PrimitiveIndexFunctor&) const { return true; }
411
412        /** Accept a PrimitiveFunctor and call its methods to tell it about the internal primitives that this Drawable has. */
413        virtual void accept(PrimitiveIndexFunctor& pf) const;
414
415
416    protected:
417
418        Geometry& operator = (const Geometry&) { return *this;}
419
420        virtual ~Geometry();
421
422        bool verifyBindings(const ArrayData& arrayData) const;
423        bool verifyBindings(const Vec3ArrayData& arrayData) const;
424
425        void computeCorrectBindingsAndArraySizes(ArrayData& arrayData,const char* arrayName);
426        void computeCorrectBindingsAndArraySizes(Vec3ArrayData& arrayData,const char* arrayName);
427
428        void addVertexBufferObjectIfRequired(osg::Array* array);
429        void addElementBufferObjectIfRequired(osg::PrimitiveSet* primitiveSet);
430
431
432        PrimitiveSetList                _primitives;
433        ArrayData                       _vertexData;
434        ArrayData                       _normalData;
435        ArrayData                       _colorData;
436        ArrayData                       _secondaryColorData;
437        ArrayData                       _fogCoordData;
438        ArrayDataList                   _texCoordList;
439        ArrayDataList                   _vertexAttribList;
440
441        mutable bool                    _fastPath;
442        bool                            _fastPathHint;
443
444        ref_ptr<Geometry>               _internalOptimizedGeometry;
445};
446
447/** Convenience function to be used for creating quad geometry with texture coords.
448  * Tex coords go from left bottom (l,b) to right top (r,t).
449*/
450extern OSG_EXPORT Geometry* createTexturedQuadGeometry(const Vec3& corner,const Vec3& widthVec,const Vec3& heightVec, float l, float b, float r, float t);
451
452/** Convenience function to be used for creating quad geometry with texture coords.
453  * Tex coords go from bottom left (0,0) to top right (s,t).
454*/
455inline Geometry* createTexturedQuadGeometry(const Vec3& corner,const Vec3& widthVec,const Vec3& heightVec, float s=1.0f, float t=1.0f)
456{
457    return createTexturedQuadGeometry(corner,widthVec,heightVec, 0.0f, 0.0f, s, t);
458}
459
460
461}
462
463#endif
Note: See TracBrowser for help on using the browser.