]> git.mxchange.org Git - simgear.git/blob - simgear/scene/tgdb/TreeBin.cxx
Merge branch 'frohlich/weak' into next
[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 #include <algorithm>
23 #include <vector>
24 #include <string>
25 #include <map>
26
27 #include <boost/tuple/tuple_comparison.hpp>
28
29 #include <osg/AlphaFunc>
30 #include <osg/Billboard>
31 #include <osg/BlendFunc>
32 #include <osg/Geode>
33 #include <osg/Geometry>
34 #include <osg/Material>
35 #include <osg/Math>
36 #include <osg/MatrixTransform>
37 #include <osg/Matrix>
38 #include <osg/StateSet>
39 #include <osg/Texture2D>
40 #include <osg/TexEnv>
41
42 #include <osgDB/ReadFile>
43 #include <osgDB/FileUtils>
44
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>
52
53 #include "ShaderGeometry.hxx"
54 #include "TreeBin.hxx"
55
56 #define SG_TREE_QUAD_TREE_DEPTH 3
57
58 using namespace osg;
59
60 namespace simgear
61 {
62
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.
68 //
69 // The tree quad is rendered twice, with different rotations, to
70 // create the crossed tree geometry.
71
72 struct TreesBoundingBoxCallback : public Drawable::ComputeBoundingBoxCallback
73 {
74     TreesBoundingBoxCallback() {}
75     TreesBoundingBoxCallback(const TreesBoundingBoxCallback&, const CopyOp&) {}
76     META_Object(simgear, TreesBoundingBoxCallback);
77     virtual BoundingBox computeBound(const Drawable&) const;
78 };
79
80 BoundingBox
81 TreesBoundingBoxCallback::computeBound(const Drawable& drawable) const
82 {
83     BoundingBox bb;
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());
89     const FloatArray* rot
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();
97          psitr != psend;
98          ++psitr, ++rotitr) {
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) {
105             Vec3 pt = (*v)[i];
106             pt = pt * trnsfrm;
107             pt += (*pos)[i];
108             bb.expandBy(pt);
109         }
110     }
111     return bb;
112 }
113
114 Geometry* makeSharedTreeGeometry(int numQuads)
115 {
116     // generate a repeatable random seed
117     mt seed;
118     mt_init(&seed, unsigned(123));
119     // set up the coords
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;
127         float cw = h * .5;
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));
141     }
142     Geometry* result = new Geometry;
143     result->setVertexArray(v);
144     result->setTexCoordArray(0, t);
145     result->setComputeBoundingBoxCallback(new TreesBoundingBoxCallback);
146     result->setUseDisplayList(false);
147     return result;
148 }
149
150 ref_ptr<Geometry> sharedTreeGeometry;
151
152 Geometry* createTreeGeometry(float width, float height, int varieties)
153 {
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);
162     // Positions
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));
172     return quadGeom;
173 }
174
175 Geode* createTreeGeode(float width, float height, int varieties)
176 {
177     Geode* result = new Geode;
178     result->addDrawable(createTreeGeometry(width, height, varieties));
179     return result;
180 }
181
182 void addTreeToLeafGeode(Geode* geode, const SGVec3f& p)
183 {
184     Vec3 pos = toOsg(p);
185     unsigned int numDrawables = geode->getNumDrawables();
186     Geometry* geom
187         = static_cast<Geometry*>(geode->getDrawable(numDrawables - 1));
188     Vec3Array* posArray = static_cast<Vec3Array*>(geom->getColorArray());
189     if (posArray->size()
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);
197     }
198     posArray->insert(posArray->end(), 4, pos);
199     size_t numVerts = posArray->size();
200     for (int i = 0; i < 2; ++i) {
201         DrawArrays* primSet
202             = static_cast<DrawArrays*>(geom->getPrimitiveSet(i));
203         primSet->setCount(numVerts);
204     }
205 }
206
207  static char vertexShaderSource[] = 
208     "varying float fogFactor;\n"
209     "\n"
210     "void main(void)\n"
211     "{\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"
218     // scaling
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"
233     "}\n";
234
235 static char fragmentShaderSource[] = 
236     "uniform sampler2D baseTexture; \n"
237     "varying float fogFactor;\n"
238     "\n"
239     "void main(void) \n"
240     "{ \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"
244     "}\n";
245
246 typedef std::map<std::string, osg::ref_ptr<StateSet> > StateSetMap;
247
248 static StateSetMap treeTextureMap;
249
250 // Helper classes for creating the quad tree
251 namespace
252 {
253 struct MakeTreesLeaf
254 {
255     MakeTreesLeaf(float range, int varieties, float width, float height) :
256         _range(range),  _varieties(varieties),
257         _width(width), _height(height) {}
258
259     MakeTreesLeaf(const MakeTreesLeaf& rhs) :
260         _range(rhs._range),
261         _varieties(rhs._varieties), _width(rhs._width), _height(rhs._height)
262     {}
263
264     LOD* operator() () const
265     {
266         LOD* result = new LOD;
267         Geode* geode = createTreeGeode(_width, _height, _varieties);
268         result->addChild(geode, 0, _range);
269         return result;
270     }
271     float _range;
272     int _varieties;
273     float _width;
274     float _height;
275 };
276
277 struct AddTreesLeafObject
278 {
279     void operator() (LOD* lod, const TreeBin::Tree& tree) const
280     {
281         Geode* geode = static_cast<Geode*>(lod->getChild(0));
282         addTreeToLeafGeode(geode, tree.position);
283     }
284 };
285
286 struct GetTreeCoord
287 {
288     Vec3 operator() (const TreeBin::Tree& tree) const
289     {
290         return toOsg(tree.position);
291     }
292 };
293
294 typedef QuadTreeBuilder<LOD*, TreeBin::Tree, MakeTreesLeaf, AddTreesLeafObject,
295                         GetTreeCoord> ShaderGeometryQuadtree;
296 }
297
298 struct TreeTransformer
299 {
300     TreeTransformer(Matrix& mat_) : mat(mat_) {}
301     TreeBin::Tree operator()(const TreeBin::Tree& tree) const
302     {
303         Vec3 pos = toOsg(tree.position);
304         return TreeBin::Tree(toSG(pos * mat));
305     }
306     Matrix mat;
307 };
308
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.
312
313 osg::Group* createForest(TreeBin& forest, const osg::Matrix& transform)
314 {
315     Matrix transInv = Matrix::inverse(transform);
316     static Matrix ident;
317     // Set up some shared structures.
318     ref_ptr<Group> group;
319
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));
327
328         static ref_ptr<AlphaFunc> alphaFunc;
329         static ref_ptr<Program> program;
330         static ref_ptr<Uniform> baseTextureSampler;
331         static ref_ptr<Material> material;
332     
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,
342                                                vertexShaderSource);
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));
354         }
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());
360
361         treeTextureMap.insert(StateSetMap::value_type(forest.texture,
362                                                       stateset));
363     } else {
364         stateset = iter->second.get();
365     }
366     // Now, create a quadtree for the forest.
367     {
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
375         // system.
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();
383     }
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);
388     return mt;
389 }
390
391 }