2002-07-17 04:07:32 +08:00
|
|
|
//C++ header - Open Scene Graph - Copyright (C) 1998-2002 Robert Osfield
|
2001-10-04 23:12:57 +08:00
|
|
|
//Distributed under the terms of the GNU Library General Public License (LGPL)
|
|
|
|
//as published by the Free Software Foundation.
|
|
|
|
|
2001-09-20 05:19:47 +08:00
|
|
|
#ifndef OSG_DRAWABLE
|
|
|
|
#define OSG_DRAWABLE 1
|
|
|
|
|
|
|
|
#include <osg/BoundingBox>
|
|
|
|
#include <osg/State>
|
|
|
|
#include <osg/Types>
|
2001-10-13 04:05:55 +08:00
|
|
|
#include <osg/Vec2>
|
2002-03-14 06:44:22 +08:00
|
|
|
#include <osg/NodeVisitor>
|
2001-09-20 05:19:47 +08:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
|
|
|
#include <set>
|
|
|
|
|
|
|
|
namespace osg {
|
|
|
|
|
2001-10-11 04:20:14 +08:00
|
|
|
class Vec2;
|
|
|
|
class Vec3;
|
|
|
|
class Vec4;
|
2002-02-09 06:55:21 +08:00
|
|
|
class Node;
|
2001-10-11 04:20:14 +08:00
|
|
|
|
2001-11-02 20:26:33 +08:00
|
|
|
// this is define to alter the way display lists are compiled inside the
|
|
|
|
// the draw method, it has been found that the NVidia drivers fail completely
|
|
|
|
// to optimize COMPILE_AND_EXECUTE in fact make it go slower than for no display
|
|
|
|
// lists, but optimize a seperate COMPILE very well?! Define it as default
|
|
|
|
// the use of a sperate COMPILE, then glCallList rather than use COMPILE_AND_EXECUTE.
|
2001-10-11 04:20:14 +08:00
|
|
|
|
2001-11-02 20:26:33 +08:00
|
|
|
#define USE_SEPERATE_COMPILE_AND_EXECUTE
|
2001-10-11 04:20:14 +08:00
|
|
|
|
2001-09-29 04:10:41 +08:00
|
|
|
/** Pure virtual base class for drawable Geometry. Contains no drawing primitives
|
2002-06-26 04:27:51 +08:00
|
|
|
directly, these are provided by subclasses such as osg::Geometry. State attributes
|
2001-09-20 05:19:47 +08:00
|
|
|
for a Drawable are maintained in StateSet which the Drawable maintains
|
|
|
|
a referenced counted pointer to. Both Drawable's and StateSet's can
|
|
|
|
be shared for optimal memory usage and graphics performance.
|
|
|
|
*/
|
|
|
|
class SG_EXPORT Drawable : public Object
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
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
|
|
|
|
2001-09-20 05:19:47 +08:00
|
|
|
virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const Drawable*>(obj)!=NULL; }
|
2002-06-06 21:25:36 +08:00
|
|
|
virtual const char* libraryName() const { return "osg"; }
|
2001-09-20 05:19:47 +08:00
|
|
|
virtual const char* className() const { return "Drawable"; }
|
|
|
|
|
2002-02-09 06:55:21 +08:00
|
|
|
|
|
|
|
/** A vector of osg::Node pointers which is used to store the parent(s) of drawable.*/
|
|
|
|
typedef std::vector<Node*> ParentList;
|
|
|
|
|
|
|
|
/** Get the parent list of drawable. */
|
|
|
|
inline const ParentList& getParents() const { return _parents; }
|
|
|
|
|
|
|
|
/** Get the a copy of parent list of node. A copy is returned to
|
|
|
|
* prevent modification of the parent list.*/
|
|
|
|
inline ParentList getParents() { return _parents; }
|
|
|
|
|
|
|
|
/** Get a single parent of Drawable.
|
|
|
|
* @param i index of the parent to get.
|
|
|
|
* @return the parent i.
|
|
|
|
*/
|
2002-04-26 16:16:14 +08:00
|
|
|
inline Node* getParent(const unsigned int i) { return _parents[i]; }
|
2002-02-09 06:55:21 +08:00
|
|
|
/** Get a single const parent of Drawable.
|
|
|
|
* @param i index of the parent to get.
|
|
|
|
* @return the parent i.
|
|
|
|
*/
|
2002-04-26 16:16:14 +08:00
|
|
|
inline const Node* getParent(const unsigned int i) const { return _parents[i]; }
|
2002-02-09 06:55:21 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the number of parents of node.
|
|
|
|
* @return the number of parents of this node.
|
|
|
|
*/
|
2002-04-26 16:16:14 +08:00
|
|
|
inline const unsigned int getNumParents() const { return _parents.size(); }
|
2002-02-09 06:55:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
2001-09-20 05:19:47 +08:00
|
|
|
/** Set the StateSet attached to the Drawable.
|
|
|
|
Previously attached StateSet are automatically unreferenced on
|
|
|
|
assignment of a new drawstate.*/
|
|
|
|
inline void setStateSet(StateSet *state) { _dstate = state; }
|
|
|
|
|
|
|
|
/** Get the attached StateSet.*/
|
|
|
|
inline StateSet* getStateSet() { return _dstate.get();}
|
|
|
|
|
|
|
|
/** Get the attached const StateSet.*/
|
|
|
|
inline const StateSet* getStateSet() const { return _dstate.get();}
|
|
|
|
|
|
|
|
|
2001-09-29 04:10:41 +08:00
|
|
|
/** Set the drawable to it can or cannot be used in conjunction with OpenGL
|
2001-09-20 05:19:47 +08:00
|
|
|
* display lists. With set to true, calls to Drawable::setUseDisplayList,
|
|
|
|
* whereas when set to false, no display lists can be created and calls
|
|
|
|
* to setUseDisplayList are ignored, and a warning is produced. The later
|
|
|
|
* 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.*/
|
|
|
|
void setSupportsDisplayList(const bool flag);
|
|
|
|
|
2001-09-29 04:10:41 +08:00
|
|
|
/** Get whether display lists are supported for this drawable instance.*/
|
2001-09-20 05:19:47 +08:00
|
|
|
inline const bool getSupportsDisplayList() const { return _supportsDisplayList; }
|
|
|
|
|
|
|
|
|
|
|
|
/** When set to true, force the draw method to use OpenGL Display List for rendering.
|
|
|
|
If false rendering directly. If the display list has not been already
|
|
|
|
compile the next call to draw will automatically create the display list.*/
|
|
|
|
void setUseDisplayList(const bool flag);
|
|
|
|
|
|
|
|
/** Return whether OpenGL display lists are being used for rendering.*/
|
|
|
|
inline const bool getUseDisplayList() const { return _useDisplayList; }
|
|
|
|
|
|
|
|
/** Force a recompile on next draw() of any OpenGL display list associated with this geoset.*/
|
|
|
|
void dirtyDisplayList();
|
|
|
|
|
2001-10-11 04:20:14 +08:00
|
|
|
|
2001-10-13 04:05:55 +08:00
|
|
|
/** Dirty the bounding box, forcing a computeBound() on the next call
|
|
|
|
* to getBound(). Should be called in the internal geometry of the Drawable
|
|
|
|
* is modified.*/
|
2002-02-09 06:55:21 +08:00
|
|
|
void dirtyBound();
|
2001-09-20 05:19:47 +08:00
|
|
|
|
|
|
|
/** get bounding box of geoset.
|
|
|
|
* Note, now made virtual to make it possible to implement user-drawn
|
|
|
|
* objects albeit so what crudely, to be improved later.
|
|
|
|
*/
|
|
|
|
inline const BoundingBox& getBound() const
|
|
|
|
{
|
|
|
|
if( !_bbox_computed)
|
|
|
|
computeBound();
|
|
|
|
return _bbox;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** draw OpenGL primitives.
|
|
|
|
* If the drawable has _useDisplayList set to true then use an OpenGL display
|
|
|
|
* list, automatically compiling one if required.
|
|
|
|
* Otherwise call drawImmediateMode().
|
2001-10-11 04:20:14 +08:00
|
|
|
* Note, draw method should *not* be overridden in subclasses as it
|
2001-09-20 05:19:47 +08:00
|
|
|
* manages the optional display list.
|
|
|
|
*/
|
2001-10-11 04:20:14 +08:00
|
|
|
inline void draw(State& state);
|
2001-09-20 05:19:47 +08:00
|
|
|
|
|
|
|
/** Immediately compile this drawable into an OpenGL Display List.
|
2001-09-29 04:10:41 +08:00
|
|
|
Note I, operation is ignored if _useDisplayList to false.
|
|
|
|
Note II, compile is not intended to be overridden in subclasses.*/
|
2001-09-20 05:19:47 +08:00
|
|
|
void compile(State& state);
|
|
|
|
|
2002-02-07 09:07:11 +08:00
|
|
|
|
2002-07-10 23:35:47 +08:00
|
|
|
struct AppCallback : public osg::Referenced
|
|
|
|
{
|
|
|
|
/** do customized app code.*/
|
2002-07-11 04:30:57 +08:00
|
|
|
virtual void app(osg::NodeVisitor *visitor, osg::Drawable* drawable) = 0;
|
2002-07-10 23:35:47 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Set the AppCallback which allows users to attach customize the undating of an object during the app traversal.*/
|
|
|
|
void setAppCallback(AppCallback* ac);
|
|
|
|
|
|
|
|
/** Get the non const AppCallback.*/
|
|
|
|
AppCallback* getAppCallback() { return _appCallback.get(); }
|
|
|
|
|
|
|
|
/** Get the const AppCallback.*/
|
|
|
|
const AppCallback* getAppCallback() const { return _appCallback.get(); }
|
|
|
|
|
|
|
|
|
|
|
|
struct CullCallback : public osg::Referenced
|
|
|
|
{
|
|
|
|
/** do customized cull code.*/
|
|
|
|
virtual bool cull(osg::NodeVisitor *visitor, osg::Drawable* drawable, osg::State *state=NULL) const = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Set the CullCallback which allows users to attach customize the culling of Drawable during the cull traversal.*/
|
|
|
|
void setCullCallback(CullCallback* cc) { _cullCallback=cc; }
|
|
|
|
|
|
|
|
/** Get the non const CullCallback.*/
|
|
|
|
CullCallback* getCullCallback() { return _cullCallback.get(); }
|
|
|
|
|
|
|
|
/** Get the const CullCallback.*/
|
|
|
|
const CullCallback* getCullCallback() const { return _cullCallback.get(); }
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
* The draw callback is implement as a replacement to the Drawable's own drawImmediateMode() method, if the
|
|
|
|
* the user intends to decorate the exist draw code then simple call the drawable->drawImmediateMode() from
|
|
|
|
* with the callbacks drawImmediateMode() method. This allows the users to do both pre and post callbacks
|
|
|
|
* without fuss and can even diable the inner draw in required.*/
|
|
|
|
struct DrawCallback : public osg::Referenced
|
|
|
|
{
|
|
|
|
/** do customized draw code.*/
|
2002-03-14 06:44:22 +08:00
|
|
|
virtual void drawImmediateMode(State& state,osg::Drawable* drawable) const = 0;
|
2002-02-07 09:07:11 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Set the DrawCallback which allows users to attach customize the drawing of existing Drawable object.*/
|
2002-02-07 16:07:33 +08:00
|
|
|
void setDrawCallback(DrawCallback* dc) { _drawCallback=dc; dirtyDisplayList(); }
|
2002-02-07 09:07:11 +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(); }
|
|
|
|
|
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(); }
|
|
|
|
|
|
|
|
|
2001-09-20 05:19:47 +08:00
|
|
|
/** draw directly ignoring an OpenGL display list which could be attached.
|
|
|
|
* This is the internal draw method which does the drawing itself,
|
|
|
|
* and is the method to override when deriving from Drawable.
|
|
|
|
*/
|
|
|
|
virtual void drawImmediateMode(State& state) = 0;
|
|
|
|
|
2002-02-07 09:07:11 +08:00
|
|
|
|
|
|
|
|
2001-09-20 05:19:47 +08:00
|
|
|
/** use deleteDisplayList instead of glDeleteList to allow
|
|
|
|
* OpenGL display list to cached until they can be deleted
|
|
|
|
* by the OpenGL context in which they were created, specified
|
|
|
|
* by contextID.*/
|
|
|
|
static void deleteDisplayList(uint contextID,uint globj);
|
|
|
|
|
|
|
|
/** flush all the cached display list which need to be deleted
|
|
|
|
* in the OpenGL context related to contextID.*/
|
|
|
|
static void flushDeletedDisplayLists(uint contextID);
|
2001-10-11 04:20:14 +08:00
|
|
|
|
2001-10-13 04:05:55 +08:00
|
|
|
typedef uint AttributeBitMask;
|
|
|
|
|
|
|
|
enum AttributeBitMaskValues
|
2001-10-11 04:20:14 +08:00
|
|
|
{
|
|
|
|
COORDS = 0x1,
|
|
|
|
NORMALS = 0x2,
|
|
|
|
COLORS = 0x4,
|
|
|
|
TEXTURE_COORDS = 0x8,
|
|
|
|
TEXTURE_COORDS_0 = TEXTURE_COORDS,
|
2001-10-14 14:01:31 +08:00
|
|
|
TEXTURE_COORDS_1 = 0x10,
|
|
|
|
TEXTURE_COORDS_2 = 0x20,
|
|
|
|
TEXTURE_COORDS_3 = 0x40
|
2001-10-11 04:20:14 +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:
|
|
|
|
|
|
|
|
AttributeFunctor(AttributeBitMask abm):_abm(abm) {}
|
|
|
|
virtual ~AttributeFunctor() {}
|
|
|
|
|
|
|
|
void setAttributeBitMask(AttributeBitMask abm) { _abm=abm; }
|
|
|
|
AttributeBitMask getAttributeBitMask() const { return _abm; }
|
|
|
|
|
2001-10-13 04:05:55 +08:00
|
|
|
virtual bool apply(AttributeBitMask,Vec2*,Vec2*) { return false; }
|
|
|
|
virtual bool apply(AttributeBitMask,Vec3*,Vec3*) { return false; }
|
|
|
|
virtual bool apply(AttributeBitMask,Vec4*,Vec4*) { return false; }
|
2001-10-13 19:16:10 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
AttributeBitMask _abm;
|
2001-10-11 04:20:14 +08:00
|
|
|
};
|
|
|
|
|
2001-10-13 19:16:10 +08:00
|
|
|
/** return the attributes supported by applyAttrbuteOperation() as an AttributeBitMask.*/
|
|
|
|
virtual AttributeBitMask suppportsAttributeOperation() const { return (AttributeBitMask)0; }
|
2001-10-11 04:20:14 +08:00
|
|
|
|
2001-10-13 04:05:55 +08:00
|
|
|
/** return the attributes successully applied in applyAttributeUpdate.*/
|
2001-10-13 19:16:10 +08:00
|
|
|
virtual AttributeBitMask applyAttributeOperation(AttributeFunctor&) { return 0; }
|
2001-10-11 04:20:14 +08:00
|
|
|
|
2001-09-20 05:19:47 +08:00
|
|
|
|
2002-06-26 04:27:51 +08:00
|
|
|
class PrimitiveFunctor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
virtual void setVertexArray(unsigned int count,Vec3* vertices) = 0;
|
|
|
|
|
|
|
|
virtual void drawArrays(GLenum mode,GLint first,GLsizei count) = 0;
|
2002-07-07 22:40:41 +08:00
|
|
|
virtual void drawElements(GLenum mode,GLsizei count,GLubyte* indices) = 0;
|
|
|
|
virtual void drawElements(GLenum mode,GLsizei count,GLushort* indices) = 0;
|
|
|
|
virtual void drawElements(GLenum mode,GLsizei count,GLuint* indices) = 0;
|
2002-06-26 04:27:51 +08:00
|
|
|
|
|
|
|
virtual void begin(GLenum mode) = 0;
|
|
|
|
virtual void vertex(const Vec3& vert) = 0;
|
|
|
|
virtual void vertex(float x,float y,float z) = 0;
|
|
|
|
virtual void end() = 0;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/** apply the internal geometry as basic primitives to a PrimitiveFunctor.*/
|
|
|
|
virtual void applyPrimitiveOperation(PrimitiveFunctor&) {}
|
2002-02-07 09:07:11 +08:00
|
|
|
|
|
|
|
|
2001-09-20 05:19:47 +08:00
|
|
|
protected:
|
|
|
|
|
|
|
|
Drawable& operator = (const Drawable&) { return *this;}
|
|
|
|
|
|
|
|
virtual ~Drawable();
|
|
|
|
|
|
|
|
/** compute the bounding box of the drawable. Method must be
|
2001-09-29 04:10:41 +08:00
|
|
|
implemented by subclasses.*/
|
2001-09-20 05:19:47 +08:00
|
|
|
virtual const bool computeBound() const = 0;
|
2002-02-09 06:55:21 +08:00
|
|
|
|
|
|
|
void addParent(osg::Node* node);
|
|
|
|
void removeParent(osg::Node* node);
|
|
|
|
|
|
|
|
ParentList _parents;
|
|
|
|
friend class Node;
|
|
|
|
friend class Geode;
|
2001-09-20 05:19:47 +08:00
|
|
|
|
2002-02-09 06:55:21 +08:00
|
|
|
ref_ptr<StateSet> _dstate;
|
2001-09-20 05:19:47 +08:00
|
|
|
|
|
|
|
bool _supportsDisplayList;
|
|
|
|
bool _useDisplayList;
|
|
|
|
|
|
|
|
typedef std::vector<uint> GLObjectList;
|
|
|
|
mutable GLObjectList _globjList;
|
|
|
|
|
|
|
|
mutable BoundingBox _bbox;
|
|
|
|
mutable bool _bbox_computed;
|
|
|
|
|
2002-07-10 23:35:47 +08:00
|
|
|
ref_ptr<AppCallback> _appCallback;
|
2002-02-07 09:07:11 +08:00
|
|
|
ref_ptr<DrawCallback> _drawCallback;
|
2002-03-14 06:44:22 +08:00
|
|
|
ref_ptr<CullCallback> _cullCallback;
|
2002-02-07 09:07:11 +08:00
|
|
|
|
2001-09-20 05:19:47 +08:00
|
|
|
// static cache of deleted display lists which can only
|
|
|
|
// by completely deleted once the appropriate OpenGL context
|
|
|
|
// is set.
|
|
|
|
typedef std::map<uint,std::set<uint> > DeletedDisplayListCache;
|
|
|
|
static DeletedDisplayListCache s_deletedDisplayListCache;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2001-10-11 04:20:14 +08:00
|
|
|
inline void Drawable::draw(State& state)
|
|
|
|
{
|
|
|
|
if (_useDisplayList)
|
|
|
|
{
|
|
|
|
|
|
|
|
// get the contextID (user defined ID of 0 upwards) for the
|
|
|
|
// current OpenGL context.
|
|
|
|
uint contextID = state.getContextID();
|
|
|
|
|
|
|
|
// fill in array if required.
|
|
|
|
while (_globjList.size()<=contextID) _globjList.push_back(0);
|
|
|
|
|
|
|
|
// get the globj for the current contextID.
|
|
|
|
uint& globj = _globjList[contextID];
|
|
|
|
|
|
|
|
// call the globj if already set otherwise compile and execute.
|
|
|
|
if( globj != 0 )
|
|
|
|
{
|
|
|
|
glCallList( globj );
|
|
|
|
}
|
|
|
|
else if (_useDisplayList)
|
|
|
|
{
|
2001-11-02 20:26:33 +08:00
|
|
|
#ifdef USE_SEPERATE_COMPILE_AND_EXECUTE
|
|
|
|
globj = glGenLists( 1 );
|
|
|
|
glNewList( globj, GL_COMPILE );
|
2002-02-07 09:07:11 +08:00
|
|
|
if (_drawCallback.valid())
|
|
|
|
_drawCallback->drawImmediateMode(state,this);
|
|
|
|
else
|
|
|
|
drawImmediateMode(state);
|
2001-11-02 20:26:33 +08:00
|
|
|
glEndList();
|
|
|
|
|
|
|
|
glCallList( globj);
|
|
|
|
#else
|
2001-10-11 04:20:14 +08:00
|
|
|
globj = glGenLists( 1 );
|
|
|
|
glNewList( globj, GL_COMPILE_AND_EXECUTE );
|
2002-02-07 09:07:11 +08:00
|
|
|
if (_drawCallback.valid())
|
|
|
|
_drawCallback->drawImmediateMode(state,this);
|
|
|
|
else
|
|
|
|
drawImmediateMode(state);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// draw object as nature intended..
|
2002-02-07 09:07:11 +08:00
|
|
|
if (_drawCallback.valid())
|
|
|
|
_drawCallback->drawImmediateMode(state,this);
|
|
|
|
else
|
|
|
|
drawImmediateMode(state);
|
2001-10-11 04:20:14 +08:00
|
|
|
}
|
2002-06-26 04:27:51 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
class TriangleFunctor : public Drawable::PrimitiveFunctor, public T
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
TriangleFunctor()
|
|
|
|
{
|
|
|
|
_vertexArraySize=0;
|
|
|
|
_vertexArrayPtr=0;
|
|
|
|
_modeCache=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~TriangleFunctor() {}
|
|
|
|
|
|
|
|
virtual void setVertexArray(unsigned int count,Vec3* vertices)
|
|
|
|
{
|
|
|
|
_vertexArraySize = count;
|
|
|
|
_vertexArrayPtr = vertices;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void drawArrays(GLenum mode,GLint first,GLsizei count)
|
|
|
|
{
|
|
|
|
if (_vertexArrayPtr==0 && count==0) return;
|
|
|
|
|
|
|
|
switch(mode)
|
|
|
|
{
|
|
|
|
case(GL_TRIANGLES):
|
|
|
|
{
|
|
|
|
Vec3* vlast = &_vertexArrayPtr[first+count];
|
|
|
|
for(Vec3* vptr=&_vertexArrayPtr[first];vptr<vlast;vptr+=3)
|
|
|
|
operator()(*(vptr),*(vptr+1),*(vptr+2));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case(GL_TRIANGLE_STRIP):
|
|
|
|
{
|
|
|
|
Vec3* vptr = &_vertexArrayPtr[first];
|
|
|
|
for(GLsizei i=2;i<count;++i,++vptr)
|
|
|
|
{
|
|
|
|
if ((i%2)) operator()(*(vptr),*(vptr+2),*(vptr+1));
|
|
|
|
else operator()(*(vptr),*(vptr+1),*(vptr+2));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case(GL_QUADS):
|
|
|
|
{
|
|
|
|
Vec3* vptr = &_vertexArrayPtr[first];
|
|
|
|
for(GLsizei i=3;i<count;i+=4,vptr+=4)
|
|
|
|
{
|
|
|
|
operator()(*(vptr),*(vptr+1),*(vptr+2));
|
|
|
|
operator()(*(vptr),*(vptr+2),*(vptr+3));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case(GL_QUAD_STRIP):
|
|
|
|
{
|
|
|
|
Vec3* vptr = &_vertexArrayPtr[first];
|
|
|
|
for(GLsizei i=3;i<count;i+=2,vptr+=2)
|
|
|
|
{
|
|
|
|
operator()(*(vptr),*(vptr+1),*(vptr+2));
|
2002-07-04 17:49:12 +08:00
|
|
|
operator()(*(vptr+1),*(vptr+3),*(vptr+2));
|
2002-06-26 04:27:51 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case(GL_POLYGON): // treat polygons as GL_TRIANGLE_FAN
|
|
|
|
case(GL_TRIANGLE_FAN):
|
|
|
|
{
|
|
|
|
Vec3* vfirst = &_vertexArrayPtr[first];
|
|
|
|
Vec3* vptr = vfirst+1;
|
|
|
|
for(GLsizei i=2;i<count;++i,++vptr)
|
|
|
|
{
|
|
|
|
operator()(*(vfirst),*(vptr),*(vptr+1));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case(GL_POINTS):
|
|
|
|
case(GL_LINES):
|
|
|
|
case(GL_LINE_STRIP):
|
|
|
|
case(GL_LINE_LOOP):
|
|
|
|
default:
|
|
|
|
// can't be converted into to triangles.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-07 22:40:41 +08:00
|
|
|
virtual void drawElements(GLenum mode,GLsizei count,GLubyte* indices)
|
2002-06-26 04:27:51 +08:00
|
|
|
{
|
|
|
|
if (indices==0 || count==0) return;
|
|
|
|
|
2002-07-11 06:33:08 +08:00
|
|
|
typedef GLubyte* IndexPointer;
|
2002-06-26 04:27:51 +08:00
|
|
|
|
|
|
|
switch(mode)
|
|
|
|
{
|
|
|
|
case(GL_TRIANGLES):
|
|
|
|
{
|
|
|
|
IndexPointer ilast = &indices[count];
|
|
|
|
for(IndexPointer iptr=indices;iptr<ilast;iptr+=3)
|
|
|
|
operator()(_vertexArrayPtr[*iptr],_vertexArrayPtr[*(iptr+1)],_vertexArrayPtr[*(iptr+2)]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case(GL_TRIANGLE_STRIP):
|
|
|
|
{
|
|
|
|
IndexPointer iptr = indices;
|
|
|
|
for(GLsizei i=2;i<count;++i,++iptr)
|
|
|
|
{
|
|
|
|
if ((i%2)) operator()(_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+2)],_vertexArrayPtr[*(iptr+1)]);
|
|
|
|
else operator()(_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+1)],_vertexArrayPtr[*(iptr+2)]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case(GL_QUADS):
|
|
|
|
{
|
|
|
|
IndexPointer iptr = indices;
|
|
|
|
for(GLsizei i=3;i<count;i+=4,iptr+=4)
|
|
|
|
{
|
|
|
|
operator()(_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+1)],_vertexArrayPtr[*(iptr+2)]);
|
|
|
|
operator()(_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+2)],_vertexArrayPtr[*(iptr+3)]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case(GL_QUAD_STRIP):
|
|
|
|
{
|
|
|
|
IndexPointer iptr = indices;
|
|
|
|
for(GLsizei i=3;i<count;i+=2,iptr+=2)
|
|
|
|
{
|
|
|
|
operator()(_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+1)],_vertexArrayPtr[*(iptr+2)]);
|
2002-07-04 17:49:12 +08:00
|
|
|
operator()(_vertexArrayPtr[*(iptr+1)],_vertexArrayPtr[*(iptr+3)],_vertexArrayPtr[*(iptr+2)]);
|
2002-06-26 04:27:51 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case(GL_POLYGON): // treat polygons as GL_TRIANGLE_FAN
|
|
|
|
case(GL_TRIANGLE_FAN):
|
|
|
|
{
|
|
|
|
IndexPointer iptr = indices;
|
|
|
|
Vec3& vfirst = _vertexArrayPtr[*iptr];
|
|
|
|
++iptr;
|
|
|
|
for(GLsizei i=2;i<count;++i,++iptr)
|
|
|
|
{
|
|
|
|
operator()(vfirst,_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+1)]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case(GL_POINTS):
|
|
|
|
case(GL_LINES):
|
|
|
|
case(GL_LINE_STRIP):
|
|
|
|
case(GL_LINE_LOOP):
|
|
|
|
default:
|
|
|
|
// can't be converted into to triangles.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-07 22:40:41 +08:00
|
|
|
virtual void drawElements(GLenum mode,GLsizei count,GLushort* indices)
|
2002-06-26 04:27:51 +08:00
|
|
|
{
|
|
|
|
if (indices==0 || count==0) return;
|
|
|
|
|
2002-07-11 06:33:08 +08:00
|
|
|
typedef GLushort* IndexPointer;
|
2002-06-26 04:27:51 +08:00
|
|
|
|
|
|
|
switch(mode)
|
|
|
|
{
|
|
|
|
case(GL_TRIANGLES):
|
|
|
|
{
|
|
|
|
IndexPointer ilast = &indices[count];
|
|
|
|
for(IndexPointer iptr=indices;iptr<ilast;iptr+=3)
|
|
|
|
{
|
|
|
|
operator()(_vertexArrayPtr[*iptr],_vertexArrayPtr[*(iptr+1)],_vertexArrayPtr[*(iptr+2)]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case(GL_TRIANGLE_STRIP):
|
|
|
|
{
|
|
|
|
IndexPointer iptr = indices;
|
|
|
|
for(GLsizei i=2;i<count;++i,++iptr)
|
|
|
|
{
|
|
|
|
if ((i%2)) operator()(_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+2)],_vertexArrayPtr[*(iptr+1)]);
|
|
|
|
else operator()(_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+1)],_vertexArrayPtr[*(iptr+2)]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case(GL_QUADS):
|
|
|
|
{
|
|
|
|
IndexPointer iptr = indices;
|
|
|
|
for(GLsizei i=3;i<count;i+=4,iptr+=4)
|
|
|
|
{
|
|
|
|
operator()(_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+1)],_vertexArrayPtr[*(iptr+2)]);
|
|
|
|
operator()(_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+2)],_vertexArrayPtr[*(iptr+3)]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case(GL_QUAD_STRIP):
|
|
|
|
{
|
|
|
|
IndexPointer iptr = indices;
|
|
|
|
for(GLsizei i=3;i<count;i+=2,iptr+=2)
|
|
|
|
{
|
|
|
|
operator()(_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+1)],_vertexArrayPtr[*(iptr+2)]);
|
2002-07-04 17:49:12 +08:00
|
|
|
operator()(_vertexArrayPtr[*(iptr+1)],_vertexArrayPtr[*(iptr+3)],_vertexArrayPtr[*(iptr+2)]);
|
2002-06-26 04:27:51 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case(GL_POLYGON): // treat polygons as GL_TRIANGLE_FAN
|
|
|
|
case(GL_TRIANGLE_FAN):
|
|
|
|
{
|
|
|
|
IndexPointer iptr = indices;
|
|
|
|
Vec3& vfirst = _vertexArrayPtr[*iptr];
|
|
|
|
++iptr;
|
|
|
|
for(GLsizei i=2;i<count;++i,++iptr)
|
|
|
|
{
|
|
|
|
operator()(vfirst,_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+1)]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case(GL_POINTS):
|
|
|
|
case(GL_LINES):
|
|
|
|
case(GL_LINE_STRIP):
|
|
|
|
case(GL_LINE_LOOP):
|
|
|
|
default:
|
|
|
|
// can't be converted into to triangles.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-07 22:40:41 +08:00
|
|
|
virtual void drawElements(GLenum mode,GLsizei count,GLuint* indices)
|
2002-06-26 04:27:51 +08:00
|
|
|
{
|
|
|
|
if (indices==0 || count==0) return;
|
|
|
|
|
2002-07-11 06:33:08 +08:00
|
|
|
typedef GLuint* IndexPointer;
|
2002-06-26 04:27:51 +08:00
|
|
|
|
|
|
|
switch(mode)
|
|
|
|
{
|
|
|
|
case(GL_TRIANGLES):
|
|
|
|
{
|
|
|
|
IndexPointer ilast = &indices[count];
|
|
|
|
for(IndexPointer iptr=indices;iptr<ilast;iptr+=3)
|
|
|
|
operator()(_vertexArrayPtr[*iptr],_vertexArrayPtr[*(iptr+1)],_vertexArrayPtr[*(iptr+2)]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case(GL_TRIANGLE_STRIP):
|
|
|
|
{
|
|
|
|
IndexPointer iptr = indices;
|
|
|
|
for(GLsizei i=2;i<count;++i,++iptr)
|
|
|
|
{
|
|
|
|
if ((i%2)) operator()(_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+2)],_vertexArrayPtr[*(iptr+1)]);
|
|
|
|
else operator()(_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+1)],_vertexArrayPtr[*(iptr+2)]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case(GL_QUADS):
|
|
|
|
{
|
|
|
|
IndexPointer iptr = indices;
|
|
|
|
for(GLsizei i=3;i<count;i+=4,iptr+=4)
|
|
|
|
{
|
|
|
|
operator()(_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+1)],_vertexArrayPtr[*(iptr+2)]);
|
|
|
|
operator()(_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+2)],_vertexArrayPtr[*(iptr+3)]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case(GL_QUAD_STRIP):
|
|
|
|
{
|
|
|
|
IndexPointer iptr = indices;
|
|
|
|
for(GLsizei i=3;i<count;i+=2,iptr+=2)
|
|
|
|
{
|
|
|
|
operator()(_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+1)],_vertexArrayPtr[*(iptr+2)]);
|
2002-07-04 17:49:12 +08:00
|
|
|
operator()(_vertexArrayPtr[*(iptr+1)],_vertexArrayPtr[*(iptr+3)],_vertexArrayPtr[*(iptr+2)]);
|
2002-06-26 04:27:51 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case(GL_POLYGON): // treat polygons as GL_TRIANGLE_FAN
|
|
|
|
case(GL_TRIANGLE_FAN):
|
|
|
|
{
|
|
|
|
IndexPointer iptr = indices;
|
|
|
|
Vec3& vfirst = _vertexArrayPtr[*iptr];
|
|
|
|
++iptr;
|
|
|
|
for(GLsizei i=2;i<count;++i,++iptr)
|
|
|
|
{
|
|
|
|
operator()(vfirst,_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+1)]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case(GL_POINTS):
|
|
|
|
case(GL_LINES):
|
|
|
|
case(GL_LINE_STRIP):
|
|
|
|
case(GL_LINE_LOOP):
|
|
|
|
default:
|
|
|
|
// can't be converted into to triangles.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** begin(..),vertex(..) & end() are convinience methods for adapting
|
|
|
|
* non vertex array primitives to vertex array based primitives.
|
|
|
|
* this is done to simplify the implementation of primtive functor
|
|
|
|
* subclasses - users only need override drawArray and drawElements.*/
|
|
|
|
inline void begin(GLenum mode)
|
|
|
|
{
|
|
|
|
_modeCache = mode;
|
|
|
|
_vertexCache.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void vertex(const Vec3& vert) { _vertexCache.push_back(vert); }
|
|
|
|
inline void vertex(float x,float y,float z) { _vertexCache.push_back(osg::Vec3(x,y,z)); }
|
|
|
|
inline void end()
|
|
|
|
{
|
|
|
|
if (!_vertexCache.empty())
|
|
|
|
{
|
|
|
|
setVertexArray(_vertexCache.size(),&_vertexCache.front());
|
|
|
|
drawArrays(_modeCache,0,_vertexCache.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
|
|
|
|
unsigned int _vertexArraySize;
|
|
|
|
Vec3* _vertexArrayPtr;
|
|
|
|
|
|
|
|
GLenum _modeCache;
|
|
|
|
std::vector<Vec3> _vertexCache;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
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
|