openscenegraph
Geometry
Go to the documentation of this file.
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
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
26
27namespace osg {
28
29
30class OSG_EXPORT Geometry : public Drawable
31{
32 public:
33
34 Geometry();
35
36 /** Copy constructor using CopyOp to manage deep vs shallow copy. */
37 Geometry(const Geometry& geometry,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
38
39 META_Node(osg, 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) { 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(); }
57
58
59
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(); }
64
65
66
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(); }
71
72
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(); }
77
78
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;
83
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; }
88
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;
93
94
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; }
99
100
101
102 typedef std::vector< ref_ptr<PrimitiveSet> > PrimitiveSetList;
103
104 void setPrimitiveSetList(const PrimitiveSetList& primitives);
105
106 PrimitiveSetList& getPrimitiveSetList() { return _primitives; }
107 const PrimitiveSetList& getPrimitiveSetList() const { return _primitives; }
108
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(); }
112
113 /** Add a primitive set to the geometry. */
114 bool addPrimitiveSet(PrimitiveSet* primitiveset);
115
116 /** Set a primitive set to the specified position in geometry's primitive set list. */
117 bool setPrimitiveSet(unsigned int i,PrimitiveSet* primitiveset);
118
119 /** Insert a primitive set to the specified position in geometry's primitive set list. */
120 bool insertPrimitiveSet(unsigned int i,PrimitiveSet* primitiveset);
121
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);
124
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())
128 */
129 unsigned int getPrimitiveSetIndex(const PrimitiveSet* primitiveset) const;
130
131
132 /** Convenience method that checks all the vertex arrays to make sure that the buffer objects are all assigned appropriate.*/
133 void configureBufferObjects();
134
135 /** return true if any arrays are shared.*/
136 bool containsSharedArrays() const;
137
138 /** duplicate any shared arrays.*/
139 void duplicateSharedArrays();
140
141
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);
145
146 /** Force a recompile on next draw() of any OpenGL objects associated with this geoset.*/
147 virtual void dirtyGLObjects();
148
149
150 /** Resize any per context GLObject buffers to specified size. */
151 virtual void resizeGLObjectBuffers(unsigned int maxSize);
152
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;
157
158 bool getArrayList(ArrayList& arrayList) const;
159
160 typedef std::vector<osg::DrawElements*> DrawElementsList;
161 bool getDrawElementsList(DrawElementsList& drawElementsList) const;
162
163 osg::VertexBufferObject* getOrCreateVertexBufferObject();
164 osg::ElementBufferObject* getOrCreateElementBufferObject();
165
166
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;
170
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.
173 */
174 virtual void compileGLObjects(RenderInfo& renderInfo) const;
175
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.
179 */
180 virtual void drawImplementation(RenderInfo& renderInfo) const;
181
182
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;
185
186 /** dispatch the primitives to OpenGL, called by drawImplemtation() after calling drawVertexArraysImplementation().*/
187 void drawPrimitivesImplementation(RenderInfo& renderInfo) const;
188
189
190 /** Return true, osg::Geometry does support accept(Drawable::AttributeFunctor&). */
191 virtual bool supports(const Drawable::AttributeFunctor&) const { return true; }
192
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);
195
196 /** Return true, osg::Geometry does support accept(Drawable::ConstAttributeFunctor&). */
197 virtual bool supports(const Drawable::ConstAttributeFunctor&) const { return true; }
198
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;
201
202 /** Return true, osg::Geometry does support accept(PrimitiveFunctor&). */
203 virtual bool supports(const PrimitiveFunctor&) const { return true; }
204
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;
207
208 /** Return true, osg::Geometry does support accept(PrimitiveIndexFunctor&). */
209 virtual bool supports(const PrimitiveIndexFunctor&) const { return true; }
210
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;
213
214
215 protected:
216
217 Geometry& operator = (const Geometry&) { return *this;}
218
219 virtual ~Geometry();
220
221
222 void addVertexBufferObjectIfRequired(osg::Array* array);
223 void addElementBufferObjectIfRequired(osg::PrimitiveSet* primitiveSet);
224
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;
233
234 bool _containsDeprecatedData;
235
236 virtual VertexArrayState* createVertexArrayStateImplementation(RenderInfo& renderInfo) const;
237
238 public:
239
240
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; }
243
244 /** fallback for deprecated functionality. Return true if the Geometry contains any array indices or BIND_PER_PRIMITIVE arrays. */
245 bool checkForDeprecatedData();
246
247 /** fallback for deprecated functionality. Removes any array indices and BIND_PER_PRIMITIVE arrays.*/
248 void fixDeprecatedData();
249
250#if defined(OSG_DEPRECATED_GEOMETRY_BINDING)
251 /** deprecated, Same values as Array::Binding.*/
252 enum AttributeBinding
253 {
254 BIND_OFF=0,
255 BIND_OVERALL=1,
256 BIND_PER_PRIMITIVE_SET=2,
257 BIND_PER_VERTEX=4
258 };
259
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);
266
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;
273
274 /** deprecated, use array->set*Normalize(..). */
275 void setVertexAttribNormalize(unsigned int index,GLboolean norm);
276
277 /** deprecated, use array->get*Normalize(..). */
278 GLboolean getVertexAttribNormalize(unsigned int index) const;
279#endif
280};
281
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
284{
285public:
286 ConfigureBufferObjectsVisitor():
287 osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) {}
288
289 void apply(osg::Geometry& geometry)
290 {
291 geometry.configureBufferObjects();
292 }
293};
294
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} // namespace osg
311
312#endif