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>
35 #include <simgear/scene/util/OsgMath.hxx>
37 #include <simgear/bvh/BVHVisitor.hxx>
38 #include <simgear/bvh/BVHNode.hxx>
39 #include <simgear/bvh/BVHGroup.hxx>
40 #include <simgear/bvh/BVHTransform.hxx>
41 #include <simgear/bvh/BVHMotionTransform.hxx>
42 #include <simgear/bvh/BVHStaticGeometry.hxx>
44 #include <simgear/bvh/BVHStaticData.hxx>
46 #include <simgear/bvh/BVHStaticNode.hxx>
47 #include <simgear/bvh/BVHStaticTriangle.hxx>
48 #include <simgear/bvh/BVHStaticBinary.hxx>
50 #include <simgear/bvh/BVHBoundingBoxVisitor.hxx>
57 class BVHDebugCollectVisitor : public BVHVisitor {
59 BVHDebugCollectVisitor(const double& time, unsigned level = ~0u) :
60 _group(new osg::Group),
65 osg::StateSet* stateSet = _group->getOrCreateStateSet();
66 stateSet->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
67 stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
68 stateSet->setAttribute(new osg::Depth(osg::Depth::LESS, 0, 1, false));
69 osg::BlendFunc *blendFunc;
70 blendFunc = new osg::BlendFunc(osg::BlendFunc::SRC_ALPHA,
71 osg::BlendFunc::DST_ALPHA);
72 stateSet->setAttributeAndModes(blendFunc);
73 osg::PolygonOffset* polygonOffset = new osg::PolygonOffset(-1, -1);
74 stateSet->setAttributeAndModes(polygonOffset);
76 virtual ~BVHDebugCollectVisitor()
79 virtual void apply(BVHGroup& node)
86 virtual void apply(BVHTransform& node)
89 osg::ref_ptr<osg::Group> oldGroup = _group;
90 osg::ref_ptr<osg::MatrixTransform> transform = new osg::MatrixTransform;
91 transform->setMatrix(osg::Matrix(node.getToWorldTransform().data()));
97 if (transform->getNumChildren())
98 _group->addChild(transform.get());
100 virtual void apply(BVHMotionTransform& node)
103 osg::ref_ptr<osg::Group> oldGroup = _group;
104 osg::ref_ptr<osg::MatrixTransform> transform = new osg::MatrixTransform;
105 transform->setMatrix(osg::Matrix(node.getToWorldTransform(_time).data()));
108 node.traverse(*this);
111 if (transform->getNumChildren())
112 _group->addChild(transform.get());
114 virtual void apply(BVHLineGeometry&)
117 virtual void apply(BVHStaticGeometry& node)
121 node.traverse(*this);
125 virtual void apply(const BVHStaticBinary& node, const BVHStaticData& data)
127 addNodeBox(node, data);
129 node.traverse(*this, data);
132 virtual void apply(const BVHStaticTriangle& node, const BVHStaticData& data)
134 addNodeBox(node, data);
135 addTriangle(node.getTriangle(data), osg::Vec4(0.5, 0, 0.5, 0.2));
138 osg::Node* getNode() const { return _group.get(); }
140 static unsigned allLevels() { return ~0u; }
141 static unsigned triangles() { return ~0u - 1; }
144 void addTriangle(const SGTrianglef& triangle, const osg::Vec4& color)
146 if (_level != triangles())
149 osg::Geometry* geometry = new osg::Geometry;
151 osg::Vec3Array* vertices = new osg::Vec3Array;
152 vertices->push_back(toOsg(triangle.getVertex(0)));
153 vertices->push_back(toOsg(triangle.getVertex(1)));
154 vertices->push_back(toOsg(triangle.getVertex(2)));
156 osg::Vec4Array* colors = new osg::Vec4Array;
157 colors->push_back(color);
159 geometry->setVertexArray(vertices);
160 geometry->setColorArray(colors);
161 geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
163 geometry->addPrimitiveSet(new osg::DrawArrays(GL_TRIANGLES, 0, 3));
165 osg::Geode* geode = new osg::Geode;
166 geode->addDrawable(geometry);
167 _group->addChild(geode);
170 void addNodeSphere(const BVHNode& node)
172 if (_level != ~0u && _level != _currentLevel)
174 SGSphered sphere = node.getBoundingSphere();
175 osg::Sphere* shape = new osg::Sphere;
176 shape->setCenter(toOsg(sphere.getCenter()));
177 shape->setRadius(sphere.getRadius());
178 addShape(shape, osg::Vec4(0.5f, 0.5f, 0.5f, 0.1f));
181 void addNodeBox(const BVHStaticNode& node, const BVHStaticData& data)
183 if (_level != ~0u && _level != _currentLevel)
185 BVHBoundingBoxVisitor bbv;
186 node.accept(bbv, data);
187 osg::Box* shape = new osg::Box;
188 shape->setCenter(toOsg(bbv.getBox().getCenter()));
189 shape->setHalfLengths(toOsg((0.5*bbv.getBox().getSize())));
190 addShape(shape, osg::Vec4(0.5f, 0, 0, 0.1f));
193 void addShape(osg::Shape* shape, const osg::Vec4& color)
195 osg::ShapeDrawable* shapeDrawable = new osg::ShapeDrawable;
196 shapeDrawable->setColor(color);
197 shapeDrawable->setShape(shape);
198 osg::Geode* geode = new osg::Geode;
199 geode->addDrawable(shapeDrawable);
200 _group->addChild(geode);
203 osg::ref_ptr<osg::Group> _group;
205 const unsigned _level;
206 unsigned _currentLevel;