2002-06-21 03:54:08 +08:00
|
|
|
#include <osg/Geometry>
|
|
|
|
|
|
|
|
using namespace osg;
|
|
|
|
|
|
|
|
Geometry::Geometry()
|
|
|
|
{
|
2002-06-22 00:45:45 +08:00
|
|
|
_normalBinding = OFF;
|
|
|
|
_colorBinding = OFF;
|
2002-06-21 03:54:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Geometry::Geometry(const Geometry& geometry,const CopyOp& copyop):
|
|
|
|
Drawable(geometry,copyop),
|
2002-06-22 00:45:45 +08:00
|
|
|
_vertexArray(geometry._vertexArray),
|
|
|
|
_normalBinding(geometry._normalBinding),
|
|
|
|
_normalArray(geometry._normalArray),
|
|
|
|
_colorBinding(geometry._colorBinding),
|
|
|
|
_colorArray(geometry._colorArray),
|
|
|
|
_texCoordList(geometry._texCoordList)
|
2002-06-21 03:54:08 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Geometry::~Geometry()
|
|
|
|
{
|
|
|
|
// no need to delete, all automatically handled by ref_ptr :-)
|
|
|
|
}
|
|
|
|
|
2002-06-22 00:45:45 +08:00
|
|
|
void Geometry::setTexCoordArray(unsigned int unit,AttributeArray* array)
|
2002-06-21 03:54:08 +08:00
|
|
|
{
|
2002-06-22 00:45:45 +08:00
|
|
|
if (_texCoordList.size()<=unit)
|
|
|
|
_texCoordList.resize(unit+1,0);
|
2002-06-21 03:54:08 +08:00
|
|
|
|
2002-06-22 00:45:45 +08:00
|
|
|
_texCoordList[unit] = array;
|
2002-06-21 03:54:08 +08:00
|
|
|
}
|
|
|
|
|
2002-06-22 00:45:45 +08:00
|
|
|
AttributeArray* Geometry::getTexCoordArray(unsigned int unit)
|
2002-06-21 03:54:08 +08:00
|
|
|
{
|
2002-06-22 00:45:45 +08:00
|
|
|
if (unit<_texCoordList.size()) return _texCoordList[unit].get();
|
2002-06-21 03:54:08 +08:00
|
|
|
else return 0;
|
|
|
|
}
|
|
|
|
|
2002-06-22 00:45:45 +08:00
|
|
|
void Geometry::drawImmediateMode(State& /*state*/)
|
2002-06-21 03:54:08 +08:00
|
|
|
{
|
2002-06-22 00:45:45 +08:00
|
|
|
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)
|
2002-06-21 03:54:08 +08:00
|
|
|
{
|
2002-06-22 00:45:45 +08:00
|
|
|
AttributeArray* array = _texCoordList[i].get();
|
|
|
|
glClientActiveTextureARB(GL_TEXTURE0_ARB+i);
|
|
|
|
if (array)
|
2002-06-21 03:54:08 +08:00
|
|
|
{
|
2002-06-22 00:45:45 +08:00
|
|
|
glEnableClientState( GL_TEXTURE_COORD_ARRAY );
|
|
|
|
glTexCoordPointer(array->dataSize(),array->dataType(),0,array->dataPointer());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glDisableClientState( GL_TEXTURE_COORD_ARRAY );
|
2002-06-21 03:54:08 +08:00
|
|
|
}
|
|
|
|
}
|
2002-06-22 00:45:45 +08:00
|
|
|
|
|
|
|
|
|
|
|
// set up normals.
|
|
|
|
Vec3* normalPointer = 0;
|
|
|
|
if (_normalArray.valid() && !_normalArray->empty()) normalPointer = &(_normalArray->front());
|
2002-06-21 03:54:08 +08:00
|
|
|
|
2002-06-22 00:45:45 +08:00
|
|
|
switch (_normalBinding)
|
2002-06-21 03:54:08 +08:00
|
|
|
{
|
2002-06-22 00:45:45 +08:00
|
|
|
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;
|
2002-06-21 03:54:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-22 00:45:45 +08:00
|
|
|
// 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())
|
2002-06-21 03:54:08 +08:00
|
|
|
{
|
2002-06-22 00:45:45 +08:00
|
|
|
colorType = _colorArray->arrayType();
|
|
|
|
switch(colorType)
|
2002-06-21 03:54:08 +08:00
|
|
|
{
|
2002-06-22 00:45:45 +08:00
|
|
|
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;
|
|
|
|
}
|
2002-06-21 03:54:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-22 00:45:45 +08:00
|
|
|
switch (_colorBinding)
|
2002-06-21 03:54:08 +08:00
|
|
|
{
|
2002-06-22 00:45:45 +08:00
|
|
|
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);
|
2002-06-21 03:54:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-22 00:45:45 +08:00
|
|
|
// draw the primitives themselves.
|
|
|
|
for(PrimitiveList::iterator itr=_primitives.begin();
|
|
|
|
itr!=_primitives.end();
|
|
|
|
++itr)
|
2002-06-21 03:54:08 +08:00
|
|
|
{
|
2002-06-22 00:45:45 +08:00
|
|
|
if (_normalBinding==PER_PRIMITIVE)
|
2002-06-21 03:54:08 +08:00
|
|
|
{
|
2002-06-22 00:45:45 +08:00
|
|
|
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;
|
2002-06-21 03:54:08 +08:00
|
|
|
}
|
|
|
|
|
2002-06-22 00:45:45 +08:00
|
|
|
(*itr)->draw();
|
|
|
|
}
|
2002-06-21 03:54:08 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Statistics collection for each drawable- 26.09.01
|
|
|
|
*/
|
|
|
|
bool Geometry::getStats(Statistics &)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Drawable::AttributeBitMask Geometry::suppportsAttributeOperation() const
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** return the attributes successully applied in applyAttributeUpdate.*/
|
|
|
|
Drawable::AttributeBitMask Geometry::applyAttributeOperation(AttributeFunctor& )
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const bool Geometry::computeBound() const
|
|
|
|
{
|
|
|
|
_bbox.init();
|
|
|
|
|
2002-06-22 00:45:45 +08:00
|
|
|
const Vec3Array* coords = dynamic_cast<const Vec3Array*>(_vertexArray.get());
|
2002-06-21 03:54:08 +08:00
|
|
|
if (coords)
|
|
|
|
{
|
|
|
|
for(Vec3Array::const_iterator itr=coords->begin();
|
|
|
|
itr!=coords->end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
_bbox.expandBy(*itr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_bbox_computed = true;
|
|
|
|
|
|
|
|
return _bbox.valid();
|
|
|
|
}
|
|
|
|
|