Drawable 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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_DRAWABLE
  14. #define OSG_DRAWABLE 1
  15. #include <osg/BoundingBox>
  16. #include <osg/Shape>
  17. #include <osg/BufferObject>
  18. #include <osg/PrimitiveSet>
  19. #include <osg/RenderInfo>
  20. #include <osg/Group>
  21. #ifndef GL_NV_occlusion_query
  22. #define GL_OCCLUSION_TEST_HP 0x8165
  23. #define GL_OCCLUSION_TEST_RESULT_HP 0x8166
  24. #define GL_PIXEL_COUNTER_BITS_NV 0x8864
  25. #define GL_CURRENT_OCCLUSION_QUERY_ID_NV 0x8865
  26. #define GL_PIXEL_COUNT_NV 0x8866
  27. #define GL_PIXEL_COUNT_AVAILABLE_NV 0x8867
  28. #endif
  29. #ifndef GL_ARB_occlusion_query
  30. #define GL_SAMPLES_PASSED_ARB 0x8914
  31. #define GL_QUERY_COUNTER_BITS_ARB 0x8864
  32. #define GL_CURRENT_QUERY_ARB 0x8865
  33. #define GL_QUERY_RESULT_ARB 0x8866
  34. #define GL_QUERY_RESULT_AVAILABLE_ARB 0x8867
  35. #endif
  36. #ifndef GL_TIME_ELAPSED
  37. #define GL_TIME_ELAPSED 0x88BF
  38. #define GL_TIMESTAMP 0x8E28
  39. #endif
  40. #ifndef GL_QUERY_RESULT
  41. #define GL_QUERY_RESULT 0x8866
  42. #define GL_QUERY_RESULT_AVAILABLE 0x8867
  43. #endif
  44. #define INLINE_DRAWABLE_DRAW
  45. namespace osg {
  46. class Vec2f;
  47. class Vec3f;
  48. class Vec4f;
  49. class Vec4ub;
  50. class Geometry;
  51. class NodeVisitor;
  52. class ArrayDispatchers;
  53. /** Pure virtual base class for drawable geometry. In OSG, everything that can
  54. * be rendered is implemented as a class derived from \c Drawable. The
  55. * \c Drawable class contains no drawing primitives, since these are provided
  56. * by subclasses such as \c osg::Geometry.
  57. * <p>Notice that a \c Drawable is not a \c Node, and therefore it cannot be
  58. * directly added to a scene graph. Instead, <tt>Drawable</tt>s are attached to
  59. * <tt>Geode</tt>s, which are scene graph nodes.
  60. * <p>The OpenGL state that must be used when rendering a \c Drawable is
  61. * represented by a \c StateSet. Since a \c Drawable has a reference
  62. * (\c osg::ref_ptr) to a \c StateSet, <tt>StateSet</tt>s can be shared between
  63. * different <tt>Drawable</tt>s. In fact, sharing <tt>StateSet</tt>s is a good
  64. * way to improve performance, since this allows OSG to reduce the number of
  65. * expensive changes in the OpenGL state.
  66. * <p>Finally, <tt>Drawable</tt>s can also be shared between different
  67. * <tt>Geode</tt>s, so that the same geometry (loaded to memory just once) can
  68. * be used in different parts of the scene graph.
  69. */
  70. class OSG_EXPORT Drawable : public Node
  71. {
  72. public:
  73. Drawable();
  74. /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
  75. Drawable(const Drawable& drawable,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
  76. META_Node(osg, Drawable);
  77. /** Convert 'this' into a Drawable pointer if Object is a Drawable, otherwise return 0.
  78. * Equivalent to dynamic_cast<Drawable*>(this).*/
  79. virtual Drawable* asDrawable() { return this; }
  80. /** convert 'const this' into a const Drawable pointer if Object is a Drawable, otherwise return 0.
  81. * Equivalent to dynamic_cast<const Drawable*>(this).*/
  82. virtual const Drawable* asDrawable() const { return this; }
  83. /** Compute the DataVariance based on an assessment of callback etc.*/
  84. virtual void computeDataVariance();
  85. /** Get the list of matrices that transform this node from local coordinates to world coordinates.
  86. * The optional Node* haltTraversalAtNode allows the user to prevent traversal beyond a specified node. */
  87. MatrixList getWorldMatrices(const osg::Node* haltTraversalAtNode=0) const;
  88. /** Set the initial bounding volume to use when computing the overall bounding volume.*/
  89. void setInitialBound(const osg::BoundingBox& bbox) { _initialBoundingBox = bbox; dirtyBound(); }
  90. /** Set the initial bounding volume to use when computing the overall bounding volume.*/
  91. const BoundingBox& getInitialBound() const { return _initialBoundingBox; }
  92. inline const BoundingSphere& getBound() const
  93. {
  94. if(!_boundingSphereComputed) getBoundingBox();
  95. return _boundingSphere;
  96. }
  97. /** Get BoundingBox of Drawable.
  98. * If the BoundingBox is not up to date then its updated via an internal call to computeBond().
  99. */
  100. inline const BoundingBox& getBoundingBox() const
  101. {
  102. if(!_boundingSphereComputed)
  103. {
  104. _boundingBox = _initialBoundingBox;
  105. if (_computeBoundingBoxCallback.valid())
  106. _boundingBox.expandBy(_computeBoundingBoxCallback->computeBound(*this));
  107. else
  108. _boundingBox.expandBy(computeBoundingBox());
  109. if(_boundingBox.valid()){
  110. _boundingSphere.set(_boundingBox.center(), _boundingBox.radius());
  111. } else {
  112. _boundingSphere.init();
  113. }
  114. _boundingSphereComputed = true;
  115. }
  116. return _boundingBox;
  117. }
  118. /** Compute the bounding sphere around Drawables's geometry.*/
  119. virtual BoundingSphere computeBound() const;
  120. /** Compute the bounding box around Drawables's geometry.*/
  121. virtual BoundingBox computeBoundingBox() const;
  122. /** Callback to allow users to override the default computation of bounding volume. */
  123. struct ComputeBoundingBoxCallback : public osg::Object
  124. {
  125. ComputeBoundingBoxCallback() {}
  126. ComputeBoundingBoxCallback(const ComputeBoundingBoxCallback& org,const CopyOp& copyop):
  127. Object(org, copyop) {}
  128. META_Object(osg,ComputeBoundingBoxCallback);
  129. virtual BoundingBox computeBound(const osg::Drawable&) const { return BoundingBox(); }
  130. };
  131. /** Set the compute bound callback to override the default computeBound.*/
  132. void setComputeBoundingBoxCallback(ComputeBoundingBoxCallback* callback) { _computeBoundingBoxCallback = callback; }
  133. /** Get the compute bound callback.*/
  134. ComputeBoundingBoxCallback* getComputeBoundingBoxCallback() { return _computeBoundingBoxCallback.get(); }
  135. /** Get the const compute bound callback.*/
  136. const ComputeBoundingBoxCallback* getComputeBoundingBoxCallback() const { return _computeBoundingBoxCallback.get(); }
  137. /** Set the Shape of the \c Drawable. The shape can be used to
  138. * speed up collision detection or as a guide for procedural
  139. * geometry generation.
  140. * @see osg::Shape.
  141. */
  142. virtual void setShape(Shape* shape) { _shape = shape; }
  143. template<class T> void setShape(const ref_ptr<T>& shape) { setShape(shape.get()); }
  144. /** Get the Shape of the Drawable.*/
  145. inline Shape* getShape() { return _shape.get(); }
  146. /** Get the const Shape of the const Drawable.*/
  147. inline const Shape* getShape() const { return _shape.get(); }
  148. /** Set the drawable so that it can or cannot be used in conjunction with OpenGL
  149. * display lists. When set to true, calls to Drawable::setUseDisplayList,
  150. * whereas when set to false, no display lists can be created and calls
  151. * to setUseDisplayList are ignored, and a warning is produced. The latter
  152. * is typically used to guard against the switching on of display lists
  153. * on objects with dynamic internal data such as continuous Level of Detail
  154. * algorithms.*/
  155. void setSupportsDisplayList(bool flag);
  156. /** Get whether display lists are supported for this drawable instance.*/
  157. inline bool getSupportsDisplayList() const { return _supportsDisplayList; }
  158. /** When set to true, force the draw method to use OpenGL Display List for rendering.
  159. If false, rendering directly. If the display list has not been compiled
  160. already, the next call to draw will automatically create the display list.*/
  161. void setUseDisplayList(bool flag);
  162. /** Return whether OpenGL display lists are being used for rendering.*/
  163. inline bool getUseDisplayList() const { return _useDisplayList; }
  164. /** Return OpenGL display list for specified contextID. */
  165. inline GLuint& getDisplayList(unsigned int contextID) const { return _globjList[contextID]; }
  166. /** When set to true, ignore the setUseDisplayList() settings, and hints to the drawImplementation
  167. method to use OpenGL vertex buffer objects for rendering.*/
  168. virtual void setUseVertexBufferObjects(bool flag);
  169. /** Return whether OpenGL vertex buffer objects should be used when supported by OpenGL driver.*/
  170. inline bool getUseVertexBufferObjects() const { return _useVertexBufferObjects; }
  171. /** Set whether to use a local VertexArrayObject for this Drawable.*/
  172. void setUseVertexArrayObject(bool flag);
  173. /** Return whether to use a local VertexArrayObject for this Drawable.*/
  174. bool getUseVertexArrayObject() const { return _useVertexArrayObject; }
  175. #ifdef OSG_USE_DEPRECATED_API
  176. /** Deprecated, use dirtyGLObjects() instead. */
  177. inline void dirtyDisplayList()
  178. {
  179. dirtyGLObjects();
  180. }
  181. #endif
  182. /** Force a recompile on next draw() of any OpenGL objects associated with this geoset.*/
  183. virtual void dirtyGLObjects();
  184. /** Return the estimated size of GLObjects (display lists/vertex buffer objects) that are associated with this drawable.
  185. * This size is used a hint for reuse of deleted display lists/vertex buffer objects. */
  186. virtual unsigned int getGLObjectSizeHint() const { return 0; }
  187. /** Draw OpenGL primitives.
  188. * If the \c Drawable has \c _useDisplayList set to \c true, then use
  189. * an OpenGL display list, automatically compiling one if required.
  190. * Otherwise, call \c drawImplementation().
  191. * @note This method should \e not be overridden in subclasses, as it
  192. * manages the optional display list (notice this is not even
  193. * \c virtual). Subclasses should override
  194. * \c drawImplementation() instead.
  195. */
  196. #ifdef INLINE_DRAWABLE_DRAW
  197. inline void draw(RenderInfo& renderInfo) const;
  198. #else
  199. void draw(RenderInfo& renderInfo) const;
  200. #endif
  201. inline void drawInner(RenderInfo& renderInfo) const
  202. {
  203. if (_drawCallback.valid())
  204. _drawCallback->drawImplementation(renderInfo,this);
  205. else
  206. drawImplementation(renderInfo);
  207. }
  208. /** Immediately compile this \c Drawable into an OpenGL Display List/VertexBufferObjects.
  209. * @note Operation is ignored if \c _useDisplayList is \c false or VertexBufferObjects are not used.
  210. */
  211. virtual void compileGLObjects(RenderInfo& renderInfo) const;
  212. /** Callback class for overriding the default Drawable::createCreateVertexArrayStateImplementation().*/
  213. struct CreateVertexArrayStateCallback : public virtual osg::Object
  214. {
  215. CreateVertexArrayStateCallback() {}
  216. CreateVertexArrayStateCallback(const CreateVertexArrayStateCallback& rhs,const CopyOp& copyop):
  217. Object(rhs, copyop) {}
  218. META_Object(osg, CreateVertexArrayStateCallback);
  219. /** do customized createVertexArrayState .*/
  220. virtual osg::VertexArrayState* createVertexArrayStateImplementation(osg::RenderInfo& renderInfo, const osg::Drawable* drawable) const
  221. {
  222. return drawable->createVertexArrayStateImplementation(renderInfo);
  223. }
  224. };
  225. /** Set the callback to override the default Drawable::createCreateVertexArrayStateImplementation().*/
  226. void setCreateVertexArrayStateCallback(CreateVertexArrayStateCallback* cb) { _createVertexArrayStateCallback = cb; }
  227. /** Get the callback that overrides the default Drawable::createCreateVertexArrayStateImplementation().*/
  228. CreateVertexArrayStateCallback* getCreateVertexArrayStateCallback() { return _createVertexArrayStateCallback.get(); }
  229. /** Get the const callback that overrides the default Drawable::createCreateVertexArrayStateImplementation().*/
  230. const CreateVertexArrayStateCallback* getCreateVertexArrayStateCallback() const { return _createVertexArrayStateCallback.get(); }
  231. /** Create the VertexArrayState object used to track vertex array and vertex array object state. This method will be called automatically during rendering setup so users should not call this themselves.*/
  232. inline VertexArrayState* createVertexArrayState(RenderInfo& renderInfo) const
  233. {
  234. if (_createVertexArrayStateCallback.valid()) return _createVertexArrayStateCallback->createVertexArrayStateImplementation(renderInfo, this);
  235. else return createVertexArrayStateImplementation(renderInfo);
  236. }
  237. /** Implementation of Create the VertexArrayState object.*/
  238. virtual VertexArrayState* createVertexArrayStateImplementation(RenderInfo& renderInfo) const;
  239. void setVertexArrayStateList(VertexArrayStateList& vasl) { _vertexArrayStateList = vasl; }
  240. VertexArrayStateList& getVertexArrayStateList() { return _vertexArrayStateList; }
  241. const VertexArrayStateList& getVertexArrayStateList() const { return _vertexArrayStateList; }
  242. /** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/
  243. virtual void setThreadSafeRefUnref(bool threadSafe);
  244. /** Resize any per context GLObject buffers to specified size. */
  245. virtual void resizeGLObjectBuffers(unsigned int maxSize);
  246. /** If State is non-zero, this function releases OpenGL objects for
  247. * the specified graphics context. Otherwise, releases OpenGL objects
  248. * for all graphics contexts. */
  249. virtual void releaseGLObjects(State* state=0) const;
  250. // for backwards compatibility as local implementations are now found in osg namespace within the include/osg/Callback header
  251. typedef DrawableUpdateCallback UpdateCallback;
  252. typedef DrawableEventCallback EventCallback;
  253. typedef DrawableCullCallback CullCallback;
  254. /** Callback attached to an Drawable which allows the users to customize the drawing of an exist Drawable object.
  255. * The draw callback is implement as a replacement to the Drawable's own drawImplementation() method, if the
  256. * the user intends to decorate the existing draw code then simple call the drawable->drawImplementation() from
  257. * with the callbacks drawImplementation() method. This allows the users to do both pre and post callbacks
  258. * without fuss and can even disable the inner draw if required.*/
  259. struct DrawCallback : public virtual osg::Object
  260. {
  261. DrawCallback() {}
  262. DrawCallback(const DrawCallback& org,const CopyOp& copyop):
  263. Object(org, copyop) {}
  264. META_Object(osg,DrawCallback);
  265. /** do customized draw code.*/
  266. virtual void drawImplementation(osg::RenderInfo& /*renderInfo*/,const osg::Drawable* /*drawable*/) const {}
  267. };
  268. /** Set the DrawCallback which allows users to attach customize the drawing of existing Drawable object.*/
  269. virtual void setDrawCallback(DrawCallback* dc) { _drawCallback=dc; dirtyGLObjects(); }
  270. /** Get the non const DrawCallback.*/
  271. DrawCallback* getDrawCallback() { return _drawCallback.get(); }
  272. /** Get the const DrawCallback.*/
  273. const DrawCallback* getDrawCallback() const { return _drawCallback.get(); }
  274. /** drawImplementation(RenderInfo&) is a pure virtual method for the actual implementation of OpenGL drawing calls, such as vertex arrays and primitives, that
  275. * must be implemented in concrete subclasses of the Drawable base class, examples include osg::Geometry and osg::ShapeDrawable.
  276. * drawImplementation(RenderInfo&) is called from the draw(RenderInfo&) method, with the draw method handling management of OpenGL display lists,
  277. * and drawImplementation(RenderInfo&) handling the actual drawing itself.
  278. * renderInfo : The osg::RenderInfo object that encapsulates the current rendering information including the osg::State OpenGL state for the current graphics context. */
  279. virtual void drawImplementation(RenderInfo& /*renderInfo*/) const {}
  280. /** Return a OpenGL display list handle a newly generated or reused from display list cache. */
  281. static GLuint generateDisplayList(unsigned int contextID, unsigned int sizeHint = 0);
  282. /** Use deleteDisplayList instead of glDeleteList to allow
  283. * OpenGL display list to be cached until they can be deleted
  284. * by the OpenGL context in which they were created, specified
  285. * by contextID.*/
  286. static void deleteDisplayList(unsigned int contextID,GLuint globj, unsigned int sizeHint = 0);
  287. /** Set the minimum number of display lists to retain in the deleted display list cache. */
  288. static void setMinimumNumberOfDisplayListsToRetainInCache(unsigned int minimum);
  289. /** Get the minimum number of display lists to retain in the deleted display list cache. */
  290. static unsigned int getMinimumNumberOfDisplayListsToRetainInCache();
  291. typedef unsigned int AttributeType;
  292. enum AttributeTypes
  293. {
  294. VERTICES = 0,
  295. WEIGHTS = 1,
  296. NORMALS = 2,
  297. COLORS = 3,
  298. SECONDARY_COLORS = 4,
  299. FOG_COORDS = 5,
  300. ATTRIBUTE_6 = 6,
  301. ATTRIBUTE_7 = 7,
  302. TEXTURE_COORDS = 8,
  303. TEXTURE_COORDS_0 = TEXTURE_COORDS,
  304. TEXTURE_COORDS_1 = TEXTURE_COORDS_0+1,
  305. TEXTURE_COORDS_2 = TEXTURE_COORDS_0+2,
  306. TEXTURE_COORDS_3 = TEXTURE_COORDS_0+3,
  307. TEXTURE_COORDS_4 = TEXTURE_COORDS_0+4,
  308. TEXTURE_COORDS_5 = TEXTURE_COORDS_0+5,
  309. TEXTURE_COORDS_6 = TEXTURE_COORDS_0+6,
  310. TEXTURE_COORDS_7 = TEXTURE_COORDS_0+7
  311. // only eight texture coord examples provided here, but underlying code can handle any no of texture units,
  312. // simply co them as (TEXTURE_COORDS_0+unit).
  313. };
  314. class AttributeFunctor
  315. {
  316. public:
  317. virtual ~AttributeFunctor() {}
  318. virtual void apply(AttributeType,unsigned int,GLbyte*) {}
  319. virtual void apply(AttributeType,unsigned int,GLshort*) {}
  320. virtual void apply(AttributeType,unsigned int,GLint*) {}
  321. virtual void apply(AttributeType,unsigned int,GLubyte*) {}
  322. virtual void apply(AttributeType,unsigned int,GLushort*) {}
  323. virtual void apply(AttributeType,unsigned int,GLuint*) {}
  324. virtual void apply(AttributeType,unsigned int,float*) {}
  325. virtual void apply(AttributeType,unsigned int,Vec2*) {}
  326. virtual void apply(AttributeType,unsigned int,Vec3*) {}
  327. virtual void apply(AttributeType,unsigned int,Vec4*) {}
  328. virtual void apply(AttributeType,unsigned int,Vec4ub*) {}
  329. virtual void apply(AttributeType,unsigned int,double*) {}
  330. virtual void apply(AttributeType,unsigned int,Vec2d*) {}
  331. virtual void apply(AttributeType,unsigned int,Vec3d*) {}
  332. virtual void apply(AttributeType,unsigned int,Vec4d*) {}
  333. };
  334. /** Return true if the Drawable subclass supports accept(AttributeFunctor&).*/
  335. virtual bool supports(const AttributeFunctor&) const { return false; }
  336. /** accept an AttributeFunctor and call its methods to tell it about the internal attributes that this Drawable has.
  337. * return true if functor handled by drawable,
  338. * return false on failure of drawable to generate functor calls.*/
  339. virtual void accept(AttributeFunctor&) {}
  340. class ConstAttributeFunctor
  341. {
  342. public:
  343. virtual ~ConstAttributeFunctor() {}
  344. virtual void apply(AttributeType,unsigned int,const GLbyte*) {}
  345. virtual void apply(AttributeType,unsigned int,const GLshort*) {}
  346. virtual void apply(AttributeType,unsigned int,const GLint*) {}
  347. virtual void apply(AttributeType,unsigned int,const GLubyte*) {}
  348. virtual void apply(AttributeType,unsigned int,const GLushort*) {}
  349. virtual void apply(AttributeType,unsigned int,const GLuint*) {}
  350. virtual void apply(AttributeType,unsigned int,const float*) {}
  351. virtual void apply(AttributeType,unsigned int,const Vec2*) {}
  352. virtual void apply(AttributeType,unsigned int,const Vec3*) {}
  353. virtual void apply(AttributeType,unsigned int,const Vec4*) {}
  354. virtual void apply(AttributeType,unsigned int,const Vec4ub*) {}
  355. virtual void apply(AttributeType,unsigned int,const double*) {}
  356. virtual void apply(AttributeType,unsigned int,const Vec2d*) {}
  357. virtual void apply(AttributeType,unsigned int,const Vec3d*) {}
  358. virtual void apply(AttributeType,unsigned int,const Vec4d*) {}
  359. };
  360. /** Return true if the Drawable subclass supports accept(ConstAttributeFunctor&).*/
  361. virtual bool supports(const ConstAttributeFunctor&) const { return false; }
  362. /** Accept an AttributeFunctor and call its methods to tell it about the internal attributes that this Drawable has.
  363. * return true if functor handled by drawable,
  364. * return false on failure of drawable to generate functor calls.*/
  365. virtual void accept(ConstAttributeFunctor&) const {}
  366. /** Return true if the Drawable subclass supports accept(PrimitiveFunctor&).*/
  367. virtual bool supports(const PrimitiveFunctor&) const { return false; }
  368. /** Accept a PrimitiveFunctor and call its methods to tell it about the internal primitives that this Drawable has.
  369. * return true if functor handled by drawable, return false on failure of drawable to generate functor calls.
  370. * Note, PrimitiveFunctor only provides const access of the primitives, as primitives may be procedurally generated
  371. * so one cannot modify it.*/
  372. virtual void accept(PrimitiveFunctor&) const {}
  373. /** Return true if the Drawable subclass supports accept(PrimitiveIndexFunctor&).*/
  374. virtual bool supports(const PrimitiveIndexFunctor&) const { return false; }
  375. /** Accept a PrimitiveIndexFunctor and call its methods to tell it about the internal primitives that this Drawable has.
  376. * return true if functor handled by drawable, return false on failure of drawable to generate functor calls.
  377. * Note, PrimitiveIndexFunctor only provide const access of the primitives, as primitives may be procedurally generated
  378. * so one cannot modify it.*/
  379. virtual void accept(PrimitiveIndexFunctor&) const {}
  380. protected:
  381. Drawable& operator = (const Drawable&) { return *this;}
  382. virtual ~Drawable();
  383. /** set the bounding box .*/
  384. void setBound(const BoundingBox& bb) const;
  385. friend class Node;
  386. friend class Geode;
  387. friend class StateSet;
  388. BoundingBox _initialBoundingBox;
  389. ref_ptr<ComputeBoundingBoxCallback> _computeBoundingBoxCallback;
  390. mutable BoundingBox _boundingBox;
  391. ref_ptr<Shape> _shape;
  392. bool _supportsDisplayList;
  393. bool _useDisplayList;
  394. bool _supportsVertexBufferObjects;
  395. bool _useVertexBufferObjects;
  396. bool _useVertexArrayObject;
  397. typedef osg::buffered_value<GLuint> GLObjectList;
  398. mutable GLObjectList _globjList;
  399. mutable VertexArrayStateList _vertexArrayStateList;
  400. ref_ptr<DrawCallback> _drawCallback;
  401. ref_ptr<CreateVertexArrayStateCallback> _createVertexArrayStateCallback;
  402. };
  403. #ifdef INLINE_DRAWABLE_DRAW
  404. inline void Drawable::draw(RenderInfo& renderInfo) const
  405. {
  406. State& state = *renderInfo.getState();
  407. bool useVertexArrayObject = state.useVertexArrayObject(_useVertexArrayObject);
  408. if (useVertexArrayObject)
  409. {
  410. unsigned int contextID = renderInfo.getContextID();
  411. VertexArrayState* vas = _vertexArrayStateList[contextID].get();
  412. if (!vas)
  413. {
  414. _vertexArrayStateList[contextID] = vas = createVertexArrayState(renderInfo);
  415. }
  416. else
  417. {
  418. // vas->setRequiresSetArrays(getDataVariance()==osg::Object::DYNAMIC);
  419. }
  420. State::SetCurrentVertexArrayStateProxy setVASProxy(state, vas);
  421. state.bindVertexArrayObject(vas);
  422. drawInner(renderInfo);
  423. vas->setRequiresSetArrays(getDataVariance()==osg::Object::DYNAMIC);
  424. return;
  425. }
  426. // TODO, add check against whether VAO is active and supported
  427. if (state.getCurrentVertexArrayState())
  428. {
  429. //OSG_NOTICE<<"state.getCurrentVertexArrayState()->getVertexArrayObject()="<< state.getCurrentVertexArrayState()->getVertexArrayObject()<<std::endl;
  430. state.bindVertexArrayObject(state.getCurrentVertexArrayState());
  431. }
  432. #ifdef OSG_GL_DISPLAYLISTS_AVAILABLE
  433. if (!state.useVertexBufferObject(_supportsVertexBufferObjects && _useVertexBufferObjects) && _useDisplayList)
  434. {
  435. // get the contextID (user defined ID of 0 upwards) for the
  436. // current OpenGL context.
  437. unsigned int contextID = renderInfo.getContextID();
  438. // get the globj for the current contextID.
  439. GLuint& globj = _globjList[contextID];
  440. if( globj == 0 )
  441. {
  442. // compile the display list
  443. globj = generateDisplayList(contextID, getGLObjectSizeHint());
  444. glNewList( globj, GL_COMPILE );
  445. drawInner(renderInfo);
  446. glEndList();
  447. }
  448. // call the display list
  449. glCallList( globj);
  450. }
  451. else
  452. #endif
  453. {
  454. // if state.previousVertexArrayState() is different than currentVertexArrayState bind current
  455. // OSG_NOTICE<<"Fallback drawInner()........................"<<std::endl;
  456. drawInner(renderInfo);
  457. }
  458. }
  459. #endif
  460. class AttributeFunctorArrayVisitor : public ArrayVisitor
  461. {
  462. public:
  463. AttributeFunctorArrayVisitor(Drawable::AttributeFunctor& af):
  464. _af(af),
  465. _type(0) {}
  466. virtual ~AttributeFunctorArrayVisitor() {}
  467. virtual void apply(ByteArray& array) { if (!array.empty()) _af.apply(_type,array.size(),&(array.front())); }
  468. virtual void apply(ShortArray& array) { if (!array.empty()) _af.apply(_type,array.size(),&(array.front())); }
  469. virtual void apply(IntArray& array) { if (!array.empty()) _af.apply(_type,array.size(),&(array.front())); }
  470. virtual void apply(UByteArray& array) { if (!array.empty()) _af.apply(_type,array.size(),&(array.front())); }
  471. virtual void apply(UShortArray& array) { if (!array.empty()) _af.apply(_type,array.size(),&(array.front())); }
  472. virtual void apply(UIntArray& array) { if (!array.empty()) _af.apply(_type,array.size(),&(array.front())); }
  473. virtual void apply(Vec4ubArray& array) { if (!array.empty()) _af.apply(_type,array.size(),&(array.front())); }
  474. virtual void apply(FloatArray& array) { if (!array.empty()) _af.apply(_type,array.size(),&(array.front())); }
  475. virtual void apply(Vec2Array& array) { if (!array.empty()) _af.apply(_type,array.size(),&(array.front())); }
  476. virtual void apply(Vec3Array& array) { if (!array.empty()) _af.apply(_type,array.size(),&(array.front())); }
  477. virtual void apply(Vec4Array& array) { if (!array.empty()) _af.apply(_type,array.size(),&(array.front())); }
  478. virtual void apply(DoubleArray& array) { if (!array.empty()) _af.apply(_type,array.size(),&(array.front())); }
  479. virtual void apply(Vec2dArray& array) { if (!array.empty()) _af.apply(_type,array.size(),&(array.front())); }
  480. virtual void apply(Vec3dArray& array) { if (!array.empty()) _af.apply(_type,array.size(),&(array.front())); }
  481. virtual void apply(Vec4dArray& array) { if (!array.empty()) _af.apply(_type,array.size(),&(array.front())); }
  482. inline void applyArray(Drawable::AttributeType type,Array* array)
  483. {
  484. if (array)
  485. {
  486. _type = type;
  487. array->accept(*this);
  488. }
  489. }
  490. protected:
  491. AttributeFunctorArrayVisitor& operator = (const AttributeFunctorArrayVisitor&) { return *this; }
  492. Drawable::AttributeFunctor& _af;
  493. Drawable::AttributeType _type;
  494. };
  495. class ConstAttributeFunctorArrayVisitor : public ConstArrayVisitor
  496. {
  497. public:
  498. ConstAttributeFunctorArrayVisitor(Drawable::ConstAttributeFunctor& af):
  499. _af(af),
  500. _type(0) {}
  501. virtual ~ConstAttributeFunctorArrayVisitor() {}
  502. virtual void apply(const ByteArray& array) { if (!array.empty()) _af.apply(_type,array.size(),&(array.front())); }
  503. virtual void apply(const ShortArray& array) { if (!array.empty()) _af.apply(_type,array.size(),&(array.front())); }
  504. virtual void apply(const IntArray& array) { if (!array.empty()) _af.apply(_type,array.size(),&(array.front())); }
  505. virtual void apply(const UByteArray& array) { if (!array.empty()) _af.apply(_type,array.size(),&(array.front())); }
  506. virtual void apply(const UShortArray& array) { if (!array.empty()) _af.apply(_type,array.size(),&(array.front())); }
  507. virtual void apply(const UIntArray& array) { if (!array.empty()) _af.apply(_type,array.size(),&(array.front())); }
  508. virtual void apply(const Vec4ubArray& array) { if (!array.empty()) _af.apply(_type,array.size(),&(array.front())); }
  509. virtual void apply(const FloatArray& array) { if (!array.empty()) _af.apply(_type,array.size(),&(array.front())); }
  510. virtual void apply(const Vec2Array& array) { if (!array.empty()) _af.apply(_type,array.size(),&(array.front())); }
  511. virtual void apply(const Vec3Array& array) { if (!array.empty()) _af.apply(_type,array.size(),&(array.front())); }
  512. virtual void apply(const Vec4Array& array) { if (!array.empty()) _af.apply(_type,array.size(),&(array.front())); }
  513. virtual void apply(const DoubleArray& array) { if (!array.empty()) _af.apply(_type,array.size(),&(array.front())); }
  514. virtual void apply(const Vec2dArray& array) { if (!array.empty()) _af.apply(_type,array.size(),&(array.front())); }
  515. virtual void apply(const Vec3dArray& array) { if (!array.empty()) _af.apply(_type,array.size(),&(array.front())); }
  516. virtual void apply(const Vec4dArray& array) { if (!array.empty()) _af.apply(_type,array.size(),&(array.front())); }
  517. inline void applyArray(Drawable::AttributeType type,const Array* array)
  518. {
  519. if (array)
  520. {
  521. _type = type;
  522. array->accept(*this);
  523. }
  524. }
  525. protected:
  526. ConstAttributeFunctorArrayVisitor& operator = (const ConstAttributeFunctorArrayVisitor&) { return *this; }
  527. Drawable::ConstAttributeFunctor& _af;
  528. Drawable::AttributeType _type;
  529. };
  530. }
  531. #endif