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,
23 # include <simgear_config.h>
31 #include <boost/tuple/tuple_comparison.hpp>
34 #include <osg/Geometry>
36 #include <osg/MatrixTransform>
38 #include <osg/NodeVisitor>
40 #include <osgDB/ReadFile>
41 #include <osgDB/FileUtils>
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>
55 #include "ShaderGeometry.hxx"
56 #include "TreeBin.hxx"
58 #define SG_TREE_QUAD_TREE_DEPTH 3
59 #define SG_TREE_FADE_OUT_LEVELS 10
66 bool use_tree_shadows;
67 bool use_tree_normals;
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.
75 // The tree quad is rendered twice, with different rotations, to
76 // create the crossed tree geometry.
78 struct TreesBoundingBoxCallback : public Drawable::ComputeBoundingBoxCallback
80 TreesBoundingBoxCallback() {}
81 TreesBoundingBoxCallback(const TreesBoundingBoxCallback&, const CopyOp&) {}
82 META_Object(simgear, TreesBoundingBoxCallback);
83 virtual BoundingBox computeBound(const Drawable&) const;
87 TreesBoundingBoxCallback::computeBound(const Drawable& drawable) const
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());
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();
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) {
120 Geometry* makeSharedTreeGeometry(int numQuads)
122 // generate a repeatable random seed
124 mt_init(&seed, unsigned(123));
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;
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));
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);
156 ref_ptr<Geometry> sharedTreeGeometry;
158 Geometry* createTreeGeometry(float width, float height, int varieties)
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);
169 quadGeom->setColorArray(new Vec3Array);
170 quadGeom->setColorBinding(Geometry::BIND_PER_VERTEX);
172 if (use_tree_shadows || use_tree_normals)
174 quadGeom->setSecondaryColorArray(new Vec3Array);
175 quadGeom->setSecondaryColorBinding(Geometry::BIND_PER_VERTEX);
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.
187 if (use_tree_shadows) {imax = 3;}
188 for (int i = 0; i < imax; ++i)
189 quadGeom->addPrimitiveSet(new DrawArrays(PrimitiveSet::QUADS));
193 EffectGeode* createTreeGeode(float width, float height, int varieties)
195 EffectGeode* result = new EffectGeode;
196 result->addDrawable(createTreeGeometry(width, height, varieties));
200 void addTreeToLeafGeode(Geode* geode, const SGVec3f& p, const SGVec3f& t)
204 unsigned int numDrawables = geode->getNumDrawables();
206 = static_cast<Geometry*>(geode->getDrawable(numDrawables - 1));
207 Vec3Array* posArray = static_cast<Vec3Array*>(geom->getColorArray());
208 Vec3Array* tnormalArray;
209 if (use_tree_shadows || use_tree_normals)
210 {tnormalArray = static_cast<Vec3Array*>(geom->getSecondaryColorArray());}
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);
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();
227 if (use_tree_shadows) {imax = 3;}
228 for (int i = 0; i < imax; ++i) {
230 = static_cast<DrawArrays*>(geom->getPrimitiveSet(i));
231 primSet->setCount(numVerts);
235 typedef std::map<std::string, osg::observer_ptr<Effect> > EffectMap;
237 static EffectMap treeEffectMap;
239 // Helper classes for creating the quad tree
244 MakeTreesLeaf(float range, int varieties, float width, float height,
246 _range(range), _varieties(varieties),
247 _width(width), _height(height), _effect(effect) {}
249 MakeTreesLeaf(const MakeTreesLeaf& rhs) :
251 _varieties(rhs._varieties), _width(rhs._width), _height(rhs._height),
255 LOD* operator() () const
257 LOD* result = new LOD;
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++)
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)));
273 ref_ptr<Effect> _effect;
276 struct AddTreesLeafObject
278 void operator() (LOD* lod, const TreeBin::Tree& tree) const
280 Geode* geode = static_cast<Geode*>(lod->getChild(int(tree.position.x() * 10.0f) % lod->getNumChildren()));
281 addTreeToLeafGeode(geode, tree.position, tree.tnormal);
287 Vec3 operator() (const TreeBin::Tree& tree) const
289 return toOsg(tree.position);
293 typedef QuadTreeBuilder<LOD*, TreeBin::Tree, MakeTreesLeaf, AddTreesLeafObject,
294 GetTreeCoord> ShaderGeometryQuadtree;
297 struct TreeTransformer
299 TreeTransformer(Matrix& mat_) : mat(mat_) {}
300 TreeBin::Tree operator()(const TreeBin::Tree& tree) const
302 Vec3 pos = toOsg(tree.position);
303 Vec3 norm = toOsg(tree.tnormal);
304 return TreeBin::Tree(toSG(pos * mat),toSG(norm * mat));
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.
313 struct QuadTreeCleaner : public osg::NodeVisitor
315 QuadTreeCleaner() : NodeVisitor(NodeVisitor::TRAVERSE_ALL_CHILDREN)
320 for (int i = lod.getNumChildren() - 1; i >= 0; --i) {
321 EffectGeode* geode = dynamic_cast<EffectGeode*>(lod.getChild(i));
324 bool geodeEmpty = true;
325 for (unsigned j = 0; j < geode->getNumDrawables(); ++j) {
326 const Geometry* geom = dynamic_cast<Geometry*>(geode->getDrawable(j));
331 for (unsigned k = 0; k < geom->getNumPrimitiveSets(); k++) {
332 const PrimitiveSet* ps = geom->getPrimitiveSet(k);
333 if (ps->getNumIndices() > 0) {
340 lod.removeChildren(i, 1);
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.
349 osg::Group* createForest(SGTreeBinList& forestList, const osg::Matrix& transform,
350 const SGReaderWriterOptions* options)
352 Matrix transInv = Matrix::inverse(transform);
354 // Set up some shared structures.
355 ref_ptr<Group> group;
356 MatrixTransform* mt = new MatrixTransform(transform);
358 SGTreeBinList::iterator i;
360 use_tree_shadows = false;
361 use_tree_normals = false;
363 SGPropertyNode* propertyNode = options->getPropertyNode().get();
366 = propertyNode->getBoolValue("/sim/rendering/random-vegetation-shadows",
369 = propertyNode->getBoolValue("/sim/rendering/random-vegetation-normals",
374 for (i = forestList.begin(); i != forestList.end(); ++i) {
375 TreeBin* forest = *i;
377 ref_ptr<Effect> effect;
378 EffectMap::iterator iter = treeEffectMap.find(forest->texture);
380 if ((iter == treeEffectMap.end())||
381 (!iter->second.lock(effect)))
383 SGPropertyNode_ptr effectProp = new SGPropertyNode;
384 makeChild(effectProp, "inherits-from")->setStringValue(forest->teffect);
385 SGPropertyNode* params = makeChild(effectProp, "parameters");
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));
393 iter->second = effect; // update existing, but empty observer
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
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();
413 for (size_t i = 0; i < group->getNumChildren(); ++i)
414 mt->addChild(group->getChild(i));
420 QuadTreeCleaner cleaner;