Added conversion to osg::GeoSet::converToGeometry() utility to help the

migration to and testing of the new osg::Geometry class.
This commit is contained in:
Robert Osfield
2002-06-23 21:43:46 +00:00
parent bbc129e5c6
commit f2e215bf44
10 changed files with 1022 additions and 88 deletions

View File

@@ -12,6 +12,10 @@
namespace osg {
// forward declare so that we don't need to include the header.
class Geometry;
/** Encapsulates OpenGL drawing primitives, geometry and
optional binding of normal, color and texture coordinates. Used
for representing the visible objects in the scene. State attributes
@@ -334,6 +338,9 @@ class SG_EXPORT GeoSet : public Drawable
virtual AttributeBitMask applyAttributeOperation(AttributeFunctor& auf);
/** convinience function for converting GeoSet's to equivilant Geometry nodes.*/
Geometry* convertToGeometry();
protected:

View File

@@ -88,8 +88,18 @@ class TemplateArray : public AttributeArray, public std::vector<T>
TemplateArray() : AttributeArray(ARRAYTYPE,DataSize,DataType) {}
TemplateArray(const TemplateArray& ta,const CopyOp& copyop=CopyOp::SHALLOW_COPY) : AttributeArray(ta,copyop), std::vector<T>(ta) {}
TemplateArray(const TemplateArray& ta,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
AttributeArray(ta,copyop),
std::vector<T>(ta) {}
TemplateArray(unsigned int no,T* ptr) :
AttributeArray(ARRAYTYPE,DataSize,DataType),
std::vector<T>(ptr,ptr+no) {}
TemplateArray(T* first,T* last) :
AttributeArray(ARRAYTYPE,DataSize,DataType),
std::vector<T>(first,last) {}
virtual Object* cloneType() const { return osgNew TemplateArray(); }
virtual Object* clone(const CopyOp& copyop) const { return osgNew TemplateArray(*this,copyop); }
virtual const char* libraryName() const { return "osg"; }
@@ -115,7 +125,7 @@ typedef TemplateArray<Vec4,Vec4ArrayType,4,GL_FLOAT> Vec4Ar
enum PrimitiveType
{
PrimitivePrimitiveType = 0,
DrawArrayPrimitiveType = 1,
DrawArraysPrimitiveType = 1,
UByteDrawElementsPrimitiveType = 2,
UShortDrawElementsPrimitiveType = 3,
UIntDrawElementsPrimitiveType = 4,
@@ -124,7 +134,7 @@ enum PrimitiveType
static char* s_PrimitiveNames[] =
{
"Primitive", // 0
"DrawArray", // 1
"DrawArrays", // 1
"UByteDrawElements", // 2
"UShortDrawElements", // 3
"UIntDrawElements" // 4
@@ -164,31 +174,31 @@ class Primitive : public Object
PrimitiveType _primitiveType;
};
class DrawArray : public Primitive
class DrawArrays : public Primitive
{
public:
DrawArray():
Primitive(DrawArrayPrimitiveType)
DrawArrays():
Primitive(DrawArraysPrimitiveType)
{}
DrawArray(GLenum mode, GLint first, GLsizei count):
Primitive(DrawArrayPrimitiveType),
DrawArrays(GLenum mode, GLint first, GLsizei count):
Primitive(DrawArraysPrimitiveType),
_mode(mode),
_first(first),
_count(count) {}
DrawArray(const DrawArray& da,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
DrawArrays(const DrawArrays& da,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
Primitive(da,copyop),
_mode(da._mode),
_first(da._first),
_count(da._count) {}
virtual Object* cloneType() const { return osgNew DrawArray(); }
virtual Object* clone(const CopyOp& copyop) const { return osgNew DrawArray(*this,copyop); }
virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const DrawArray*>(obj)!=NULL; }
virtual Object* cloneType() const { return osgNew DrawArrays(); }
virtual Object* clone(const CopyOp& copyop) const { return osgNew DrawArrays(*this,copyop); }
virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const DrawArrays*>(obj)!=NULL; }
virtual const char* libraryName() const { return "osg"; }
virtual const char* className() const { return "DrawArray"; }
virtual const char* className() const { return "DrawArrays"; }
virtual void draw() const
{
@@ -200,15 +210,33 @@ class DrawArray : public Primitive
GLsizei _count;
};
template<typename T, int PRIMTYPE, int DataType>
template<typename T, PrimitiveType PRIMTYPE, int DataType>
class DrawElements : public Primitive, public std::vector<T>
{
public:
DrawElements():Primitive(PRIMTYPE),_dataType(_dataType) {}
DrawElements(GLenum mode=0):
Primitive(PRIMTYPE),
_mode(mode),
_dataType(DataType) {}
DrawElements(const DrawElements& array,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
Primitive(array,copyop) {}
Primitive(array,copyop),
std::vector<T>(array),
_mode(array._mode),
_dataType(array._dataType) {}
DrawElements(GLenum mode,unsigned int no,T* ptr) :
Primitive(PRIMTYPE),
std::vector<T>(ptr,ptr+no),
_mode(mode),
_dataType(DataType) {}
DrawElements(GLenum mode, T* first,T* last) :
Primitive(PRIMTYPE),
std::vector<T>(first,last),
_mode(mode),
_dataType(DataType) {}
virtual Object* cloneType() const { return osgNew DrawElements(); }
virtual Object* clone(const CopyOp& copyop) const { return osgNew DrawElements(*this,copyop); }
@@ -252,10 +280,10 @@ class SG_EXPORT Geometry : public Drawable
enum AttributeBinding
{
OFF=0,
OVERALL,
PER_PRIMITIVE,
PER_VERTEX,
BIND_OFF=0,
BIND_OVERALL,
BIND_PER_PRIMITIVE,
BIND_PER_VERTEX,
};
void setVertexArray(Vec3Array* array) { _vertexArray = array; }
@@ -266,7 +294,7 @@ class SG_EXPORT Geometry : public Drawable
void setNormalBinding(AttributeBinding ab) { _normalBinding = ab; }
AttributeBinding getNormalBinding() const { return _normalBinding; }
void setNormalArray(Vec3Array* array) { _normalArray = array; if (!_normalArray.valid()) _normalBinding=OFF; }
void setNormalArray(Vec3Array* array) { _normalArray = array; if (!_normalArray.valid()) _normalBinding=BIND_OFF; }
Vec3Array* getNormalArray() { return _normalArray.get(); }
const Vec3Array* getNormalArray() const { return _normalArray.get(); }
@@ -274,7 +302,7 @@ class SG_EXPORT Geometry : public Drawable
void setColorBinding(AttributeBinding ab) { _colorBinding = ab; }
AttributeBinding getColorBinding() const { return _colorBinding; }
void setColorArray(AttributeArray* array) { _colorArray = array; if (!_colorArray.valid()) _colorBinding=OFF; }
void setColorArray(AttributeArray* array) { _colorArray = array; if (!_colorArray.valid()) _colorBinding=BIND_OFF; }
AttributeArray* getColorArray() { return _colorArray.get(); }
const AttributeArray* getColorArray() const { return _colorArray.get(); }
@@ -292,7 +320,7 @@ class SG_EXPORT Geometry : public Drawable
PrimitiveList& getPrimitiveList() { return _primitives; }
const PrimitiveList& getPrimitiveList() const { return _primitives; }
void addPrimtive(Primitive* primitive) { if (primitive) _primitives.push_back(primitive); }
void addPrimitive(Primitive* primitive) { if (primitive) _primitives.push_back(primitive); }

View File

@@ -274,7 +274,7 @@ class SG_EXPORT Texture : public StateAttribute
{
// pad out handle list if required.
if (_handleList.size()<=contextID)
_handleList.resize(contextID,0);
_handleList.resize(contextID+1,0);
// get the globj for the current contextID.
return _handleList[contextID];

View File

@@ -95,6 +95,12 @@ class ref_ptr
inline const T* get() const { return _ptr; }
/** take control over the object pointed to by ref_ptr, unreference but do not delete even if ref count goes to 0,
* return the pointer to the object.
* Note, do not use this unless you are 100% sure your code handles the deletion of the object correctly, and
* only use when absolutely required.*/
inline T* take() { T* tmp=_ptr; if (_ptr) _ptr->unref_nodelete(); _ptr=0; return tmp;}
private:
T* _ptr;
};

View File

@@ -42,7 +42,19 @@ class OSGUTIL_EXPORT Optimizer
virtual void optimize(osg::Node* node, unsigned int options = ALL_OPTIMIZATIONS);
/** ConvertGeoSetsToGeometryVisitor all the old GeoSet Drawables to the new Geometry Drawables.*/
class OSGUTIL_EXPORT ConvertGeoSetsToGeometryVisitor : public osg::NodeVisitor
{
public:
ConvertGeoSetsToGeometryVisitor():osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) {}
virtual void apply(osg::Geode& geode);
virtual void apply(osg::Node& node) { traverse(node); }
};
/** Flatten Static Trasform nodes by applying their transform to the
* geometry on the leaves of the scene graph, then removing the
* now redundent transforms.*/
@@ -133,6 +145,113 @@ class OSGUTIL_EXPORT Optimizer
};
/** Remove the lowest static transforms in the scene.*/
class OSGUTIL_EXPORT RemoveLowestStaticTransformsVisitor : public osg::NodeVisitor
{
public:
RemoveLowestStaticTransformsVisitor(bool ignoreDynamicTransforms=true):
osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
_ignoreDynamicTransforms(ignoreDynamicTransforms) {}
virtual void apply(osg::Geode& geode);
virtual void apply(osg::Billboard& billboard);
virtual void apply(osg::LOD& lod);
virtual void apply(osg::Transform& transform);
bool removeTransforms();
typedef std::vector<osg::Transform*> TransformStack;
typedef std::vector<osg::Matrix> MatrixStack;
protected:
struct TransformStruct
{
typedef std::set<osg::Object*> ObjectSet;
TransformStruct():_containsTransform(false),_canBeApplied(true) {}
void add(osg::Object* obj) { _objectSet.insert(obj); }
bool _containsTransform;
bool _canBeApplied;
ObjectSet _objectSet;
};
struct ObjectStruct
{
typedef std::set<osg::Transform*> TransformSet;
ObjectStruct():_canBeApplied(true),_matrixSet(false),_moreThanOneMatrixRequired(false) {}
void add(osg::Transform* transform,osg::Matrix& matrix)
{
_transformSet.insert(transform);
if (!_matrixSet)
{
_matrixSet = true;
_moreThanOneMatrixRequired = false;
_matrix = matrix;
}
else if (_matrix!=matrix)
{
_moreThanOneMatrixRequired = true;
}
}
bool _canBeApplied;
bool _matrixSet;
bool _moreThanOneMatrixRequired;
osg::Matrix _matrix;
TransformSet _transformSet;
};
typedef std::map<osg::Transform*,TransformStruct> TransformMap;
typedef std::map<osg::Object*,ObjectStruct> ObjectMap;
void disableObject(osg::Object* object)
{
disableObject(_objectMap.find(object));
}
void disableObject(ObjectMap::iterator itr);
void disableTransform(osg::Transform* transform);
void doTransform(osg::Object* obj,osg::Matrix& matrix);
bool _ignoreDynamicTransforms;
MatrixStack _matrixStack;
TransformStack _transformStack;
TransformMap _transformMap;
ObjectMap _objectMap;
};
/** Remove rendundent nodes, such as groups with one single child.*/
class OSGUTIL_EXPORT RemoveEmptyNodesVisitor : public osg::NodeVisitor
{
public:
typedef std::set<osg::Node*> NodeList;
NodeList _redundentNodeList;
RemoveEmptyNodesVisitor():osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) {}
virtual void apply(osg::Geode& geode);
virtual void apply(osg::Group& group);
void removeEmptyNodes();
};
/** Remove rendundent nodes, such as groups with one single child.*/
class OSGUTIL_EXPORT RemoveRedundentNodesVisitor : public osg::NodeVisitor
{
@@ -142,8 +261,9 @@ class OSGUTIL_EXPORT Optimizer
NodeList _redundentNodeList;
RemoveRedundentNodesVisitor():osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) {}
virtual void apply(osg::Group& group);
virtual void apply(osg::Transform& transform);
void removeRedundentNodes();