From: Tim Moore Date: Wed, 28 Jan 2009 06:43:09 +0000 (+0100) Subject: Use osg::Geometry code to draw trees ourselves. X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=31d9eaf54cc6f57cd01aad4c4b9c1b0fe4046e25;p=simgear.git Use osg::Geometry code to draw trees ourselves. 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. --- diff --git a/simgear/scene/tgdb/ShaderGeometry.cxx b/simgear/scene/tgdb/ShaderGeometry.cxx index 632555fc..1148aeac 100644 --- a/simgear/scene/tgdb/ShaderGeometry.cxx +++ b/simgear/scene/tgdb/ShaderGeometry.cxx @@ -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(getColorArray()); FloatArray *vertexAttribs = static_cast(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(getColorArray()); + const FloatArray* vertexAttribs + = static_cast(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(); diff --git a/simgear/scene/tgdb/ShaderGeometry.hxx b/simgear/scene/tgdb/ShaderGeometry.hxx index 0044dd4e..243384c5 100644 --- a/simgear/scene/tgdb/ShaderGeometry.hxx +++ b/simgear/scene/tgdb/ShaderGeometry.hxx @@ -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)