]> git.mxchange.org Git - simgear.git/blob - simgear/scene/tgdb/TreeBin.cxx
Cleanup and performance tuning of the random trees code.
[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 <string>
24 #include <map>
25
26 #include <osg/AlphaFunc>
27 #include <osg/Billboard>
28 #include <osg/BlendFunc>
29 #include <osg/Geode>
30 #include <osg/Geometry>
31 #include <osg/Material>
32 #include <osg/Math>
33 #include <osg/MatrixTransform>
34 #include <osg/Matrix>
35 #include <osg/StateSet>
36 #include <osg/Texture2D>
37 #include <osg/TexEnv>
38
39 #include <osgDB/ReadFile>
40 #include <osgDB/FileUtils>
41
42 #include <simgear/misc/sg_path.hxx>
43 #include <simgear/scene/util/QuadTreeBuilder.hxx>
44 #include <simgear/scene/util/RenderConstants.hxx>
45 #include <simgear/scene/util/StateAttributeFactory.hxx>
46
47 #include "ShaderGeometry.hxx"
48 #include "TreeBin.hxx"
49
50 #define SG_TREE_QUAD_TREE_DEPTH 3
51
52 // Comments from Tim Moore:
53 // Some work remains for this code. Stuart's enhancement for multiple
54 // textures per forest should be integrated. We should try to use one
55 // ShaderGeometry for *all* the trees in the scene graph and do the
56 // rotation and scale with a MatrixTransform above the trees quad
57 // tree. The positions would of course have to be transformed by the
58 // inverse of that transform. Also, we should investigate whether it
59 // would be better to instantiate trees as polygons in a osg::Geometry
60 // object instead of using the ShaderGeometry instancing technique.
61
62 using namespace osg;
63
64 namespace simgear
65 {
66
67 osg::Geometry* createOrthQuads(float w, float h, const osg::Matrix& rotate)
68 {
69
70     //const osg::Vec3& pos = osg::Vec3(0.0f,0.0f,0.0f),
71     // set up the coords
72     // Create front and back polygons so we don't need to screw around
73     // with two-sided lighting in the shader.
74     osg::Vec3Array& v = *(new osg::Vec3Array(8));
75     osg::Vec3Array& n = *(new osg::Vec3Array(8));
76     osg::Vec2Array& t = *(new osg::Vec2Array(8));
77     
78     float cw = w*0.5f;
79
80     v[0].set(0.0f,-cw,0.0f);
81     v[1].set(0.0f, cw,0.0f);
82     v[2].set(0.0f, cw,h);
83     v[3].set(0.0f,-cw,h);
84
85     v[4].set(-cw,0.0f,0.0f);
86     v[5].set( cw,0.0f,0.0f);
87     v[6].set( cw,0.0f,h);
88     v[7].set(-cw,0.0f,h);
89
90     t[0].set(0.0f,0.0f);
91     t[1].set(1.0f,0.0f);
92     t[2].set(1.0f,1.0f);
93     t[3].set(0.0f,1.0f);
94
95     t[4].set(0.0f,0.0f);
96     t[5].set(1.0f,0.0f);
97     t[6].set(1.0f,1.0f);
98     t[7].set(0.0f,1.0f);
99
100     // For now the normal is normal to the quad. If we want to get
101     // fancier and approximate a cylindrical tree or something, then
102     // we would really want more geometry.
103     std::fill(n.begin(), n.begin() + 4, Vec3f(1.0f, 0.0f, 0.0f));
104     std::fill(n.begin() + 4, n.end(), Vec3f(0.0f, -1.0f, 0.0f));
105     for (unsigned int i = 0; i < 8; i++) {
106         v[i] = v[i] * rotate;
107         // Should be the inverse transpose, but assume that rotate is
108         // orthonormal.
109         n[i] = n[i] * rotate;     
110     }
111
112     osg::Geometry *geom = new osg::Geometry;
113
114     geom->setVertexArray(&v);
115     geom->setTexCoordArray(0, &t);
116     geom->setNormalArray(&n);
117     geom->setNormalBinding(Geometry::BIND_PER_VERTEX);
118     // No color for now; that's used to pass the position.
119     geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,8));
120
121     return geom;
122 }
123
124  static char vertexShaderSource[] = 
125     "varying float fogFactor;\n"
126     "\n"
127     "void main(void)\n"
128     "{\n"
129     "  vec3 position = gl_Vertex.xyz * gl_Color.w + gl_Color.xyz;\n"
130     "  gl_Position   = gl_ModelViewProjectionMatrix * vec4(position,1.0);\n"
131     "  vec3 ecPosition = vec3(gl_ModelViewMatrix * vec4(position, 1.0));\n"
132     "  vec3 N = normalize(gl_NormalMatrix * gl_Normal);\n"
133     "  vec3 diffuse = gl_FrontMaterial.diffuse.rgb * max(0.0, dot(N, gl_LightSource[0].position.xyz));\n"
134     "  vec3 backDiffuse = gl_FrontMaterial.diffuse.rgb * max(0.0, dot(-N, gl_LightSource[0].position.xyz));\n"
135     " vec4 ambientColor = gl_FrontLightModelProduct.sceneColor + gl_LightSource[0].ambient * gl_FrontMaterial.ambient;\n"
136     " gl_FrontColor = ambientColor + gl_LightSource[0].diffuse * vec4(diffuse, 1.0);\n"
137     " gl_BackColor = ambientColor + gl_LightSource[0].diffuse * vec4(backDiffuse, 1.0)\n;"
138     "  gl_TexCoord[0] = gl_MultiTexCoord0;\n"
139     " float fogCoord = abs(ecPosition.z);\n"
140     "  fogFactor = exp( -gl_Fog.density * gl_Fog.density * fogCoord * fogCoord);\n"
141     "  fogFactor = clamp(fogFactor, 0.0, 1.0);\n"
142     "}\n";
143
144 static char fragmentShaderSource[] = 
145     "uniform sampler2D baseTexture; \n"
146 //        "varying vec3 N;\n"\r
147 //        "varying vec3 v;\n"
148     "varying float fogFactor;\n"
149     "\n"
150     "void main(void) \n"
151     "{ \n"
152     "  vec4 base = texture2D( baseTexture, gl_TexCoord[0].st);\n"
153     
154     "  vec4 finalColor = base * gl_Color;\n"
155     "  gl_FragColor = mix(gl_Fog.color, finalColor, fogFactor );\n"
156     "}\n";
157
158 typedef std::map<std::string, osg::ref_ptr<StateSet> > StateSetMap;
159
160 static StateSetMap treeTextureMap;
161
162 // Helper classes for creating the quad tree
163 namespace
164 {
165 struct MakeTreesLeaf
166 {
167     MakeTreesLeaf(float range, Geometry* geometry) :
168         _range(range), _geometry(geometry)
169     {}
170     MakeTreesLeaf(const MakeTreesLeaf& rhs) :
171         _range(rhs._range), _geometry(rhs._geometry) {}
172     LOD* operator() () const
173     {
174         LOD* result = new LOD;
175         Geode* geode = new Geode;
176         ShaderGeometry* sg = new ShaderGeometry;
177         sg->setGeometry(_geometry);
178         geode->addDrawable(sg);
179         result->addChild(geode, 0, _range);
180         return result;
181     }
182     float _range;
183     Geometry* _geometry;
184 };
185
186 struct AddTreesLeafObject
187 {
188     void operator() (LOD* lod, const TreeBin::Tree& tree) const
189     {
190         Geode* geode = static_cast<Geode*>(lod->getChild(0));
191         ShaderGeometry* sg
192             = static_cast<ShaderGeometry*>(geode->getDrawable(0));
193         sg->addTree(tree.position.osg(), tree.height);
194     }
195 };
196
197 struct GetTreeCoord
198 {
199     GetTreeCoord(const Matrix& transform) : _transform(transform) {}
200     GetTreeCoord(const GetTreeCoord& rhs) : _transform(rhs._transform) {}
201     Vec3 operator() (const TreeBin::Tree& tree) const
202     {
203         return tree.position.osg() * _transform;
204     }
205     Matrix _transform;
206 };
207
208 typedef QuadTreeBuilder<LOD*, TreeBin::Tree, MakeTreesLeaf, AddTreesLeafObject,
209                         GetTreeCoord> ShaderGeometryQuadtree;
210 }
211
212 osg::Group* createForest(TreeBin& forest, const osg::Matrix& transform)
213 {
214     // Set up some shared structures. 
215     // FIXME: Currently we only take the texture, height and width of the first tree in the forest. In the future
216     // we should be able to handle multiple textures etc.    
217     TreeBin::Tree firstTree = forest.getTree(0);
218     
219     osg::Geometry* shared_geometry = createOrthQuads(firstTree.width, 
220                                                      firstTree.height, 
221                                                      transform);
222     ref_ptr<Group> group;
223
224     osg::StateSet* stateset = 0;
225     StateSetMap::iterator iter = treeTextureMap.find(firstTree.texture);
226     if (iter == treeTextureMap.end()) {
227         osg::Texture2D *tex = new osg::Texture2D;
228         tex->setWrap( osg::Texture2D::WRAP_S, osg::Texture2D::CLAMP );
229         tex->setWrap( osg::Texture2D::WRAP_T, osg::Texture2D::CLAMP );
230         tex->setImage(osgDB::readImageFile(firstTree.texture));
231
232         static ref_ptr<AlphaFunc> alphaFunc;
233         static ref_ptr<Program> program;
234         static ref_ptr<Uniform> baseTextureSampler;
235         static ref_ptr<Material> material;
236     
237         stateset = new osg::StateSet;
238         stateset->setTextureAttributeAndModes(0, tex, osg::StateAttribute::ON );
239         stateset->setRenderBinDetails(RANDOM_OBJECTS_BIN, "DepthSortedBin");
240         if (!program.valid()) {
241             alphaFunc = new AlphaFunc;
242             alphaFunc->setFunction(AlphaFunc::GEQUAL,0.33f);
243             program  = new Program;
244             baseTextureSampler = new osg::Uniform("baseTexture", 0);
245             Shader* vertex_shader = new Shader(Shader::VERTEX, vertexShaderSource);
246             program->addShader(vertex_shader);
247             Shader* fragment_shader = new Shader(Shader::FRAGMENT,
248                                                  fragmentShaderSource);
249             program->addShader(fragment_shader);
250             material = new Material;
251             // DonĀ“t track vertex color
252             material->setColorMode(Material::OFF);
253             material->setAmbient(Material::FRONT_AND_BACK,
254                                  Vec4(.6f, .6f, .6f, 1.0f));
255             material->setDiffuse(Material::FRONT_AND_BACK,
256                                  Vec4(.4f, .4f, .4f, 1.0f));
257         }
258         stateset->setAttributeAndModes(alphaFunc.get());
259         stateset->setAttribute(program.get());
260         stateset->addUniform(baseTextureSampler.get());
261         stateset->setMode(GL_VERTEX_PROGRAM_TWO_SIDE, StateAttribute::ON);
262         stateset->setAttribute(material.get());
263         // XXX This should really come from a material definition
264         // instead of being hard-coded.
265         treeTextureMap.insert(StateSetMap::value_type(firstTree.texture,
266                                                       stateset));
267     } else {
268         stateset = iter->second.get();
269     }
270     // Now, create a quadtree for the forest.
271     {
272         ShaderGeometryQuadtree quadtree(GetTreeCoord(Matrix::inverse(transform)),
273                                         AddTreesLeafObject(),
274                                         SG_TREE_QUAD_TREE_DEPTH,
275                                         MakeTreesLeaf(firstTree.range,
276                                                       shared_geometry));
277         quadtree.buildQuadTree(forest._trees.begin(), forest._trees.end());
278         group = quadtree.getRoot();
279     }
280     group->setStateSet(stateset);
281     return group.release();    
282 }
283
284 }