1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
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.
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.
17#include <osg/Drawable>
22#include <osg/PrimitiveSet>
24// leave defined for OpenSceneGraph-3.2 release, post 3.2 associated methods will be only be available in deprecated_osg::Geometry
25#define OSG_DEPRECATED_GEOMETRY_BINDING 1
30class OSG_EXPORT Geometry : public Drawable
36 /** Copy constructor using CopyOp to manage deep vs shallow copy. */
37 Geometry(const Geometry& geometry,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
39 META_Node(osg, Geometry);
41 virtual Geometry* asGeometry() { return this; }
42 virtual const Geometry* asGeometry() const { return this; }
46 typedef std::vector< osg::ref_ptr<osg::Array> > ArrayList;
48 void setVertexArray(Array* array);
49 Array* getVertexArray() { return _vertexArray.get(); }
50 const Array* getVertexArray() const { return _vertexArray.get(); }
53 void setNormalArray(Array* array) { setNormalArray(array, osg::Array::BIND_UNDEFINED); }
54 void setNormalArray(Array* array, osg::Array::Binding binding);
55 Array* getNormalArray() { return _normalArray.get(); }
56 const Array* getNormalArray() const { return _normalArray.get(); }
60 void setColorArray(Array* array) { setColorArray(array, osg::Array::BIND_UNDEFINED); }
61 void setColorArray(Array* array, osg::Array::Binding binding);
62 Array* getColorArray() { return _colorArray.get(); }
63 const Array* getColorArray() const { return _colorArray.get(); }
67 void setSecondaryColorArray(Array* array) { setSecondaryColorArray(array, osg::Array::BIND_UNDEFINED); }
68 void setSecondaryColorArray(Array* array, osg::Array::Binding binding);
69 Array* getSecondaryColorArray() { return _secondaryColorArray.get(); }
70 const Array* getSecondaryColorArray() const { return _secondaryColorArray.get(); }
73 void setFogCoordArray(Array* array) { setFogCoordArray(array, osg::Array::BIND_UNDEFINED); }
74 void setFogCoordArray(Array* array, osg::Array::Binding binding);
75 Array* getFogCoordArray() { return _fogCoordArray.get(); }
76 const Array* getFogCoordArray() const { return _fogCoordArray.get(); }
79 void setTexCoordArray(unsigned int unit, Array* array) { setTexCoordArray(unit, array, osg::Array::BIND_UNDEFINED); }
80 void setTexCoordArray(unsigned int unit, Array* array, osg::Array::Binding binding);
81 Array* getTexCoordArray(unsigned int unit);
82 const Array* getTexCoordArray(unsigned int unit) const;
84 unsigned int getNumTexCoordArrays() const { return static_cast<unsigned int>(_texCoordList.size()); }
85 void setTexCoordArrayList(const ArrayList& arrrayList);
86 ArrayList& getTexCoordArrayList() { return _texCoordList; }
87 const ArrayList& getTexCoordArrayList() const { return _texCoordList; }
89 void setVertexAttribArray(unsigned int index, Array* array) { setVertexAttribArray(index, array, osg::Array::BIND_UNDEFINED); }
90 void setVertexAttribArray(unsigned int index, Array* array, osg::Array::Binding binding);
91 Array *getVertexAttribArray(unsigned int index);
92 const Array *getVertexAttribArray(unsigned int index) const;
95 unsigned int getNumVertexAttribArrays() const { return static_cast<unsigned int>(_vertexAttribList.size()); }
96 void setVertexAttribArrayList(const ArrayList& arrayList);
97 ArrayList& getVertexAttribArrayList() { return _vertexAttribList; }
98 const ArrayList& getVertexAttribArrayList() const { return _vertexAttribList; }
102 typedef std::vector< ref_ptr<PrimitiveSet> > PrimitiveSetList;
104 void setPrimitiveSetList(const PrimitiveSetList& primitives);
106 PrimitiveSetList& getPrimitiveSetList() { return _primitives; }
107 const PrimitiveSetList& getPrimitiveSetList() const { return _primitives; }
109 unsigned int getNumPrimitiveSets() const { return static_cast<unsigned int>(_primitives.size()); }
110 PrimitiveSet* getPrimitiveSet(unsigned int pos) { return _primitives[pos].get(); }
111 const PrimitiveSet* getPrimitiveSet(unsigned int pos) const { return _primitives[pos].get(); }
113 /** Add a primitive set to the geometry. */
114 bool addPrimitiveSet(PrimitiveSet* primitiveset);
116 /** Set a primitive set to the specified position in geometry's primitive set list. */
117 bool setPrimitiveSet(unsigned int i,PrimitiveSet* primitiveset);
119 /** Insert a primitive set to the specified position in geometry's primitive set list. */
120 bool insertPrimitiveSet(unsigned int i,PrimitiveSet* primitiveset);
122 /** Remove primitive set(s) from the specified position in geometry's primitive set list. */
123 bool removePrimitiveSet(unsigned int i,unsigned int numElementsToRemove=1);
125 /** Get the index number of a primitive set, return a value between
126 * 0 and getNumPrimitiveSet()-1 if found, if not found then return getNumPrimitiveSet().
127 * When checking for a valid find value use if ((value=geometry->getPrimitiveSetIndex(primitive))!=geometry.getNumPrimitiveSet())
129 unsigned int getPrimitiveSetIndex(const PrimitiveSet* primitiveset) const;
132 /** Convenience method that checks all the vertex arrays to make sure that the buffer objects are all assigned appropriate.*/
133 void configureBufferObjects();
135 /** return true if any arrays are shared.*/
136 bool containsSharedArrays() const;
138 /** duplicate any shared arrays.*/
139 void duplicateSharedArrays();
142 /** When set to true, ignore the setUseDisplayList() settings, and hints to the drawImplementation
143 method to use OpenGL vertex buffer objects for rendering.*/
144 virtual void setUseVertexBufferObjects(bool flag);
146 /** Force a recompile on next draw() of any OpenGL objects associated with this geoset.*/
147 virtual void dirtyGLObjects();
150 /** Resize any per context GLObject buffers to specified size. */
151 virtual void resizeGLObjectBuffers(unsigned int maxSize);
153 /** If State is non-zero, this function releases OpenGL objects for
154 * the specified graphics context. Otherwise, releases OpenGL objects
155 * for all graphics contexts. */
156 virtual void releaseGLObjects(State* state=0) const;
158 bool getArrayList(ArrayList& arrayList) const;
160 typedef std::vector<osg::DrawElements*> DrawElementsList;
161 bool getDrawElementsList(DrawElementsList& drawElementsList) const;
163 osg::VertexBufferObject* getOrCreateVertexBufferObject();
164 osg::ElementBufferObject* getOrCreateElementBufferObject();
167 /** Return the estimated size of GLObjects (display lists/vertex buffer objects) that are associated with this drawable.
168 * This size is used a hint for reuse of deleted display lists/vertex buffer objects. */
169 virtual unsigned int getGLObjectSizeHint() const;
171 /** Immediately compile this \c Drawable into an OpenGL Display List/VertexBufferObjects.
172 * @note Operation is ignored if \c _useDisplayList is \c false or VertexBufferObjects are not used.
174 virtual void compileGLObjects(RenderInfo& renderInfo) const;
176 /** Draw Geometry directly ignoring an OpenGL display list which could be attached.
177 * This is the internal draw method which does the drawing itself,
178 * and is the method to override when deriving from Geometry for user-drawn objects.
180 virtual void drawImplementation(RenderInfo& renderInfo) const;
183 /** Set up the vertex arrays for the purpose of rendering, called by drawImplemtation() prior to it calling drawPrimitivesImplementation().*/
184 void drawVertexArraysImplementation(RenderInfo& renderInfo) const;
186 /** dispatch the primitives to OpenGL, called by drawImplemtation() after calling drawVertexArraysImplementation().*/
187 void drawPrimitivesImplementation(RenderInfo& renderInfo) const;
190 /** Return true, osg::Geometry does support accept(Drawable::AttributeFunctor&). */
191 virtual bool supports(const Drawable::AttributeFunctor&) const { return true; }
193 /** Accept an Drawable::AttributeFunctor and call its methods to tell it about the internal attributes that this Drawable has. */
194 virtual void accept(Drawable::AttributeFunctor& af);
196 /** Return true, osg::Geometry does support accept(Drawable::ConstAttributeFunctor&). */
197 virtual bool supports(const Drawable::ConstAttributeFunctor&) const { return true; }
199 /** Accept a Drawable::ConstAttributeFunctor and call its methods to tell it about the internal attributes that this Drawable has. */
200 virtual void accept(Drawable::ConstAttributeFunctor& af) const;
202 /** Return true, osg::Geometry does support accept(PrimitiveFunctor&). */
203 virtual bool supports(const PrimitiveFunctor&) const { return true; }
205 /** Accept a PrimitiveFunctor and call its methods to tell it about the internal primitives that this Drawable has. */
206 virtual void accept(PrimitiveFunctor& pf) const;
208 /** Return true, osg::Geometry does support accept(PrimitiveIndexFunctor&). */
209 virtual bool supports(const PrimitiveIndexFunctor&) const { return true; }
211 /** Accept a PrimitiveFunctor and call its methods to tell it about the internal primitives that this Drawable has. */
212 virtual void accept(PrimitiveIndexFunctor& pf) const;
217 Geometry& operator = (const Geometry&) { return *this;}
222 void addVertexBufferObjectIfRequired(osg::Array* array);
223 void addElementBufferObjectIfRequired(osg::PrimitiveSet* primitiveSet);
225 PrimitiveSetList _primitives;
226 osg::ref_ptr<Array> _vertexArray;
227 osg::ref_ptr<Array> _normalArray;
228 osg::ref_ptr<Array> _colorArray;
229 osg::ref_ptr<Array> _secondaryColorArray;
230 osg::ref_ptr<Array> _fogCoordArray;
231 ArrayList _texCoordList;
232 ArrayList _vertexAttribList;
234 bool _containsDeprecatedData;
236 virtual VertexArrayState* createVertexArrayStateImplementation(RenderInfo& renderInfo) const;
241 /** Return true if the deprecated use array indices or BIND_PER_PRIMITIVE binding has been assigned to arrays.*/
242 bool containsDeprecatedData() const { return _containsDeprecatedData; }
244 /** fallback for deprecated functionality. Return true if the Geometry contains any array indices or BIND_PER_PRIMITIVE arrays. */
245 bool checkForDeprecatedData();
247 /** fallback for deprecated functionality. Removes any array indices and BIND_PER_PRIMITIVE arrays.*/
248 void fixDeprecatedData();
250#if defined(OSG_DEPRECATED_GEOMETRY_BINDING)
251 /** deprecated, Same values as Array::Binding.*/
252 enum AttributeBinding
256 BIND_PER_PRIMITIVE_SET=2,
260 /** deprecated, use array->set*Binding(..). */
261 void setNormalBinding(AttributeBinding ab);
262 void setColorBinding(AttributeBinding ab);
263 void setSecondaryColorBinding(AttributeBinding ab);
264 void setFogCoordBinding(AttributeBinding ab);
265 void setVertexAttribBinding(unsigned int index,AttributeBinding ab);
267 /** deprecated, use array->get*Binding(..). */
268 AttributeBinding getNormalBinding() const;
269 AttributeBinding getColorBinding() const;
270 AttributeBinding getSecondaryColorBinding() const;
271 AttributeBinding getFogCoordBinding() const;
272 AttributeBinding getVertexAttribBinding(unsigned int index) const;
274 /** deprecated, use array->set*Normalize(..). */
275 void setVertexAttribNormalize(unsigned int index,GLboolean norm);
277 /** deprecated, use array->get*Normalize(..). */
278 GLboolean getVertexAttribNormalize(unsigned int index) const;
282/** Convenience visitor for making sure that any BufferObjects that might be required are set up in the scene graph.*/
283class ConfigureBufferObjectsVisitor : public osg::NodeVisitor
286 ConfigureBufferObjectsVisitor():
287 osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) {}
289 void apply(osg::Geometry& geometry)
291 geometry.configureBufferObjects();
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).
300extern OSG_EXPORT Geometry* createTexturedQuadGeometry(const Vec3& corner,const Vec3& widthVec,const Vec3& heightVec, float l, float b, float r, float t);
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).
305inline Geometry* createTexturedQuadGeometry(const Vec3& corner,const Vec3& widthVec,const Vec3& heightVec, float s=1.0f, float t=1.0f)
307 return createTexturedQuadGeometry(corner,widthVec,heightVec, 0.0f, 0.0f, s, t);