From 20ad2468969128f610a5056ebf5ee2fcdea10c41 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 25 Mar 2009 11:17:21 +0000 Subject: [PATCH] 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. --- include/osg/PrimitiveSet | 21 +++++ src/osgPlugins/ffmpeg/FFmpegDecoder.cpp | 7 +- src/osgTerrain/GeometryTechnique.cpp | 107 ++++++++++++++---------- src/osgWrappers/osg/PrimitiveSet.cpp | 20 +++++ 4 files changed, 112 insertions(+), 43 deletions(-) diff --git a/include/osg/PrimitiveSet b/include/osg/PrimitiveSet index 992f57212..7f3f3c315 100644 --- a/include/osg/PrimitiveSet +++ b/include/osg/PrimitiveSet @@ -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(); diff --git a/src/osgPlugins/ffmpeg/FFmpegDecoder.cpp b/src/osgPlugins/ffmpeg/FFmpegDecoder.cpp index fd657aabb..d822d070e 100644 --- a/src/osgPlugins/ffmpeg/FFmpegDecoder.cpp +++ b/src/osgPlugins/ffmpeg/FFmpegDecoder.cpp @@ -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"); diff --git a/src/osgTerrain/GeometryTechnique.cpp b/src/osgTerrain/GeometryTechnique.cpp index 5d1953955..1982e4bbf 100644 --- a/src/osgTerrain/GeometryTechnique.cpp +++ b/src/osgTerrain/GeometryTechnique.cpp @@ -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 = "< elements = new osg::DrawElementsUInt(GL_TRIANGLES); - elements->reserve((numRows-1) * (numColumns-1) * 6); + osg::ref_ptr elements = smallTile ? + static_cast(new osg::DrawElementsUShort(GL_TRIANGLES)) : + static_cast(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)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 skirtDrawElements = new osg::DrawElementsUShort(GL_QUAD_STRIP); + osg::ref_ptr skirtDrawElements = smallTile ? + static_cast(new osg::DrawElementsUShort(GL_QUAD_STRIP)) : + static_cast(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(new osg::DrawElementsUShort(GL_QUAD_STRIP)) : + static_cast(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(new osg::DrawElementsUShort(GL_QUAD_STRIP)) : + static_cast(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(new osg::DrawElementsUShort(GL_QUAD_STRIP)) : + static_cast(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(new osg::DrawElementsUShort(GL_QUAD_STRIP)) : + static_cast(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(new osg::DrawElementsUShort(GL_QUAD_STRIP)) : + static_cast(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(new osg::DrawElementsUShort(GL_QUAD_STRIP)) : + static_cast(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(new osg::DrawElementsUShort(GL_QUAD_STRIP)) : + static_cast(new osg::DrawElementsUInt(GL_QUAD_STRIP)); } } diff --git a/src/osgWrappers/osg/PrimitiveSet.cpp b/src/osgWrappers/osg/PrimitiveSet.cpp index 49e70f277..b99285283 100644 --- a/src/osgWrappers/osg/PrimitiveSet.cpp +++ b/src/osgWrappers/osg/PrimitiveSet.cpp @@ -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);