Added new osggeometry demo to test the work on the new osg::Geometry Drawable.

This commit is contained in:
Robert Osfield
2002-06-21 16:45:45 +00:00
parent 53b6d0c92e
commit 49ab8f4706
8 changed files with 956 additions and 248 deletions

View File

@@ -0,0 +1,15 @@
TOPDIR = ../../..
include $(TOPDIR)/Make/makedefs
CXXFILES =\
osggeometry.cpp\
LIBS += $(OSG_LIBS) $(GLUT_LIB) $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS)
INSTFILES = \
$(CXXFILES)\
Makefile.inst=Makefile
EXEC = osggeometry
include $(TOPDIR)/Make/makerules

View File

@@ -0,0 +1,11 @@
TOPDIR = ../..
include $(TOPDIR)/Make/makedefs
CXXFILES =\
osggeometry.cpp\
LIBS += $(OSG_LIBS) $(GLUT_LIB) $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS)
EXEC = osggeometry
include $(TOPDIR)/Make/makerules

View File

@@ -0,0 +1,300 @@
#include <osg/Geode>
#include <osg/GeoSet>
#include <osg/Geometry>
#include <osg/Material>
#include <osg/Vec3>
#include <osg/Transform>
#include <osgGA/TrackballManipulator>
#include <osgGLUT/Viewer>
#include <osgGLUT/glut>
#include <osg/Math>
// ----------------------------------------------------------------------
// Global variables - this is basically the stuff which will be animated
// ----------------------------------------------------------------------
class MyTransformCallback : public osg::NodeCallback{
public:
MyTransformCallback(osg::Transform* node,float angularVelocity)
{
_nodeToOperateOn = node;
_angular_velocity = angularVelocity;
_previousTraversalNumber = -1;
_orig_t = _timer.tick();
}
virtual void operator() (osg::Node* node, osg::NodeVisitor* nv)
{
if (nv)
{
if (_nodeToOperateOn && node==_nodeToOperateOn)
{
// ensure that we do not operate on this node more than
// once during this traversal. This is an issue since node
// can be shared between multiple parents.
if (nv->getTraversalNumber()!=_previousTraversalNumber)
{
osg::Timer_t new_t = _timer.tick();
float delta_angle = _angular_velocity*_timer.delta_s(_orig_t,new_t);
osg::Matrix matrix;
matrix.makeRotate(delta_angle,1.0f,1.0f,1.0f);
matrix *= osg::Matrix::translate(1.0f,0.0f,0.0f);
matrix *= osg::Matrix::rotate(delta_angle,0.0f,0.0f,1.0f);
_nodeToOperateOn->setMatrix(matrix);
_previousTraversalNumber = nv->getTraversalNumber();
// Some memory stress testing added by Steve to reveal crashes under Windows.
// // Start Added by SMW
// osg::Transform* Tnode = (osg::Transform *)node;
// int i;
// osg::Node *n;
// while (Tnode->getNumChildren() > 0)
// {
// n = Tnode->getChild(0);
// Tnode->removeChild(n);
// }
// for (i = 0;i < 500;i++)
// {
// Tnode->addChild( createCube() );
// }
// // End Added by SMW
}
}
}
// must continue subgraph traversal.
traverse(node,nv);
}
protected:
osg::Transform* _nodeToOperateOn;
float _angular_velocity;
int _previousTraversalNumber;
osg::Timer _timer;
osg::Timer_t _orig_t;
};
osg::Geode* createCube()
{
osg::Geode* geode = new osg::Geode();
// -------------------------------------------
// Set up a new GeoSet which will be our cube
// -------------------------------------------
osg::GeoSet* cube = new osg::GeoSet();
// set up the primitives
cube->setPrimType( osg::GeoSet::POLYGON );
cube->setNumPrims( 6 ); // the six square faces
// set up the primitive indices
int* cubeLengthList = new int[6];
cubeLengthList[0] = 4; // each side of the cube has 4 vertices
cubeLengthList[1] = 4;
cubeLengthList[2] = 4;
cubeLengthList[3] = 4;
cubeLengthList[4] = 4;
cubeLengthList[5] = 4;
cube->setPrimLengths( cubeLengthList );
// set up the coordinates.
osg::Vec3 *cubeCoords = new osg::Vec3[24];
cubeCoords[0].set( -1.0000f, 1.0000f, -1.000f );
cubeCoords[1].set( 1.0000f, 1.0000f, -1.0000f );
cubeCoords[2].set( 1.0000f, -1.0000f, -1.0000f );
cubeCoords[3].set( -1.0000f, -1.0000f, -1.000 );
cubeCoords[4].set( 1.0000f, 1.0000f, -1.0000f );
cubeCoords[5].set( 1.0000f, 1.0000f, 1.0000f );
cubeCoords[6].set( 1.0000f, -1.0000f, 1.0000f );
cubeCoords[7].set( 1.0000f, -1.0000f, -1.0000f );
cubeCoords[8].set( 1.0000f, 1.0000f, 1.0000f );
cubeCoords[9].set( -1.0000f, 1.0000f, 1.000f );
cubeCoords[10].set( -1.0000f, -1.0000f, 1.000f );
cubeCoords[11].set( 1.0000f, -1.0000f, 1.0000f );
cubeCoords[12].set( -1.0000f, 1.0000f, 1.000 );
cubeCoords[13].set( -1.0000f, 1.0000f, -1.000 );
cubeCoords[14].set( -1.0000f, -1.0000f, -1.000 );
cubeCoords[15].set( -1.0000f, -1.0000f, 1.000 );
cubeCoords[16].set( -1.0000f, 1.0000f, 1.000 );
cubeCoords[17].set( 1.0000f, 1.0000f, 1.0000f );
cubeCoords[18].set( 1.0000f, 1.0000f, -1.0000f );
cubeCoords[19].set( -1.0000f, 1.0000f, -1.000f );
cubeCoords[20].set( -1.0000f, -1.0000f, 1.000f );
cubeCoords[21].set( -1.0000f, -1.0000f, -1.000f );
cubeCoords[22].set( 1.0000f, -1.0000f, -1.0000f );
cubeCoords[23].set( 1.0000f, -1.0000f, 1.0000f );
cube->setCoords( cubeCoords );
// set up the normals.
osg::Vec3 *cubeNormals = new osg::Vec3[6];
cubeNormals[0].set(0.0f,0.0f,-1.0f);
cubeNormals[1].set(1.0f,0.0f,0.0f);
cubeNormals[2].set(0.0f,0.0f,1.0f);
cubeNormals[3].set(-1.0f,0.0f,0.0f);
cubeNormals[4].set(0.0f,1.0f,0.0f);
cubeNormals[5].set(0.0f,-1.0f,0.0f);
cube->setNormals( cubeNormals );
cube->setNormalBinding( osg::GeoSet::BIND_PERPRIM );
// ---------------------------------------
// Set up a StateSet to make the cube red
// ---------------------------------------
osg::StateSet* cubeState = new osg::StateSet();
osg::Material* redMaterial = new osg::Material();
osg::Vec4 red( 1.0f, 0.0f, 0.0f, 1.0f );
redMaterial->setDiffuse( osg::Material::FRONT_AND_BACK, red );
cubeState->setAttribute( redMaterial );
cube->setStateSet( cubeState );
geode->addDrawable( cube );
return geode;
}
osg::Geode* createGeometryCube()
{
osg::Geode* geode = new osg::Geode();
// -------------------------------------------
// Set up a new Geometry which will be our cube
// -------------------------------------------
osg::Geometry* cube = new osg::Geometry();
// set up the primitives
cube->addPrimtive(new osg::DrawArray(osg::Primitive::POLYGON,0,4));
cube->addPrimtive(new osg::DrawArray(osg::Primitive::POLYGON,4,4));
cube->addPrimtive(new osg::DrawArray(osg::Primitive::POLYGON,8,4));
cube->addPrimtive(new osg::DrawArray(osg::Primitive::POLYGON,12,4));
cube->addPrimtive(new osg::DrawArray(osg::Primitive::POLYGON,16,4));
cube->addPrimtive(new osg::DrawArray(osg::Primitive::POLYGON,20,4));
// set up coords.
osg::Vec3Array* coords = new osg::Vec3Array;
coords->resize(24);
(*coords)[0].set( -1.0000f, 1.0000f, -1.000f );
(*coords)[1].set( 1.0000f, 1.0000f, -1.0000f );
(*coords)[2].set( 1.0000f, -1.0000f, -1.0000f );
(*coords)[3].set( -1.0000f, -1.0000f, -1.000 );
(*coords)[4].set( 1.0000f, 1.0000f, -1.0000f );
(*coords)[5].set( 1.0000f, 1.0000f, 1.0000f );
(*coords)[6].set( 1.0000f, -1.0000f, 1.0000f );
(*coords)[7].set( 1.0000f, -1.0000f, -1.0000f );
(*coords)[8].set( 1.0000f, 1.0000f, 1.0000f );
(*coords)[9].set( -1.0000f, 1.0000f, 1.000f );
(*coords)[10].set( -1.0000f, -1.0000f, 1.000f );
(*coords)[11].set( 1.0000f, -1.0000f, 1.0000f );
(*coords)[12].set( -1.0000f, 1.0000f, 1.000 );
(*coords)[13].set( -1.0000f, 1.0000f, -1.000 );
(*coords)[14].set( -1.0000f, -1.0000f, -1.000 );
(*coords)[15].set( -1.0000f, -1.0000f, 1.000 );
(*coords)[16].set( -1.0000f, 1.0000f, 1.000 );
(*coords)[17].set( 1.0000f, 1.0000f, 1.0000f );
(*coords)[18].set( 1.0000f, 1.0000f, -1.0000f );
(*coords)[19].set( -1.0000f, 1.0000f, -1.000f );
(*coords)[20].set( -1.0000f, -1.0000f, 1.000f );
(*coords)[21].set( -1.0000f, -1.0000f, -1.000f );
(*coords)[22].set( 1.0000f, -1.0000f, -1.0000f );
(*coords)[23].set( 1.0000f, -1.0000f, 1.0000f );
cube->setVertexArray( coords );
// set up the normals.
osg::Vec3Array* cubeNormals = new osg::Vec3Array;
cubeNormals->resize(6);
(*cubeNormals)[0].set(0.0f,0.0f,-1.0f);
(*cubeNormals)[1].set(1.0f,0.0f,0.0f);
(*cubeNormals)[2].set(0.0f,0.0f,1.0f);
(*cubeNormals)[3].set(-1.0f,0.0f,0.0f);
(*cubeNormals)[4].set(0.0f,1.0f,0.0f);
(*cubeNormals)[5].set(0.0f,-1.0f,0.0f);
cube->setNormalArray( cubeNormals );
cube->setNormalBinding( osg::Geometry::PER_PRIMITIVE );
// ---------------------------------------
// Set up a StateSet to make the cube red
// ---------------------------------------
osg::StateSet* cubeState = new osg::StateSet();
osg::Material* redMaterial = new osg::Material();
osg::Vec4 red( 1.0f, 0.0f, 0.0f, 1.0f );
redMaterial->setDiffuse( osg::Material::FRONT_AND_BACK, red );
cubeState->setAttribute( redMaterial );
cube->setStateSet( cubeState );
geode->addDrawable( cube );
return geode;
}
int main( int argc, char **argv )
{
glutInit( &argc, argv );
// create the commandline args.
std::vector<std::string> commandLine;
for(int i=1;i<argc;++i) commandLine.push_back(argv[i]);
// create the viewer and the model to it.
osgGLUT::Viewer viewer;
viewer.setWindowTitle(argv[0]);
// configure the viewer from the commandline arguments, and eat any
// parameters that have been matched.
viewer.readCommandLine(commandLine);
osg::Transform* myTransform = new osg::Transform();
// myTransform->addChild( createCube() );
myTransform->addChild( createGeometryCube() );
// move node in a circle at 90 degrees a sec.
myTransform->setAppCallback(new MyTransformCallback(myTransform,osg::inDegrees(90.0f)));
// add model to viewer.
viewer.addViewport( myTransform );
// register trackball maniupulators.
viewer.registerCameraManipulator(new osgGA::TrackballManipulator);
viewer.open();
viewer.run();
return 0;
}

View File

@@ -4,18 +4,18 @@ using namespace osg;
Geometry::Geometry()
{
_normalBinding = OFF;
_colorBinding = OFF;
}
Geometry::Geometry(const Geometry& geometry,const CopyOp& copyop):
Drawable(geometry,copyop),
_coords(geometry._coords),
_coordIndices(geometry._coordIndices),
_normals(geometry._normals),
_normalIndices(geometry._normalIndices),
_colors(geometry._colors),
_colorIndices(geometry._colorIndices),
_texCoordList(geometry._texCoordList),
_texCoordIndicesList(geometry._texCoordIndicesList)
_vertexArray(geometry._vertexArray),
_normalBinding(geometry._normalBinding),
_normalArray(geometry._normalArray),
_colorBinding(geometry._colorBinding),
_colorArray(geometry._colorArray),
_texCoordList(geometry._texCoordList)
{
}
@@ -24,170 +24,163 @@ Geometry::~Geometry()
// no need to delete, all automatically handled by ref_ptr :-)
}
void Geometry::setTexCoordArray(unsigned int pos,AttributeArray* array)
void Geometry::setTexCoordArray(unsigned int unit,AttributeArray* array)
{
if (_texCoordList.size()<=pos)
_texCoordList.resize(pos+1,0);
if (_texCoordList.size()<=unit)
_texCoordList.resize(unit+1,0);
_texCoordList[pos] = array;
_texCoordList[unit] = array;
}
AttributeArray* Geometry::getTexCoordArray(unsigned int pos)
AttributeArray* Geometry::getTexCoordArray(unsigned int unit)
{
if (pos<_texCoordList.size()) return _texCoordList[pos].get();
if (unit<_texCoordList.size()) return _texCoordList[unit].get();
else return 0;
}
void Geometry::setTexCoordIndicesArray(unsigned int pos,AttributeArray* array)
{
if (_texCoordList.size()<=pos)
_texCoordList.resize(pos+1,0);
_texCoordIndicesList[pos] = array;
}
AttributeArray* Geometry::getTexCoordIndicesArray(unsigned int pos)
{
if (pos<_texCoordIndicesList.size()) return _texCoordIndicesList[pos].get();
else return 0;
}
void Geometry::setAttribute(AttributeType type,AttributeArray* array)
{
switch(type)
{
case(PRIMITIVES):
_primitives = array;
break;
case(COORDS):
_coords = array;
break;
case(NORMALS):
_normals = array;
break;
case(COLORS):
_colors = array;
break;
default:
if (type>=TEX_COORDS_0)
{
setTexCoordArray(type-TEX_COORDS_0,array);
}
break;
}
}
void Geometry::setAttribute(AttributeType type,AttributeArray* array,AttributeArray* indices)
{
switch(type)
{
case(PRIMITIVES):
_primitives = array;
// indices not appropriate!
break;
case(COORDS):
_coords = array;
_coordIndices = indices;
break;
case(NORMALS):
_normals = array;
_normalIndices = indices;
break;
case(COLORS):
_colors = array;
_colorIndices = indices;
break;
default:
if (type>=TEX_COORDS_0)
{
setTexCoordArray(type-TEX_COORDS_0,array);
setTexCoordIndicesArray(type-TEX_COORDS_0,indices);
}
break;
}
}
AttributeArray* Geometry::getAttribute(AttributeType type)
{
switch(type)
{
case(PRIMITIVES):
return _primitives.get();
break;
case(COORDS):
return _coords.get();
break;
case(NORMALS):
return _normals.get();
break;
case(COLORS):
return _colors.get();
break;
default:
if (type>=TEX_COORDS_0)
{
return getTexCoordArray(type-TEX_COORDS_0);
}
break;
}
return 0;
}
void Geometry::setIndices(AttributeType type,AttributeArray* indices)
{
switch(type)
{
case(PRIMITIVES):
// indices not appropriate!
break;
case(COORDS):
_coordIndices = indices;
break;
case(NORMALS):
_normalIndices = indices;
break;
case(COLORS):
_colorIndices = indices;
break;
default:
if (type>=TEX_COORDS_0)
{
setTexCoordIndicesArray(type-TEX_COORDS_0,indices);
}
break;
}
}
AttributeArray* Geometry::getIndices(AttributeType type)
{
switch(type)
{
case(COORDS):
return _coordIndices.get();
break;
case(NORMALS):
return _normalIndices.get();
break;
case(COLORS):
return _colorIndices.get();
break;
default:
if (type>=TEX_COORDS_0)
{
return getTexCoordIndicesArray(type-TEX_COORDS_0);
}
break;
}
return 0;
}
void Geometry::drawImmediateMode(State& /*state*/)
{
if (!_vertexArray.valid()) return;
// set up the vertex arrays.
glEnableClientState( GL_VERTEX_ARRAY );
glVertexPointer(3,GL_FLOAT,0,_vertexArray->dataPointer());
// set up texture coordinates.
for(unsigned int i=0;i<_texCoordList.size();++i)
{
AttributeArray* array = _texCoordList[i].get();
glClientActiveTextureARB(GL_TEXTURE0_ARB+i);
if (array)
{
glEnableClientState( GL_TEXTURE_COORD_ARRAY );
glTexCoordPointer(array->dataSize(),array->dataType(),0,array->dataPointer());
}
else
{
glDisableClientState( GL_TEXTURE_COORD_ARRAY );
}
}
// set up normals.
Vec3* normalPointer = 0;
if (_normalArray.valid() && !_normalArray->empty()) normalPointer = &(_normalArray->front());
switch (_normalBinding)
{
case(OFF):
glDisableClientState( GL_NORMAL_ARRAY );
break;
case(OVERALL):
if (normalPointer) glNormal3fv(reinterpret_cast<const GLfloat*>(normalPointer));
glDisableClientState( GL_NORMAL_ARRAY );
break;
case(PER_PRIMITIVE):
glDisableClientState( GL_NORMAL_ARRAY );
break;
case(PER_VERTEX):
glEnableClientState( GL_NORMAL_ARRAY );
if (normalPointer) glNormalPointer(GL_FLOAT,0,normalPointer);
break;
}
// set up colors, complicated by the fact that the color array
// might be bound in 4 different ways, and be represented as 3 different data types -
// Vec3, Vec4 or UByte4 Arrays.
const unsigned char* colorPointer = 0;
unsigned int colorStride = 0;
ArrayType colorType = AttributeArrayType;
if (_colorArray.valid())
{
colorType = _colorArray->arrayType();
switch(colorType)
{
case(UByte4ArrayType):
{
colorPointer = reinterpret_cast<const unsigned char*>(_colorArray->dataPointer());
colorStride = 4;
break;
}
case(Vec3ArrayType):
{
colorPointer = reinterpret_cast<const unsigned char*>(_colorArray->dataPointer());
colorStride = 12;
break;
}
case(Vec4ArrayType):
{
colorPointer = reinterpret_cast<const unsigned char*>(_colorArray->dataPointer());
colorStride = 16;
break;
}
}
}
switch (_colorBinding)
{
case(OFF):
glDisableClientState( GL_COLOR_ARRAY );
break;
case(OVERALL):
glDisableClientState( GL_COLOR_ARRAY );
if (colorPointer)
{
switch(colorType)
{
case(UByte4ArrayType):
glColor4ubv(reinterpret_cast<const GLubyte*>(colorPointer));
break;
case(Vec3ArrayType):
glColor3fv(reinterpret_cast<const GLfloat*>(colorPointer));
break;
case(Vec4ArrayType):
glColor4fv(reinterpret_cast<const GLfloat*>(colorPointer));
break;
}
}
break;
case(PER_PRIMITIVE):
glDisableClientState( GL_COLOR_ARRAY );
break;
case(PER_VERTEX):
glEnableClientState( GL_COLOR_ARRAY );
if (colorPointer) glColorPointer(_colorArray->dataSize(),_colorArray->dataType(),0,colorPointer);
}
// draw the primitives themselves.
for(PrimitiveList::iterator itr=_primitives.begin();
itr!=_primitives.end();
++itr)
{
if (_normalBinding==PER_PRIMITIVE)
{
glNormal3fv((const GLfloat *)normalPointer++);
}
if (_colorBinding==PER_PRIMITIVE)
{
switch(colorType)
{
case(UByte4ArrayType):
glColor4ubv(reinterpret_cast<const GLubyte*>(colorPointer));
break;
case(Vec3ArrayType):
glColor3fv(reinterpret_cast<const GLfloat*>(colorPointer));
break;
case(Vec4ArrayType):
glColor4fv(reinterpret_cast<const GLfloat*>(colorPointer));
break;
}
colorPointer += colorStride;
}
(*itr)->draw();
}
}
/** Statistics collection for each drawable- 26.09.01
@@ -213,7 +206,7 @@ const bool Geometry::computeBound() const
{
_bbox.init();
const Vec3Array* coords = dynamic_cast<const Vec3Array*>(_coords.get());
const Vec3Array* coords = dynamic_cast<const Vec3Array*>(_vertexArray.get());
if (coords)
{
for(Vec3Array::const_iterator itr=coords->begin();