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

Revision 13502, 17.1 kB (checked in by robert, 4 days ago)

Added shaders to support experimental shader based displacement mapping technique osgTerrain::ShaderTerrain?.

  • 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#if defined(OSG_USE_DEPRECATED_GEOMETRY_METHODS)
239            BIND_PER_PRIMITIVE=3, /// no longer supported
240#endif
241            BIND_PER_VERTEX=4
242        };
243
244        /** deprecated, use array->setBinding(..). */
245        void setNormalBinding(AttributeBinding ab);
246        AttributeBinding getNormalBinding() const;
247
248        /** deprecated, use array->setBinding(..). */
249        void setColorBinding(AttributeBinding ab);
250        AttributeBinding getColorBinding() const;
251
252        /** deprecated, use array->setBinding(..). */
253        void setSecondaryColorBinding(AttributeBinding ab);
254        AttributeBinding getSecondaryColorBinding() const;
255
256        /** deprecated, use array->setBinding(..). */
257        void setFogCoordBinding(AttributeBinding ab);
258        AttributeBinding getFogCoordBinding() const;
259
260        /** deprecated, use array->setBinding(..). */
261        void setVertexAttribBinding(unsigned int index,AttributeBinding ab);
262        AttributeBinding getVertexAttribBinding(unsigned int index) const;
263
264        /** deprecated, use array->setNormalize(..). */
265        void setVertexAttribNormalize(unsigned int index,GLboolean norm);
266        GLboolean getVertexAttribNormalize(unsigned int index) const;
267
268#if defined(OSG_USE_DEPRECATED_GEOMETRY_METHODS)
269        /** no longer supported.*/
270        inline void setVertexIndices(IndexArray* array);
271        inline const IndexArray* getVertexIndices() const;
272
273        /** no longer supported.*/
274        inline void setNormalIndices(IndexArray* array);
275        inline const IndexArray* getNormalIndices() const;
276
277        /** no longer supported.*/
278        inline void setColorIndices(IndexArray* array);
279        inline const IndexArray* getColorIndices() const;
280
281        /** no longer supported.*/
282        inline void setSecondaryColorIndices(IndexArray* array);
283        inline const IndexArray* getSecondaryColorIndices() const;
284
285        /** no longer supported.*/
286        inline void setFogCoordIndices(IndexArray* array);
287        inline const IndexArray* getFogCoordIndices() const;
288
289        /** no longer supported.*/
290        inline void setTexCoordIndices(unsigned int unit,IndexArray*);
291        inline const IndexArray* getTexCoordIndices(unsigned int unit) const;
292
293        /** no longer supported.*/
294        inline void setVertexAttribIndices(unsigned int index,IndexArray* array);
295        inline const IndexArray* getVertexAttribIndices(unsigned int index) const;
296#endif   
297};
298
299/** Convenience function to be used for creating quad geometry with texture coords.
300  * Tex coords go from left bottom (l,b) to right top (r,t).
301*/
302extern OSG_EXPORT Geometry* createTexturedQuadGeometry(const Vec3& corner,const Vec3& widthVec,const Vec3& heightVec, float l, float b, float r, float t);
303
304/** Convenience function to be used for creating quad geometry with texture coords.
305  * Tex coords go from bottom left (0,0) to top right (s,t).
306*/
307inline Geometry* createTexturedQuadGeometry(const Vec3& corner,const Vec3& widthVec,const Vec3& heightVec, float s=1.0f, float t=1.0f)
308{
309    return createTexturedQuadGeometry(corner,widthVec,heightVec, 0.0f, 0.0f, s, t);
310}
311
312
313#if defined(OSG_USE_DEPRECATED_GEOMETRY_METHODS)
314
315#include <osg/Notify>
316
317inline void Geometry::setVertexIndices(IndexArray* array)
318{
319    if (_vertexArray.valid()) { _vertexArray->setUserData(array); if (array)  _containsDeprecatedData = true; }
320    else { OSG_WARN<<"Geometry::setVertexIndicies(..) function failed as there is no vertex array to associate inidices with."<<std::endl; }
321}
322
323inline const IndexArray* Geometry::getVertexIndices() const
324{
325    if (_vertexArray.valid()) return dynamic_cast<IndexArray*>(_vertexArray->getUserData());
326    else return 0;
327}
328
329inline void Geometry::setNormalIndices(IndexArray* array)
330{
331    if (_normalArray.valid()) { _normalArray->setUserData(array); if (array)  _containsDeprecatedData = true; }
332    else { OSG_WARN<<"Geometry::setNormalIndicies(..) function failed as there is no normal array to associate inidices with."<<std::endl; }
333}
334inline const IndexArray* Geometry::getNormalIndices() const
335{
336    if (_normalArray.valid()) return dynamic_cast<IndexArray*>(_normalArray->getUserData());
337    else return 0;
338}
339
340inline void Geometry::setColorIndices(IndexArray* array)
341{
342    if (_colorArray.valid()) { _colorArray->setUserData(array); if (array)  _containsDeprecatedData = true; }
343    else { OSG_WARN<<"Geometry::setColorIndicies(..) function failed as there is no color array to associate inidices with."<<std::endl; }
344}
345inline const IndexArray* Geometry::getColorIndices() const
346{
347    if (_colorArray.valid()) return dynamic_cast<IndexArray*>(_colorArray->getUserData());
348    else return 0;
349}
350
351inline void Geometry::setSecondaryColorIndices(IndexArray* array)
352{
353    if (_secondaryColorArray.valid()) { _secondaryColorArray->setUserData(array); if (array)  _containsDeprecatedData = true; }
354    else { OSG_WARN<<"Geometry::setSecondaryColorArray(..) function failed as there is no secondary color array to associate inidices with."<<std::endl; }
355}
356inline const IndexArray* Geometry::getSecondaryColorIndices() const
357{
358    if (_secondaryColorArray.valid()) return dynamic_cast<IndexArray*>(_secondaryColorArray->getUserData());
359    else return 0;
360}
361
362inline void Geometry::setFogCoordIndices(IndexArray* array)
363{
364    if (_fogCoordArray.valid()) { _fogCoordArray->setUserData(array); if (array)  _containsDeprecatedData = true; }
365    else { OSG_WARN<<"Geometry::setFogCoordIndicies(..) function failed as there is no fog coord array to associate inidices with."<<std::endl; }
366}
367inline const IndexArray* Geometry::getFogCoordIndices() const
368{
369    if (_fogCoordArray.valid()) return dynamic_cast<IndexArray*>(_fogCoordArray->getUserData());
370    else return 0;
371}
372
373inline void Geometry::setTexCoordIndices(unsigned int unit,IndexArray* array)
374{
375    if (unit<_texCoordList.size() && _texCoordList[unit].valid()) { _texCoordList[unit]->setUserData(array); if (array)  _containsDeprecatedData = true; }
376    else { OSG_WARN<<"Geometry::setTexCoordIndices(..) function failed as there is no texcoord array to associate inidices with."<<std::endl; }
377}
378inline const IndexArray* Geometry::getTexCoordIndices(unsigned int unit) const
379{
380    if (unit<_texCoordList.size() && _texCoordList[unit].valid()) return dynamic_cast<IndexArray*>(_texCoordList[unit]->getUserData());
381    else return 0;
382}
383
384inline void Geometry::setVertexAttribIndices(unsigned int index,IndexArray* array)
385{
386    if (index<_vertexAttribList.size() && _vertexAttribList[index].valid()) { _vertexAttribList[index]->setUserData(array); if (array)  _containsDeprecatedData = true; }
387    else { OSG_WARN<<"Geometry::setVertexAttribIndices(..) function failed as there is no vertex attrib array to associate inidices with."<<std::endl; }
388}
389inline const IndexArray* Geometry::getVertexAttribIndices(unsigned int index) const
390{
391    if (index<_vertexAttribList.size() && _vertexAttribList[index].valid()) return dynamic_cast<IndexArray*>(_vertexAttribList[index]->getUserData());
392    else return 0;
393}
394#endif
395   
396}
397
398#endif
Note: See TracBrowser for help on using the browser.