Added new virtual reseveElements, setElement, getElment and addElement methods to DrawElements

to make is easier to write code that can work on DrawElementUByte, UShort or UInt.

Changed the osgTerrain::GeometryTechnique so that it automatically chooses 
the use of DrawElementUShort or DrawElementsUInt accordining to the size of the tile.
This commit is contained in:
Robert Osfield 2009-03-25 11:17:21 +00:00
parent 36b3907d79
commit 20ad246896
4 changed files with 112 additions and 43 deletions

View File

@ -449,6 +449,12 @@ class DrawElements : public PrimitiveSet
{
if (_ebo.valid()) _ebo->releaseGLObjects(state);
}
virtual void reserveElements(unsigned int numIndices) = 0;
virtual void setElement(unsigned int, unsigned int) = 0;
virtual unsigned int getElement(unsigned int) = 0;
virtual void addElement(unsigned int) = 0;
protected:
@ -526,6 +532,11 @@ class OSG_EXPORT DrawElementsUByte : public DrawElements, public VectorGLubyte
_rangeModifiedCount = _modifiedCount;
}
virtual void reserveElements(unsigned int numIndices) { reserve(numIndices); }
virtual void setElement(unsigned int i, unsigned int v) { (*this)[i] = v; }
virtual unsigned int getElement(unsigned int i) { return (*this)[i]; }
virtual void addElement(unsigned int v) { push_back(GLubyte(v)); }
protected:
virtual ~DrawElementsUByte();
@ -601,6 +612,11 @@ class OSG_EXPORT DrawElementsUShort : public DrawElements, public VectorGLushort
_rangeModifiedCount = _modifiedCount;
}
virtual void reserveElements(unsigned int numIndices) { reserve(numIndices); }
virtual void setElement(unsigned int i, unsigned int v) { (*this)[i] = v; }
virtual unsigned int getElement(unsigned int i) { return (*this)[i]; }
virtual void addElement(unsigned int v) { push_back(GLushort(v)); }
protected:
virtual ~DrawElementsUShort();
@ -676,6 +692,11 @@ class OSG_EXPORT DrawElementsUInt : public DrawElements, public VectorGLuint
_rangeModifiedCount = _modifiedCount;
}
virtual void reserveElements(unsigned int numIndices) { reserve(numIndices); }
virtual void setElement(unsigned int i, unsigned int v) { (*this)[i] = v; }
virtual unsigned int getElement(unsigned int i) { return (*this)[i]; }
virtual void addElement(unsigned int v) { push_back(GLuint(v)); }
protected:
virtual ~DrawElementsUInt();

View File

@ -54,10 +54,15 @@ bool FFmpegDecoder::open(const std::string & filename)
formatParams.channel = 0;
formatParams.standard = 0;
#if 1
formatParams.width = 640;
formatParams.height = 480;
#else
formatParams.width = 640;
formatParams.height = 480;
#endif
formatParams.time_base.num = 1;
formatParams.time_base.den = 50;
formatParams.time_base.den = 15;
iformat = av_find_input_format("video4linux2");

View File

@ -416,9 +416,16 @@ void GeometryTechnique::generateGeometry(Locator* masterLocator, const osg::Vec3
// populate primitive sets
// bool optimizeOrientations = elevations!=0;
bool swapOrientation = !(masterLocator->orientationOpenGL());
bool smallTile = numVertices <= 16384;
// osg::notify(osg::NOTICE)<<"smallTile = "<<smallTile<<std::endl;
osg::ref_ptr<osg::DrawElementsUInt> elements = new osg::DrawElementsUInt(GL_TRIANGLES);
elements->reserve((numRows-1) * (numColumns-1) * 6);
osg::ref_ptr<osg::DrawElements> elements = smallTile ?
static_cast<osg::DrawElements*>(new osg::DrawElementsUShort(GL_TRIANGLES)) :
static_cast<osg::DrawElements*>(new osg::DrawElementsUInt(GL_TRIANGLES));
elements->reserveElements((numRows-1) * (numColumns-1) * 6);
geometry->addPrimitiveSet(elements.get());
@ -463,31 +470,31 @@ void GeometryTechnique::generateGeometry(Locator* masterLocator, const osg::Vec3
if (fabsf(e00-e11)<fabsf(e01-e10))
{
elements->push_back(i01);
elements->push_back(i00);
elements->push_back(i11);
elements->addElement(i01);
elements->addElement(i00);
elements->addElement(i11);
elements->push_back(i00);
elements->push_back(i10);
elements->push_back(i11);
elements->addElement(i00);
elements->addElement(i10);
elements->addElement(i11);
}
else
{
elements->push_back(i01);
elements->push_back(i00);
elements->push_back(i10);
elements->addElement(i01);
elements->addElement(i00);
elements->addElement(i10);
elements->push_back(i01);
elements->push_back(i10);
elements->push_back(i11);
elements->addElement(i01);
elements->addElement(i10);
elements->addElement(i11);
}
}
else if (numValid==3)
{
if (i00>=0) elements->push_back(i00);
if (i01>=0) elements->push_back(i01);
if (i11>=0) elements->push_back(i11);
if (i10>=0) elements->push_back(i10);
if (i00>=0) elements->addElement(i00);
if (i01>=0) elements->addElement(i01);
if (i11>=0) elements->addElement(i11);
if (i10>=0) elements->addElement(i10);
}
}
@ -506,7 +513,9 @@ void GeometryTechnique::generateGeometry(Locator* masterLocator, const osg::Vec3
if (createSkirt)
{
osg::ref_ptr<osg::DrawElementsUShort> skirtDrawElements = new osg::DrawElementsUShort(GL_QUAD_STRIP);
osg::ref_ptr<osg::DrawElements> skirtDrawElements = smallTile ?
static_cast<osg::DrawElements*>(new osg::DrawElementsUShort(GL_QUAD_STRIP)) :
static_cast<osg::DrawElements*>(new osg::DrawElementsUInt(GL_QUAD_STRIP));
// create bottom skirt vertices
int r,c;
@ -528,24 +537,28 @@ void GeometryTechnique::generateGeometry(Locator* masterLocator, const osg::Vec3
itr->second.first->push_back((*itr->second.first)[orig_i]);
}
skirtDrawElements->push_back(orig_i);
skirtDrawElements->push_back(new_i);
skirtDrawElements->addElement(orig_i);
skirtDrawElements->addElement(new_i);
}
else
{
if (!skirtDrawElements->empty())
if (skirtDrawElements->getNumIndices()!=0)
{
geometry->addPrimitiveSet(skirtDrawElements.get());
skirtDrawElements = new osg::DrawElementsUShort(GL_QUAD_STRIP);
skirtDrawElements = smallTile ?
static_cast<osg::DrawElements*>(new osg::DrawElementsUShort(GL_QUAD_STRIP)) :
static_cast<osg::DrawElements*>(new osg::DrawElementsUInt(GL_QUAD_STRIP));
}
}
}
if (!skirtDrawElements->empty())
if (skirtDrawElements->getNumIndices()!=0)
{
geometry->addPrimitiveSet(skirtDrawElements.get());
skirtDrawElements = new osg::DrawElementsUShort(GL_QUAD_STRIP);
skirtDrawElements = smallTile ?
static_cast<osg::DrawElements*>(new osg::DrawElementsUShort(GL_QUAD_STRIP)) :
static_cast<osg::DrawElements*>(new osg::DrawElementsUInt(GL_QUAD_STRIP));
}
// create right skirt vertices
@ -566,24 +579,28 @@ void GeometryTechnique::generateGeometry(Locator* masterLocator, const osg::Vec3
itr->second.first->push_back((*itr->second.first)[orig_i]);
}
skirtDrawElements->push_back(orig_i);
skirtDrawElements->push_back(new_i);
skirtDrawElements->addElement(orig_i);
skirtDrawElements->addElement(new_i);
}
else
{
if (!skirtDrawElements->empty())
if (skirtDrawElements->getNumIndices()!=0)
{
geometry->addPrimitiveSet(skirtDrawElements.get());
skirtDrawElements = new osg::DrawElementsUShort(GL_QUAD_STRIP);
skirtDrawElements = smallTile ?
static_cast<osg::DrawElements*>(new osg::DrawElementsUShort(GL_QUAD_STRIP)) :
static_cast<osg::DrawElements*>(new osg::DrawElementsUInt(GL_QUAD_STRIP));
}
}
}
if (!skirtDrawElements->empty())
if (skirtDrawElements->getNumIndices()!=0)
{
geometry->addPrimitiveSet(skirtDrawElements.get());
skirtDrawElements = new osg::DrawElementsUShort(GL_QUAD_STRIP);
skirtDrawElements = smallTile ?
static_cast<osg::DrawElements*>(new osg::DrawElementsUShort(GL_QUAD_STRIP)) :
static_cast<osg::DrawElements*>(new osg::DrawElementsUInt(GL_QUAD_STRIP));
}
// create top skirt vertices
@ -604,24 +621,28 @@ void GeometryTechnique::generateGeometry(Locator* masterLocator, const osg::Vec3
itr->second.first->push_back((*itr->second.first)[orig_i]);
}
skirtDrawElements->push_back(orig_i);
skirtDrawElements->push_back(new_i);
skirtDrawElements->addElement(orig_i);
skirtDrawElements->addElement(new_i);
}
else
{
if (!skirtDrawElements->empty())
if (skirtDrawElements->getNumIndices()!=0)
{
geometry->addPrimitiveSet(skirtDrawElements.get());
skirtDrawElements = new osg::DrawElementsUShort(GL_QUAD_STRIP);
skirtDrawElements = smallTile ?
static_cast<osg::DrawElements*>(new osg::DrawElementsUShort(GL_QUAD_STRIP)) :
static_cast<osg::DrawElements*>(new osg::DrawElementsUInt(GL_QUAD_STRIP));
}
}
}
if (!skirtDrawElements->empty())
if (skirtDrawElements->getNumIndices()!=0)
{
geometry->addPrimitiveSet(skirtDrawElements.get());
skirtDrawElements = new osg::DrawElementsUShort(GL_QUAD_STRIP);
skirtDrawElements = smallTile ?
static_cast<osg::DrawElements*>(new osg::DrawElementsUShort(GL_QUAD_STRIP)) :
static_cast<osg::DrawElements*>(new osg::DrawElementsUInt(GL_QUAD_STRIP));
}
// create left skirt vertices
@ -642,12 +663,12 @@ void GeometryTechnique::generateGeometry(Locator* masterLocator, const osg::Vec3
itr->second.first->push_back((*itr->second.first)[orig_i]);
}
skirtDrawElements->push_back(orig_i);
skirtDrawElements->push_back(new_i);
skirtDrawElements->addElement(orig_i);
skirtDrawElements->addElement(new_i);
}
else
{
if (!skirtDrawElements->empty())
if (skirtDrawElements->getNumIndices()!=0)
{
geometry->addPrimitiveSet(skirtDrawElements.get());
skirtDrawElements = new osg::DrawElementsUShort(GL_QUAD_STRIP);
@ -656,10 +677,12 @@ void GeometryTechnique::generateGeometry(Locator* masterLocator, const osg::Vec3
}
}
if (!skirtDrawElements->empty())
if (skirtDrawElements->getNumIndices()!=0)
{
geometry->addPrimitiveSet(skirtDrawElements.get());
skirtDrawElements = new osg::DrawElementsUShort(GL_QUAD_STRIP);
smallTile ?
static_cast<osg::DrawElements*>(new osg::DrawElementsUShort(GL_QUAD_STRIP)) :
static_cast<osg::DrawElements*>(new osg::DrawElementsUInt(GL_QUAD_STRIP));
}
}

View File

@ -197,6 +197,26 @@ BEGIN_ABSTRACT_OBJECT_REFLECTOR(osg::DrawElements)
__void__releaseGLObjects__State_P1,
"If State is non-zero, this function releases OpenGL objects for the specified graphics context. ",
"Otherwise, releases OpenGL objects for all graphics contexts. ");
I_Method1(void, reserveElements, IN, unsigned int, numIndices,
Properties::PURE_VIRTUAL,
__void__reserveElements__unsigned_int,
"",
"");
I_Method2(void, setElement, IN, unsigned, int, IN, unsigned, int,
Properties::PURE_VIRTUAL,
__void__setElement__unsigned__unsigned,
"",
"");
I_Method1(unsigned int, getElement, IN, unsigned, int,
Properties::PURE_VIRTUAL,
__unsigned_int__getElement__unsigned,
"",
"");
I_Method1(void, addElement, IN, unsigned, int,
Properties::PURE_VIRTUAL,
__void__addElement__unsigned,
"",
"");
I_SimpleProperty(osg::DrawElements *, DrawElements,
__DrawElements_P1__getDrawElements,
0);