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/MatrixTransform>
27 #include <osg/PagedLOD>
28 #include <osg/Transform>
29 #include <osg/TriangleFunctor>
31 #include <simgear/scene/material/mat.hxx>
32 #include <simgear/scene/material/matlib.hxx>
33 #include <simgear/scene/util/SGNodeMasks.hxx>
34 #include <simgear/scene/util/SGSceneUserData.hxx>
35 #include <simgear/math/SGGeometry.hxx>
37 #include <simgear/scene/bvh/BVHStaticGeometryBuilder.hxx>
41 class BoundingVolumeBuildVisitor : public osg::NodeVisitor {
43 class PFunctor : public osg::PrimitiveFunctor {
48 _geometryBuilder = new BVHStaticGeometryBuilder;
53 virtual void setVertexArray(unsigned int count, const osg::Vec2* vertices)
55 _vertices.resize(count);
56 for (unsigned i = 0; i < count; ++i)
57 _vertices[i] = SGVec3f(vertices[i][0], vertices[i][1], 0);
60 virtual void setVertexArray(unsigned int count, const osg::Vec3* vertices)
62 _vertices.resize(count);
63 for (unsigned i = 0; i < count; ++i)
64 _vertices[i] = SGVec3f(vertices[i][0], vertices[i][1], vertices[i][2]);
67 virtual void setVertexArray(unsigned int count, const osg::Vec4* vertices)
69 _vertices.resize(count);
70 for (unsigned i = 0; i < count; ++i)
71 _vertices[i] = SGVec3f(vertices[i][0]/vertices[i][3],
72 vertices[i][1]/vertices[i][3],
73 vertices[i][2]/vertices[i][3]);
76 virtual void setVertexArray(unsigned int count, const osg::Vec2d* vertices)
78 _vertices.resize(count);
79 for (unsigned i = 0; i < count; ++i)
80 _vertices[i] = SGVec3f(vertices[i][0], vertices[i][1], 0);
83 virtual void setVertexArray(unsigned int count, const osg::Vec3d* vertices)
85 _vertices.resize(count);
86 for (unsigned i = 0; i < count; ++i)
87 _vertices[i] = SGVec3f(vertices[i][0], vertices[i][1], vertices[i][2]);
90 virtual void setVertexArray(unsigned int count, const osg::Vec4d* vertices)
92 _vertices.resize(count);
93 for (unsigned i = 0; i < count; ++i)
94 _vertices[i] = SGVec3f(vertices[i][0]/vertices[i][3],
95 vertices[i][1]/vertices[i][3],
96 vertices[i][2]/vertices[i][3]);
99 virtual void drawArrays(GLenum mode, GLint first, GLsizei count)
101 if (_vertices.empty() || count <= 0)
104 GLsizei end = first + count;
107 for (GLsizei i = first; i < end - 2; i += 3) {
108 addTriangle(i, i + 1, i + 2);
112 case (GL_TRIANGLE_STRIP):
113 for (GLsizei i = first; i < end - 2; ++i) {
114 addTriangle(i, i + 1, i + 2);
119 for (GLsizei i = first; i < end - 3; i += 4) {
120 addQuad(i, i + 1, i + 2, i + 3);
124 case (GL_QUAD_STRIP):
125 for (GLsizei i = first; i < end - 3; i += 2) {
126 addQuad(i, i + 1, i + 2, i + 3);
130 case (GL_POLYGON): // treat polygons as GL_TRIANGLE_FAN
131 case (GL_TRIANGLE_FAN):
132 for (GLsizei i = first; i < end - 2; ++i) {
133 addTriangle(first, i + 1, i + 2);
138 for (GLsizei i = first; i < end; ++i) {
144 for (GLsizei i = first; i < end - 1; i += 2) {
149 case (GL_LINE_STRIP):
150 for (GLsizei i = first; i < end - 1; ++i) {
156 for (GLsizei i = first; i < end - 1; ++i) {
159 addLine(end - 1, first);
167 virtual void drawElements(GLenum mode, GLsizei count, const GLubyte* indices)
169 drawElementsTemplate(mode, count, indices);
172 virtual void drawElements(GLenum mode, GLsizei count, const GLushort* indices)
174 drawElementsTemplate(mode, count, indices);
177 virtual void drawElements(GLenum mode, GLsizei count, const GLuint* indices)
179 drawElementsTemplate(mode, count, indices);
182 virtual void begin(GLenum mode)
188 virtual void vertex(const osg::Vec2& v)
190 _vertices.push_back(SGVec3f(v[0], v[1], 0));
192 virtual void vertex(const osg::Vec3& v)
194 _vertices.push_back(SGVec3f(v[0], v[1], v[2]));
196 virtual void vertex(const osg::Vec4& v)
198 _vertices.push_back(SGVec3f(v[0]/v[3], v[1]/v[3], v[2]/v[3]));
200 virtual void vertex(float x, float y)
202 _vertices.push_back(SGVec3f(x, y, 0));
204 virtual void vertex(float x, float y, float z)
206 _vertices.push_back(SGVec3f(x, y, z));
208 virtual void vertex(float x, float y, float z, float w)
210 _vertices.push_back(SGVec3f(x/w, y/w, z/w));
214 if (_vertices.empty())
217 drawArrays(_modeCache, 0, _vertices.size());
220 template<typename index_type>
221 void drawElementsTemplate(GLenum mode, GLsizei count,
222 const index_type* indices)
224 if (_vertices.empty() || indices == 0 || count <= 0)
229 for (GLsizei i = 0; i < count - 2; i += 3) {
230 addTriangle(indices[i], indices[i + 1], indices[i + 2]);
234 case (GL_TRIANGLE_STRIP):
235 for (GLsizei i = 0; i < count - 2; ++i) {
236 addTriangle(indices[i], indices[i + 1], indices[i + 2]);
241 for (GLsizei i = 0; i < count - 3; i += 4) {
242 addQuad(indices[i], indices[i + 1], indices[i + 2], indices[i + 3]);
246 case (GL_QUAD_STRIP):
247 for (GLsizei i = 0; i < count - 3; i += 2) {
248 addQuad(indices[i], indices[i + 1], indices[i + 2], indices[i + 3]);
253 case (GL_TRIANGLE_FAN):
254 for (GLsizei i = 0; i < count - 2; ++i) {
255 addTriangle(indices[0], indices[i + 1], indices[i + 2]);
260 for(GLsizei i = 0; i < count; ++i) {
261 addPoint(indices[i]);
266 for (GLsizei i = 0; i < count - 1; i += 2) {
267 addLine(indices[i], indices[i + 1]);
271 case (GL_LINE_STRIP):
272 for (GLsizei i = 0; i < count - 1; ++i) {
273 addLine(indices[i], indices[i + 1]);
278 for (GLsizei i = 0; i < count - 1; ++i) {
279 addLine(indices[i], indices[i + 1]);
281 addLine(indices[count - 1], indices[0]);
289 void addPoint(unsigned i1)
291 addPoint(_vertices[i1]);
293 void addLine(unsigned i1, unsigned i2)
295 addLine(_vertices[i1], _vertices[i2]);
297 void addTriangle(unsigned i1, unsigned i2, unsigned i3)
299 addTriangle(_vertices[i1], _vertices[i2], _vertices[i3]);
301 void addQuad(unsigned i1, unsigned i2, unsigned i3, unsigned i4)
303 addQuad(_vertices[i1], _vertices[i2], _vertices[i3], _vertices[i4]);
306 void addPoint(const SGVec3f& v1)
309 void addLine(const SGVec3f& v1, const SGVec3f& v2)
312 void addTriangle(const SGVec3f& v1, const SGVec3f& v2, const SGVec3f& v3)
314 _geometryBuilder->addTriangle(v1, v2, v3);
316 void addQuad(const SGVec3f& v1, const SGVec3f& v2,
317 const SGVec3f& v3, const SGVec3f& v4)
319 _geometryBuilder->addTriangle(v1, v2, v3);
320 _geometryBuilder->addTriangle(v1, v3, v4);
323 BVHNode* buildTreeAndClear()
325 BVHNode* bvNode = _geometryBuilder->buildTree();
326 _geometryBuilder = new BVHStaticGeometryBuilder;
331 void swap(PFunctor& primitiveFunctor)
333 _vertices.swap(primitiveFunctor._vertices);
334 std::swap(_modeCache, primitiveFunctor._modeCache);
335 std::swap(_geometryBuilder, primitiveFunctor._geometryBuilder);
338 void setCurrentMaterial(const SGMaterial* material)
340 _geometryBuilder->setCurrentMaterial(material);
342 const SGMaterial* getCurrentMaterial() const
344 return _geometryBuilder->getCurrentMaterial();
347 std::vector<SGVec3f> _vertices;
350 SGSharedPtr<BVHStaticGeometryBuilder> _geometryBuilder;
354 // class PrimitiveIndexFunctor
358 // virtual ~PrimitiveIndexFunctor() {}
360 // virtual void setVertexArray(unsigned int count,const Vec2* vertices) = 0;
361 // virtual void setVertexArray(unsigned int count,const Vec3* vertices) = 0;
362 // virtual void setVertexArray(unsigned int count,const Vec4* vertices) = 0;
364 // virtual void setVertexArray(unsigned int count,const Vec2d* vertices) = 0;
365 // virtual void setVertexArray(unsigned int count,const Vec3d* vertices) = 0;
366 // virtual void setVertexArray(unsigned int count,const Vec4d* vertices) = 0;
368 // virtual void drawArrays(GLenum mode,GLint first,GLsizei count) = 0;
369 // virtual void drawElements(GLenum mode,GLsizei count,const GLubyte* indices) = 0;
370 // virtual void drawElements(GLenum mode,GLsizei count,const GLushort* indices) = 0;
371 // virtual void drawElements(GLenum mode,GLsizei count,const GLuint* indices) = 0;
373 // virtual void begin(GLenum mode) = 0;
374 // virtual void vertex(unsigned int pos) = 0;
375 // virtual void end() = 0;
378 BoundingVolumeBuildVisitor() :
379 osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ACTIVE_CHILDREN)
381 setTraversalMask(SG_NODEMASK_TERRAIN_BIT);
383 ~BoundingVolumeBuildVisitor()
387 const SGMaterial* pushMaterial(osg::StateSet* stateSet)
389 const SGMaterial* oldMaterial = _primitiveFunctor.getCurrentMaterial();
390 const SGMaterial* material = SGMaterialLib::findMaterial(stateSet);
392 _primitiveFunctor.setCurrentMaterial(material);
396 void fillWith(osg::Drawable* drawable)
398 const SGMaterial* oldMaterial = pushMaterial(drawable->getStateSet());
399 drawable->accept(_primitiveFunctor);
400 _primitiveFunctor.setCurrentMaterial(oldMaterial);
403 virtual void apply(osg::Geode& geode)
405 const SGMaterial* oldMaterial = pushMaterial(geode.getStateSet());
407 if (!hasBoundingVolumeTree(geode))
408 for(unsigned i = 0; i < geode.getNumDrawables(); ++i)
409 fillWith(geode.getDrawable(i));
411 // Flush the bounding volume tree if we reached the topmost group
412 if (getNodePath().size() <= 1)
413 addBoundingVolumeTreeToNode(geode);
414 _primitiveFunctor.setCurrentMaterial(oldMaterial);
417 virtual void apply(osg::Group& group)
419 // Note that we do not need to push the already collected list of
420 // primitives, since we are now in the topmost node ...
422 const SGMaterial* oldMaterial = pushMaterial(group.getStateSet());
424 if (!hasBoundingVolumeTree(group))
427 // Flush the bounding volume tree if we reached the topmost group
428 if (getNodePath().size() <= 1)
429 addBoundingVolumeTreeToNode(group);
431 _primitiveFunctor.setCurrentMaterial(oldMaterial);
434 virtual void apply(osg::Transform& transform)
436 // push the current active primitive list
437 PFunctor previousPrimitives;
438 _primitiveFunctor.swap(previousPrimitives);
440 const SGMaterial* oldMaterial = pushMaterial(transform.getStateSet());
443 if (!hasBoundingVolumeTree(transform))
446 // We know whenever we see a transform, we need to flush the
447 // collected bounding volume tree since these transforms are not
448 // handled by the plain leafs.
449 addBoundingVolumeTreeToNode(transform);
451 _primitiveFunctor.setCurrentMaterial(oldMaterial);
453 // pop the current active primitive list
454 _primitiveFunctor.swap(previousPrimitives);
457 virtual void apply(osg::PagedLOD&)
459 // Do nothing. In this case we get called by the loading process anyway
462 virtual void apply(osg::Camera& camera)
464 if (camera.getRenderOrder() != osg::Camera::NESTED_RENDER)
466 apply(static_cast<osg::Transform&>(camera));
469 void addBoundingVolumeTreeToNode(osg::Node& node)
471 // Build the flat tree.
472 BVHNode* bvNode = _primitiveFunctor.buildTreeAndClear();
478 SGSceneUserData* userData;
479 userData = SGSceneUserData::getOrCreateSceneUserData(&node);
480 userData->setBVHNode(bvNode);
483 bool hasBoundingVolumeTree(osg::Node& node)
485 SGSceneUserData* userData;
486 userData = SGSceneUserData::getSceneUserData(&node);
489 if (!userData->getBVHNode())
495 PFunctor _primitiveFunctor;