3 * Copyright (C) 2008 Stuart Buchanan
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22 #include <osgDB/Registry>
23 #include <osgDB/Input>
24 #include <osgDB/ParameterOutput>
26 #include "ShaderGeometry.hxx"
31 using namespace osgDB;
35 void ShaderGeometry::addTree(const TreeBin::Tree& t)
37 if (!getVertexArray()) {
38 setVertexData(_geometry->getVertexData());
39 setNormalData(_geometry->getNormalData());
40 setTexCoordData(0, _geometry->getTexCoordData(0));
41 setColorArray(new Vec4Array());
42 setColorBinding(Geometry::BIND_PER_PRIMITIVE_SET);
43 setVertexAttribArray(1, new FloatArray());
44 setVertexAttribBinding(1, Geometry::BIND_PER_PRIMITIVE_SET);
46 Geometry::PrimitiveSetList& modelSets = _geometry->getPrimitiveSetList();
47 size_t numSets = modelSets.size();
48 Vec4Array *colors = static_cast<Vec4Array*>(getColorArray());
49 FloatArray *vertexAttribs
50 = static_cast<FloatArray*>(getVertexAttribArray(1));
51 colors->insert(colors->end(), numSets, Vec4(t.position.osg(), t.scale));
52 vertexAttribs->insert(vertexAttribs->end(), numSets,
53 (float)t.texture_index / varieties);
54 _primitives.insert(_primitives.end(), modelSets.begin(), modelSets.end());
59 BoundingBox ShaderGeometry::computeBound() const
61 BoundingBox geom_box = _geometry->getBound();
63 unsigned numSets = _geometry->getNumPrimitiveSets();
64 const Vec4Array* posScales = static_cast<const Vec4Array*>(getColorArray());
67 size_t numPosScales = posScales->size();
68 for (int i = 0; i < numPosScales; i += numSets) {
69 const Vec4& posScale((*posScales)[i]);
70 const float scale = posScale.w();
71 const Vec3 pos(posScale.x(), posScale.y(), posScale.z());
72 for (unsigned j = 0; j < 7; ++j)
73 bb.expandBy(geom_box.corner(j) * scale + pos);
78 bool ShaderGeometry_readLocalData(Object& obj, Input& fr)
80 bool iteratorAdvanced = false;
82 ShaderGeometry& geom = static_cast<ShaderGeometry&>(obj);
84 if ((fr[0].matchWord("geometry"))) {
86 iteratorAdvanced = true;
87 osg::Geometry* drawable = dynamic_cast<osg::Geometry*>(fr.readDrawable());
89 geom._geometry = drawable;
92 return iteratorAdvanced;
95 bool ShaderGeometry_writeLocalData(const Object& obj, Output& fw)
97 const ShaderGeometry& geom = static_cast<const ShaderGeometry&>(obj);
99 fw.indent() << "geometry" << std::endl;
100 fw.writeObject(*geom._geometry);
104 osgDB::RegisterDotOsgWrapperProxy shaderGeometryProxy
108 "Object Drawable Geometry ShaderGeometry",
109 &ShaderGeometry_readLocalData,
110 &ShaderGeometry_writeLocalData