Added first cut of new primtive shapes support.

This commit is contained in:
Robert Osfield
2002-10-30 13:27:15 +00:00
parent 2e99fdacfc
commit da84f9b4aa
17 changed files with 1674 additions and 33 deletions

View File

@@ -20,6 +20,7 @@ class Node;
class Drawable;
class Array;
class PrimitiveSet;
class Shape;
/** Copy Op(erator) used to control the whether shallow or deep copy is used
* during copy construction and clone operation.*/
@@ -30,17 +31,18 @@ class SG_EXPORT CopyOp
enum Options
{
SHALLOW_COPY = 0,
DEEP_COPY_OBJECTS = 1,
DEEP_COPY_NODES = 2,
DEEP_COPY_DRAWABLES = 4,
DEEP_COPY_STATESETS = 8,
DEEP_COPY_STATEATTRIBUTES = 16,
DEEP_COPY_TEXTURES = 32,
DEEP_COPY_IMAGES = 64,
DEEP_COPY_ARRAYS = 128,
DEEP_COPY_PRIMITIVES = 256,
DEEP_COPY_ALL = 0xffffffff
SHALLOW_COPY = 0,
DEEP_COPY_OBJECTS = 1,
DEEP_COPY_NODES = 2,
DEEP_COPY_DRAWABLES = 4,
DEEP_COPY_STATESETS = 8,
DEEP_COPY_STATEATTRIBUTES = 16,
DEEP_COPY_TEXTURES = 32,
DEEP_COPY_IMAGES = 64,
DEEP_COPY_ARRAYS = 128,
DEEP_COPY_PRIMITIVES = 256,
DEEP_COPY_SHAPES = 512,
DEEP_COPY_ALL = 0xffffffff
};
typedef unsigned int CopyFlags;
@@ -58,6 +60,7 @@ class SG_EXPORT CopyOp
virtual Image* operator() (const Image* image) const;
virtual Array* operator() (const Array* array) const;
virtual PrimitiveSet* operator() (const PrimitiveSet* primitives) const;
virtual Shape* operator() (const Shape* shape) const;
protected:

View File

@@ -9,6 +9,7 @@
#include <osg/State>
#include <osg/Types>
#include <osg/NodeVisitor>
#include <osg/Shape>
#include <vector>
#include <map>
@@ -20,7 +21,6 @@ class Vec2;
class Vec3;
class Vec4;
class UByte4;
class Node;
class Geometry;
// this is define to alter the way display lists are compiled inside the
@@ -57,6 +57,7 @@ class SG_EXPORT Drawable : public Object
* Equivalent to dynamic_cast<const Geometry*>(this).*/
virtual const Geometry* asGeometry() const { return 0; }
/** A vector of osg::Node pointers which is used to store the parent(s) of drawable.*/
typedef std::vector<Node*> ParentList;
@@ -103,6 +104,36 @@ class SG_EXPORT Drawable : public Object
StateSet* getOrCreateStateSet();
/** 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.*/
void dirtyBound();
/** 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;
}
/** Set the Shape of the drawable. The shape can be used to
* speed up collision detection or as a guide for produral
* geometry generation - see osg::ProduralGeometry.*/
inline void setShape(Shape* shape) { _shape = shape; }
/** Get the Shape of the Drawable.*/
inline Shape* getShape() { return _shape.get(); }
/** Get the const Shape of the const Drawable.*/
inline const Shape* getShape() const { return _shape.get(); }
/** Set the drawable to it can or cannot be used in conjunction with OpenGL
* display lists. With set to true, calls to Drawable::setUseDisplayList,
* whereas when set to false, no display lists can be created and calls
@@ -126,23 +157,8 @@ class SG_EXPORT Drawable : public Object
/** Force a recompile on next draw() of any OpenGL display list associated with this geoset.*/
void dirtyDisplayList();
/** 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.*/
void dirtyBound();
/** 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
@@ -315,15 +331,17 @@ class SG_EXPORT Drawable : public Object
ref_ptr<StateSet> _stateset;
mutable BoundingBox _bbox;
mutable bool _bbox_computed;
ref_ptr<Shape> _shape;
bool _supportsDisplayList;
bool _useDisplayList;
typedef std::vector<uint> GLObjectList;
mutable GLObjectList _globjList;
mutable BoundingBox _bbox;
mutable bool _bbox_computed;
ref_ptr<AppCallback> _appCallback;
ref_ptr<DrawCallback> _drawCallback;
ref_ptr<CullCallback> _cullCallback;

View File

@@ -0,0 +1,150 @@
//C++ header - Open Scene Graph - Copyright (C) 1998-2002 Robert Osfield
//Distributed under the terms of the GNU Library General Public License (LGPL)
//as published by the Free Software Foundation.
#ifndef OSG_ProceduralGeometry
#define OSG_ProceduralGeometry 1
#include <osg/Drawable>
#include <osg/Vec2>
#include <osg/Vec3>
#include <osg/Vec4>
#include <osg/Array>
#include <osg/PrimitiveSet>
namespace osg {
class TessellationHints : public Object
{
public:
TessellationHints():
_TessellationMode(USE_SHAPE_DEFAULTS),
_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),
_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 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) { _createFrontFace=on; }
inline bool getCreateBackFace() const { return _createFrontFace; }
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;
unsigned int _targetNumFaces;
bool _createFrontFace;
bool _createBackFace;
bool _createNormals;
bool _createTextureCoords;
bool _createTop;
bool _createBody;
bool _createBottom;
};
class SG_EXPORT ProceduralGeometry : public Drawable
{
public:
ProceduralGeometry();
ProceduralGeometry(Shape* shape);
/** Copy constructor using CopyOp to manage deep vs shallow copy.*/
ProceduralGeometry(const ProceduralGeometry& pg,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
virtual Object* cloneType() const { return osgNew ProceduralGeometry(); }
virtual Object* clone(const CopyOp& copyop) const { return osgNew ProceduralGeometry(*this,copyop); }
virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const ProceduralGeometry*>(obj)!=NULL; }
virtual const char* libraryName() const { return "osg"; }
virtual const char* className() const { return "ProceduralGeometry"; }
void setTessellationHints(TessellationHints* hints) { _tessellationHints = hints; }
TessellationHints* getTessellationHints() { return _tessellationHints.get(); }
const TessellationHints* getTessellationHints() const { return _tessellationHints.get(); }
/** draw ProceduralGeometry 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 ProceduralGeometry for user-drawn objects.
*/
virtual void drawImmediateMode(State& state);
/** accept an AttributeFunctor and call its methods to tell it about the interal attributes that this Drawable has.*/
virtual void accept(AttributeFunctor& af);
/** accept a PrimtiveFunctor and call its methods to tell it about the interal primtives that this Drawable has.*/
virtual void accept(PrimitiveFunctor& pf);
protected:
ProceduralGeometry& operator = (const ProceduralGeometry&) { return *this;}
virtual ~ProceduralGeometry();
virtual bool computeBound() const;
ref_ptr<TessellationHints> _tessellationHints;
};
}
#endif

517
include/osg/Shape Normal file
View File

@@ -0,0 +1,517 @@
//C++ header - Open Scene Graph - Copyright (C) 1998-2002 Robert Osfield
//Distributed under the terms of the GNU Library General Public License (LGPL)
//as published by the Free Software Foundation.
#ifndef OSG_SHAPE
#define OSG_SHAPE 1
#include <osg/Object>
#include <osg/Vec3>
#include <osg/Quat>
#include <osg/Plane>
#include <osg/Array>
namespace osg {
// forward decare visitors.
class ShapeVisitor;
class ConstShapeVisitor;
/** META_StateAttribute macro define the standard clone, isSameKindAs,
* className and getType methods.
* Use when subclassing from Object to make it more convinient to define
* the standard pure virtual methods which are required for all Object
* subclasses.*/
#define META_Shape(library,name) \
virtual osg::Object* cloneType() const { return osgNew name(); } \
virtual osg::Object* clone(const osg::CopyOp& copyop) const { return osgNew name (*this,copyop); } \
virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const name *>(obj)!=NULL; } \
virtual const char* libraryName() const { return #library; } \
virtual const char* className() const { return #name; } \
virtual void accept(osg::ShapeVisitor& sv) { sv.apply(*this); } \
virtual void accept(osg::ConstShapeVisitor& csv) const { csv.apply(*this); }
/** Base class for all shape types.
* Shapes are used to either for culling and collision detection or
* to define the geometric shape of procedurally generate Geometry.
*/
class SG_EXPORT Shape : public Object
{
public:
Shape() {}
Shape(const Shape& sa,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
Object(sa,copyop) {}
/** Clone the type of an attribute, with Object* return type.
Must be defined by derived classes.*/
virtual Object* cloneType() const = 0;
/** Clone an attribute, with Object* return type.
Must be defined by derived classes.*/
virtual Object* clone(const CopyOp&) const = 0;
/** return true if this and obj are of the same kind of object.*/
virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const Shape*>(obj)!=NULL; }
/** return the name of the attribute's library.*/
virtual const char* libraryName() const { return "osg"; }
/** return the name of the attribute's class type.*/
virtual const char* className() const { return "Shape"; }
/** accept a non const shape visitor which can be used on non const shape objects.
Must be defined by derived classes.*/
virtual void accept(ShapeVisitor&)=0;
/** accept a const shape visitor which can be used on const shape objects.
Must be defined by derived classes.*/
virtual void accept(ConstShapeVisitor&) const =0;
};
// forward declartions of Shape types.
class Sphere;
class Box;
class Cone;
class Cylinder;
class InfinitePlane;
class TriangleMesh;
class ConvexHull;
class HeightField;
class CompositeShape;
class ShapeVisitor
{
public:
ShapeVisitor() {}
virtual void apply(Sphere&) {}
virtual void apply(Box&) {}
virtual void apply(Cone&) {}
virtual void apply(Cylinder&) {}
virtual void apply(InfinitePlane&) {}
virtual void apply(TriangleMesh&) {}
virtual void apply(ConvexHull&) {}
virtual void apply(HeightField&) {}
virtual void apply(CompositeShape&) {}
};
class ConstShapeVisitor
{
public:
ConstShapeVisitor() {}
virtual void apply(const Sphere&) {}
virtual void apply(const Box&) {}
virtual void apply(const Cone&) {}
virtual void apply(const Cylinder&) {}
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&) {}
};
class Sphere : public Shape
{
public:
Sphere():
_center(0.0f,0.0f,0.0f),
_radius(1.0f) {}
Sphere(const osg::Vec3& center,float radius):
_center(center),
_radius(radius) {}
Sphere(const Sphere& sphere,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
Shape(sphere,copyop),
_center(sphere._center),
_radius(sphere._radius) {}
META_Shape(osg, Sphere)
inline bool valid() const { return _radius>=0.0f; }
inline void set(const Vec3& center,float radius)
{
_center = center;
_radius = radius;
}
inline void setCenter(const Vec3& center) { _center = center; }
inline const Vec3& getCenter() const { return _center; }
inline void setRadius(float radius) { _radius = radius; }
inline float getRadius() const { return _radius; }
protected:
virtual ~Sphere() {}
Vec3 _center;
float _radius;
};
class Box : public Shape
{
public:
Box():
_center(0.0f,0.0f,0.0f),
_halfLengths(0.5f,0.5f,0.5f) {}
Box(const osg::Vec3& center,float width):
_center(center),
_halfLengths(width*0.5f,width*0.5f,width*0.5f) {}
Box(const osg::Vec3& center,float lengthX,float lengthY, float lengthZ):
_center(center),
_halfLengths(lengthX*0.5f,lengthY*0.5f,lengthZ*0.5f) {}
Box(const Box& box,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
Shape(box,copyop),
_center(box._center),
_halfLengths(box._halfLengths),
_rotation(box._rotation) {}
META_Shape(osg, Box)
inline bool valid() const { return _halfLengths.x()>=0.0f; }
inline void set(const Vec3& center,const Vec3& halfLengths)
{
_center = center;
_halfLengths = halfLengths;
}
inline void setCenter(const Vec3& center) { _center = center; }
inline const Vec3& getCenter() const { return _center; }
inline void setHalfLengths(const Vec3& halfLengths) { _halfLengths = halfLengths; }
inline const Vec3& getHalfLengths() const { return _halfLengths; }
inline void setRotation(const Quat& quat) { _rotation = quat; }
inline const Quat& getRotation() const { return _rotation; }
inline Matrix getRotationMatrix() const { Matrix matrix; _rotation.get(matrix); return matrix; }
inline bool zeroRotation() const { return _rotation.zeroRotation(); }
protected:
virtual ~Box() {}
Vec3 _center;
Vec3 _halfLengths;
Quat _rotation;
};
class Cone : public Shape
{
public:
Cone():
_center(0.0f,0.0f,0.0f),
_radius(1.0f),
_height(1.0f) {}
Cone(const osg::Vec3& center,float radius,float height):
_center(center),
_radius(radius),
_height(height) {}
Cone(const Cone& cone,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
Shape(cone,copyop),
_center(cone._center),
_radius(cone._radius),
_height(cone._height),
_rotation(cone._rotation) {}
META_Shape(osg, Cone)
inline bool valid() const { return _radius>=0.0f; }
inline void set(const Vec3& center,float radius, float height)
{
_center = center;
_radius = radius;
_height = height;
}
inline void setCenter(const Vec3& center) { _center = center; }
inline const Vec3& getCenter() const { return _center; }
inline void setRadius(float radius) { _radius = radius; }
inline float getRadius() const { return _radius; }
inline void setHeight(float height) { _height = height; }
inline float getHeight() const { return _height; }
inline void setRotation(const Quat& quat) { _rotation = quat; }
inline const Quat& getRotation() const { return _rotation; }
inline Matrix getRotationMatrix() const { Matrix matrix; _rotation.get(matrix); return matrix; }
inline bool zeroRotation() const { return _rotation.zeroRotation(); }
inline float getBaseOffsetFactor() const { return 0.25f; }
inline float getBaseOffset() const { return -getBaseOffsetFactor()*getHeight(); }
protected:
virtual ~Cone() {}
Vec3 _center;
float _radius;
float _height;
Quat _rotation;
};
class Cylinder : public Shape
{
public:
Cylinder():
_center(0.0f,0.0f,0.0f),
_radius(-1.0f),
_height(0.0f) {}
Cylinder(const osg::Vec3& center,float radius,float height):
_center(center),
_radius(radius),
_height(height) {}
Cylinder(const Cylinder& cylinder,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
Shape(cylinder,copyop),
_center(cylinder._center),
_radius(cylinder._radius),
_height(cylinder._height),
_rotation(cylinder._rotation) {}
META_Shape(osg, Cylinder)
inline bool valid() const { return _radius>=0.0f; }
inline void set(const Vec3& center,float radius, float height)
{
_center = center;
_radius = radius;
_height = height;
}
inline void setCenter(const Vec3& center) { _center = center; }
inline const Vec3& getCenter() const { return _center; }
inline void setRadius(float radius) { _radius = radius; }
inline float getRadius() const { return _radius; }
inline void setHeight(float height) { _height = height; }
inline float getHeight() const { return _height; }
inline void setRotation(const Quat& quat) { _rotation = quat; }
inline const Quat& getRotation() const { return _rotation; }
inline Matrix getRotationMatrix() const { Matrix matrix; _rotation.get(matrix); return matrix; }
bool zeroRotation() const { return _rotation.zeroRotation(); }
protected:
virtual ~Cylinder() {}
Vec3 _center;
float _radius;
float _height;
Quat _rotation;
};
class InfinitePlane : public Shape, public Plane
{
public:
};
class TriangleMesh : public Shape
{
public:
TriangleMesh() {}
TriangleMesh(const TriangleMesh& mesh,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
Shape(mesh,copyop),
_vertices(mesh._vertices),
_indices(mesh._indices) {}
META_Shape(osg, TriangleMesh)
void setVertices(Vec3Array* vertices) { _vertices = vertices; }
Vec3Array* getVertices() { return _vertices.get(); }
const Vec3Array* getVertices() const { return _vertices.get(); }
void setIndices(IndexArray* indices) { _indices = indices; }
IndexArray* getIndices() { return _indices.get(); }
const IndexArray* getIndices() const { return _indices.get(); }
protected:
~TriangleMesh() {}
ref_ptr<Vec3Array> _vertices;
ref_ptr<IndexArray> _indices;
};
class ConvexHull : public TriangleMesh
{
public:
ConvexHull() {}
ConvexHull(const ConvexHull& hull,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
TriangleMesh(hull,copyop) {}
META_Shape(osg, TriangleMesh)
protected:
~ConvexHull() {}
};
class HeightField : public Shape
{
public:
HeightField() {}
HeightField(const HeightField& mesh,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
Shape(mesh,copyop),
_zeroRotation(true),
_columns(0),
_rows(0),
_origin(0.0f,0.0f,0.0f),
_dx(1.0f),
_dy(1.0f) {}
virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const HeightField*>(obj)!=NULL; }
virtual const char* libraryName() const { return "osg"; }
virtual const char* className() const { return "HeightFiled"; }
virtual void accept(osg::ShapeVisitor& sv) { sv.apply(*this); }
virtual void accept(osg::ConstShapeVisitor& csv) const { csv.apply(*this); }
virtual void setNumColumnsAndRows(unsigned int col,unsigned int rows) = 0;
inline unsigned int getNumColumns() const { return _columns; }
inline unsigned int getNumRows() const { return _rows; }
inline void setOrigin(const osg::Vec3& origin) { _origin = origin; }
inline const osg::Vec3& getOrigin() const { return _origin; }
inline void setXInterval(float dx) { _dx = dx; }
inline float getXInterval() const { return _dx; }
inline void setYInterval(float dy) { _dy = dy; }
inline float getYInterval() const { return _dy; }
virtual void setHeight(unsigned int c,unsigned int r) const = 0;
virtual float getHeight(unsigned int c,unsigned int r) const = 0;
virtual void setNormal(unsigned int c,unsigned int r,const osg::Vec3& normal) const = 0;
virtual Vec3 getNormal(unsigned int c,unsigned int r) const = 0;
inline Vec3 getVertex(unsigned int c,unsigned int r) const
{
return Vec3(_origin.x()+_dx*(float)c,
_origin.y()+_dy*(float)r,
_origin.z()+getHeight(c,r));
}
inline void setRotation(const Quat& quat) { _rotation = quat; }
inline const Quat& getRotation() const { return _rotation; }
inline Matrix getRotationMatrix() const { Matrix matrix; _rotation.get(matrix); return matrix; }
inline bool zeroRotation() const { return _rotation.zeroRotation(); }
protected:
~HeightField() {}
bool _zeroRotation;
unsigned int _columns,_rows;
osg::Vec3 _origin;
float _dx;
float _dy;
Quat _rotation;
};
class CompositeShape : public Shape
{
public:
typedef std::vector< ref_ptr<Shape> > ChildList;
CompositeShape();
CompositeShape(const CompositeShape& cs,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
Shape(cs,copyop),
_children(cs._children) {}
META_Shape(osg, CompositeShape)
/** Set the shape that encloses all of the children.*/
void setShape(Shape* shape) { _shape = shape; }
/** Get the shape that encloses all of the children.*/
Shape* getShape() { return _shape.get(); }
/** Get the const shape that encloses all of the children.*/
const Shape* getShape() const { return _shape.get(); }
/** Get the number of children of this composite shape.*/
unsigned int getNumChildren() const { return _children.size(); }
/** Get a child.*/
Shape* getChild(unsigned int i) { return _children[i].get(); }
/** Get a const child.*/
const Shape* getChild(unsigned int i) const { return _children[i].get(); }
/** Add a child to the list.*/
void addChild(Shape* shape) { _children.push_back(shape); }
/** remove a child from the list.*/
void removeChild(unsigned int i) { _children.erase(_children.begin()+i); }
/** find the index number of child, if child is not found then it returns getNumChildren(),
* so should be used in similar sytle of STL's result!=end().*/
unsigned int findChildNo(Shape* shape);
protected:
~CompositeShape() {}
ref_ptr<Shape> _shape;
ChildList _children;
};
}
#endif