]> git.mxchange.org Git - simgear.git/commitdiff
- fix for index overrun when building TexturedTriangleArray
authorPeter Sadrozinski <psadrozinski@gmail.com>
Fri, 26 Dec 2014 15:24:34 +0000 (10:24 -0500)
committerPeter Sadrozinski <psadrozinski@gmail.com>
Fri, 26 Dec 2014 18:07:33 +0000 (13:07 -0500)
- fix for ws2.0 zero area triangles - drop them when loading

simgear/io/sg_binobj.cxx
simgear/scene/tgdb/SGTexturedTriangleBin.hxx

index 28dd626dc67abdd481f9f2a79589d91bffa2c835..1c604a28b39fb7e2555324050b3197c026071cae 100644 (file)
@@ -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 <class T>
@@ -468,12 +477,15 @@ void SGBinObject::read_object( gzFile fp,
             read_indices<uint16_t>(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
 }
 
index 0b96a1fff3dd3ffe8ba418643bb5ea4b9285e206..79f7459c05b089b6814d172781479571e6ad433d 100644 (file)
@@ -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<SGVertNormTex> {
@@ -398,7 +399,7 @@ public:
     const unsigned invalid = ~unsigned(0);
     std::vector<unsigned> 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) {