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

Revision 13491, 17.1 kB (checked in by robert, 18 hours ago)

Release OpenSceneGraph-3.3.3

  • 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
26class OSG_EXPORT Geometry : public Drawable
27{
28    public:
29
30        Geometry();
31
32        /** Copy constructor using CopyOp to manage deep vs shallow copy. */
33        Geometry(const Geometry& geometry,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
34
35        virtual Object* cloneType() const { return new Geometry(); }
36        virtual Object* clone(const CopyOp& copyop) const { return new Geometry(*this,copyop); }
37        virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const Geometry*>(obj)!=NULL; }
38        virtual const char* libraryName() const { return "osg"; }
39        virtual const char* className() const { return "Geometry"; }
40
41        virtual Geometry* asGeometry() { return this; }
42        virtual const Geometry* asGeometry() const { return this; }
43
44        bool empty() const;
45
46        typedef std::vector< osg::ref_ptr<osg::Array> >  ArrayList;
47
48        void setVertexArray(Array* array);
49        Array* getVertexArray() { return _vertexArray.get(); }
50        const Array* getVertexArray() const  { return _vertexArray.get(); }
51
52
53        void setNormalArray(Array* array);
54        Array* getNormalArray() { return _normalArray.get(); }
55        const Array* getNormalArray() const { return _normalArray.get(); }
56
57       
58
59        void setColorArray(Array* array);
60        Array* getColorArray() { return _colorArray.get(); }
61        const Array* getColorArray() const { return _colorArray.get(); }
62
63
64
65        void setSecondaryColorArray(Array* array);
66        Array* getSecondaryColorArray() { return _secondaryColorArray.get(); }
67        const Array* getSecondaryColorArray() const  { return _secondaryColorArray.get(); }
68
69
70        void setFogCoordArray(Array* array);
71        Array* getFogCoordArray() { return _fogCoordArray.get(); }
72        const Array* getFogCoordArray() const { return _fogCoordArray.get(); }
73
74
75        void setTexCoordArray(unsigned int unit,Array*);
76        Array* getTexCoordArray(unsigned int unit);
77        const Array* getTexCoordArray(unsigned int unit) const;
78
79        unsigned int getNumTexCoordArrays() const {  return static_cast<unsigned int>(_texCoordList.size()); }
80        ArrayList& getTexCoordArrayList() { return _texCoordList; }
81        const ArrayList& getTexCoordArrayList() const { return _texCoordList; }
82
83
84
85        void setVertexAttribArray(unsigned int index,Array* array);
86        Array *getVertexAttribArray(unsigned int index);
87        const Array *getVertexAttribArray(unsigned int index) const;
88
89
90        unsigned int getNumVertexAttribArrays() const { return static_cast<unsigned int>(_vertexAttribList.size()); }
91        ArrayList& getVertexAttribArrayList() { return _vertexAttribList; }
92        const ArrayList& getVertexAttribArrayList() const { return _vertexAttribList; }
93
94
95
96        typedef std::vector< ref_ptr<PrimitiveSet> > PrimitiveSetList;
97
98        void setPrimitiveSetList(const PrimitiveSetList& primitives);
99
100        PrimitiveSetList& getPrimitiveSetList() { return _primitives; }
101        const PrimitiveSetList& getPrimitiveSetList() const { return _primitives; }
102
103        unsigned int getNumPrimitiveSets() const { return static_cast<unsigned int>(_primitives.size()); }
104        PrimitiveSet* getPrimitiveSet(unsigned int pos) { return _primitives[pos].get(); }
105        const PrimitiveSet* getPrimitiveSet(unsigned int pos) const { return _primitives[pos].get(); }
106
107        /** Add a primitive set to the geometry. */
108        bool addPrimitiveSet(PrimitiveSet* primitiveset);
109
110        /** Set a primitive set to the specified position in geometry's primitive set list. */
111        bool setPrimitiveSet(unsigned int i,PrimitiveSet* primitiveset);
112
113        /** Insert a primitive set to the specified position in geometry's primitive set list. */
114        bool insertPrimitiveSet(unsigned int i,PrimitiveSet* primitiveset);
115
116        /** Remove primitive set(s) from the specified position in geometry's primitive set list. */
117        bool removePrimitiveSet(unsigned int i,unsigned int numElementsToRemove=1);
118
119        /** Get the index number of a primitive set, return a value between
120          * 0 and getNumPrimitiveSet()-1 if found, if not found then return getNumPrimitiveSet().
121          * When checking for a valid find value use if ((value=geometry->getPrimitiveSetIndex(primitive))!=geometry.getNumPrimitiveSet())
122        */
123        unsigned int getPrimitiveSetIndex(const PrimitiveSet* primitiveset) const;
124
125
126        /** return true if any arrays are shared.*/
127        bool containsSharedArrays() const;
128
129        /** duplicate any shared arrays.*/
130        void duplicateSharedArrays();
131
132
133        /** When set to true, ignore the setUseDisplayList() settings, and hints to the drawImplementation
134            method to use OpenGL vertex buffer objects for rendering.*/
135        virtual void setUseVertexBufferObjects(bool flag);
136
137        /** Force a recompile on next draw() of any OpenGL display list associated with this geoset.*/
138        virtual void dirtyDisplayList();
139
140
141        /** Resize any per context GLObject buffers to specified size. */
142        virtual void resizeGLObjectBuffers(unsigned int maxSize);
143
144        /** If State is non-zero, this function releases OpenGL objects for
145          * the specified graphics context. Otherwise, releases OpenGL objects
146          * for all graphics contexts. */
147        virtual void releaseGLObjects(State* state=0) const;
148
149        bool getArrayList(ArrayList& arrayList) const;
150
151        typedef std::vector<osg::DrawElements*>  DrawElementsList;
152        bool getDrawElementsList(DrawElementsList& drawElementsList) const;
153
154        osg::VertexBufferObject* getOrCreateVertexBufferObject();
155        osg::ElementBufferObject* getOrCreateElementBufferObject();
156
157
158        /** Return the estimated size of GLObjects (display lists/vertex buffer objects) that are associated with this drawable.
159          * This size is used a hint for reuse of deleted display lists/vertex buffer objects. */
160        virtual unsigned int getGLObjectSizeHint() const;
161
162        /** Immediately compile this \c Drawable into an OpenGL Display List/VertexBufferObjects.
163          * @note Operation is ignored if \c _useDisplayList is \c false or VertexBufferObjects are not used.
164        */
165        virtual void compileGLObjects(RenderInfo& renderInfo) const;
166
167        /** Draw Geometry directly ignoring an OpenGL display list which could be attached.
168          * This is the internal draw method which does the drawing itself,
169          * and is the method to override when deriving from Geometry for user-drawn objects.
170        */
171        virtual void drawImplementation(RenderInfo& renderInfo) const;
172
173        /** Return true, osg::Geometry does support accept(Drawable::AttributeFunctor&). */
174        virtual bool supports(const Drawable::AttributeFunctor&) const { return true; }
175
176        /** Accept an Drawable::AttributeFunctor and call its methods to tell it about the internal attributes that this Drawable has. */
177        virtual void accept(Drawable::AttributeFunctor& af);
178
179        /** Return true, osg::Geometry does support accept(Drawable::ConstAttributeFunctor&). */
180        virtual bool supports(const Drawable::ConstAttributeFunctor&) const { return true; }
181
182        /** Accept a Drawable::ConstAttributeFunctor and call its methods to tell it about the internal attributes that this Drawable has. */
183        virtual void accept(Drawable::ConstAttributeFunctor& af) const;
184
185        /** Return true, osg::Geometry does support accept(PrimitiveFunctor&). */
186        virtual bool supports(const PrimitiveFunctor&) const { return true; }
187
188        /** Accept a PrimitiveFunctor and call its methods to tell it about the internal primitives that this Drawable has. */
189        virtual void accept(PrimitiveFunctor& pf) const;
190
191        /** Return true, osg::Geometry does support accept(PrimitiveIndexFunctor&). */
192        virtual bool supports(const PrimitiveIndexFunctor&) const { return true; }
193
194        /** Accept a PrimitiveFunctor and call its methods to tell it about the internal primitives that this Drawable has. */
195        virtual void accept(PrimitiveIndexFunctor& pf) const;
196
197
198    protected:
199
200        Geometry& operator = (const Geometry&) { return *this;}
201
202        virtual ~Geometry();
203
204
205        void addVertexBufferObjectIfRequired(osg::Array* array);
206        void addElementBufferObjectIfRequired(osg::PrimitiveSet* primitiveSet);
207
208
209        PrimitiveSetList                _primitives;
210        osg::ref_ptr<Array>             _vertexArray;
211        osg::ref_ptr<Array>             _normalArray;
212        osg::ref_ptr<Array>             _colorArray;
213        osg::ref_ptr<Array>             _secondaryColorArray;
214        osg::ref_ptr<Array>             _fogCoordArray;
215        ArrayList                       _texCoordList;
216        ArrayList                       _vertexAttribList;
217       
218        bool                            _containsDeprecatedData;
219       
220    public:
221       
222       
223        /** Return true if the deprecated use array indicies or BIND_PER_PRIMITIVE binding has been assigned to arrays.*/
224        bool containsDeprecatedData() const { return  _containsDeprecatedData; }
225
226        /** fallback for deprecated functionality. Return true if the Geometry contains any array indices or BIND_PER_PRIMITIVE arrays. */
227        bool checkForDeprecatedData();
228
229        /** fallback for deprecated functionality. Removes any array indices and BIND_PER_PRIMITIVE arrays.*/
230        void fixDeprecatedData();
231
232        /** Same values as Array::Binding.*/
233        enum AttributeBinding
234        {
235            BIND_OFF=0,
236            BIND_OVERALL=1,
237            BIND_PER_PRIMITIVE_SET=2,
238            BIND_PER_PRIMITIVE=3, /// no longer supported
239            BIND_PER_VERTEX=4
240        };
241
242        /** deprecated, use array->setBinding(..). */
243        void setNormalBinding(AttributeBinding ab);
244        AttributeBinding getNormalBinding() const;
245
246        /** deprecated, use array->setBinding(..). */
247        void setColorBinding(AttributeBinding ab);
248        AttributeBinding getColorBinding() const;
249
250        /** deprecated, use array->setBinding(..). */
251        void setSecondaryColorBinding(AttributeBinding ab);
252        AttributeBinding getSecondaryColorBinding() const;
253
254        /** deprecated, use array->setBinding(..). */
255        void setFogCoordBinding(AttributeBinding ab);
256        AttributeBinding getFogCoordBinding() const;
257
258        /** deprecated, use array->setBinding(..). */
259        void setVertexAttribBinding(unsigned int index,AttributeBinding ab);
260        AttributeBinding getVertexAttribBinding(unsigned int index) const;
261
262        /** deprecated, use array->setNormalize(..). */
263        void setVertexAttribNormalize(unsigned int index,GLboolean norm);
264        GLboolean getVertexAttribNormalize(unsigned int index) const;
265
266#if defined(OSG_USE_DEPRECATED_GEOMETRY_METHODS)
267        /** no longer supported.*/
268        inline void setVertexIndices(IndexArray* array);
269        inline const IndexArray* getVertexIndices() const;
270
271        /** no longer supported.*/
272        inline void setNormalIndices(IndexArray* array);
273        inline const IndexArray* getNormalIndices() const;
274
275        /** no longer supported.*/
276        inline void setColorIndices(IndexArray* array);
277        inline const IndexArray* getColorIndices() const;
278
279        /** no longer supported.*/
280        inline void setSecondaryColorIndices(IndexArray* array);
281        inline const IndexArray* getSecondaryColorIndices() const;
282
283        /** no longer supported.*/
284        inline void setFogCoordIndices(IndexArray* array);
285        inline const IndexArray* getFogCoordIndices() const;
286
287        /** no longer supported.*/
288        inline void setTexCoordIndices(unsigned int unit,IndexArray*);
289        inline const IndexArray* getTexCoordIndices(unsigned int unit) const;
290
291        /** no longer supported.*/
292        inline void setVertexAttribIndices(unsigned int index,IndexArray* array);
293        inline const IndexArray* getVertexAttribIndices(unsigned int index) const;
294#endif   
295};
296
297/** Convenience function to be used for creating quad geometry with texture coords.
298  * Tex coords go from left bottom (l,b) to right top (r,t).
299*/
300extern OSG_EXPORT Geometry* createTexturedQuadGeometry(const Vec3& corner,const Vec3& widthVec,const Vec3& heightVec, float l, float b, float r, float t);
301
302/** Convenience function to be used for creating quad geometry with texture coords.
303  * Tex coords go from bottom left (0,0) to top right (s,t).
304*/
305inline Geometry* createTexturedQuadGeometry(const Vec3& corner,const Vec3& widthVec,const Vec3& heightVec, float s=1.0f, float t=1.0f)
306{
307    return createTexturedQuadGeometry(corner,widthVec,heightVec, 0.0f, 0.0f, s, t);
308}
309
310
311#if defined(OSG_USE_DEPRECATED_GEOMETRY_METHODS)
312
313#include <osg/Notify>
314
315inline void Geometry::setVertexIndices(IndexArray* array)
316{
317    if (_vertexArray.valid()) { _vertexArray->setUserData(array); if (array)  _containsDeprecatedData = true; }
318    else { OSG_WARN<<"Geometry::setVertexIndicies(..) function failed as there is no vertex array to associate inidices with."<<std::endl; }
319}
320
321inline const IndexArray* Geometry::getVertexIndices() const
322{
323    if (_vertexArray.valid()) return dynamic_cast<IndexArray*>(_vertexArray->getUserData());
324    else return 0;
325}
326
327inline void Geometry::setNormalIndices(IndexArray* array)
328{
329    if (_normalArray.valid()) { _normalArray->setUserData(array); if (array)  _containsDeprecatedData = true; }
330    else { OSG_WARN<<"Geometry::setNormalIndicies(..) function failed as there is no normal array to associate inidices with."<<std::endl; }
331}
332inline const IndexArray* Geometry::getNormalIndices() const
333{
334    if (_normalArray.valid()) return dynamic_cast<IndexArray*>(_normalArray->getUserData());
335    else return 0;
336}
337
338inline void Geometry::setColorIndices(IndexArray* array)
339{
340    if (_colorArray.valid()) { _colorArray->setUserData(array); if (array)  _containsDeprecatedData = true; }
341    else { OSG_WARN<<"Geometry::setColorIndicies(..) function failed as there is no color array to associate inidices with."<<std::endl; }
342}
343inline const IndexArray* Geometry::getColorIndices() const
344{
345    if (_colorArray.valid()) return dynamic_cast<IndexArray*>(_colorArray->getUserData());
346    else return 0;
347}
348
349inline void Geometry::setSecondaryColorIndices(IndexArray* array)
350{
351    if (_secondaryColorArray.valid()) { _secondaryColorArray->setUserData(array); if (array)  _containsDeprecatedData = true; }
352    else { OSG_WARN<<"Geometry::setSecondaryColorArray(..) function failed as there is no secondary color array to associate inidices with."<<std::endl; }
353}
354inline const IndexArray* Geometry::getSecondaryColorIndices() const
355{
356    if (_secondaryColorArray.valid()) return dynamic_cast<IndexArray*>(_secondaryColorArray->getUserData());
357    else return 0;
358}
359
360inline void Geometry::setFogCoordIndices(IndexArray* array)
361{
362    if (_fogCoordArray.valid()) { _fogCoordArray->setUserData(array); if (array)  _containsDeprecatedData = true; }
363    else { OSG_WARN<<"Geometry::setFogCoordIndicies(..) function failed as there is no fog coord array to associate inidices with."<<std::endl; }
364}
365inline const IndexArray* Geometry::getFogCoordIndices() const
366{
367    if (_fogCoordArray.valid()) return dynamic_cast<IndexArray*>(_fogCoordArray->getUserData());
368    else return 0;
369}
370
371inline void Geometry::setTexCoordIndices(unsigned int unit,IndexArray* array)
372{
373    if (unit<_texCoordList.size() && _texCoordList[unit].valid()) { _texCoordList[unit]->setUserData(array); if (array)  _containsDeprecatedData = true; }
374    else { OSG_WARN<<"Geometry::setTexCoordIndices(..) function failed as there is no texcoord array to associate inidices with."<<std::endl; }
375}
376inline const IndexArray* Geometry::getTexCoordIndices(unsigned int unit) const
377{
378    if (unit<_texCoordList.size() && _texCoordList[unit].valid()) return dynamic_cast<IndexArray*>(_texCoordList[unit]->getUserData());
379    else return 0;
380}
381
382inline void Geometry::setVertexAttribIndices(unsigned int index,IndexArray* array)
383{
384    if (index<_vertexAttribList.size() && _vertexAttribList[index].valid()) { _vertexAttribList[index]->setUserData(array); if (array)  _containsDeprecatedData = true; }
385    else { OSG_WARN<<"Geometry::setVertexAttribIndices(..) function failed as there is no vertex attrib array to associate inidices with."<<std::endl; }
386}
387inline const IndexArray* Geometry::getVertexAttribIndices(unsigned int index) const
388{
389    if (index<_vertexAttribList.size() && _vertexAttribList[index].valid()) return dynamic_cast<IndexArray*>(_vertexAttribList[index]->getUserData());
390    else return 0;
391}
392#endif
393   
394}
395
396#endif
Note: See TracBrowser for help on using the browser.