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,
27 #include <boost/tuple/tuple_comparison.hpp>
29 #include <osg/AlphaFunc>
30 #include <osg/Billboard>
31 #include <osg/BlendFunc>
33 #include <osg/Geometry>
34 #include <osg/Material>
36 #include <osg/MatrixTransform>
38 #include <osg/StateSet>
39 #include <osg/Texture2D>
42 #include <osgDB/ReadFile>
43 #include <osgDB/FileUtils>
45 #include <simgear/debug/logstream.hxx>
46 #include <simgear/math/sg_random.h>
47 #include <simgear/misc/sg_path.hxx>
48 #include <simgear/scene/util/QuadTreeBuilder.hxx>
49 #include <simgear/scene/util/RenderConstants.hxx>
50 #include <simgear/scene/util/StateAttributeFactory.hxx>
51 #include <simgear/structure/OSGUtils.hxx>
53 #include "ShaderGeometry.hxx"
54 #include "TreeBin.hxx"
56 #define SG_TREE_QUAD_TREE_DEPTH 3
63 // Tree instance scheme:
64 // vertex - local position of quad vertex.
65 // normal - x y scaling, z number of varieties
66 // fog coord - rotation
67 // color - xyz of tree quad origin, replicated 4 times.
69 // The tree quad is rendered twice, with different rotations, to
70 // create the crossed tree geometry.
72 struct TreesBoundingBoxCallback : public Drawable::ComputeBoundingBoxCallback
74 TreesBoundingBoxCallback() {}
75 TreesBoundingBoxCallback(const TreesBoundingBoxCallback&, const CopyOp&) {}
76 META_Object(simgear, TreesBoundingBoxCallback);
77 virtual BoundingBox computeBound(const Drawable&) const;
81 TreesBoundingBoxCallback::computeBound(const Drawable& drawable) const
84 const Geometry* geom = static_cast<const Geometry*>(&drawable);
85 const Vec3Array* v = static_cast<const Vec3Array*>(geom->getVertexArray());
86 const Vec3Array* pos = static_cast<const Vec3Array*>(geom->getColorArray());
87 const Vec3Array* params
88 = static_cast<const Vec3Array*>(geom->getNormalArray());
90 = static_cast<const FloatArray*>(geom->getFogCoordArray());
91 float w = (*params)[0].x();
92 float h = (*params)[0].y();
93 Geometry::PrimitiveSetList primSets = geom->getPrimitiveSetList();
94 FloatArray::const_iterator rotitr = rot->begin();
95 for (Geometry::PrimitiveSetList::const_iterator psitr = primSets.begin(),
96 psend = primSets.end();
99 Matrixd trnsfrm = (Matrixd::scale(w, w, h)
100 * Matrixd::rotate(*rotitr, Vec3(0.0f, 0.0f, 1.0f)));
101 DrawArrays* da = static_cast<DrawArrays*>(psitr->get());
102 GLint psFirst = da->getFirst();
103 GLint psEndVert = psFirst + da->getCount();
104 for (GLint i = psFirst;i < psEndVert; ++i) {
114 Geometry* makeSharedTreeGeometry(int numQuads)
116 // generate a repeatable random seed
118 mt_init(&seed, unsigned(123));
120 osg::Vec3Array* v = new osg::Vec3Array;
121 osg::Vec2Array* t = new osg::Vec2Array;
122 v->reserve(numQuads * 4);
123 t->reserve(numQuads * 4);
124 for (int i = 0; i < numQuads; ++i) {
125 // Apply a random scaling factor and texture index.
126 float h = (mt_rand(&seed) + mt_rand(&seed)) / 2.0f + 0.5f;
128 v->push_back(Vec3(0.0f, -cw, 0.0f));
129 v->push_back(Vec3(0.0f, cw, 0.0f));
130 v->push_back(Vec3(0.0f, cw, h));
131 v->push_back(Vec3(0.0f,-cw, h));
132 // The texture coordinate range is not the entire coordinate
133 // space, as the texture has a number of different trees on
134 // it. Here we assign random coordinates and let the shader
135 // choose the variety.
136 float variety = mt_rand(&seed);
137 t->push_back(Vec2(variety, 0.0f));
138 t->push_back(Vec2(variety + 1.0f, 0.0f));
139 t->push_back(Vec2(variety + 1.0f, 1.0f));
140 t->push_back(Vec2(variety, 1.0f));
142 Geometry* result = new Geometry;
143 result->setVertexArray(v);
144 result->setTexCoordArray(0, t);
145 result->setComputeBoundingBoxCallback(new TreesBoundingBoxCallback);
146 result->setUseDisplayList(false);
150 ref_ptr<Geometry> sharedTreeGeometry;
152 Geometry* createTreeGeometry(float width, float height, int varieties)
154 if (!sharedTreeGeometry)
155 sharedTreeGeometry = makeSharedTreeGeometry(1600);
156 Geometry* quadGeom = simgear::clone(sharedTreeGeometry.get(),
157 CopyOp::SHALLOW_COPY);
158 Vec3Array* params = new Vec3Array;
159 params->push_back(Vec3(width, height, (float)varieties));
160 quadGeom->setNormalArray(params);
161 quadGeom->setNormalBinding(Geometry::BIND_OVERALL);
163 quadGeom->setColorArray(new Vec3Array);
164 quadGeom->setColorBinding(Geometry::BIND_PER_VERTEX);
165 FloatArray* rotation = new FloatArray(2);
166 (*rotation)[0] = 0.0;
167 (*rotation)[1] = PI_2;
168 quadGeom->setFogCoordArray(rotation);
169 quadGeom->setFogCoordBinding(Geometry::BIND_PER_PRIMITIVE_SET);
170 for (int i = 0; i < 2; ++i)
171 quadGeom->addPrimitiveSet(new DrawArrays(PrimitiveSet::QUADS));
175 Geode* createTreeGeode(float width, float height, int varieties)
177 Geode* result = new Geode;
178 result->addDrawable(createTreeGeometry(width, height, varieties));
182 void addTreeToLeafGeode(Geode* geode, const SGVec3f& p)
184 const Vec3& pos = p.osg();
185 unsigned int numDrawables = geode->getNumDrawables();
187 = static_cast<Geometry*>(geode->getDrawable(numDrawables - 1));
188 Vec3Array* posArray = static_cast<Vec3Array*>(geom->getColorArray());
190 >= static_cast<Vec3Array*>(geom->getVertexArray())->size()) {
191 Vec3Array* paramsArray
192 = static_cast<Vec3Array*>(geom->getNormalArray());
193 Vec3 params = (*paramsArray)[0];
194 geom = createTreeGeometry(params.x(), params.y(), params.z());
195 posArray = static_cast<Vec3Array*>(geom->getColorArray());
196 geode->addDrawable(geom);
198 posArray->insert(posArray->end(), 4, pos);
199 size_t numVerts = posArray->size();
200 for (int i = 0; i < 2; ++i) {
202 = static_cast<DrawArrays*>(geom->getPrimitiveSet(i));
203 primSet->setCount(numVerts);
207 static char vertexShaderSource[] =
208 "varying float fogFactor;\n"
212 " float numVarieties = gl_Normal.z;\n"
213 " float texFract = floor(fract(gl_MultiTexCoord0.x) * numVarieties) / numVarieties;\n"
214 " texFract += floor(gl_MultiTexCoord0.x) / numVarieties;\n"
215 " float sr = sin(gl_FogCoord);\n"
216 " float cr = cos(gl_FogCoord);\n"
217 " gl_TexCoord[0] = vec4(texFract, gl_MultiTexCoord0.y, 0.0, 0.0);\n"
219 " vec3 position = gl_Vertex.xyz * gl_Normal.xxy;\n"
220 // Rotation of the generic quad to specific one for the tree.
221 " position.xy = vec2(dot(position.xy, vec2(cr, sr)), dot(position.xy, vec2(-sr, cr)));\n"
222 " position = position + gl_Color.xyz;\n"
223 " gl_Position = gl_ModelViewProjectionMatrix * vec4(position,1.0);\n"
224 " vec3 ecPosition = vec3(gl_ModelViewMatrix * vec4(position, 1.0));\n"
225 " float n = dot(normalize(gl_LightSource[0].position.xyz), normalize(-ecPosition));\n"
226 " vec3 diffuse = gl_FrontMaterial.diffuse.rgb * max(0.1, n);\n"
227 " vec4 ambientColor = gl_FrontLightModelProduct.sceneColor + gl_LightSource[0].ambient * gl_FrontMaterial.ambient;\n"
228 " gl_FrontColor = ambientColor + gl_LightSource[0].diffuse * vec4(diffuse, 1.0);\n"
229 " gl_BackColor = gl_FrontColor;\n"
230 " float fogCoord = abs(ecPosition.z);\n"
231 " fogFactor = exp( -gl_Fog.density * gl_Fog.density * fogCoord * fogCoord);\n"
232 " fogFactor = clamp(fogFactor, 0.0, 1.0);\n"
235 static char fragmentShaderSource[] =
236 "uniform sampler2D baseTexture; \n"
237 "varying float fogFactor;\n"
241 " vec4 base = texture2D( baseTexture, gl_TexCoord[0].st);\n"
242 " vec4 finalColor = base * gl_Color;\n"
243 " gl_FragColor = mix(gl_Fog.color, finalColor, fogFactor );\n"
246 typedef std::map<std::string, osg::ref_ptr<StateSet> > StateSetMap;
248 static StateSetMap treeTextureMap;
250 // Helper classes for creating the quad tree
255 MakeTreesLeaf(float range, int varieties, float width, float height) :
256 _range(range), _varieties(varieties),
257 _width(width), _height(height) {}
259 MakeTreesLeaf(const MakeTreesLeaf& rhs) :
261 _varieties(rhs._varieties), _width(rhs._width), _height(rhs._height)
264 LOD* operator() () const
266 LOD* result = new LOD;
267 Geode* geode = createTreeGeode(_width, _height, _varieties);
268 result->addChild(geode, 0, _range);
277 struct AddTreesLeafObject
279 void operator() (LOD* lod, const TreeBin::Tree& tree) const
281 Geode* geode = static_cast<Geode*>(lod->getChild(0));
282 addTreeToLeafGeode(geode, tree.position);
288 Vec3 operator() (const TreeBin::Tree& tree) const
290 return tree.position.osg();
294 typedef QuadTreeBuilder<LOD*, TreeBin::Tree, MakeTreesLeaf, AddTreesLeafObject,
295 GetTreeCoord> ShaderGeometryQuadtree;
298 struct TreeTransformer
300 TreeTransformer(Matrix& mat_) : mat(mat_) {}
301 TreeBin::Tree operator()(const TreeBin::Tree& tree) const
303 const Vec3& pos = tree.position.osg();
304 return TreeBin::Tree(SGVec3f(pos * mat));
309 // This actually returns a MatrixTransform node. If we rotate the whole
310 // forest into the local Z-up coordinate system we can reuse the
311 // primitive tree geometry for all the forests of the same type.
313 osg::Group* createForest(TreeBin& forest, const osg::Matrix& transform)
315 Matrix transInv = Matrix::inverse(transform);
317 // Set up some shared structures.
318 ref_ptr<Group> group;
320 osg::StateSet* stateset = 0;
321 StateSetMap::iterator iter = treeTextureMap.find(forest.texture);
322 if (iter == treeTextureMap.end()) {
323 osg::Texture2D *tex = new osg::Texture2D;
324 tex->setWrap( osg::Texture2D::WRAP_S, osg::Texture2D::CLAMP );
325 tex->setWrap( osg::Texture2D::WRAP_T, osg::Texture2D::CLAMP );
326 tex->setImage(osgDB::readImageFile(forest.texture));
328 static ref_ptr<AlphaFunc> alphaFunc;
329 static ref_ptr<Program> program;
330 static ref_ptr<Uniform> baseTextureSampler;
331 static ref_ptr<Material> material;
333 stateset = new osg::StateSet;
334 stateset->setTextureAttributeAndModes(0, tex, osg::StateAttribute::ON );
335 stateset->setRenderBinDetails(RANDOM_OBJECTS_BIN, "DepthSortedBin");
336 if (!program.valid()) {
337 alphaFunc = new AlphaFunc;
338 alphaFunc->setFunction(AlphaFunc::GEQUAL,0.33f);
339 program = new Program;
340 baseTextureSampler = new osg::Uniform("baseTexture", 0);
341 Shader* vertex_shader = new Shader(Shader::VERTEX,
343 program->addShader(vertex_shader);
344 Shader* fragment_shader = new Shader(Shader::FRAGMENT,
345 fragmentShaderSource);
346 program->addShader(fragment_shader);
347 material = new Material;
348 // DonĀ“t track vertex color
349 material->setColorMode(Material::OFF);
350 material->setAmbient(Material::FRONT_AND_BACK,
351 Vec4(1.0f, 1.0f, 1.0f, 1.0f));
352 material->setDiffuse(Material::FRONT_AND_BACK,
353 Vec4(1.0f, 1.0f, 1.0f, 1.0f));
355 stateset->setAttributeAndModes(alphaFunc.get());
356 stateset->setAttribute(program.get());
357 stateset->addUniform(baseTextureSampler.get());
358 stateset->setMode(GL_VERTEX_PROGRAM_TWO_SIDE, StateAttribute::ON);
359 stateset->setAttribute(material.get());
361 treeTextureMap.insert(StateSetMap::value_type(forest.texture,
364 stateset = iter->second.get();
366 // Now, create a quadtree for the forest.
368 ShaderGeometryQuadtree
369 quadtree(GetTreeCoord(), AddTreesLeafObject(),
370 SG_TREE_QUAD_TREE_DEPTH,
371 MakeTreesLeaf(forest.range, forest.texture_varieties,
372 forest.width, forest.height));
373 // Transform tree positions from the "geocentric" positions we
374 // get from the scenery polys into the local Z-up coordinate
376 std::vector<TreeBin::Tree> rotatedTrees;
377 rotatedTrees.reserve(forest._trees.size());
378 std::transform(forest._trees.begin(), forest._trees.end(),
379 std::back_inserter(rotatedTrees),
380 TreeTransformer(transInv));
381 quadtree.buildQuadTree(rotatedTrees.begin(), rotatedTrees.end());
382 group = quadtree.getRoot();
384 MatrixTransform* mt = new MatrixTransform(transform);
385 for (size_t i = 0; i < group->getNumChildren(); ++i)
386 mt->addChild(group->getChild(i));
387 mt->setStateSet(stateset);