]> git.mxchange.org Git - simgear.git/blob - simgear/scene/tgdb/SGTexturedTriangleBin.hxx
Random object support from Stuart Buchanan
[simgear.git] / simgear / scene / tgdb / SGTexturedTriangleBin.hxx
1 /* -*-c++-*-
2  *
3  * Copyright (C) 2006-2007 Mathias Froehlich 
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 #ifndef SG_TEXTURED_TRIANGLE_BIN_HXX
23 #define SG_TEXTURED_TRIANGLE_BIN_HXX
24
25 #include <osg/Array>
26 #include <osg/Geometry>
27 #include <osg/PrimitiveSet>
28
29 #include <simgear/math/sg_random.h>
30 #include <simgear/math/SGMath.hxx>
31 #include "SGTriangleBin.hxx"
32
33
34
35 struct SGVertNormTex {
36   SGVertNormTex()
37   { }
38   SGVertNormTex(const SGVec3f& v, const SGVec3f& n, const SGVec2f& t) :
39     vertex(v), normal(n), texCoord(t)
40   { }
41   struct less
42   {
43     inline bool operator() (const SGVertNormTex& l,
44                             const SGVertNormTex& r) const
45     {
46       if (l.vertex < r.vertex) return true;
47       else if (r.vertex < l.vertex) return false;
48       else if (l.normal < r.normal) return true;
49       else if (r.normal < l.normal) return false;
50       else return l.texCoord < r.texCoord;
51     }
52   };
53
54   SGVec3f vertex;
55   SGVec3f normal;
56   SGVec2f texCoord;
57 };
58
59 // Use a DrawElementsUShort if there are few enough vertices,
60 // otherwise fallback to DrawElementsUInt. Hide the differences
61 // between the two from the rest of the code.
62 //
63 // We don't bother with DrawElementsUByte because that is generally
64 // not an advantage on modern hardware.
65 class DrawElementsFacade {
66 public:
67     DrawElementsFacade(unsigned numVerts) :
68         _ushortElements(0), _uintElements(0)
69     {
70         if (numVerts > 65535)
71             _uintElements
72                 = new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES);
73         else
74             _ushortElements
75                 = new osg::DrawElementsUShort(osg::PrimitiveSet::TRIANGLES);
76     }
77     
78     void push_back(unsigned val)
79     {
80         if (_uintElements)
81             _uintElements->push_back(val);
82         else
83             _ushortElements->push_back(val);
84     }
85
86     osg::DrawElements* getDrawElements()
87     {
88         if (_uintElements)
89             return _uintElements;
90         return _ushortElements;
91     }
92 protected:
93     osg::DrawElementsUShort* _ushortElements;
94     osg::DrawElementsUInt* _uintElements;
95 };
96
97 class SGTexturedTriangleBin : public SGTriangleBin<SGVertNormTex> {
98 public:
99   SGTexturedTriangleBin()
100   {
101     seed = new mt;
102     mt_init(seed, 123);
103   }
104
105   // Computes and adds random surface points to the points list.
106   // The random points are computed with a density of (coverage points)/1
107   // The points are offsetted away from the triangles in
108   // offset * positive normal direction.
109   void addRandomSurfacePoints(float coverage, float offset,
110                               std::vector<SGVec3f>& points) const
111   {
112     unsigned num = getNumTriangles();
113     for (unsigned i = 0; i < num; ++i) {
114       triangle_ref triangleRef = getTriangleRef(i);
115       SGVec3f v0 = getVertex(triangleRef[0]).vertex;
116       SGVec3f v1 = getVertex(triangleRef[1]).vertex;
117       SGVec3f v2 = getVertex(triangleRef[2]).vertex;
118       SGVec3f normal = cross(v1 - v0, v2 - v0);
119       
120       // Compute the area
121       float area = 0.5f*length(normal);
122       if (area <= SGLimitsf::min())
123         continue;
124       
125       // For partial units of area, use a zombie door method to
126       // create the proper random chance of a light being created
127       // for this triangle
128       float unit = area + mt_rand(seed)*coverage;
129       
130       SGVec3f offsetVector = offset*normalize(normal);
131       // generate a light point for each unit of area
132       while ( coverage < unit ) {
133         float a = mt_rand(seed);
134         float b = mt_rand(seed);
135         if ( a + b > 1 ) {
136           a = 1 - a;
137           b = 1 - b;
138         }
139         float c = 1 - a - b;
140         SGVec3f randomPoint = offsetVector + a*v0 + b*v1 + c*v2;
141         points.push_back(randomPoint);
142         unit -= coverage;
143       }
144     }
145   }
146   
147    void addRandomPoints(float coverage, 
148                         std::vector<SGVec3f>& points) const
149   {
150     unsigned num = getNumTriangles();
151     for (unsigned i = 0; i < num; ++i) {
152       triangle_ref triangleRef = getTriangleRef(i);
153       SGVec3f v0 = getVertex(triangleRef[0]).vertex;
154       SGVec3f v1 = getVertex(triangleRef[1]).vertex;
155       SGVec3f v2 = getVertex(triangleRef[2]).vertex;
156       SGVec3f normal = cross(v1 - v0, v2 - v0);
157       
158       // Compute the area
159       float area = 0.5f*length(normal);
160       if (area <= SGLimitsf::min())
161         continue;
162       
163       // for partial units of area, use a zombie door method to
164       // create the proper random chance of an object being created
165       // for this triangle.
166       double num = area / coverage + mt_rand(seed);
167
168       // place an object each unit of area
169       while ( num > 1.0 ) {
170         float a = mt_rand(seed);
171         float b = mt_rand(seed);
172         if ( a + b > 1 ) {
173           a = 1 - a;
174           b = 1 - b;
175         }
176         float c = 1 - a - b;
177         SGVec3f randomPoint = a*v0 + b*v1 + c*v2;
178         points.push_back(randomPoint);
179         num -= 1.0;
180       }
181     }
182   }
183
184   osg::Geometry* buildGeometry(const TriangleVector& triangles) const
185   {
186     // Do not build anything if there is nothing in here ...
187     if (empty() || triangles.empty())
188       return 0;
189
190     // FIXME: do not include all values here ...
191     osg::Vec3Array* vertices = new osg::Vec3Array;
192     osg::Vec3Array* normals = new osg::Vec3Array;
193     osg::Vec2Array* texCoords = new osg::Vec2Array;
194
195     osg::Vec4Array* colors = new osg::Vec4Array;
196     colors->push_back(osg::Vec4(1, 1, 1, 1));
197
198     osg::Geometry* geometry = new osg::Geometry;
199     geometry->setVertexArray(vertices);
200     geometry->setNormalArray(normals);
201     geometry->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
202     geometry->setColorArray(colors);
203     geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
204     geometry->setTexCoordArray(0, texCoords);
205
206     const unsigned invalid = ~unsigned(0);
207     std::vector<unsigned> indexMap(getNumVertices(), invalid);
208
209     DrawElementsFacade deFacade(vertices->size());
210     for (index_type i = 0; i < triangles.size(); ++i) {
211       triangle_ref triangle = triangles[i];
212       if (indexMap[triangle[0]] == invalid) {
213         indexMap[triangle[0]] = vertices->size();
214         vertices->push_back(getVertex(triangle[0]).vertex.osg());
215         normals->push_back(getVertex(triangle[0]).normal.osg());
216         texCoords->push_back(getVertex(triangle[0]).texCoord.osg());
217       }
218       deFacade.push_back(indexMap[triangle[0]]);
219
220       if (indexMap[triangle[1]] == invalid) {
221         indexMap[triangle[1]] = vertices->size();
222         vertices->push_back(getVertex(triangle[1]).vertex.osg());
223         normals->push_back(getVertex(triangle[1]).normal.osg());
224         texCoords->push_back(getVertex(triangle[1]).texCoord.osg());
225       }
226       deFacade.push_back(indexMap[triangle[1]]);
227
228       if (indexMap[triangle[2]] == invalid) {
229         indexMap[triangle[2]] = vertices->size();
230         vertices->push_back(getVertex(triangle[2]).vertex.osg());
231         normals->push_back(getVertex(triangle[2]).normal.osg());
232         texCoords->push_back(getVertex(triangle[2]).texCoord.osg());
233       }
234       deFacade.push_back(indexMap[triangle[2]]);
235     }
236     geometry->addPrimitiveSet(deFacade.getDrawElements());
237
238     return geometry;
239   }
240
241   osg::Geometry* buildGeometry() const
242   { return buildGeometry(getTriangles()); }
243
244 private:
245   // Random seed for the triangle.
246   mt* seed;
247 };
248
249 #endif