1 // Copyright (C) 2008 - 2009 Mathias Froehlich - Mathias.Froehlich@web.de
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Library General Public
5 // License as published by the Free Software Foundation; either
6 // version 2 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Library General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 #ifndef BVHDebugCollectVisitor_hxx
19 #define BVHDebugCollectVisitor_hxx
21 #include <osg/ref_ptr>
23 #include <osg/Geometry>
25 #include <osg/PolygonOffset>
26 #include <osg/PrimitiveSet>
27 #include <osg/MatrixTransform>
28 #include <osg/ShapeDrawable>
31 #include <osg/BlendFunc>
32 #include <osg/StateSet>
34 #include <simgear/math/SGGeometry.hxx>
36 #include "BVHVisitor.hxx"
37 #include "BVHNode.hxx"
38 #include "BVHGroup.hxx"
39 #include "BVHTransform.hxx"
40 #include "BVHMotionTransform.hxx"
41 #include "BVHStaticGeometry.hxx"
43 #include "BVHStaticData.hxx"
45 #include "BVHStaticNode.hxx"
46 #include "BVHStaticTriangle.hxx"
47 #include "BVHStaticBinary.hxx"
49 #include "BVHBoundingBoxVisitor.hxx"
56 class BVHDebugCollectVisitor : public BVHVisitor {
58 BVHDebugCollectVisitor(const double& time, unsigned level = ~0u) :
59 _group(new osg::Group),
64 osg::StateSet* stateSet = _group->getOrCreateStateSet();
65 stateSet->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
66 stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
67 stateSet->setAttribute(new osg::Depth(osg::Depth::LESS, 0, 1, false));
68 osg::BlendFunc *blendFunc;
69 blendFunc = new osg::BlendFunc(osg::BlendFunc::SRC_ALPHA,
70 osg::BlendFunc::DST_ALPHA);
71 stateSet->setAttributeAndModes(blendFunc);
72 osg::PolygonOffset* polygonOffset = new osg::PolygonOffset(-1, -1);
73 stateSet->setAttributeAndModes(polygonOffset);
75 virtual ~BVHDebugCollectVisitor()
78 virtual void apply(BVHGroup& node)
85 virtual void apply(BVHTransform& node)
88 osg::ref_ptr<osg::Group> oldGroup = _group;
89 osg::ref_ptr<osg::MatrixTransform> transform = new osg::MatrixTransform;
90 transform->setMatrix(osg::Matrix(node.getToWorldTransform().data()));
96 if (transform->getNumChildren())
97 _group->addChild(transform.get());
99 virtual void apply(BVHMotionTransform& node)
102 osg::ref_ptr<osg::Group> oldGroup = _group;
103 osg::ref_ptr<osg::MatrixTransform> transform = new osg::MatrixTransform;
104 transform->setMatrix(osg::Matrix(node.getToWorldTransform(_time).data()));
107 node.traverse(*this);
110 if (transform->getNumChildren())
111 _group->addChild(transform.get());
113 virtual void apply(BVHLineGeometry&)
116 virtual void apply(BVHStaticGeometry& node)
120 node.traverse(*this);
124 virtual void apply(const BVHStaticBinary& node, const BVHStaticData& data)
126 addNodeBox(node, data);
128 node.traverse(*this, data);
131 virtual void apply(const BVHStaticTriangle& node, const BVHStaticData& data)
133 addNodeBox(node, data);
134 addTriangle(node.getTriangle(data), osg::Vec4(0.5, 0, 0.5, 0.2));
137 osg::Node* getNode() const { return _group.get(); }
139 static unsigned allLevels() { return ~0u; }
140 static unsigned triangles() { return ~0u - 1; }
143 void addTriangle(const SGTrianglef& triangle, const osg::Vec4& color)
145 if (_level != triangles())
148 osg::Geometry* geometry = new osg::Geometry;
150 osg::Vec3Array* vertices = new osg::Vec3Array;
151 vertices->push_back(triangle.getVertex(0).osg());
152 vertices->push_back(triangle.getVertex(1).osg());
153 vertices->push_back(triangle.getVertex(2).osg());
155 osg::Vec4Array* colors = new osg::Vec4Array;
156 colors->push_back(color);
158 geometry->setVertexArray(vertices);
159 geometry->setColorArray(colors);
160 geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
162 geometry->addPrimitiveSet(new osg::DrawArrays(GL_TRIANGLES, 0, 3));
164 osg::Geode* geode = new osg::Geode;
165 geode->addDrawable(geometry);
166 _group->addChild(geode);
169 void addNodeSphere(const BVHNode& node)
171 if (_level != ~0u && _level != _currentLevel)
173 SGSphered sphere = node.getBoundingSphere();
174 osg::Sphere* shape = new osg::Sphere;
175 shape->setCenter(SGVec3f(sphere.getCenter()).osg());
176 shape->setRadius(sphere.getRadius());
177 addShape(shape, osg::Vec4(0.5f, 0.5f, 0.5f, 0.1f));
180 void addNodeBox(const BVHStaticNode& node, const BVHStaticData& data)
182 if (_level != ~0u && _level != _currentLevel)
184 BVHBoundingBoxVisitor bbv;
185 node.accept(bbv, data);
186 osg::Box* shape = new osg::Box;
187 shape->setCenter(bbv.getBox().getCenter().osg());
188 shape->setHalfLengths((0.5*bbv.getBox().getSize()).osg());
189 addShape(shape, osg::Vec4(0.5f, 0, 0, 0.1f));
192 void addShape(osg::Shape* shape, const osg::Vec4& color)
194 osg::ShapeDrawable* shapeDrawable = new osg::ShapeDrawable;
195 shapeDrawable->setColor(color);
196 shapeDrawable->setShape(shape);
197 osg::Geode* geode = new osg::Geode;
198 geode->addDrawable(shapeDrawable);
199 _group->addChild(geode);
202 osg::ref_ptr<osg::Group> _group;
204 const unsigned _level;
205 unsigned _currentLevel;