From: Peter Sadrozinski Date: Fri, 26 Dec 2014 15:24:34 +0000 (-0500) Subject: - fix for index overrun when building TexturedTriangleArray X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=beeaef3868dd6d28a65cc768825f214cf9706560;p=simgear.git - fix for index overrun when building TexturedTriangleArray - fix for ws2.0 zero area triangles - drop them when loading --- diff --git a/simgear/io/sg_binobj.cxx b/simgear/io/sg_binobj.cxx index 28dd626d..1c604a28 100644 --- a/simgear/io/sg_binobj.cxx +++ b/simgear/io/sg_binobj.cxx @@ -274,6 +274,15 @@ static void read_indices(char* buffer, if (vaMask & SG_VA_FLOAT_3) vas[7].push_back(*src++); } } // of elements in the index + + // WS2.0 fix : toss zero area triangles + if ( ( count == 3 ) && (indexMask & SG_IDX_VERTICES) ) { + if ( (vertices[0] == vertices[1]) || + (vertices[1] == vertices[2]) || + (vertices[2] == vertices[0]) ) { + vertices.clear(); + } + } } template @@ -468,12 +477,15 @@ void SGBinObject::read_object( gzFile fp, read_indices(ptr, nbytes, idx_mask, vertex_attrib_mask, vs, ns, cs, tcs, vas ); } - vertices.push_back( vs ); - normals.push_back( ns ); - colors.push_back( cs ); - texCoords.push_back( tcs ); - vertexAttribs.push_back( vas ); - materials.push_back( material ); + // Fix for WS2.0 - ignore zero area triangles + if ( !vs.empty() ) { + vertices.push_back( vs ); + normals.push_back( ns ); + colors.push_back( cs ); + texCoords.push_back( tcs ); + vertexAttribs.push_back( vas ); + materials.push_back( material ); + } } // of element iteration } diff --git a/simgear/scene/tgdb/SGTexturedTriangleBin.hxx b/simgear/scene/tgdb/SGTexturedTriangleBin.hxx index 0b96a1ff..79f7459c 100644 --- a/simgear/scene/tgdb/SGTexturedTriangleBin.hxx +++ b/simgear/scene/tgdb/SGTexturedTriangleBin.hxx @@ -104,34 +104,35 @@ private: // not an advantage on modern hardware. class DrawElementsFacade { public: - DrawElementsFacade(unsigned numVerts) : - _ushortElements(0), _uintElements(0) + DrawElementsFacade(void) : count(0) { - if (numVerts > 65535) - _uintElements - = new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES); - else - _ushortElements - = new osg::DrawElementsUShort(osg::PrimitiveSet::TRIANGLES); + _uintElements = new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES); + _ushortElements = new osg::DrawElementsUShort(osg::PrimitiveSet::TRIANGLES); } void push_back(unsigned val) { - if (_uintElements) - _uintElements->push_back(val); - else + count++; + if (count < 65536) { _ushortElements->push_back(val); + } + _uintElements->push_back(val); } osg::DrawElements* getDrawElements() { - if (_uintElements) + if (count > 65535) { + free (_ushortElements); return _uintElements; - return _ushortElements; + } else { + free (_uintElements); + return _ushortElements; + } } protected: osg::DrawElementsUShort* _ushortElements; osg::DrawElementsUInt* _uintElements; + unsigned count; }; class SGTexturedTriangleBin : public SGTriangleBin { @@ -398,7 +399,7 @@ public: const unsigned invalid = ~unsigned(0); std::vector indexMap(getNumVertices(), invalid); - DrawElementsFacade deFacade(vertices->size()); + DrawElementsFacade deFacade; for (index_type i = 0; i < triangles.size(); ++i) { triangle_ref triangle = triangles[i]; if (indexMap[triangle[0]] == invalid) {