3 * Copyright (C) 2006-2007 Mathias Froehlich
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,
22 #ifndef SG_TEXTURED_TRIANGLE_BIN_HXX
23 #define SG_TEXTURED_TRIANGLE_BIN_HXX
26 #include <osg/Geometry>
27 #include <osg/PrimitiveSet>
29 #include <simgear/math/sg_random.h>
30 #include <simgear/math/SGMath.hxx>
31 #include "SGTriangleBin.hxx"
33 struct SGVertNormTex {
36 SGVertNormTex(const SGVec3f& v, const SGVec3f& n, const SGVec2f& t) :
37 vertex(v), normal(n), texCoord(t)
41 inline bool operator() (const SGVertNormTex& l,
42 const SGVertNormTex& r) const
44 if (l.vertex < r.vertex) return true;
45 else if (r.vertex < l.vertex) return false;
46 else if (l.normal < r.normal) return true;
47 else if (r.normal < l.normal) return false;
48 else return l.texCoord < r.texCoord;
57 class SGTexturedTriangleBin : public SGTriangleBin<SGVertNormTex> {
60 // Computes and adds random surface points to the points list.
61 // The random points are computed with a density of (coverage points)/1
62 // The points are offsetted away from the triangles in
63 // offset * positive normal direction.
64 void addRandomSurfacePoints(float coverage, float offset,
65 std::vector<SGVec3f>& points) const
67 unsigned num = getNumTriangles();
68 for (unsigned i = 0; i < num; ++i) {
69 triangle_ref triangleRef = getTriangleRef(i);
70 SGVec3f v0 = getVertex(triangleRef[0]).vertex;
71 SGVec3f v1 = getVertex(triangleRef[1]).vertex;
72 SGVec3f v2 = getVertex(triangleRef[2]).vertex;
73 SGVec3f normal = cross(v1 - v0, v2 - v0);
76 float area = 0.5f*length(normal);
77 if (area <= SGLimitsf::min())
80 // For partial units of area, use a zombie door method to
81 // create the proper random chance of a light being created
83 float unit = area + sg_random()*coverage;
85 SGVec3f offsetVector = offset*normalize(normal);
86 // generate a light point for each unit of area
87 while ( coverage < unit ) {
88 float a = sg_random();
89 float b = sg_random();
95 SGVec3f randomPoint = offsetVector + a*v0 + b*v1 + c*v2;
96 points.push_back(randomPoint);
102 osg::Geometry* buildGeometry(const TriangleVector& triangles) const
104 // Do not build anything if there is nothing in here ...
105 if (empty() || triangles.empty())
108 // FIXME: do not include all values here ...
109 osg::Vec3Array* vertices = new osg::Vec3Array;
110 osg::Vec3Array* normals = new osg::Vec3Array;
111 osg::Vec2Array* texCoords = new osg::Vec2Array;
113 osg::Vec4Array* colors = new osg::Vec4Array;
114 colors->push_back(osg::Vec4(1, 1, 1, 1));
116 osg::Geometry* geometry = new osg::Geometry;
117 geometry->setVertexArray(vertices);
118 geometry->setNormalArray(normals);
119 geometry->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
120 geometry->setColorArray(colors);
121 geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
122 geometry->setTexCoordArray(0, texCoords);
124 const unsigned invalid = ~unsigned(0);
125 std::vector<unsigned> indexMap(getNumVertices(), invalid);
127 osg::DrawElementsUInt* drawElements;
128 drawElements = new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES);
129 for (index_type i = 0; i < triangles.size(); ++i) {
130 triangle_ref triangle = triangles[i];
131 if (indexMap[triangle[0]] == invalid) {
132 indexMap[triangle[0]] = vertices->size();
133 vertices->push_back(getVertex(triangle[0]).vertex.osg());
134 normals->push_back(getVertex(triangle[0]).normal.osg());
135 texCoords->push_back(getVertex(triangle[0]).texCoord.osg());
137 drawElements->push_back(indexMap[triangle[0]]);
139 if (indexMap[triangle[1]] == invalid) {
140 indexMap[triangle[1]] = vertices->size();
141 vertices->push_back(getVertex(triangle[1]).vertex.osg());
142 normals->push_back(getVertex(triangle[1]).normal.osg());
143 texCoords->push_back(getVertex(triangle[1]).texCoord.osg());
145 drawElements->push_back(indexMap[triangle[1]]);
147 if (indexMap[triangle[2]] == invalid) {
148 indexMap[triangle[2]] = vertices->size();
149 vertices->push_back(getVertex(triangle[2]).vertex.osg());
150 normals->push_back(getVertex(triangle[2]).normal.osg());
151 texCoords->push_back(getVertex(triangle[2]).texCoord.osg());
153 drawElements->push_back(indexMap[triangle[2]]);
155 geometry->addPrimitiveSet(drawElements);
160 osg::Geometry* buildGeometry() const
161 { return buildGeometry(getTriangles()); }