Geometry 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. #ifndef OSG_GEOMETRY
  14. #define OSG_GEOMETRY 1
  15. #include <osg/Drawable>
  16. #include <osg/Vec2>
  17. #include <osg/Vec3>
  18. #include <osg/Vec4>
  19. #include <osg/Array>
  20. #include <osg/PrimitiveSet>
  21. // leave defined for OpenSceneGraph-3.2 release, post 3.2 associated methods will be only be available in deprecated_osg::Geometry
  22. #define OSG_DEPRECATED_GEOMETRY_BINDING 1
  23. namespace osg {
  24. class OSG_EXPORT Geometry : public Drawable
  25. {
  26. public:
  27. Geometry();
  28. /** Copy constructor using CopyOp to manage deep vs shallow copy. */
  29. Geometry(const Geometry& geometry,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
  30. META_Node(osg, Geometry);
  31. virtual Geometry* asGeometry() { return this; }
  32. virtual const Geometry* asGeometry() const { return this; }
  33. bool empty() const;
  34. typedef std::vector< osg::ref_ptr<osg::Array> > ArrayList;
  35. void setVertexArray(Array* array);
  36. Array* getVertexArray() { return _vertexArray.get(); }
  37. const Array* getVertexArray() const { return _vertexArray.get(); }
  38. void setNormalArray(Array* array) { setNormalArray(array, osg::Array::BIND_UNDEFINED); }
  39. void setNormalArray(Array* array, osg::Array::Binding binding);
  40. Array* getNormalArray() { return _normalArray.get(); }
  41. const Array* getNormalArray() const { return _normalArray.get(); }
  42. void setColorArray(Array* array) { setColorArray(array, osg::Array::BIND_UNDEFINED); }
  43. void setColorArray(Array* array, osg::Array::Binding binding);
  44. Array* getColorArray() { return _colorArray.get(); }
  45. const Array* getColorArray() const { return _colorArray.get(); }
  46. void setSecondaryColorArray(Array* array) { setSecondaryColorArray(array, osg::Array::BIND_UNDEFINED); }
  47. void setSecondaryColorArray(Array* array, osg::Array::Binding binding);
  48. Array* getSecondaryColorArray() { return _secondaryColorArray.get(); }
  49. const Array* getSecondaryColorArray() const { return _secondaryColorArray.get(); }
  50. void setFogCoordArray(Array* array) { setFogCoordArray(array, osg::Array::BIND_UNDEFINED); }
  51. void setFogCoordArray(Array* array, osg::Array::Binding binding);
  52. Array* getFogCoordArray() { return _fogCoordArray.get(); }
  53. const Array* getFogCoordArray() const { return _fogCoordArray.get(); }
  54. void setTexCoordArray(unsigned int unit, Array* array) { setTexCoordArray(unit, array, osg::Array::BIND_UNDEFINED); }
  55. void setTexCoordArray(unsigned int unit, Array* array, osg::Array::Binding binding);
  56. Array* getTexCoordArray(unsigned int unit);
  57. const Array* getTexCoordArray(unsigned int unit) const;
  58. unsigned int getNumTexCoordArrays() const { return static_cast<unsigned int>(_texCoordList.size()); }
  59. void setTexCoordArrayList(const ArrayList& arrrayList);
  60. ArrayList& getTexCoordArrayList() { return _texCoordList; }
  61. const ArrayList& getTexCoordArrayList() const { return _texCoordList; }
  62. void setVertexAttribArray(unsigned int index, Array* array) { setVertexAttribArray(index, array, osg::Array::BIND_UNDEFINED); }
  63. void setVertexAttribArray(unsigned int index, Array* array, osg::Array::Binding binding);
  64. Array *getVertexAttribArray(unsigned int index);
  65. const Array *getVertexAttribArray(unsigned int index) const;
  66. unsigned int getNumVertexAttribArrays() const { return static_cast<unsigned int>(_vertexAttribList.size()); }
  67. void setVertexAttribArrayList(const ArrayList& arrayList);
  68. ArrayList& getVertexAttribArrayList() { return _vertexAttribList; }
  69. const ArrayList& getVertexAttribArrayList() const { return _vertexAttribList; }
  70. typedef std::vector< ref_ptr<PrimitiveSet> > PrimitiveSetList;
  71. void setPrimitiveSetList(const PrimitiveSetList& primitives);
  72. PrimitiveSetList& getPrimitiveSetList() { return _primitives; }
  73. const PrimitiveSetList& getPrimitiveSetList() const { return _primitives; }
  74. unsigned int getNumPrimitiveSets() const { return static_cast<unsigned int>(_primitives.size()); }
  75. PrimitiveSet* getPrimitiveSet(unsigned int pos) { return _primitives[pos].get(); }
  76. const PrimitiveSet* getPrimitiveSet(unsigned int pos) const { return _primitives[pos].get(); }
  77. /** Add a primitive set to the geometry. */
  78. bool addPrimitiveSet(PrimitiveSet* primitiveset);
  79. /** Set a primitive set to the specified position in geometry's primitive set list. */
  80. bool setPrimitiveSet(unsigned int i,PrimitiveSet* primitiveset);
  81. /** Insert a primitive set to the specified position in geometry's primitive set list. */
  82. bool insertPrimitiveSet(unsigned int i,PrimitiveSet* primitiveset);
  83. /** Remove primitive set(s) from the specified position in geometry's primitive set list. */
  84. bool removePrimitiveSet(unsigned int i,unsigned int numElementsToRemove=1);
  85. /** Get the index number of a primitive set, return a value between
  86. * 0 and getNumPrimitiveSet()-1 if found, if not found then return getNumPrimitiveSet().
  87. * When checking for a valid find value use if ((value=geometry->getPrimitiveSetIndex(primitive))!=geometry.getNumPrimitiveSet())
  88. */
  89. unsigned int getPrimitiveSetIndex(const PrimitiveSet* primitiveset) const;
  90. /** Convenience method that checks all the vertex arrays to make sure that the buffer objects are all assigned appropriate.*/
  91. void configureBufferObjects();
  92. /** return true if any arrays are shared.*/
  93. bool containsSharedArrays() const;
  94. /** duplicate any shared arrays.*/
  95. void duplicateSharedArrays();
  96. /** When set to true, ignore the setUseDisplayList() settings, and hints to the drawImplementation
  97. method to use OpenGL vertex buffer objects for rendering.*/
  98. virtual void setUseVertexBufferObjects(bool flag);
  99. /** Force a recompile on next draw() of any OpenGL objects associated with this geoset.*/
  100. virtual void dirtyGLObjects();
  101. /** Resize any per context GLObject buffers to specified size. */
  102. virtual void resizeGLObjectBuffers(unsigned int maxSize);
  103. /** If State is non-zero, this function releases OpenGL objects for
  104. * the specified graphics context. Otherwise, releases OpenGL objects
  105. * for all graphics contexts. */
  106. virtual void releaseGLObjects(State* state=0) const;
  107. bool getArrayList(ArrayList& arrayList) const;
  108. typedef std::vector<osg::DrawElements*> DrawElementsList;
  109. bool getDrawElementsList(DrawElementsList& drawElementsList) const;
  110. osg::VertexBufferObject* getOrCreateVertexBufferObject();
  111. osg::ElementBufferObject* getOrCreateElementBufferObject();
  112. /** Return the estimated size of GLObjects (display lists/vertex buffer objects) that are associated with this drawable.
  113. * This size is used a hint for reuse of deleted display lists/vertex buffer objects. */
  114. virtual unsigned int getGLObjectSizeHint() const;
  115. /** Immediately compile this \c Drawable into an OpenGL Display List/VertexBufferObjects.
  116. * @note Operation is ignored if \c _useDisplayList is \c false or VertexBufferObjects are not used.
  117. */
  118. virtual void compileGLObjects(RenderInfo& renderInfo) const;
  119. /** Draw Geometry directly ignoring an OpenGL display list which could be attached.
  120. * This is the internal draw method which does the drawing itself,
  121. * and is the method to override when deriving from Geometry for user-drawn objects.
  122. */
  123. virtual void drawImplementation(RenderInfo& renderInfo) const;
  124. /** Set up the vertex arrays for the purpose of rendering, called by drawImplemtation() prior to it calling drawPrimitivesImplementation().*/
  125. void drawVertexArraysImplementation(RenderInfo& renderInfo) const;
  126. /** dispatch the primitives to OpenGL, called by drawImplemtation() after calling drawVertexArraysImplementation().*/
  127. void drawPrimitivesImplementation(RenderInfo& renderInfo) const;
  128. /** Return true, osg::Geometry does support accept(Drawable::AttributeFunctor&). */
  129. virtual bool supports(const Drawable::AttributeFunctor&) const { return true; }
  130. /** Accept an Drawable::AttributeFunctor and call its methods to tell it about the internal attributes that this Drawable has. */
  131. virtual void accept(Drawable::AttributeFunctor& af);
  132. /** Return true, osg::Geometry does support accept(Drawable::ConstAttributeFunctor&). */
  133. virtual bool supports(const Drawable::ConstAttributeFunctor&) const { return true; }
  134. /** Accept a Drawable::ConstAttributeFunctor and call its methods to tell it about the internal attributes that this Drawable has. */
  135. virtual void accept(Drawable::ConstAttributeFunctor& af) const;
  136. /** Return true, osg::Geometry does support accept(PrimitiveFunctor&). */
  137. virtual bool supports(const PrimitiveFunctor&) const { return true; }
  138. /** Accept a PrimitiveFunctor and call its methods to tell it about the internal primitives that this Drawable has. */
  139. virtual void accept(PrimitiveFunctor& pf) const;
  140. /** Return true, osg::Geometry does support accept(PrimitiveIndexFunctor&). */
  141. virtual bool supports(const PrimitiveIndexFunctor&) const { return true; }
  142. /** Accept a PrimitiveFunctor and call its methods to tell it about the internal primitives that this Drawable has. */
  143. virtual void accept(PrimitiveIndexFunctor& pf) const;
  144. protected:
  145. Geometry& operator = (const Geometry&) { return *this;}
  146. virtual ~Geometry();
  147. void addVertexBufferObjectIfRequired(osg::Array* array);
  148. void addElementBufferObjectIfRequired(osg::PrimitiveSet* primitiveSet);
  149. PrimitiveSetList _primitives;
  150. osg::ref_ptr<Array> _vertexArray;
  151. osg::ref_ptr<Array> _normalArray;
  152. osg::ref_ptr<Array> _colorArray;
  153. osg::ref_ptr<Array> _secondaryColorArray;
  154. osg::ref_ptr<Array> _fogCoordArray;
  155. ArrayList _texCoordList;
  156. ArrayList _vertexAttribList;
  157. bool _containsDeprecatedData;
  158. virtual VertexArrayState* createVertexArrayStateImplementation(RenderInfo& renderInfo) const;
  159. public:
  160. /** Return true if the deprecated use array indices or BIND_PER_PRIMITIVE binding has been assigned to arrays.*/
  161. bool containsDeprecatedData() const { return _containsDeprecatedData; }
  162. /** fallback for deprecated functionality. Return true if the Geometry contains any array indices or BIND_PER_PRIMITIVE arrays. */
  163. bool checkForDeprecatedData();
  164. /** fallback for deprecated functionality. Removes any array indices and BIND_PER_PRIMITIVE arrays.*/
  165. void fixDeprecatedData();
  166. #if defined(OSG_DEPRECATED_GEOMETRY_BINDING)
  167. /** deprecated, Same values as Array::Binding.*/
  168. enum AttributeBinding
  169. {
  170. BIND_OFF=0,
  171. BIND_OVERALL=1,
  172. BIND_PER_PRIMITIVE_SET=2,
  173. BIND_PER_VERTEX=4
  174. };
  175. /** deprecated, use array->set*Binding(..). */
  176. void setNormalBinding(AttributeBinding ab);
  177. void setColorBinding(AttributeBinding ab);
  178. void setSecondaryColorBinding(AttributeBinding ab);
  179. void setFogCoordBinding(AttributeBinding ab);
  180. void setVertexAttribBinding(unsigned int index,AttributeBinding ab);
  181. /** deprecated, use array->get*Binding(..). */
  182. AttributeBinding getNormalBinding() const;
  183. AttributeBinding getColorBinding() const;
  184. AttributeBinding getSecondaryColorBinding() const;
  185. AttributeBinding getFogCoordBinding() const;
  186. AttributeBinding getVertexAttribBinding(unsigned int index) const;
  187. /** deprecated, use array->set*Normalize(..). */
  188. void setVertexAttribNormalize(unsigned int index,GLboolean norm);
  189. /** deprecated, use array->get*Normalize(..). */
  190. GLboolean getVertexAttribNormalize(unsigned int index) const;
  191. #endif
  192. };
  193. /** Convenience visitor for making sure that any BufferObjects that might be required are set up in the scene graph.*/
  194. class ConfigureBufferObjectsVisitor : public osg::NodeVisitor
  195. {
  196. public:
  197. ConfigureBufferObjectsVisitor():
  198. osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) {}
  199. void apply(osg::Geometry& geometry)
  200. {
  201. geometry.configureBufferObjects();
  202. }
  203. };
  204. /** Convenience function to be used for creating quad geometry with texture coords.
  205. * Tex coords go from left bottom (l,b) to right top (r,t).
  206. */
  207. extern OSG_EXPORT Geometry* createTexturedQuadGeometry(const Vec3& corner,const Vec3& widthVec,const Vec3& heightVec, float l, float b, float r, float t);
  208. /** Convenience function to be used for creating quad geometry with texture coords.
  209. * Tex coords go from bottom left (0,0) to top right (s,t).
  210. */
  211. inline Geometry* createTexturedQuadGeometry(const Vec3& corner,const Vec3& widthVec,const Vec3& heightVec, float s=1.0f, float t=1.0f)
  212. {
  213. return createTexturedQuadGeometry(corner,widthVec,heightVec, 0.0f, 0.0f, s, t);
  214. }
  215. } // namespace osg
  216. #endif