Refactored ShapeDrawable so that it is subclassed from osg::Geometry rather than osg::Drawable.

Created a BuildShapeGeometryVisitor visitor that can create osg::Geometry for osg::Shape objects
This commit is contained in:
Robert Osfield 2016-08-17 21:34:32 +01:00
parent 3be951f279
commit 83b5cabac9
5 changed files with 1210 additions and 2079 deletions

View File

@ -185,7 +185,7 @@ class OSG_EXPORT Drawable : public Node
* geometry generation. * geometry generation.
* @see osg::Shape. * @see osg::Shape.
*/ */
inline void setShape(Shape* shape) { _shape = shape; } virtual void setShape(Shape* shape) { _shape = shape; }
template<class T> void setShape(const ref_ptr<T>& shape) { setShape(shape.get()); } template<class T> void setShape(const ref_ptr<T>& shape) { setShape(shape.get()); }

View File

@ -650,6 +650,157 @@ class OSG_EXPORT CompositeShape : public Shape
}; };
/** Describe several hints that can be passed to a Tessellator (like the one used
* by \c ShapeDrawable) as a mean to try to influence the way it works.
*/
class TessellationHints : public Object
{
public:
TessellationHints():
_TessellationMode(USE_SHAPE_DEFAULTS),
_detailRatio(1.0f),
_targetNumFaces(100),
_createFrontFace(true),
_createBackFace(false),
_createNormals(true),
_createTextureCoords(false),
_createTop(true),
_createBody(true),
_createBottom(true) {}
TessellationHints(const TessellationHints& tess, const CopyOp& copyop=CopyOp::SHALLOW_COPY):
Object(tess,copyop),
_TessellationMode(tess._TessellationMode),
_detailRatio(tess._detailRatio),
_targetNumFaces(tess._targetNumFaces),
_createFrontFace(tess._createFrontFace),
_createBackFace(tess._createBackFace),
_createNormals(tess._createNormals),
_createTextureCoords(tess._createTextureCoords),
_createTop(tess._createTop),
_createBody(tess._createBody),
_createBottom(tess._createBottom) {}
META_Object(osg,TessellationHints);
enum TessellationMode
{
USE_SHAPE_DEFAULTS,
USE_TARGET_NUM_FACES
};
inline void setTessellationMode(TessellationMode mode) { _TessellationMode=mode; }
inline TessellationMode getTessellationMode() const { return _TessellationMode; }
inline void setDetailRatio(float ratio) { _detailRatio = ratio; }
inline float getDetailRatio() const { return _detailRatio; }
inline void setTargetNumFaces(unsigned int target) { _targetNumFaces=target; }
inline unsigned int getTargetNumFaces() const { return _targetNumFaces; }
inline void setCreateFrontFace(bool on) { _createFrontFace=on; }
inline bool getCreateFrontFace() const { return _createFrontFace; }
inline void setCreateBackFace(bool on) { _createBackFace=on; }
inline bool getCreateBackFace() const { return _createBackFace; }
inline void setCreateNormals(bool on) { _createNormals=on; }
inline bool getCreateNormals() const { return _createNormals; }
inline void setCreateTextureCoords(bool on) { _createTextureCoords=on; }
inline bool getCreateTextureCoords() const { return _createTextureCoords; }
inline void setCreateTop(bool on) { _createTop=on; }
inline bool getCreateTop() const { return _createTop; }
inline void setCreateBody(bool on) { _createBody=on; }
inline bool getCreateBody() const { return _createBody; }
inline void setCreateBottom(bool on) { _createBottom=on; }
inline bool getCreateBottom() const { return _createBottom; }
protected:
~TessellationHints() {}
TessellationMode _TessellationMode;
float _detailRatio;
unsigned int _targetNumFaces;
bool _createFrontFace;
bool _createBackFace;
bool _createNormals;
bool _createTextureCoords;
bool _createTop;
bool _createBody;
bool _createBottom;
};
// forward declare;
class Geometry;
/** Convinience class for populating an osg::Geomtry with vertex, normals, texture coords and primitives that can render a Shape. */
class OSG_EXPORT BuildShapeGeometryVisitor : public ConstShapeVisitor
{
public:
BuildShapeGeometryVisitor(Geometry* geometry, TessellationHints* hints);
virtual void apply(const Sphere&);
virtual void apply(const Box&);
virtual void apply(const Cone&);
virtual void apply(const Cylinder&);
virtual void apply(const Capsule&);
virtual void apply(const InfinitePlane&);
virtual void apply(const TriangleMesh&);
virtual void apply(const ConvexHull&);
virtual void apply(const HeightField&);
virtual void apply(const CompositeShape&);
void Normal(const Vec3f& v) { _normals->push_back(v); }
void Normal3f(float x, float y, float z) { _normals->push_back(Vec3(x,y,z)); }
void TexCoord2f(float x, float y) { _texcoords->push_back(Vec2(x,y)); }
void Vertex(const Vec3f& v) { _vertices->push_back(v); }
void Vertex3f(float x, float y, float z) { _vertices->push_back(Vec3(x,y,z)); }
void setMatrix(const osg::Matrixd& m);
void Begin(GLenum mode);
void End();
protected:
BuildShapeGeometryVisitor& operator = (const BuildShapeGeometryVisitor&) { return *this; }
enum SphereHalf { SphereTopHalf, SphereBottomHalf };
// helpers for apply( Cylinder | Sphere | Capsule )
void drawCylinderBody(unsigned int numSegments, float radius, float height);
void drawHalfSphere(unsigned int numSegments, unsigned int numRows, float radius, SphereHalf which, float zOffset = 0.0f);
Geometry* _geometry;
const TessellationHints* _hints;
ref_ptr<Vec3Array> _vertices;
ref_ptr<Vec3Array> _normals;
ref_ptr<Vec2Array> _texcoords;
GLenum _mode;
unsigned int _start_index;
Matrixd _matrix;
Matrixd _inverse;
};
} }
#endif #endif

View File

@ -14,108 +14,10 @@
#ifndef OSG_SHAPEDRAWABLE #ifndef OSG_SHAPEDRAWABLE
#define OSG_SHAPEDRAWABLE 1 #define OSG_SHAPEDRAWABLE 1
#include <osg/Drawable> #include <osg/Geometry>
#include <osg/Vec2>
#include <osg/Vec3>
#include <osg/Vec4>
#include <osg/Array>
#include <osg/PrimitiveSet>
namespace osg { namespace osg {
/** Describe several hints that can be passed to a Tessellator (like the one used
* by \c ShapeDrawable) as a mean to try to influence the way it works.
*/
class TessellationHints : public Object
{
public:
TessellationHints():
_TessellationMode(USE_SHAPE_DEFAULTS),
_detailRatio(1.0f),
_targetNumFaces(100),
_createFrontFace(true),
_createBackFace(false),
_createNormals(true),
_createTextureCoords(false),
_createTop(true),
_createBody(true),
_createBottom(true) {}
TessellationHints(const TessellationHints& tess, const CopyOp& copyop=CopyOp::SHALLOW_COPY):
Object(tess,copyop),
_TessellationMode(tess._TessellationMode),
_detailRatio(tess._detailRatio),
_targetNumFaces(tess._targetNumFaces),
_createFrontFace(tess._createFrontFace),
_createBackFace(tess._createBackFace),
_createNormals(tess._createNormals),
_createTextureCoords(tess._createTextureCoords),
_createTop(tess._createTop),
_createBody(tess._createBody),
_createBottom(tess._createBottom) {}
META_Object(osg,TessellationHints);
enum TessellationMode
{
USE_SHAPE_DEFAULTS,
USE_TARGET_NUM_FACES
};
inline void setTessellationMode(TessellationMode mode) { _TessellationMode=mode; }
inline TessellationMode getTessellationMode() const { return _TessellationMode; }
inline void setDetailRatio(float ratio) { _detailRatio = ratio; }
inline float getDetailRatio() const { return _detailRatio; }
inline void setTargetNumFaces(unsigned int target) { _targetNumFaces=target; }
inline unsigned int getTargetNumFaces() const { return _targetNumFaces; }
inline void setCreateFrontFace(bool on) { _createFrontFace=on; }
inline bool getCreateFrontFace() const { return _createFrontFace; }
inline void setCreateBackFace(bool on) { _createBackFace=on; }
inline bool getCreateBackFace() const { return _createBackFace; }
inline void setCreateNormals(bool on) { _createNormals=on; }
inline bool getCreateNormals() const { return _createNormals; }
inline void setCreateTextureCoords(bool on) { _createTextureCoords=on; }
inline bool getCreateTextureCoords() const { return _createTextureCoords; }
inline void setCreateTop(bool on) { _createTop=on; }
inline bool getCreateTop() const { return _createTop; }
inline void setCreateBody(bool on) { _createBody=on; }
inline bool getCreateBody() const { return _createBody; }
inline void setCreateBottom(bool on) { _createBottom=on; }
inline bool getCreateBottom() const { return _createBottom; }
protected:
~TessellationHints() {}
TessellationMode _TessellationMode;
float _detailRatio;
unsigned int _targetNumFaces;
bool _createFrontFace;
bool _createBackFace;
bool _createNormals;
bool _createTextureCoords;
bool _createTop;
bool _createBody;
bool _createBottom;
};
/** Allow the use of <tt>Shape</tt>s as <tt>Drawable</tt>s, so that they can /** Allow the use of <tt>Shape</tt>s as <tt>Drawable</tt>s, so that they can
* be rendered with reduced effort. The implementation of \c ShapeDrawable is * be rendered with reduced effort. The implementation of \c ShapeDrawable is
@ -124,7 +26,7 @@ class TessellationHints : public Object
* as the right way to render basic shapes in some efficiency-critical section * as the right way to render basic shapes in some efficiency-critical section
* of code. * of code.
*/ */
class OSG_EXPORT ShapeDrawable : public Drawable class OSG_EXPORT ShapeDrawable : public osg::Geometry
{ {
public: public:
@ -133,11 +35,8 @@ class OSG_EXPORT ShapeDrawable : public Drawable
ShapeDrawable(Shape* shape, TessellationHints* hints=0); ShapeDrawable(Shape* shape, TessellationHints* hints=0);
template<class T> ShapeDrawable(const ref_ptr<T>& shape, TessellationHints* hints=0): template<class T> ShapeDrawable(const ref_ptr<T>& shape, TessellationHints* hints=0):
_color(1.0f,1.0f,1.0f,1.0f),
_tessellationHints(hints) { setShape(shape.get()); } _tessellationHints(hints) { setShape(shape.get()); }
/** Copy constructor using CopyOp to manage deep vs shallow copy.*/ /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
ShapeDrawable(const ShapeDrawable& pg,const CopyOp& copyop=CopyOp::SHALLOW_COPY); ShapeDrawable(const ShapeDrawable& pg,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
@ -147,6 +46,8 @@ class OSG_EXPORT ShapeDrawable : public Drawable
virtual const char* libraryName() const { return "osg"; } virtual const char* libraryName() const { return "osg"; }
virtual const char* className() const { return "ShapeDrawable"; } virtual const char* className() const { return "ShapeDrawable"; }
virtual void setShape(Shape* shape);
/** Set the color of the shape.*/ /** Set the color of the shape.*/
void setColor(const Vec4& color); void setColor(const Vec4& color);
@ -158,36 +59,8 @@ class OSG_EXPORT ShapeDrawable : public Drawable
TessellationHints* getTessellationHints() { return _tessellationHints.get(); } TessellationHints* getTessellationHints() { return _tessellationHints.get(); }
const TessellationHints* getTessellationHints() const { return _tessellationHints.get(); } const TessellationHints* getTessellationHints() const { return _tessellationHints.get(); }
/** method to invoke to rebuild the geometry that renders the shape.*/
void build();
/** Draw ShapeDrawable 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
* ShapeDrawable for user-drawn objects.
*/
virtual void drawImplementation(RenderInfo& renderInfo) const;
/* Not all virtual overloads of these methods are overridden in this class, so
bring the base class implementation in to avoid hiding the non-used ones. */
using Drawable::supports;
using Drawable::accept;
/** Return false, osg::ShapeDrawable does not support accept(AttributeFunctor&).*/
virtual bool supports(const AttributeFunctor&) const { return false; }
/** Return true, osg::ShapeDrawable does support accept(Drawable::ConstAttributeFunctor&).*/
virtual bool supports(const Drawable::ConstAttributeFunctor&) const { return true; }
/** Accept a Drawable::ConstAttributeFunctor and call its methods to tell it about the internal attributes that this Drawable has.*/
virtual void accept(Drawable::ConstAttributeFunctor& af) const;
/** Return true, osg::ShapeDrawable does support accept(PrimitiveFunctor&) .*/
virtual bool supports(const PrimitiveFunctor&) const { return true; }
/** Accept a PrimitiveFunctor and call its methods to tell it about the internal primitives that this Drawable has.*/
virtual void accept(PrimitiveFunctor& pf) const;
virtual BoundingBox computeBoundingBox() const;
protected: protected:

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff