#include "ShaderGeometry.hxx"
+#include <algorithm>
+
using namespace osg;
using namespace osgDB;
namespace simgear
{
-void ShaderGeometry::drawImplementation(RenderInfo& renderInfo) const
+void ShaderGeometry::addObject(const Vec3& position, float scale,
+ int texture_index)
{
- osg::State& state = *renderInfo.getState();
- const Extensions* extensions = getExtensions(state.getContextID(),true);
+ if (!_posScaleArray.valid()) {
+ _posScaleArray = new Vec4Array();
+ _vertexAttribArray = new FloatArray();
+ }
+ _posScaleArray->push_back(Vec4(position, scale));
+ _vertexAttribArray->push_back((float)texture_index / varieties);
+ dirtyBound();
+}
- for(TreeBin::TreeList::const_iterator t = _trees.begin(); t != _trees.end(); ++t)
- {
- extensions->glVertexAttrib1f(1, (float) t->texture_index/varieties);
- glColor4f(t->position.x(), t->position.y(), t->position.z(), t->scale);
+void ShaderGeometry::drawImplementation(osg::RenderInfo& renderInfo) const
+{
+ State& state = *renderInfo.getState();
+ const Extensions* extensions = getExtensions(state.getContextID(), true);
+ Vec4Array::const_iterator citer = _posScaleArray->begin();
+ Vec4Array::const_iterator cend = _posScaleArray->end();
+ FloatArray::const_iterator viter = _vertexAttribArray->begin();
+ for (; citer != cend; ++citer, ++viter) {
+ const Vec4& color = *citer;
+ const float attrib = *viter;
+ glColor4fv(color.ptr());
+ extensions->glVertexAttrib1f(1, attrib);
_geometry->draw(renderInfo);
}
}
BoundingBox ShaderGeometry::computeBound() const
{
- BoundingBox geom_box = _geometry->getBound();
+ const BoundingBox& geom_box = _geometry->getBound();
BoundingBox bb;
- for(TreeBin::TreeList::const_iterator itr = _trees.begin();
- itr != _trees.end();
- ++itr) {
- bb.expandBy(geom_box.corner(0)*itr->scale +
- osg::Vec3( itr->position.x(), itr->position.y(), itr->position.z() ));
- bb.expandBy(geom_box.corner(7)*itr->scale +
- osg::Vec3( itr->position.x(), itr->position.y(), itr->position.z() ));
+ const Vec4Array* posScales = _posScaleArray.get();
+ if (!posScales)
+ return bb;
+// size_t numPosScales = posScales->size();
+ for (Vec4Array::const_iterator iter = _posScaleArray->begin(),
+ e = _posScaleArray->end();
+ iter != e;
+ ++iter) {
+ const Vec4& posScale = *iter;
+ const float scale = posScale.w();
+ const Vec3 pos(posScale.x(), posScale.y(), posScale.z());
+ for (unsigned j = 0; j < 7; ++j)
+ bb.expandBy(geom_box.corner(j) * scale + pos);
}
return bb;
}
if ((fr[0].matchWord("geometry"))) {
++fr;
iteratorAdvanced = true;
- osg::Drawable* drawable = fr.readDrawable();
+ osg::Geometry* drawable = dynamic_cast<osg::Geometry*>(fr.readDrawable());
if (drawable) {
geom._geometry = drawable;
}
}
- if ((fr.matchSequence("instances %i"))) {
- int entry = fr[0].getNoNestedBrackets();
+// int capacity = 0;
+ if (fr.matchSequence("posScale %i {")) {
+ int entry = fr[1].getNoNestedBrackets();
int capacity;
fr[1].getInt(capacity);
- geom._trees.reserve(capacity);
+ Vec4Array* posScale = new Vec4Array;
+ posScale->reserve(capacity);
fr += 3;
- iteratorAdvanced = true;
- // skip {
while (!fr.eof() && fr[0].getNoNestedBrackets() > entry) {
- SGVec3f v;
- int t;
- float s;
+ Vec4 v;
if (fr[0].getFloat(v.x()) && fr[1].getFloat(v.y())
- && fr[2].getFloat(v.z()) && fr[3].getInt(t) && fr[4].getFloat(s)) {
- fr += 4;
- //SGVec3f* v = new SGVec3f(v.x(), v.y(), v.z());
- //TreeBin::Tree tree = new TreeBin::Tree(v, t, s);
- geom._trees.push_back(TreeBin::Tree(v, t, s));
- } else {
+ && fr[2].getFloat(v.z()) && fr[3].getFloat(v.w())) {
+ fr += 4;
+ posScale->push_back(v);
+ }
+ else ++fr;
+ }
+ ++fr;
+ geom._posScaleArray = posScale;
+ }
+ if (fr.matchSequence("variety %i {")) {
+ int entry = fr[1].getNoNestedBrackets();
+ int capacity;
+ fr[1].getInt(capacity);
+ FloatArray* variety = new FloatArray;
+ variety->reserve(capacity);
+ fr += 3;
+ while (!fr.eof() && fr[0].getNoNestedBrackets() > entry) {
+ float val;
+ if (fr[0].getFloat(val)) {
++fr;
+ variety->push_back(val);
}
+ else ++fr;
}
+ ++fr;
+ geom._vertexAttribArray = variety;
}
+
return iteratorAdvanced;
}
fw.indent() << "geometry" << std::endl;
fw.writeObject(*geom._geometry);
- fw.indent() << "instances " << geom._trees.size() << std::endl;
- fw.indent() << "{" << std::endl;
- fw.moveIn();
- for (TreeBin::TreeList::const_iterator iter
- = geom._trees.begin();
- iter != geom._trees.end();
- ++iter) {
- fw.indent() << iter->position.x() << " " << iter->position.y() << " " << iter->position.z() << " "
- << iter->texture_index << " " << iter->scale << std::endl;
+ if (geom._posScaleArray.valid()) {
+ fw.indent() << "posScale " << geom._posScaleArray->size() << " {\n";
+ fw.moveIn();
+ for (Vec4Array::const_iterator iter = geom._posScaleArray->begin();
+ iter != geom._posScaleArray->end();
+ ++iter) {
+ fw.indent() << iter->x() << " " << iter->y() << " " << iter->z() << " "
+ << iter->w() << "\n";
+ }
+ fw.moveOut();
+ fw.indent() << "}\n";
+ }
+ if (geom._vertexAttribArray.valid()) {
+ fw.indent() << "variety" << geom._vertexAttribArray->size() << " {\n";
+ fw.moveIn();
+ for (FloatArray::const_iterator iter = geom._vertexAttribArray->begin();
+ iter != geom._vertexAttribArray->end();
+ ++iter) {
+ fw.indent() << *iter << "\n";
+ }
+ fw.moveOut();
+ fw.indent() << "}\n";
}
- fw.moveOut();
- fw.indent() << "}" << std::endl;
return true;
}
&ShaderGeometry_writeLocalData
);
}
+