]> git.mxchange.org Git - simgear.git/blob - simgear/scene/tgdb/ShaderGeometry.cxx
Rewrite ShaderGeometry to use display lists and OSG primitives.
[simgear.git] / simgear / scene / tgdb / ShaderGeometry.cxx
1 /* -*-c++-*-
2  *
3  * Copyright (C) 2008 Stuart Buchanan
4  *
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.
9  *
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.
14  *
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,
18  * MA 02110-1301, USA.
19  *
20  */
21
22 #include <osgDB/Registry>
23 #include <osgDB/Input>
24 #include <osgDB/ParameterOutput>
25
26 #include "ShaderGeometry.hxx"
27
28 #include <algorithm>
29
30 using namespace osg;
31 using namespace osgDB;
32
33 namespace simgear
34 {
35 void ShaderGeometry::addTree(const TreeBin::Tree& t)
36 {
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);
45     }
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());
55     dirtyDisplayList();
56     dirtyBound();
57 }
58
59 BoundingBox ShaderGeometry::computeBound() const
60 {
61     BoundingBox geom_box = _geometry->getBound();
62     BoundingBox bb;
63     unsigned numSets = _geometry->getNumPrimitiveSets();
64     const Vec4Array* posScales = static_cast<const Vec4Array*>(getColorArray());
65     if (!posScales)
66         return bb;
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);
74     }
75     return bb;
76 }
77
78 bool ShaderGeometry_readLocalData(Object& obj, Input& fr)
79 {
80     bool iteratorAdvanced = false;
81
82     ShaderGeometry& geom = static_cast<ShaderGeometry&>(obj);
83
84     if ((fr[0].matchWord("geometry"))) {
85         ++fr;
86         iteratorAdvanced = true;
87         osg::Geometry* drawable = dynamic_cast<osg::Geometry*>(fr.readDrawable());
88         if (drawable) {
89             geom._geometry = drawable;
90         }
91     }
92     return iteratorAdvanced;
93 }
94
95 bool ShaderGeometry_writeLocalData(const Object& obj, Output& fw)
96 {
97     const ShaderGeometry& geom = static_cast<const ShaderGeometry&>(obj);
98
99     fw.indent() << "geometry" << std::endl;
100     fw.writeObject(*geom._geometry);
101     return true;
102 }
103
104 osgDB::RegisterDotOsgWrapperProxy shaderGeometryProxy
105 (
106     new ShaderGeometry,
107     "ShaderGeometry",
108     "Object Drawable Geometry ShaderGeometry",
109     &ShaderGeometry_readLocalData,
110     &ShaderGeometry_writeLocalData
111     );
112 }
113