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