]> git.mxchange.org Git - simgear.git/blob - simgear/scene/tgdb/ShaderGeometry.cxx
Cleanup and performance tuning of the random trees code.
[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 using namespace osg;
29 using namespace osgDB;
30
31 namespace simgear
32 {
33 void ShaderGeometry::drawImplementation(RenderInfo& renderInfo) const
34 {
35     for(PositionSizeList::const_iterator itr = _trees.begin();
36         itr != _trees.end();
37         ++itr) {
38         glColor4fv(itr->ptr());
39         _geometry->draw(renderInfo);
40     }
41 }
42
43 BoundingBox ShaderGeometry::computeBound() const
44 {
45     BoundingBox geom_box = _geometry->getBound();
46     BoundingBox bb;
47     for(PositionSizeList::const_iterator itr = _trees.begin();
48         itr != _trees.end();
49         ++itr) {
50         bb.expandBy(geom_box.corner(0)*(*itr)[3] +
51                     Vec3((*itr)[0], (*itr)[1], (*itr)[2]));
52         bb.expandBy(geom_box.corner(7)*(*itr)[3] +
53                     Vec3((*itr)[0], (*itr)[1], (*itr)[2]));
54     }
55     return bb;
56 }
57
58 bool ShaderGeometry_readLocalData(Object& obj, Input& fr)
59 {
60     bool iteratorAdvanced = false;
61
62     ShaderGeometry& geom = static_cast<ShaderGeometry&>(obj);
63
64     if ((fr[0].matchWord("geometry"))) {
65         ++fr;
66         iteratorAdvanced = true;
67         Drawable* drawable = fr.readDrawable();
68         if (drawable) {
69             geom._geometry = drawable;
70         }
71     }
72     if ((fr.matchSequence("instances %i"))) {
73         int entry = fr[0].getNoNestedBrackets();
74         int capacity;
75         fr[1].getInt(capacity);
76         geom._trees.reserve(capacity);
77         fr += 3;
78         iteratorAdvanced = true;
79         // skip {
80         while (!fr.eof() && fr[0].getNoNestedBrackets() > entry) {
81             Vec4 v;
82             if (fr[0].getFloat(v.x()) && fr[1].getFloat(v.y())
83                 && fr[2].getFloat(v.z()) && fr[3].getFloat(v.w())) {
84                     fr += 4;
85                     geom._trees.push_back(v);
86             } else {
87                 ++fr;
88             }
89         }
90     }
91     return iteratorAdvanced;
92 }
93
94 bool ShaderGeometry_writeLocalData(const Object& obj, Output& fw)
95 {
96     const ShaderGeometry& geom = static_cast<const ShaderGeometry&>(obj);
97
98     fw.indent() << "geometry" << std::endl;
99     fw.writeObject(*geom._geometry);
100     fw.indent() << "instances " << geom._trees.size() << std::endl;
101     fw.indent() << "{" << std::endl;
102     fw.moveIn();
103     for (ShaderGeometry::PositionSizeList::const_iterator iter
104              = geom._trees.begin();
105          iter != geom._trees.end();
106          ++iter) {
107         fw.indent() << iter->x() << " " << iter->y() << " " << iter->z() << " "
108                     << iter->w() << std::endl;
109     }
110     fw.moveOut();
111     fw.indent() << "}" << std::endl;
112     return true;
113 }
114
115 osgDB::RegisterDotOsgWrapperProxy shaderGeometryProxy
116 (
117     new ShaderGeometry,
118     "ShaderGeometry",
119     "Object Drawable ShaderGeometry",
120     &ShaderGeometry_readLocalData,
121     &ShaderGeometry_writeLocalData
122     );
123 }