]> git.mxchange.org Git - simgear.git/blob - simgear/scene/tgdb/TreeBin.cxx
Fix uninitialized var
[simgear.git] / simgear / scene / tgdb / TreeBin.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 #ifdef HAVE_CONFIG_H
23 #  include <simgear_config.h>
24 #endif
25
26 #include <algorithm>
27 #include <vector>
28 #include <string>
29 #include <map>
30
31 #include <boost/tuple/tuple_comparison.hpp>
32
33 #include <osg/Geode>
34 #include <osg/Geometry>
35 #include <osg/Math>
36 #include <osg/MatrixTransform>
37 #include <osg/Matrix>
38 #include <osg/NodeVisitor>
39
40 #include <osgDB/ReadFile>
41 #include <osgDB/FileUtils>
42
43 #include <simgear/debug/logstream.hxx>
44 #include <simgear/math/sg_random.h>
45 #include <simgear/misc/sg_path.hxx>
46 #include <simgear/scene/material/Effect.hxx>
47 #include <simgear/scene/material/EffectGeode.hxx>
48 #include <simgear/props/props.hxx>
49 #include <simgear/scene/util/QuadTreeBuilder.hxx>
50 #include <simgear/scene/util/RenderConstants.hxx>
51 #include <simgear/scene/util/StateAttributeFactory.hxx>
52 #include <simgear/scene/util/SGReaderWriterOptions.hxx>
53 #include <simgear/structure/OSGUtils.hxx>
54
55 #include "ShaderGeometry.hxx"
56 #include "TreeBin.hxx"
57
58 #define SG_TREE_QUAD_TREE_DEPTH 3
59 #define SG_TREE_FADE_OUT_LEVELS 10
60
61 using namespace osg;
62
63 namespace simgear
64 {
65
66 bool use_tree_shadows;
67 bool use_tree_normals;
68
69 // Tree instance scheme:
70 // vertex - local position of quad vertex.
71 // normal - x y scaling, z number of varieties
72 // fog coord - rotation
73 // color - xyz of tree quad origin, replicated 4 times.
74 //
75 // The tree quad is rendered twice, with different rotations, to
76 // create the crossed tree geometry.
77
78 struct TreesBoundingBoxCallback : public Drawable::ComputeBoundingBoxCallback
79 {
80     TreesBoundingBoxCallback() {}
81     TreesBoundingBoxCallback(const TreesBoundingBoxCallback&, const CopyOp&) {}
82     META_Object(simgear, TreesBoundingBoxCallback);
83     virtual BoundingBox computeBound(const Drawable&) const;
84 };
85
86 BoundingBox
87 TreesBoundingBoxCallback::computeBound(const Drawable& drawable) const
88 {
89     BoundingBox bb;
90     const Geometry* geom = static_cast<const Geometry*>(&drawable);
91     const Vec3Array* v = static_cast<const Vec3Array*>(geom->getVertexArray());
92     const Vec3Array* pos = static_cast<const Vec3Array*>(geom->getColorArray());
93     const Vec3Array* params
94         = static_cast<const Vec3Array*>(geom->getNormalArray());
95     const FloatArray* rot
96         = static_cast<const FloatArray*>(geom->getFogCoordArray());
97     float w = (*params)[0].x();
98     float h = (*params)[0].y();
99     Geometry::PrimitiveSetList primSets = geom->getPrimitiveSetList();
100     FloatArray::const_iterator rotitr = rot->begin();
101     for (Geometry::PrimitiveSetList::const_iterator psitr = primSets.begin(),
102              psend = primSets.end();
103          psitr != psend;
104          ++psitr, ++rotitr) {
105         Matrixd trnsfrm = (Matrixd::scale(w, w, h)
106                            * Matrixd::rotate(*rotitr, Vec3(0.0f, 0.0f, 1.0f)));
107         DrawArrays* da = static_cast<DrawArrays*>(psitr->get());
108         GLint psFirst = da->getFirst();
109         GLint psEndVert = psFirst + da->getCount();
110         for (GLint i = psFirst;i < psEndVert; ++i) {
111             Vec3 pt = (*v)[i];
112             pt = pt * trnsfrm;
113             pt += (*pos)[i];
114             bb.expandBy(pt);
115         }
116     }
117     return bb;
118 }
119
120 Geometry* makeSharedTreeGeometry(int numQuads)
121 {
122     // generate a repeatable random seed
123     mt seed;
124     mt_init(&seed, unsigned(123));
125     // set up the coords
126     osg::Vec3Array* v = new osg::Vec3Array;
127     osg::Vec2Array* t = new osg::Vec2Array;
128     v->reserve(numQuads * 4);
129     t->reserve(numQuads * 4);
130     for (int i = 0; i < numQuads; ++i) {
131         // Apply a random scaling factor and texture index.
132         float h = (mt_rand(&seed) + mt_rand(&seed)) / 2.0f + 0.5f;
133         float cw = h * .5;
134         v->push_back(Vec3(0.0f, -cw, 0.0f));
135         v->push_back(Vec3(0.0f, cw, 0.0f));
136         v->push_back(Vec3(0.0f, cw, h));
137         v->push_back(Vec3(0.0f,-cw, h));
138         // The texture coordinate range is not the entire coordinate
139         // space, as the texture has a number of different trees on
140         // it. Here we assign random coordinates and let the shader
141         // choose the variety.
142         float variety = mt_rand(&seed);
143         t->push_back(Vec2(variety, 0.0f));
144         t->push_back(Vec2(variety + 1.0f, 0.0f));
145         t->push_back(Vec2(variety + 1.0f, 0.234f));
146         t->push_back(Vec2(variety, 0.234f));
147     }
148     Geometry* result = new Geometry;
149     result->setVertexArray(v);
150     result->setTexCoordArray(0, t, Array::BIND_PER_VERTEX);
151     result->setComputeBoundingBoxCallback(new TreesBoundingBoxCallback);
152     result->setUseDisplayList(false);
153     return result;
154 }
155
156 ref_ptr<Geometry> sharedTreeGeometry;
157
158 Geometry* createTreeGeometry(float width, float height, int varieties)
159 {
160     if (!sharedTreeGeometry)
161         sharedTreeGeometry = makeSharedTreeGeometry(1600);
162     Geometry* quadGeom = simgear::clone(sharedTreeGeometry.get(),
163                                         CopyOp::SHALLOW_COPY);
164     Vec3Array* params = new Vec3Array;
165     params->push_back(Vec3(width, height, (float)varieties));
166     quadGeom->setNormalArray(params);
167     quadGeom->setNormalBinding(Geometry::BIND_OVERALL);
168     // Positions
169     quadGeom->setColorArray(new Vec3Array);
170     quadGeom->setColorBinding(Geometry::BIND_PER_VERTEX);
171     // Normals
172     if (use_tree_shadows || use_tree_normals)
173         {
174         quadGeom->setSecondaryColorArray(new Vec3Array);
175         quadGeom->setSecondaryColorBinding(Geometry::BIND_PER_VERTEX);
176         }
177     FloatArray* rotation = new FloatArray(3);
178     (*rotation)[0] = 0.0;
179     (*rotation)[1] = PI_2;
180     if (use_tree_shadows) {(*rotation)[2] = -1.0;}
181     quadGeom->setFogCoordArray(rotation);
182     quadGeom->setFogCoordBinding(Geometry::BIND_PER_PRIMITIVE_SET);
183     // The primitive sets render the same geometry, but the second
184     // will rotated 90 degrees by the vertex shader, which uses the
185     // fog coordinate as a rotation.
186     int imax = 2;
187     if (use_tree_shadows) {imax = 3;}
188     for (int i = 0; i < imax; ++i)
189         quadGeom->addPrimitiveSet(new DrawArrays(PrimitiveSet::QUADS));
190     return quadGeom;
191 }
192
193 EffectGeode* createTreeGeode(float width, float height, int varieties)
194 {
195     EffectGeode* result = new EffectGeode;
196     result->addDrawable(createTreeGeometry(width, height, varieties));
197     return result;
198 }
199
200 void addTreeToLeafGeode(Geode* geode, const SGVec3f& p, const SGVec3f& t)
201 {
202     Vec3 pos = toOsg(p);
203     Vec3 ter = toOsg(t);
204     unsigned int numDrawables = geode->getNumDrawables();
205     Geometry* geom
206         = static_cast<Geometry*>(geode->getDrawable(numDrawables - 1));
207     Vec3Array* posArray = static_cast<Vec3Array*>(geom->getColorArray());
208     Vec3Array* tnormalArray = NULL;
209     if (use_tree_shadows || use_tree_normals)
210         {tnormalArray = static_cast<Vec3Array*>(geom->getSecondaryColorArray());}
211     if (posArray->size()
212         >= static_cast<Vec3Array*>(geom->getVertexArray())->size()) {
213         Vec3Array* paramsArray
214             = static_cast<Vec3Array*>(geom->getNormalArray());
215         Vec3 params = (*paramsArray)[0];
216         geom = createTreeGeometry(params.x(), params.y(), params.z());
217         posArray = static_cast<Vec3Array*>(geom->getColorArray());
218         if (use_tree_shadows || use_tree_normals)
219                 {tnormalArray = static_cast<Vec3Array*>(geom->getSecondaryColorArray());}
220         geode->addDrawable(geom);
221     }
222     posArray->insert(posArray->end(), 4, pos);
223     if (use_tree_shadows || use_tree_normals)
224         {tnormalArray->insert(tnormalArray->end(),4,ter);}
225     size_t numVerts = posArray->size();
226     int imax = 2;
227     if (use_tree_shadows) {imax = 3;}
228     for (int i = 0; i < imax; ++i) {
229         DrawArrays* primSet
230             = static_cast<DrawArrays*>(geom->getPrimitiveSet(i));
231         primSet->setCount(numVerts);
232     }
233 }
234
235 typedef std::map<std::string, osg::observer_ptr<Effect> > EffectMap;
236
237 static EffectMap treeEffectMap;
238
239 // Helper classes for creating the quad tree
240 namespace
241 {
242 struct MakeTreesLeaf
243 {
244     MakeTreesLeaf(float range, int varieties, float width, float height,
245         Effect* effect) :
246         _range(range),  _varieties(varieties),
247         _width(width), _height(height), _effect(effect) {}
248
249     MakeTreesLeaf(const MakeTreesLeaf& rhs) :
250         _range(rhs._range),
251         _varieties(rhs._varieties), _width(rhs._width), _height(rhs._height),
252         _effect(rhs._effect)
253     {}
254
255     LOD* operator() () const
256     {
257         LOD* result = new LOD;
258
259         // Create a series of LOD nodes so trees cover decreases slightly
260         // gradually with distance from _range to 2*_range
261         for (float i = 0.0; i < SG_TREE_FADE_OUT_LEVELS; i++)
262         {
263             EffectGeode* geode = createTreeGeode(_width, _height, _varieties);
264             geode->setEffect(_effect.get());
265             result->addChild(geode, 0, _range * (1.0 + i / (SG_TREE_FADE_OUT_LEVELS - 1.0)));
266         }
267         return result;
268     }
269     float _range;
270     int _varieties;
271     float _width;
272     float _height;
273     ref_ptr<Effect> _effect;
274 };
275
276 struct AddTreesLeafObject
277 {
278     void operator() (LOD* lod, const TreeBin::Tree& tree) const
279     {
280         Geode* geode = static_cast<Geode*>(lod->getChild(int(tree.position.x() * 10.0f) % lod->getNumChildren()));
281         addTreeToLeafGeode(geode, tree.position, tree.tnormal);
282     }
283 };
284
285 struct GetTreeCoord
286 {
287     Vec3 operator() (const TreeBin::Tree& tree) const
288     {
289         return toOsg(tree.position);
290     }
291 };
292
293 typedef QuadTreeBuilder<LOD*, TreeBin::Tree, MakeTreesLeaf, AddTreesLeafObject,
294                         GetTreeCoord> ShaderGeometryQuadtree;
295 }
296
297 struct TreeTransformer
298 {
299     TreeTransformer(Matrix& mat_) : mat(mat_) {}
300     TreeBin::Tree operator()(const TreeBin::Tree& tree) const
301     {
302         Vec3 pos = toOsg(tree.position);
303         Vec3 norm = toOsg(tree.tnormal);
304         return TreeBin::Tree(toSG(pos * mat),toSG(norm * mat));
305     }
306     Matrix mat;
307 };
308
309 // We may end up with a quadtree with many empty leaves. One might say
310 // that we should avoid constructing the leaves in the first place,
311 // but this node visitor tries to clean up after the fact.
312
313 struct QuadTreeCleaner : public osg::NodeVisitor
314 {
315     QuadTreeCleaner() : NodeVisitor(NodeVisitor::TRAVERSE_ALL_CHILDREN)
316     {
317     }
318     void apply(LOD& lod)
319     {
320         for (int i  = lod.getNumChildren() - 1; i >= 0; --i) {
321             EffectGeode* geode = dynamic_cast<EffectGeode*>(lod.getChild(i));
322             if (!geode)
323                 continue;
324             bool geodeEmpty = true;
325             for (unsigned j = 0; j < geode->getNumDrawables(); ++j) {
326                 const Geometry* geom = dynamic_cast<Geometry*>(geode->getDrawable(j));
327                 if (!geom) {
328                     geodeEmpty = false;
329                     break;
330                 }
331                 for (unsigned k = 0; k < geom->getNumPrimitiveSets(); k++) {
332                     const PrimitiveSet* ps = geom->getPrimitiveSet(k);
333                     if (ps->getNumIndices() > 0) {
334                         geodeEmpty = false;
335                         break;
336                     }
337                 }
338             }
339             if (geodeEmpty)
340                 lod.removeChildren(i, 1);
341         }
342     }
343 };
344
345 // This actually returns a MatrixTransform node. If we rotate the whole
346 // forest into the local Z-up coordinate system we can reuse the
347 // primitive tree geometry for all the forests of the same type.
348
349 osg::Group* createForest(SGTreeBinList& forestList, const osg::Matrix& transform,
350                          const SGReaderWriterOptions* options)
351 {
352     Matrix transInv = Matrix::inverse(transform);
353     static Matrix ident;
354     // Set up some shared structures.
355     ref_ptr<Group> group;
356     MatrixTransform* mt = new MatrixTransform(transform);
357
358     SGTreeBinList::iterator i;
359
360     use_tree_shadows = false;
361     use_tree_normals = false;
362     if (options) {
363         SGPropertyNode* propertyNode = options->getPropertyNode().get();
364         if (propertyNode) {
365             use_tree_shadows
366                 = propertyNode->getBoolValue("/sim/rendering/random-vegetation-shadows",
367                                              use_tree_shadows);
368            use_tree_normals
369                 = propertyNode->getBoolValue("/sim/rendering/random-vegetation-normals",
370                                              use_tree_normals);
371                 }
372         }
373
374     for (i = forestList.begin(); i != forestList.end(); ++i) {
375         TreeBin* forest = *i;
376
377         ref_ptr<Effect> effect;
378         EffectMap::iterator iter = treeEffectMap.find(forest->texture);
379
380         if ((iter == treeEffectMap.end())||
381             (!iter->second.lock(effect)))
382         {
383             SGPropertyNode_ptr effectProp = new SGPropertyNode;
384             makeChild(effectProp, "inherits-from")->setStringValue(forest->teffect);
385             SGPropertyNode* params = makeChild(effectProp, "parameters");
386             // emphasize n = 0
387             params->getChild("texture", 0, true)->getChild("image", 0, true)
388                 ->setStringValue(forest->texture);
389             effect = makeEffect(effectProp, true, options);
390             if (iter == treeEffectMap.end())
391                 treeEffectMap.insert(EffectMap::value_type(forest->texture, effect));
392             else
393                 iter->second = effect; // update existing, but empty observer
394         }
395
396         // Now, create a quadtree for the forest.
397         ShaderGeometryQuadtree
398             quadtree(GetTreeCoord(), AddTreesLeafObject(),
399                      SG_TREE_QUAD_TREE_DEPTH,
400                      MakeTreesLeaf(forest->range, forest->texture_varieties,
401                                    forest->width, forest->height, effect));
402         // Transform tree positions from the "geocentric" positions we
403         // get from the scenery polys into the local Z-up coordinate
404         // system.
405         std::vector<TreeBin::Tree> rotatedTrees;
406         rotatedTrees.reserve(forest->_trees.size());
407         std::transform(forest->_trees.begin(), forest->_trees.end(),
408                        std::back_inserter(rotatedTrees),
409                        TreeTransformer(transInv));
410         quadtree.buildQuadTree(rotatedTrees.begin(), rotatedTrees.end());
411         group = quadtree.getRoot();
412
413         for (size_t i = 0; i < group->getNumChildren(); ++i)
414             mt->addChild(group->getChild(i));
415
416         delete forest;
417     }
418
419     forestList.clear();
420     QuadTreeCleaner cleaner;
421     mt->accept(cleaner);
422     return mt;
423 }
424
425
426 }