]> git.mxchange.org Git - simgear.git/commitdiff
Use osg::Geometry code to draw trees ourselves.
authorTim Moore <timoore@redhat.com>
Wed, 28 Jan 2009 06:43:09 +0000 (07:43 +0100)
committerTim Moore <timoore@redhat.com>
Wed, 28 Jan 2009 06:43:09 +0000 (07:43 +0100)
This reverts back in the direction of Yon's original patch: the model is
drawn within ShaderGeometry::drawImplementation. This saves a lot of memory
over the previous approach of creating a PrimitiveSet for each model.

simgear/scene/tgdb/ShaderGeometry.cxx
simgear/scene/tgdb/ShaderGeometry.hxx

index 632555fc66daf2201cb630be7256870d9e8b267c..1148aeacc367add2ac57d30ed63797ef23b46943 100644 (file)
@@ -43,19 +43,52 @@ void ShaderGeometry::addTree(const TreeBin::Tree& t)
         setVertexAttribArray(1, new FloatArray());
         setVertexAttribBinding(1, Geometry::BIND_PER_PRIMITIVE_SET);
     }
-    Geometry::PrimitiveSetList& modelSets = _geometry->getPrimitiveSetList();
-    size_t numSets = modelSets.size();
     Vec4Array *colors = static_cast<Vec4Array*>(getColorArray());
     FloatArray *vertexAttribs
         = static_cast<FloatArray*>(getVertexAttribArray(1));
-    colors->insert(colors->end(), numSets, Vec4(t.position.osg(), t.scale));
-    vertexAttribs->insert(vertexAttribs->end(), numSets,
-                          (float)t.texture_index / varieties);
-    _primitives.insert(_primitives.end(), modelSets.begin(), modelSets.end());
+    colors->push_back(Vec4(t.position.osg(), t.scale));
+    vertexAttribs->push_back((float)t.texture_index / varieties);
     dirtyDisplayList();
     dirtyBound();
 }
 
+// The good bits from osg::Geometry::drawImplementation
+void ShaderGeometry::drawImplementation(osg::RenderInfo& renderInfo) const
+{
+    osg::State& state = *renderInfo.getState();
+    const Extensions* extensions = getExtensions(state.getContextID(), true);
+    state.setVertexPointer(_vertexData.array->getDataSize(),
+                           _vertexData.array->getDataType(), 0,
+                           _vertexData.array->getDataPointer());
+    if (_normalData.array.valid())
+        state.setNormalPointer(_normalData.array->getDataType(), 0,
+                               _normalData.array->getDataPointer());
+    else
+        state.disableNormalPointer();
+    Array* texArray = _texCoordList[0].array.get();
+    state.setTexCoordPointer(0, texArray->getDataSize(),
+                             texArray->getDataType(), 0,
+                             texArray->getDataPointer());
+    const Vec4Array* colors = static_cast<const Vec4Array*>(getColorArray());
+    const FloatArray* vertexAttribs
+        = static_cast<const FloatArray*>(getVertexAttribArray(1));
+    Vec4Array::const_iterator citer = colors->begin(), cend = colors->end();
+    FloatArray::const_iterator viter = vertexAttribs->begin();
+    const Geometry::PrimitiveSetList& modelSets
+        = _geometry->getPrimitiveSetList();
+    for (; citer != cend; ++citer, ++viter) {
+        const Vec4& color = *citer;
+        const float attrib = *viter;
+        glColor4fv(color.ptr());
+        extensions->glVertexAttrib1f(1, attrib);
+        for (Geometry::PrimitiveSetList::const_iterator piter = modelSets.begin(),
+                 e = modelSets.end();
+             piter != e;
+            ++piter)
+            (*piter)->draw(state, false);
+    }
+}
+
 BoundingBox ShaderGeometry::computeBound() const
 {
     BoundingBox geom_box = _geometry->getBound();
index 0044dd4e918468f65022d228e7c100b9c18b4120..243384c54be4ec9da4200f4b7a6e317abd6f7b1f 100644 (file)
@@ -55,7 +55,8 @@ class ShaderGeometry : public osg::Geometry
             osg::Geometry(ShaderGeometry,copyop) {}
 
         META_Object(flightgear, ShaderGeometry);
-        
+
+        virtual void drawImplementation(osg::RenderInfo& renderInfo) const;
         virtual osg::BoundingBox computeBound() const;
         
         void setGeometry(osg::Geometry* geometry)