2 // Copyright (C) 2008 - 2009 Mathias Froehlich - Mathias.Froehlich@web.de
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Library General Public
6 // License as published by the Free Software Foundation; either
7 // version 2 of the License, or (at your option) any later version.
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Library General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #ifndef SimGear_BoundingVolumeBuildVisitor_hxx
20 #define SimGear_BoundingVolumeBuildVisitor_hxx
23 #include <osg/Drawable>
26 #include <osg/PagedLOD>
27 #include <osg/Transform>
28 #include <osg/TriangleFunctor>
30 #include <simgear/scene/material/mat.hxx>
31 #include <simgear/scene/material/matlib.hxx>
32 #include <simgear/scene/util/SGNodeMasks.hxx>
33 #include <simgear/scene/util/SGSceneUserData.hxx>
34 #include <simgear/math/SGGeometry.hxx>
36 #include <simgear/scene/bvh/BVHStaticGeometryBuilder.hxx>
40 class BoundingVolumeBuildVisitor : public osg::NodeVisitor {
42 class PFunctor : public osg::PrimitiveFunctor {
47 _geometryBuilder = new BVHStaticGeometryBuilder;
52 virtual void setVertexArray(unsigned int count, const osg::Vec2* vertices)
54 _vertices.resize(count);
55 for (unsigned i = 0; i < count; ++i)
56 _vertices[i] = SGVec3f(vertices[i][0], vertices[i][1], 0);
59 virtual void setVertexArray(unsigned int count, const osg::Vec3* vertices)
61 _vertices.resize(count);
62 for (unsigned i = 0; i < count; ++i)
63 _vertices[i] = SGVec3f(vertices[i][0], vertices[i][1], vertices[i][2]);
66 virtual void setVertexArray(unsigned int count, const osg::Vec4* vertices)
68 _vertices.resize(count);
69 for (unsigned i = 0; i < count; ++i)
70 _vertices[i] = SGVec3f(vertices[i][0]/vertices[i][3],
71 vertices[i][1]/vertices[i][3],
72 vertices[i][2]/vertices[i][3]);
75 virtual void setVertexArray(unsigned int count, const osg::Vec2d* vertices)
77 _vertices.resize(count);
78 for (unsigned i = 0; i < count; ++i)
79 _vertices[i] = SGVec3f(vertices[i][0], vertices[i][1], 0);
82 virtual void setVertexArray(unsigned int count, const osg::Vec3d* vertices)
84 _vertices.resize(count);
85 for (unsigned i = 0; i < count; ++i)
86 _vertices[i] = SGVec3f(vertices[i][0], vertices[i][1], vertices[i][2]);
89 virtual void setVertexArray(unsigned int count, const osg::Vec4d* vertices)
91 _vertices.resize(count);
92 for (unsigned i = 0; i < count; ++i)
93 _vertices[i] = SGVec3f(vertices[i][0]/vertices[i][3],
94 vertices[i][1]/vertices[i][3],
95 vertices[i][2]/vertices[i][3]);
98 virtual void drawArrays(GLenum mode, GLint first, GLsizei count)
100 if (_vertices.empty() || count <= 0)
103 GLsizei end = first + count;
106 for (GLsizei i = first; i < end - 2; i += 3) {
107 addTriangle(i, i + 1, i + 2);
111 case (GL_TRIANGLE_STRIP):
112 for (GLsizei i = first; i < end - 2; ++i) {
113 addTriangle(i, i + 1, i + 2);
118 for (GLsizei i = first; i < end - 3; i += 4) {
119 addQuad(i, i + 1, i + 2, i + 3);
123 case (GL_QUAD_STRIP):
124 for (GLsizei i = first; i < end - 3; i += 2) {
125 addQuad(i, i + 1, i + 2, i + 3);
129 case (GL_POLYGON): // treat polygons as GL_TRIANGLE_FAN
130 case (GL_TRIANGLE_FAN):
131 for (GLsizei i = first; i < end - 2; ++i) {
132 addTriangle(first, i + 1, i + 2);
137 for (GLsizei i = first; i < end; ++i) {
143 for (GLsizei i = first; i < end - 1; i += 2) {
148 case (GL_LINE_STRIP):
149 for (GLsizei i = first; i < end - 1; ++i) {
155 for (GLsizei i = first; i < end - 1; ++i) {
158 addLine(end - 1, first);
166 virtual void drawElements(GLenum mode, GLsizei count, const GLubyte* indices)
168 drawElementsTemplate(mode, count, indices);
171 virtual void drawElements(GLenum mode, GLsizei count, const GLushort* indices)
173 drawElementsTemplate(mode, count, indices);
176 virtual void drawElements(GLenum mode, GLsizei count, const GLuint* indices)
178 drawElementsTemplate(mode, count, indices);
181 virtual void begin(GLenum mode)
187 virtual void vertex(const osg::Vec2& v)
189 _vertices.push_back(SGVec3f(v[0], v[1], 0));
191 virtual void vertex(const osg::Vec3& v)
193 _vertices.push_back(SGVec3f(v[0], v[1], v[2]));
195 virtual void vertex(const osg::Vec4& v)
197 _vertices.push_back(SGVec3f(v[0]/v[3], v[1]/v[3], v[2]/v[3]));
199 virtual void vertex(float x, float y)
201 _vertices.push_back(SGVec3f(x, y, 0));
203 virtual void vertex(float x, float y, float z)
205 _vertices.push_back(SGVec3f(x, y, z));
207 virtual void vertex(float x, float y, float z, float w)
209 _vertices.push_back(SGVec3f(x/w, y/w, z/w));
213 if (_vertices.empty())
216 drawArrays(_modeCache, 0, _vertices.size());
219 template<typename index_type>
220 void drawElementsTemplate(GLenum mode, GLsizei count,
221 const index_type* indices)
223 if (_vertices.empty() || indices == 0 || count <= 0)
228 for (GLsizei i = 0; i < count - 2; i += 3) {
229 addTriangle(indices[i], indices[i + 1], indices[i + 2]);
233 case (GL_TRIANGLE_STRIP):
234 for (GLsizei i = 0; i < count - 2; ++i) {
235 addTriangle(indices[i], indices[i + 1], indices[i + 2]);
240 for (GLsizei i = 0; i < count - 3; i += 4) {
241 addQuad(indices[i], indices[i + 1], indices[i + 2], indices[i + 3]);
245 case (GL_QUAD_STRIP):
246 for (GLsizei i = 0; i < count - 3; i += 2) {
247 addQuad(indices[i], indices[i + 1], indices[i + 2], indices[i + 3]);
252 case (GL_TRIANGLE_FAN):
253 for (GLsizei i = 0; i < count - 2; ++i) {
254 addTriangle(indices[0], indices[i + 1], indices[i + 2]);
259 for(GLsizei i = 0; i < count; ++i) {
260 addPoint(indices[i]);
265 for (GLsizei i = 0; i < count - 1; i += 2) {
266 addLine(indices[i], indices[i + 1]);
270 case (GL_LINE_STRIP):
271 for (GLsizei i = 0; i < count - 1; ++i) {
272 addLine(indices[i], indices[i + 1]);
277 for (GLsizei i = 0; i < count - 1; ++i) {
278 addLine(indices[i], indices[i + 1]);
280 addLine(indices[count - 1], indices[0]);
288 void addPoint(unsigned i1)
290 addPoint(_vertices[i1]);
292 void addLine(unsigned i1, unsigned i2)
294 addLine(_vertices[i1], _vertices[i2]);
296 void addTriangle(unsigned i1, unsigned i2, unsigned i3)
298 addTriangle(_vertices[i1], _vertices[i2], _vertices[i3]);
300 void addQuad(unsigned i1, unsigned i2, unsigned i3, unsigned i4)
302 addQuad(_vertices[i1], _vertices[i2], _vertices[i3], _vertices[i4]);
305 void addPoint(const SGVec3f& v1)
308 void addLine(const SGVec3f& v1, const SGVec3f& v2)
311 void addTriangle(const SGVec3f& v1, const SGVec3f& v2, const SGVec3f& v3)
313 _geometryBuilder->addTriangle(v1, v2, v3);
315 void addQuad(const SGVec3f& v1, const SGVec3f& v2,
316 const SGVec3f& v3, const SGVec3f& v4)
318 _geometryBuilder->addTriangle(v1, v2, v3);
319 _geometryBuilder->addTriangle(v1, v3, v4);
322 BVHNode* buildTreeAndClear()
324 BVHNode* bvNode = _geometryBuilder->buildTree();
325 _geometryBuilder = new BVHStaticGeometryBuilder;
330 void swap(PFunctor& primitiveFunctor)
332 _vertices.swap(primitiveFunctor._vertices);
333 std::swap(_modeCache, primitiveFunctor._modeCache);
334 std::swap(_geometryBuilder, primitiveFunctor._geometryBuilder);
337 void setCurrentMaterial(const SGMaterial* material)
339 _geometryBuilder->setCurrentMaterial(material);
341 const SGMaterial* getCurrentMaterial() const
343 return _geometryBuilder->getCurrentMaterial();
346 std::vector<SGVec3f> _vertices;
349 SGSharedPtr<BVHStaticGeometryBuilder> _geometryBuilder;
353 // class PrimitiveIndexFunctor
357 // virtual ~PrimitiveIndexFunctor() {}
359 // virtual void setVertexArray(unsigned int count,const Vec2* vertices) = 0;
360 // virtual void setVertexArray(unsigned int count,const Vec3* vertices) = 0;
361 // virtual void setVertexArray(unsigned int count,const Vec4* vertices) = 0;
363 // virtual void setVertexArray(unsigned int count,const Vec2d* vertices) = 0;
364 // virtual void setVertexArray(unsigned int count,const Vec3d* vertices) = 0;
365 // virtual void setVertexArray(unsigned int count,const Vec4d* vertices) = 0;
367 // virtual void drawArrays(GLenum mode,GLint first,GLsizei count) = 0;
368 // virtual void drawElements(GLenum mode,GLsizei count,const GLubyte* indices) = 0;
369 // virtual void drawElements(GLenum mode,GLsizei count,const GLushort* indices) = 0;
370 // virtual void drawElements(GLenum mode,GLsizei count,const GLuint* indices) = 0;
372 // virtual void begin(GLenum mode) = 0;
373 // virtual void vertex(unsigned int pos) = 0;
374 // virtual void end() = 0;
377 BoundingVolumeBuildVisitor(bool dumpIntoLeafs) :
378 osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ACTIVE_CHILDREN),
379 _dumpIntoLeafs(dumpIntoLeafs)
381 setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
383 ~BoundingVolumeBuildVisitor()
387 const SGMaterial* pushMaterial(osg::Geode* geode)
389 const SGMaterial* oldMaterial = _primitiveFunctor.getCurrentMaterial();
390 const SGMaterial* material = SGMaterialLib::findMaterial(geode);
392 _primitiveFunctor.setCurrentMaterial(material);
396 void fillWith(osg::Drawable* drawable)
398 drawable->accept(_primitiveFunctor);
401 virtual void apply(osg::Geode& geode)
403 if (hasBoundingVolumeTree(geode))
406 const SGMaterial* oldMaterial = pushMaterial(&geode);
408 bool flushHere = getNodePath().size() <= 1 || _dumpIntoLeafs;
410 // push the current active primitive list
411 PFunctor previousPrimitives;
412 _primitiveFunctor.swap(previousPrimitives);
414 const SGMaterial* mat = previousPrimitives.getCurrentMaterial();
415 _primitiveFunctor.setCurrentMaterial(mat);
418 for(unsigned i = 0; i < geode.getNumDrawables(); ++i)
419 fillWith(geode.getDrawable(i));
421 // Flush the bounding volume tree if we reached the topmost group
422 addBoundingVolumeTreeToNode(geode);
424 // pop the current active primitive list
425 _primitiveFunctor.swap(previousPrimitives);
427 for(unsigned i = 0; i < geode.getNumDrawables(); ++i)
428 fillWith(geode.getDrawable(i));
431 _primitiveFunctor.setCurrentMaterial(oldMaterial);
434 virtual void apply(osg::Group& group)
435 { traverseAndCollect(group); }
437 virtual void apply(osg::Transform& transform)
438 { traverseAndDump(transform); }
440 virtual void apply(osg::PagedLOD&)
442 // Do nothing. In this case we get called by the loading process anyway
445 virtual void apply(osg::Camera& camera)
447 if (camera.getRenderOrder() != osg::Camera::NESTED_RENDER)
449 traverseAndDump(camera);
452 void traverseAndDump(osg::Node& node)
454 if (hasBoundingVolumeTree(node))
457 // push the current active primitive list
458 PFunctor previousPrimitives;
459 _primitiveFunctor.swap(previousPrimitives);
461 const SGMaterial* mat = previousPrimitives.getCurrentMaterial();
462 _primitiveFunctor.setCurrentMaterial(mat);
467 // We know whenever we see a transform, we need to flush the
468 // collected bounding volume tree since these transforms are not
469 // handled by the plain leafs.
470 addBoundingVolumeTreeToNode(node);
472 // pop the current active primitive list
473 _primitiveFunctor.swap(previousPrimitives);
476 void traverseAndCollect(osg::Node& node)
478 // Already been here??
479 if (hasBoundingVolumeTree(node))
482 // Force a flush of the bvtree if we are in the topmost node.
483 if (getNodePath().size() <= 1) {
484 traverseAndDump(node);
488 // Note that we do not need to push the already collected list of
489 // primitives, since we are now in the topmost node ...
495 void addBoundingVolumeTreeToNode(osg::Node& node)
497 // Build the flat tree.
498 BVHNode* bvNode = _primitiveFunctor.buildTreeAndClear();
504 SGSceneUserData* userData;
505 userData = SGSceneUserData::getOrCreateSceneUserData(&node);
506 userData->setBVHNode(bvNode);
509 bool hasBoundingVolumeTree(osg::Node& node)
511 SGSceneUserData* userData;
512 userData = SGSceneUserData::getSceneUserData(&node);
515 if (!userData->getBVHNode())
521 PFunctor _primitiveFunctor;