]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/BVHDebugCollectVisitor.hxx
Make return type from loadPagedModel explicit.
[simgear.git] / simgear / scene / model / BVHDebugCollectVisitor.hxx
1 // Copyright (C) 2008 - 2009  Mathias Froehlich - Mathias.Froehlich@web.de
2 //
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.
7 //
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.
12 //
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.
16 //
17
18 #ifndef BVHDebugCollectVisitor_hxx
19 #define BVHDebugCollectVisitor_hxx
20
21 #include <osg/ref_ptr>
22 #include <osg/Geode>
23 #include <osg/Geometry>
24 #include <osg/Group>
25 #include <osg/PolygonOffset>
26 #include <osg/PrimitiveSet>
27 #include <osg/MatrixTransform>
28 #include <osg/ShapeDrawable>
29 #include <osg/Shape>
30 #include <osg/Depth>
31 #include <osg/BlendFunc>
32 #include <osg/StateSet>
33
34 #include <simgear/math/SGGeometry.hxx>
35 #include <simgear/scene/util/OsgMath.hxx>
36
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>
43
44 #include <simgear/bvh/BVHStaticData.hxx>
45
46 #include <simgear/bvh/BVHStaticNode.hxx>
47 #include <simgear/bvh/BVHStaticTriangle.hxx>
48 #include <simgear/bvh/BVHStaticBinary.hxx>
49
50 #include <simgear/bvh/BVHBoundingBoxVisitor.hxx>
51
52 namespace simgear {
53
54 class BVHNode;
55 class BVHStaticNode;
56
57 class BVHDebugCollectVisitor : public BVHVisitor {
58 public:
59     BVHDebugCollectVisitor(const double& time, unsigned level = ~0u) :
60         _group(new osg::Group),
61         _time(time),
62         _level(level),
63         _currentLevel(0)
64     {
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);
75     }
76     virtual ~BVHDebugCollectVisitor()
77     { }
78
79     virtual void apply(BVHGroup& node)
80     {
81         addNodeSphere(node);
82         ++_currentLevel;
83         node.traverse(*this);
84         --_currentLevel;
85     }
86     virtual void apply(BVHTransform& node)
87     {
88         addNodeSphere(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()));
92         _group = transform;
93         ++_currentLevel;
94         node.traverse(*this);
95         --_currentLevel;
96         _group = oldGroup;
97         if (transform->getNumChildren())
98             _group->addChild(transform.get());
99     }
100     virtual void apply(BVHMotionTransform& node)
101     {
102         addNodeSphere(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()));
106         _group = transform;
107         ++_currentLevel;
108         node.traverse(*this);
109         --_currentLevel;
110         _group = oldGroup;
111         if (transform->getNumChildren())
112             _group->addChild(transform.get());
113     }
114     virtual void apply(BVHLineGeometry&)
115     {
116     }
117     virtual void apply(BVHStaticGeometry& node)
118     {
119         addNodeSphere(node);
120         ++_currentLevel;
121         node.traverse(*this);
122         --_currentLevel;
123     }
124
125     virtual void apply(const BVHStaticBinary& node, const BVHStaticData& data)
126     {
127         addNodeBox(node, data);
128         ++_currentLevel;
129         node.traverse(*this, data);
130         --_currentLevel;
131     }
132     virtual void apply(const BVHStaticTriangle& node, const BVHStaticData& data)
133     {
134         addNodeBox(node, data);
135         addTriangle(node.getTriangle(data), osg::Vec4(0.5, 0, 0.5, 0.2));
136     }
137
138     osg::Node* getNode() const { return _group.get(); }
139
140     static unsigned allLevels() { return ~0u; }
141     static unsigned triangles() { return ~0u - 1; }
142
143 private:
144     void addTriangle(const SGTrianglef& triangle, const osg::Vec4& color)
145     {
146         if (_level != triangles())
147             return;
148         
149         osg::Geometry* geometry = new osg::Geometry;
150         
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)));
155         
156         osg::Vec4Array* colors = new osg::Vec4Array;
157         colors->push_back(color);
158         
159         geometry->setVertexArray(vertices);
160         geometry->setColorArray(colors);
161         geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
162         
163         geometry->addPrimitiveSet(new osg::DrawArrays(GL_TRIANGLES, 0, 3));
164         
165         osg::Geode* geode = new osg::Geode;
166         geode->addDrawable(geometry);
167         _group->addChild(geode);
168     }
169     
170     void addNodeSphere(const BVHNode& node)
171     {
172         if (_level != ~0u && _level != _currentLevel)
173             return;
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));
179     }
180     
181     void addNodeBox(const BVHStaticNode& node, const BVHStaticData& data)
182     {
183         if (_level != ~0u && _level != _currentLevel)
184             return;
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));
191     }
192     
193     void addShape(osg::Shape* shape, const osg::Vec4& color)
194     {
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);
201     }
202     
203     osg::ref_ptr<osg::Group> _group;
204     const double _time;
205     const unsigned _level;
206     unsigned _currentLevel;
207 };
208
209 }
210
211 #endif