2006-07-18 23:21:48 +08:00
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2003-01-22 00:45:36 +08:00
*
2005-01-27 21:15:21 +08:00
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
2003-01-22 00:45:36 +08:00
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
2012-03-22 01:36:20 +08:00
*
2003-01-22 00:45:36 +08:00
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
2005-01-27 21:15:21 +08:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2003-01-22 00:45:36 +08:00
* OpenSceneGraph Public License for more details.
*/
2001-10-04 23:12:57 +08:00
2001-09-20 05:19:47 +08:00
#ifndef OSG_DRAWABLE
#define OSG_DRAWABLE 1
#include <osg/BoundingBox>
2002-10-30 21:27:15 +08:00
#include <osg/Shape>
2005-02-09 18:39:45 +08:00
#include <osg/BufferObject>
#include <osg/PrimitiveSet>
2006-09-19 04:54:48 +08:00
#include <osg/RenderInfo>
2014-06-03 17:23:24 +08:00
#include <osg/Group>
2003-06-30 05:41:57 +08:00
2003-08-21 22:26:40 +08:00
#ifndef GL_NV_occlusion_query
#define GL_OCCLUSION_TEST_HP 0x8165
#define GL_OCCLUSION_TEST_RESULT_HP 0x8166
#define GL_PIXEL_COUNTER_BITS_NV 0x8864
#define GL_CURRENT_OCCLUSION_QUERY_ID_NV 0x8865
#define GL_PIXEL_COUNT_NV 0x8866
#define GL_PIXEL_COUNT_AVAILABLE_NV 0x8867
#endif
2003-06-30 05:41:57 +08:00
2003-12-09 19:20:44 +08:00
#ifndef GL_ARB_occlusion_query
#define GL_SAMPLES_PASSED_ARB 0x8914
#define GL_QUERY_COUNTER_BITS_ARB 0x8864
#define GL_CURRENT_QUERY_ARB 0x8865
#define GL_QUERY_RESULT_ARB 0x8866
#define GL_QUERY_RESULT_AVAILABLE_ARB 0x8867
#endif
2005-01-08 03:00:40 +08:00
2007-01-19 22:41:29 +08:00
#ifndef GL_TIME_ELAPSED
#define GL_TIME_ELAPSED 0x88BF
2010-11-11 00:58:58 +08:00
#define GL_TIMESTAMP 0x8E28
2007-01-19 22:41:29 +08:00
#endif
2007-01-24 00:02:37 +08:00
#ifndef GL_QUERY_RESULT
#define GL_QUERY_RESULT 0x8866
#define GL_QUERY_RESULT_AVAILABLE 0x8867
#endif
2005-01-08 03:00:40 +08:00
namespace osg {
class Vec2f;
class Vec3f;
class Vec4f;
2005-07-15 22:41:19 +08:00
class Vec4ub;
2005-01-08 03:00:40 +08:00
class Geometry;
2005-04-11 23:17:24 +08:00
class NodeVisitor;
2009-10-21 19:18:13 +08:00
class ArrayDispatchers;
2005-01-08 03:00:40 +08:00
2004-08-31 22:08:12 +08:00
// this is defined to alter the way display lists are compiled inside the
2001-11-02 20:26:33 +08:00
// the draw method, it has been found that the NVidia drivers fail completely
2005-01-27 21:15:21 +08:00
// to optimize COMPILE_AND_EXECUTE in fact make it go slower than for no display
2002-08-29 11:22:27 +08:00
// lists, but optimize a separate COMPILE very well?! Define it as default
2004-08-31 22:08:12 +08:00
// the use of a separate COMPILE, then glCallList rather than use COMPILE_AND_EXECUTE.
2001-10-11 04:20:14 +08:00
2002-08-29 11:22:27 +08:00
#define USE_SEPARATE_COMPILE_AND_EXECUTE
2001-10-11 04:20:14 +08:00
2005-01-27 21:15:21 +08:00
/** Pure virtual base class for drawable geometry. In OSG, everything that can
* be rendered is implemented as a class derived from \c Drawable. The
* \c Drawable class contains no drawing primitives, since these are provided
* by subclasses such as \c osg::Geometry.
* <p>Notice that a \c Drawable is not a \c Node, and therefore it cannot be
* directly added to a scene graph. Instead, <tt>Drawable</tt>s are attached to
* <tt>Geode</tt>s, which are scene graph nodes.
* <p>The OpenGL state that must be used when rendering a \c Drawable is
* represented by a \c StateSet. Since a \c Drawable has a reference
* (\c osg::ref_ptr) to a \c StateSet, <tt>StateSet</tt>s can be shared between
* different <tt>Drawable</tt>s. In fact, sharing <tt>StateSet</tt>s is a good
* way to improve performance, since this allows OSG to reduce the number of
* expensive changes in the OpenGL state.
* <p>Finally, <tt>Drawable</tt>s can also be shared between different
* <tt>Geode</tt>s, so that the same geometry (loaded to memory just once) can
* be used in different parts of the scene graph.
2001-09-20 05:19:47 +08:00
*/
2014-05-14 18:19:43 +08:00
class OSG_EXPORT Drawable : public Node
2001-09-20 05:19:47 +08:00
{
public:
2004-09-22 05:33:52 +08:00
static unsigned int s_numberDrawablesReusedLastInLastFrame;
2004-09-22 01:26:08 +08:00
static unsigned int s_numberNewDrawablesInLastFrame;
static unsigned int s_numberDeletedDrawablesInLastFrame;
2001-09-20 05:19:47 +08:00
Drawable();
2002-01-29 22:04:06 +08:00
/** Copy constructor using CopyOp to manage deep vs shallow copy.*/
2002-02-07 09:07:11 +08:00
Drawable(const Drawable& drawable,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
Added support for shallow and deep copy of nodes, drawables and state, via a
copy constructor which takes an optional Cloner object, and the old
osg::Object::clone() has changed so that it now requires a Cloner as paramter.
This is passed on to the copy constructor to help control the shallow vs
deep copying. The old functionality of clone() which was clone of type has
been renamed to cloneType().
Updated all of the OSG to work with these new conventions, implemention all
the required copy constructors etc. A couple of areas will do shallow
copies by design, a couple of other still need to be updated to do either
shallow or deep.
Neither of the shallow or deep copy operations have been tested yet, only
the old functionality of the OSG has been checked so far, such running the
viewer on various demo datasets.
Also fixed a problem in osg::Optimize::RemoveRendundentNodesVisitor which
was not checking that Group didn't have have any attached StateSet's, Callbacks
or UserData. These checks have now been added, which fixes a bug which was
revealled by the new osgscribe demo, this related to removal of group acting
as state decorator.
method
2002-01-29 05:17:01 +08:00
2014-05-14 18:19:43 +08:00
META_Node(osg, Drawable);
2001-09-20 05:19:47 +08:00
2014-05-15 17:26:59 +08:00
virtual Drawable* asDrawable() { return this; }
virtual const Drawable* asDrawable() const { return this; }
2002-02-09 06:55:21 +08:00
2007-12-11 01:30:18 +08:00
/** Compute the DataVariance based on an assessment of callback etc.*/
2007-02-14 19:24:42 +08:00
virtual void computeDataVariance();
2008-09-18 18:38:18 +08:00
/** Get the list of matrices that transform this node from local coordinates to world coordinates.
* The optional Node* haltTraversalAtNode allows the user to prevent traversal beyond a specifed node. */
MatrixList getWorldMatrices(const osg::Node* haltTraversalAtNode=0) const;
2002-02-09 06:55:21 +08:00
2006-02-23 20:41:05 +08:00
/** Set the initial bounding volume to use when computing the overall bounding volume.*/
2005-05-12 22:03:22 +08:00
void setInitialBound(const osg::BoundingBox& bbox) { _initialBound = bbox; dirtyBound(); }
2006-02-23 20:41:05 +08:00
/** Set the initial bounding volume to use when computing the overall bounding volume.*/
2005-05-12 22:03:22 +08:00
const BoundingBox& getInitialBound() const { return _initialBound; }
2014-05-15 00:01:40 +08:00
inline const BoundingSphere& getBound() const
{
if(!_boundingSphereComputed) getBoundingBox();
return _boundingSphere;
}
2006-02-23 04:51:26 +08:00
/** Get BoundingBox of Drawable.
* If the BoundingBox is not up to date then its updated via an internal call to computeBond().
2002-10-30 21:27:15 +08:00
*/
2014-05-14 18:19:43 +08:00
inline const BoundingBox& getBoundingBox() const
2002-10-30 21:27:15 +08:00
{
2014-05-14 18:19:43 +08:00
if(!_boundingSphereComputed)
2005-05-12 22:03:22 +08:00
{
_boundingBox = _initialBound;
2014-05-21 17:06:17 +08:00
2012-03-22 01:36:20 +08:00
if (_computeBoundCallback.valid())
2005-05-12 22:03:22 +08:00
_boundingBox.expandBy(_computeBoundCallback->computeBound(*this));
else
2014-05-14 18:19:43 +08:00
_boundingBox.expandBy(computeBoundingBox());
2012-03-22 01:36:20 +08:00
2014-05-21 00:09:34 +08:00
if(_boundingBox.valid()){
_boundingSphere.set(_boundingBox.center(), _boundingBox.radius());
} else {
_boundingSphere.init();
}
2014-05-14 18:19:43 +08:00
_boundingSphereComputed = true;
2005-05-12 22:03:22 +08:00
}
2014-05-15 00:52:18 +08:00
2005-05-12 22:03:22 +08:00
return _boundingBox;
2002-10-30 21:27:15 +08:00
}
2014-05-14 18:19:43 +08:00
/** Compute the bounding sphere around Drawables's geometry.*/
virtual BoundingSphere computeBound() const;
2005-05-12 22:03:22 +08:00
/** Compute the bounding box around Drawables's geometry.*/
2014-05-14 18:19:43 +08:00
virtual BoundingBox computeBoundingBox() const;
2005-05-12 22:03:22 +08:00
2006-02-23 20:41:05 +08:00
/** Callback to allow users to override the default computation of bounding volume. */
2005-05-12 22:48:56 +08:00
struct ComputeBoundingBoxCallback : public osg::Object
2005-05-12 22:03:22 +08:00
{
2005-05-12 22:48:56 +08:00
ComputeBoundingBoxCallback() {}
ComputeBoundingBoxCallback(const ComputeBoundingBoxCallback&,const CopyOp&) {}
META_Object(osg,ComputeBoundingBoxCallback);
virtual BoundingBox computeBound(const osg::Drawable&) const { return BoundingBox(); }
2005-05-12 22:03:22 +08:00
};
/** Set the compute bound callback to override the default computeBound.*/
2005-05-12 22:48:56 +08:00
void setComputeBoundingBoxCallback(ComputeBoundingBoxCallback* callback) { _computeBoundCallback = callback; }
2005-05-12 22:03:22 +08:00
/** Get the compute bound callback.*/
2005-05-12 22:48:56 +08:00
ComputeBoundingBoxCallback* getComputeBoundingBoxCallback() { return _computeBoundCallback.get(); }
2005-05-12 22:03:22 +08:00
/** Get the const compute bound callback.*/
2005-05-12 22:48:56 +08:00
const ComputeBoundingBoxCallback* getComputeBoundingBoxCallback() const { return _computeBoundCallback.get(); }
2005-05-12 22:03:22 +08:00
2005-01-27 21:15:21 +08:00
/** Set the Shape of the \c Drawable. The shape can be used to
* speed up collision detection or as a guide for procedural
* geometry generation.
* @see osg::Shape.
*/
2003-08-21 22:26:40 +08:00
inline void setShape(Shape* shape) { _shape = shape; }
2002-10-30 21:27:15 +08:00
2003-08-21 22:26:40 +08:00
/** Get the Shape of the Drawable.*/
inline Shape* getShape() { return _shape.get(); }
2005-01-27 21:15:21 +08:00
2003-08-21 22:26:40 +08:00
/** Get the const Shape of the const Drawable.*/
inline const Shape* getShape() const { return _shape.get(); }
2002-10-30 21:27:15 +08:00
2004-08-31 22:08:12 +08:00
/** Set the drawable so that it can or cannot be used in conjunction with OpenGL
* display lists. When set to true, calls to Drawable::setUseDisplayList,
2001-09-20 05:19:47 +08:00
* whereas when set to false, no display lists can be created and calls
2004-08-31 22:08:12 +08:00
* to setUseDisplayList are ignored, and a warning is produced. The latter
2001-09-20 05:19:47 +08:00
* is typically used to guard against the switching on of display lists
* on objects with dynamic internal data such as continuous Level of Detail
* algorithms.*/
2002-09-02 20:31:35 +08:00
void setSupportsDisplayList(bool flag);
2005-01-27 21:15:21 +08:00
/** Get whether display lists are supported for this drawable instance.*/
2002-09-02 20:31:35 +08:00
inline bool getSupportsDisplayList() const { return _supportsDisplayList; }
2001-09-20 05:19:47 +08:00
/** When set to true, force the draw method to use OpenGL Display List for rendering.
2004-08-31 22:08:12 +08:00
If false, rendering directly. If the display list has not been compiled
already, the next call to draw will automatically create the display list.*/
2002-09-02 20:31:35 +08:00
void setUseDisplayList(bool flag);
2001-09-20 05:19:47 +08:00
/** Return whether OpenGL display lists are being used for rendering.*/
2002-09-02 20:31:35 +08:00
inline bool getUseDisplayList() const { return _useDisplayList; }
2001-09-20 05:19:47 +08:00
2005-08-09 18:48:26 +08:00
/** Return OpenGL display list for specified contextID. */
inline GLuint& getDisplayList(unsigned int contextID) const { return _globjList[contextID]; }
2012-03-22 01:36:20 +08:00
/** When set to true, ignore the setUseDisplayList() settings, and hints to the drawImplementation
2004-09-22 01:26:08 +08:00
method to use OpenGL vertex buffer objects for rendering.*/
2007-04-27 22:52:30 +08:00
virtual void setUseVertexBufferObjects(bool flag);
2003-06-30 05:41:57 +08:00
/** Return whether OpenGL vertex buffer objects should be used when supported by OpenGL driver.*/
inline bool getUseVertexBufferObjects() const { return _useVertexBufferObjects; }
2001-09-20 05:19:47 +08:00
/** Force a recompile on next draw() of any OpenGL display list associated with this geoset.*/
2007-04-27 22:52:30 +08:00
virtual void dirtyDisplayList();
2001-09-20 05:19:47 +08:00
2002-10-30 21:27:15 +08:00
2004-09-22 01:26:08 +08:00
/** Return the estimated size of GLObjects (display lists/vertex buffer objects) that are associated with this drawable.
2006-02-23 20:41:05 +08:00
* This size is used a hint for reuse of deleted display lists/vertex buffer objects. */
2004-09-22 01:26:08 +08:00
virtual unsigned int getGLObjectSizeHint() const { return 0; }
2001-09-20 05:19:47 +08:00
2005-01-27 21:15:21 +08:00
/** Draw OpenGL primitives.
* If the \c Drawable has \c _useDisplayList set to \c true, then use
* an OpenGL display list, automatically compiling one if required.
* Otherwise, call \c drawImplementation().
* @note This method should \e not be overridden in subclasses, as it
* manages the optional display list (notice this is not even
* \c virtual). Subclasses should override
* \c drawImplementation() instead.
2001-09-20 05:19:47 +08:00
*/
2006-09-19 04:54:48 +08:00
inline void draw(RenderInfo& renderInfo) const;
2005-01-27 21:15:21 +08:00
2009-12-09 21:51:02 +08:00
/** Immediately compile this \c Drawable into an OpenGL Display List/VertexBufferObjects.
* @note Operation is ignored if \c _useDisplayList is \c false or VertexBufferObjects are not used.
2005-01-27 21:15:21 +08:00
*/
2006-11-14 20:51:31 +08:00
virtual void compileGLObjects(RenderInfo& renderInfo) const;
2001-09-20 05:19:47 +08:00
2007-01-05 00:49:58 +08:00
/** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/
virtual void setThreadSafeRefUnref(bool threadSafe);
2007-01-04 22:11:51 +08:00
/** Resize any per context GLObject buffers to specified size. */
virtual void resizeGLObjectBuffers(unsigned int maxSize);
2005-05-08 04:47:09 +08:00
/** If State is non-zero, this function releases OpenGL objects for
2007-12-11 01:30:18 +08:00
* the specified graphics context. Otherwise, releases OpenGL objects
2005-05-08 04:47:09 +08:00
* for all graphics contexts. */
2004-07-20 13:37:59 +08:00
virtual void releaseGLObjects(State* state=0) const;
2002-02-07 09:07:11 +08:00
2014-06-06 00:26:13 +08:00
struct OSG_EXPORT UpdateCallback : public virtual Callback
2002-12-19 23:55:40 +08:00
{
2003-10-09 05:29:45 +08:00
UpdateCallback() {}
UpdateCallback(const UpdateCallback&,const CopyOp&) {}
2004-09-28 17:14:04 +08:00
META_Object(osg,UpdateCallback);
2003-10-09 05:29:45 +08:00
2014-06-06 00:26:13 +08:00
/** override Callback::run() entry point to adapt to StateAttributeCallback::run(..) method.*/
virtual bool run(osg::Object* object, osg::Object* data);
2003-10-09 05:29:45 +08:00
/** do customized update code.*/
virtual void update(osg::NodeVisitor*, osg::Drawable*) {}
2002-12-19 23:55:40 +08:00
};
2005-01-27 21:15:21 +08:00
2005-05-02 17:57:58 +08:00
2014-06-06 00:26:13 +08:00
struct OSG_EXPORT EventCallback : public virtual Callback
2005-02-25 22:02:48 +08:00
{
EventCallback() {}
EventCallback(const EventCallback&,const CopyOp&) {}
META_Object(osg,EventCallback);
2014-06-06 00:26:13 +08:00
/** override Callback::run() entry point to adapt to StateAttributeCallback::run(..) method.*/
virtual bool run(osg::Object* object, osg::Object* data);
2006-02-23 20:41:05 +08:00
/** do customized Event code. */
2005-02-25 22:02:48 +08:00
virtual void event(osg::NodeVisitor*, osg::Drawable*) {}
};
2014-06-06 00:26:13 +08:00
struct CullCallback : public virtual Callback
2002-07-10 23:35:47 +08:00
{
2003-10-09 05:29:45 +08:00
CullCallback() {}
CullCallback(const CullCallback&,const CopyOp&) {}
2004-09-28 17:14:04 +08:00
META_Object(osg,CullCallback);
2003-10-09 05:29:45 +08:00
2014-06-10 16:50:42 +08:00
// just use the standard run implementation to passes run onto any nested callbacks.
using Callback::run;
2006-09-19 04:54:48 +08:00
/** deprecated.*/
2003-10-09 05:29:45 +08:00
virtual bool cull(osg::NodeVisitor*, osg::Drawable*, osg::State*) const { return false; }
2006-09-19 04:54:48 +08:00
/** do customized cull code, return true if drawable should be culled.*/
virtual bool cull(osg::NodeVisitor* nv, osg::Drawable* drawable, osg::RenderInfo* renderInfo) const { return cull(nv, drawable, renderInfo? renderInfo->getState():0); }
2002-07-10 23:35:47 +08:00
};
2002-02-07 09:07:11 +08:00
/** Callback attached to an Drawable which allows the users to customize the drawing of an exist Drawable object.
2002-11-06 23:43:11 +08:00
* The draw callback is implement as a replacement to the Drawable's own drawImplementation() method, if the
2004-08-31 22:08:12 +08:00
* the user intends to decorate the existing draw code then simple call the drawable->drawImplementation() from
2006-02-23 20:41:05 +08:00
* with the callbacks drawImplementation() method. This allows the users to do both pre and post callbacks
2004-08-31 22:08:12 +08:00
* without fuss and can even disable the inner draw if required.*/
2003-10-09 05:29:45 +08:00
struct DrawCallback : public virtual osg::Object
2002-02-07 09:07:11 +08:00
{
2003-10-09 05:29:45 +08:00
DrawCallback() {}
DrawCallback(const DrawCallback&,const CopyOp&) {}
2004-09-28 17:14:04 +08:00
META_Object(osg,DrawCallback);
2003-10-09 05:29:45 +08:00
2006-09-19 04:54:48 +08:00
/** do customized draw code.*/
2007-03-28 19:30:38 +08:00
virtual void drawImplementation(osg::RenderInfo& /*renderInfo*/,const osg::Drawable* /*drawable*/) const {}
2002-02-07 09:07:11 +08:00
};
/** Set the DrawCallback which allows users to attach customize the drawing of existing Drawable object.*/
2004-09-10 16:26:53 +08:00
virtual void setDrawCallback(DrawCallback* dc) { _drawCallback=dc; dirtyDisplayList(); }
2005-01-27 21:15:21 +08:00
2002-03-15 00:01:21 +08:00
/** Get the non const DrawCallback.*/
2002-02-07 09:07:11 +08:00
DrawCallback* getDrawCallback() { return _drawCallback.get(); }
2005-01-27 21:15:21 +08:00
2002-03-15 00:01:21 +08:00
/** Get the const DrawCallback.*/
2002-02-07 09:07:11 +08:00
const DrawCallback* getDrawCallback() const { return _drawCallback.get(); }
2007-03-28 19:30:38 +08:00
/** drawImplementation(RenderInfo&) is a pure virtual method for the actual implementation of OpenGL drawing calls, such as vertex arrays and primitives, that
2005-02-02 23:08:55 +08:00
* must be implemented in concrete subclasses of the Drawable base class, examples include osg::Geometry and osg::ShapeDrawable.
2007-03-28 19:30:38 +08:00
* drawImplementation(RenderInfo&) is called from the draw(RenderInfo&) method, with the draw method handling management of OpenGL display lists,
2012-03-22 01:36:20 +08:00
* and drawImplementation(RenderInfo&) handling the actual drawing itself.
2007-12-11 01:30:18 +08:00
* @param renderInfo The osg::RenderInfo object that encapsulates the current rendering information including the osg::State OpenGL state for the current graphics context. */
2014-05-14 18:19:43 +08:00
virtual void drawImplementation(RenderInfo& /*renderInfo*/) const {}
2002-02-07 09:07:11 +08:00
2002-11-08 22:03:44 +08:00
2004-09-22 05:33:52 +08:00
/** Return a OpenGL display list handle a newly generated or reused from display list cache. */
2004-09-22 01:26:08 +08:00
static GLuint generateDisplayList(unsigned int contextID, unsigned int sizeHint = 0);
2002-02-07 09:07:11 +08:00
2004-09-22 05:33:52 +08:00
/** Set the minimum number of display lists to retain in the deleted display list cache. */
static void setMinimumNumberOfDisplayListsToRetainInCache(unsigned int minimum);
/** Get the minimum number of display lists to retain in the deleted display list cache. */
static unsigned int getMinimumNumberOfDisplayListsToRetainInCache();
2005-01-27 21:15:21 +08:00
/** Use deleteDisplayList instead of glDeleteList to allow
2004-08-31 22:08:12 +08:00
* OpenGL display list to be cached until they can be deleted
2001-09-20 05:19:47 +08:00
* by the OpenGL context in which they were created, specified
* by contextID.*/
2004-09-22 01:26:08 +08:00
static void deleteDisplayList(unsigned int contextID,GLuint globj, unsigned int sizeHint = 0);
2005-01-27 21:15:21 +08:00
/** Flush all the cached display list which need to be deleted
2001-09-20 05:19:47 +08:00
* in the OpenGL context related to contextID.*/
2004-09-22 05:33:52 +08:00
static void flushAllDeletedDisplayLists(unsigned int contextID);
2008-01-08 21:24:29 +08:00
/** Flush all the cached display list which need to be deleted
* in the OpenGL context related to contextID.
* Note, unlike flush no OpenGL calls are made, instead the handles are all removed.
* this call is useful for when an OpenGL context has been destroyed. */
static void discardAllDeletedDisplayLists(unsigned int contextID);
2005-01-27 21:15:21 +08:00
/** Flush the cached display list which need to be deleted
2004-09-22 05:33:52 +08:00
* in the OpenGL context related to contextID.*/
static void flushDeletedDisplayLists(unsigned int contextID,double& availableTime);
2001-10-11 04:20:14 +08:00
2003-05-09 21:07:06 +08:00
typedef unsigned int AttributeType;
2005-01-27 21:15:21 +08:00
2003-05-09 21:07:06 +08:00
enum AttributeTypes
2001-10-11 04:20:14 +08:00
{
2003-05-09 21:07:06 +08:00
VERTICES = 0,
WEIGHTS = 1,
NORMALS = 2,
COLORS = 3,
SECONDARY_COLORS = 4,
FOG_COORDS = 5,
2005-09-28 21:55:22 +08:00
ATTRIBUTE_6 = 6,
ATTRIBUTE_7 = 7,
2003-05-09 21:07:06 +08:00
TEXTURE_COORDS = 8,
2001-10-11 04:20:14 +08:00
TEXTURE_COORDS_0 = TEXTURE_COORDS,
2002-07-18 23:36:14 +08:00
TEXTURE_COORDS_1 = TEXTURE_COORDS_0+1,
TEXTURE_COORDS_2 = TEXTURE_COORDS_0+2,
TEXTURE_COORDS_3 = TEXTURE_COORDS_0+3,
TEXTURE_COORDS_4 = TEXTURE_COORDS_0+4,
TEXTURE_COORDS_5 = TEXTURE_COORDS_0+5,
TEXTURE_COORDS_6 = TEXTURE_COORDS_0+6,
2005-01-27 21:15:21 +08:00
TEXTURE_COORDS_7 = TEXTURE_COORDS_0+7
2007-12-11 01:30:18 +08:00
// only eight texture coord examples provided here, but underlying code can handle any no of texture units,
2002-07-18 23:36:14 +08:00
// simply co them as (TEXTURE_COORDS_0+unit).
2001-10-11 04:20:14 +08:00
};
2005-01-27 21:15:21 +08:00
2001-10-13 19:16:10 +08:00
class AttributeFunctor
2001-10-11 04:20:14 +08:00
{
2001-10-13 19:16:10 +08:00
public:
virtual ~AttributeFunctor() {}
2005-01-27 21:15:21 +08:00
2002-07-18 23:36:14 +08:00
virtual void apply(AttributeType,unsigned int,GLbyte*) {}
virtual void apply(AttributeType,unsigned int,GLshort*) {}
virtual void apply(AttributeType,unsigned int,GLint*) {}
virtual void apply(AttributeType,unsigned int,GLubyte*) {}
virtual void apply(AttributeType,unsigned int,GLushort*) {}
virtual void apply(AttributeType,unsigned int,GLuint*) {}
virtual void apply(AttributeType,unsigned int,float*) {}
virtual void apply(AttributeType,unsigned int,Vec2*) {}
virtual void apply(AttributeType,unsigned int,Vec3*) {}
virtual void apply(AttributeType,unsigned int,Vec4*) {}
2005-07-15 22:41:19 +08:00
virtual void apply(AttributeType,unsigned int,Vec4ub*) {}
2007-12-04 22:46:46 +08:00
virtual void apply(AttributeType,unsigned int,double*) {}
virtual void apply(AttributeType,unsigned int,Vec2d*) {}
virtual void apply(AttributeType,unsigned int,Vec3d*) {}
virtual void apply(AttributeType,unsigned int,Vec4d*) {}
2001-10-11 04:20:14 +08:00
};
2005-01-27 21:15:21 +08:00
/** Return true if the Drawable subclass supports accept(AttributeFunctor&).*/
2005-03-18 10:42:59 +08:00
virtual bool supports(const AttributeFunctor&) const { return false; }
2001-10-11 04:20:14 +08:00
2004-08-31 22:08:12 +08:00
/** accept an AttributeFunctor and call its methods to tell it about the internal attributes that this Drawable has.
2005-01-27 21:15:21 +08:00
* return true if functor handled by drawable,
2004-08-31 22:08:12 +08:00
* return false on failure of drawable to generate functor calls.*/
2002-07-18 23:36:14 +08:00
virtual void accept(AttributeFunctor&) {}
2005-01-27 21:15:21 +08:00
2002-11-06 18:24:33 +08:00
class ConstAttributeFunctor
{
public:
2005-01-27 21:15:21 +08:00
2002-11-06 18:24:33 +08:00
virtual ~ConstAttributeFunctor() {}
2005-01-27 21:15:21 +08:00
2007-03-24 01:27:53 +08:00
virtual void apply(AttributeType,unsigned int,const GLbyte*) {}
virtual void apply(AttributeType,unsigned int,const GLshort*) {}
virtual void apply(AttributeType,unsigned int,const GLint*) {}
virtual void apply(AttributeType,unsigned int,const GLubyte*) {}
virtual void apply(AttributeType,unsigned int,const GLushort*) {}
virtual void apply(AttributeType,unsigned int,const GLuint*) {}
virtual void apply(AttributeType,unsigned int,const float*) {}
virtual void apply(AttributeType,unsigned int,const Vec2*) {}
virtual void apply(AttributeType,unsigned int,const Vec3*) {}
virtual void apply(AttributeType,unsigned int,const Vec4*) {}
virtual void apply(AttributeType,unsigned int,const Vec4ub*) {}
2007-12-04 22:46:46 +08:00
virtual void apply(AttributeType,unsigned int,const double*) {}
virtual void apply(AttributeType,unsigned int,const Vec2d*) {}
virtual void apply(AttributeType,unsigned int,const Vec3d*) {}
virtual void apply(AttributeType,unsigned int,const Vec4d*) {}
2002-11-06 18:24:33 +08:00
};
2005-01-27 21:15:21 +08:00
/** Return true if the Drawable subclass supports accept(ConstAttributeFunctor&).*/
2005-03-18 10:42:59 +08:00
virtual bool supports(const ConstAttributeFunctor&) const { return false; }
2002-11-06 18:24:33 +08:00
2005-01-27 21:15:21 +08:00
/** Accept an AttributeFunctor and call its methods to tell it about the internal attributes that this Drawable has.
* return true if functor handled by drawable,
2004-08-31 22:08:12 +08:00
* return false on failure of drawable to generate functor calls.*/
2002-11-06 18:24:33 +08:00
virtual void accept(ConstAttributeFunctor&) const {}
2004-03-15 05:54:17 +08:00
2005-01-27 21:15:21 +08:00
/** Return true if the Drawable subclass supports accept(PrimitiveFunctor&).*/
2005-03-18 10:42:59 +08:00
virtual bool supports(const PrimitiveFunctor&) const { return false; }
2002-02-07 09:07:11 +08:00
2005-01-27 21:15:21 +08:00
/** Accept a PrimitiveFunctor and call its methods to tell it about the internal primitives that this Drawable has.
2002-11-06 18:24:33 +08:00
* return true if functor handled by drawable, return false on failure of drawable to generate functor calls.
2004-08-31 22:08:12 +08:00
* Note, PrimtiveFunctor only provides const access of the primitives, as primitives may be procedurally generated
2002-11-06 18:24:33 +08:00
* so one cannot modify it.*/
virtual void accept(PrimitiveFunctor&) const {}
2002-02-07 09:07:11 +08:00
2005-01-27 21:15:21 +08:00
/** Return true if the Drawable subclass supports accept(PrimitiveIndexFunctor&).*/
2005-03-18 10:42:59 +08:00
virtual bool supports(const PrimitiveIndexFunctor&) const { return false; }
2004-03-15 05:54:17 +08:00
2005-01-27 21:15:21 +08:00
/** Accept a PrimitiveIndexFunctor and call its methods to tell it about the internal primitives that this Drawable has.
2004-03-15 05:54:17 +08:00
* return true if functor handled by drawable, return false on failure of drawable to generate functor calls.
2004-08-31 22:08:12 +08:00
* Note, PrimtiveIndexFunctor only provide const access of the primitives, as primitives may be procedurally generated
2004-03-15 05:54:17 +08:00
* so one cannot modify it.*/
virtual void accept(PrimitiveIndexFunctor&) const {}
2001-09-20 05:19:47 +08:00
protected:
Drawable& operator = (const Drawable&) { return *this;}
virtual ~Drawable();
2003-03-12 05:53:28 +08:00
/** set the bounding box .*/
void setBound(const BoundingBox& bb) const;
2005-01-27 21:15:21 +08:00
2002-02-09 06:55:21 +08:00
friend class Node;
friend class Geode;
2005-04-26 17:58:19 +08:00
friend class StateSet;
2001-09-20 05:19:47 +08:00
2005-05-12 22:48:56 +08:00
BoundingBox _initialBound;
ref_ptr<ComputeBoundingBoxCallback> _computeBoundCallback;
mutable BoundingBox _boundingBox;
mutable bool _boundingBoxComputed;
2005-01-27 21:15:21 +08:00
2005-05-12 22:03:22 +08:00
ref_ptr<Shape> _shape;
2002-10-30 21:27:15 +08:00
2001-09-20 05:19:47 +08:00
bool _supportsDisplayList;
bool _useDisplayList;
2003-06-30 05:41:57 +08:00
bool _supportsVertexBufferObjects;
bool _useVertexBufferObjects;
2001-09-20 05:19:47 +08:00
2003-02-14 19:41:52 +08:00
typedef osg::buffered_value<GLuint> GLObjectList;
2003-06-30 05:41:57 +08:00
mutable GLObjectList _globjList;
2001-09-20 05:19:47 +08:00
2014-05-21 17:06:17 +08:00
ref_ptr<UpdateCallback> _drawableUpdateCallback;
ref_ptr<EventCallback> _drawableEventCallback;
2005-04-26 17:58:19 +08:00
2014-05-21 17:06:17 +08:00
ref_ptr<CullCallback> _drawableCullCallback;
2003-12-16 16:56:49 +08:00
ref_ptr<DrawCallback> _drawCallback;
2001-09-20 05:19:47 +08:00
};
2006-09-19 04:54:48 +08:00
inline void Drawable::draw(RenderInfo& renderInfo) const
2001-10-11 04:20:14 +08:00
{
2009-10-23 21:19:57 +08:00
#ifdef OSG_GL_DISPLAYLISTS_AVAILABLE
2006-09-19 04:54:48 +08:00
if (_useDisplayList && !(_supportsVertexBufferObjects && _useVertexBufferObjects && renderInfo.getState()->isVertexBufferObjectSupported()))
2001-10-11 04:20:14 +08:00
{
2005-01-27 21:15:21 +08:00
// get the contextID (user defined ID of 0 upwards) for the
2001-10-11 04:20:14 +08:00
// current OpenGL context.
2006-09-19 04:54:48 +08:00
unsigned int contextID = renderInfo.getContextID();
2001-10-11 04:20:14 +08:00
// get the globj for the current contextID.
2003-02-14 19:41:52 +08:00
GLuint& globj = _globjList[contextID];
2001-10-11 04:20:14 +08:00
// call the globj if already set otherwise compile and execute.
if( globj != 0 )
{
glCallList( globj );
}
else if (_useDisplayList)
{
2002-08-29 11:22:27 +08:00
#ifdef USE_SEPARATE_COMPILE_AND_EXECUTE
2004-09-22 05:33:52 +08:00
globj = generateDisplayList(contextID, getGLObjectSizeHint());
2001-11-02 20:26:33 +08:00
glNewList( globj, GL_COMPILE );
2002-02-07 09:07:11 +08:00
if (_drawCallback.valid())
2006-09-19 04:54:48 +08:00
_drawCallback->drawImplementation(renderInfo,this);
2005-01-27 21:15:21 +08:00
else
2006-09-19 04:54:48 +08:00
drawImplementation(renderInfo);
2001-11-02 20:26:33 +08:00
glEndList();
2005-01-27 21:15:21 +08:00
2001-11-02 20:26:33 +08:00
glCallList( globj);
#else
2004-09-22 05:33:52 +08:00
globj = generateDisplayList(contextID, getGLObjectSizeHint());
2001-10-11 04:20:14 +08:00
glNewList( globj, GL_COMPILE_AND_EXECUTE );
2002-02-07 09:07:11 +08:00
if (_drawCallback.valid())
2006-09-19 04:54:48 +08:00
_drawCallback->drawImplementation(renderInfo,this);
2005-01-27 21:15:21 +08:00
else
2006-09-19 04:54:48 +08:00
drawImplementation(renderInfo);
2001-10-11 04:20:14 +08:00
glEndList();
2001-11-02 20:26:33 +08:00
#endif
2001-10-11 04:20:14 +08:00
}
2005-01-27 21:15:21 +08:00
2003-06-30 05:41:57 +08:00
return;
2001-10-11 04:20:14 +08:00
}
2009-10-23 21:19:57 +08:00
#endif
2003-06-30 05:41:57 +08:00
// draw object as nature intended..
if (_drawCallback.valid())
2006-09-19 04:54:48 +08:00
_drawCallback->drawImplementation(renderInfo,this);
2005-01-27 21:15:21 +08:00
else
2006-09-19 04:54:48 +08:00
drawImplementation(renderInfo);
2006-05-02 00:05:16 +08:00
}
2002-06-26 04:27:51 +08:00
2001-10-11 04:20:14 +08:00
2002-02-03 20:33:41 +08:00
}
2001-09-20 05:19:47 +08:00
#endif