MEPP2 Project
OSGDebug.inl
Go to the documentation of this file.
1 // Copyright (c) 2012-2019 University of Lyon and CNRS (France).
2 // All rights reserved.
3 //
4 // This file is part of MEPP2; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published
6 // by the Free Software Foundation; either version 3 of the License,
7 // or (at your option) any later version.
8 //
9 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
10 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11 
13 
14 #include <osg/PolygonMode>
15 #include <osg/ShapeDrawable>
16 #include <osg/Material>
17 #include <osg/Geometry>
18 #include <osg/Geode>
19 
20 #include <osg/Texture2D>
21 #include <osgDB/ReadFile>
22 
23 inline
24 osg::ref_ptr< osg::Group >
25 FEVV::Debug::createLine(const double &_x,
26  const double &_y,
27  const double &_z,
28  const double &_x2,
29  const double &_y2,
30  const double &_z2,
31  const FEVV::Color &_color,
32  const std::string &_name,
33  osg::ref_ptr< osg::Group > _group)
34 {
35  osg::ref_ptr< osg::Vec3Array > points = new osg::Vec3Array;
36  osg::ref_ptr< osg::Geometry > geometry = new osg::Geometry;
37  osg::ref_ptr< osg::Material > pMaterial = new osg::Material;
38  osg::ref_ptr< osg::Geode > geode = new osg::Geode;
39 
40  points->push_back(osg::Vec3(_x, _y, _z));
41  points->push_back(osg::Vec3(_x2, _y2, _z2));
42 
43  geometry->setVertexArray(points.get());
44  geometry->addPrimitiveSet(new osg::DrawArrays(GL_LINES, 0, 2));
45 
46  geode->addDrawable(geometry);
47 
48  pMaterial->setDiffuse(osg::Material::FRONT, Helpers::ColorConverter(_color));
49  geode->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
50  geode->getOrCreateStateSet()->setAttribute(pMaterial,
51  osg::StateAttribute::OVERRIDE);
52 
53  geode->setName(_name);
54 
55  _group->addChild(geode);
56  return _group;
57 }
58 
59 inline
60 osg::ref_ptr< osg::Group >
61 FEVV::Debug::createCylinder(const double &_x,
62  const double &_y,
63  const double &_z,
64  const double &_x2,
65  const double &_y2,
66  const double &_z2,
67  const double &_r,
68  const FEVV::Color &_color,
69  const std::string &_name,
70  osg::ref_ptr< osg::Group > _group)
71 {
72  osg::ref_ptr< osg::Cylinder > cylinder;
73  osg::ref_ptr< osg::ShapeDrawable > cylinderDrawable;
74  osg::ref_ptr< osg::Material > pMaterial = new osg::Material;
75  osg::ref_ptr< osg::Geode > geode = new osg::Geode;
76 
77  double height = osg::Vec3(_x - _x2, _y - _y2, _z - _z2).length();
78  osg::Vec3 center = osg::Vec3((_x + _x2) / 2, (_y + _y2) / 2, (_z + _z2) / 2);
79  osg::Vec3 dir = osg::Vec3(
80  0,
81  0,
82  1); // This is the default direction for the cylinders to face in OpenGL
83  osg::Vec3 p = osg::Vec3(_x - _x2, _y - _y2, _z - _z2);
84  osg::Vec3 t = dir ^ p;
85  double angle = acos((dir * p) / p.length());
86 
87  cylinder = new osg::Cylinder(center, _r, height);
88  cylinder->setRotation(osg::Quat(angle, osg::Vec3(t.x(), t.y(), t.z())));
89 
90  cylinderDrawable = new osg::ShapeDrawable(cylinder);
91  geode->addDrawable(cylinderDrawable);
92 
93  pMaterial->setDiffuse(osg::Material::FRONT, Helpers::ColorConverter(_color));
94  geode->getOrCreateStateSet()->setAttribute(pMaterial,
95  osg::StateAttribute::OVERRIDE);
96 
97  geode->setName(_name);
98 
99  _group->addChild(geode);
100  return _group;
101 }
102 
103 inline
104 osg::ref_ptr< osg::Group >
105 FEVV::Debug::createBox(const double &_x,
106  const double &_y,
107  const double &_z,
108  const double &_r,
109  const FEVV::Color &_color,
110  const std::string &_name,
111  osg::ref_ptr< osg::Group > _group)
112 {
113  osg::ref_ptr< osg::Box > box;
114  osg::ref_ptr< osg::ShapeDrawable > boxDrawable;
115  osg::ref_ptr< osg::Material > pMaterial = new osg::Material;
116  osg::ref_ptr< osg::Geode > geode = new osg::Geode;
117 
118  box = new osg::Box(osg::Vec3(_x, _y, _z), _r);
119 
120  boxDrawable = new osg::ShapeDrawable(box);
121  geode->addDrawable(boxDrawable);
122 
123  pMaterial->setDiffuse(osg::Material::FRONT, Helpers::ColorConverter(_color));
124  geode->getOrCreateStateSet()->setAttribute(pMaterial,
125  osg::StateAttribute::OVERRIDE);
126 
127  geode->setName(_name);
128 
129  _group->addChild(geode);
130  return _group;
131 }
132 
133 inline
134 osg::ref_ptr< osg::Group >
135 FEVV::Debug::createBall(const double &_x,
136  const double &_y,
137  const double &_z,
138  const double &_r,
139  const FEVV::Color &_color,
140  const std::string &_name,
141  osg::ref_ptr< osg::Group > _group)
142 {
143  osg::ref_ptr< osg::Sphere > sphere;
144  osg::ref_ptr< osg::ShapeDrawable > sphereDrawable;
145  osg::ref_ptr< osg::Material > pMaterial = new osg::Material;
146  osg::ref_ptr< osg::Geode > geode = new osg::Geode;
147 
148  osg::Vec3 center = osg::Vec3(_x, _y, _z);
149  sphere = new osg::Sphere(center, _r);
150 
151  sphereDrawable = new osg::ShapeDrawable(sphere);
152  geode->addDrawable(sphereDrawable);
153 
154  pMaterial->setDiffuse(osg::Material::FRONT, Helpers::ColorConverter(_color));
155  geode->getOrCreateStateSet()->setAttribute(pMaterial,
156  osg::StateAttribute::OVERRIDE);
157 
158  geode->setName(_name);
159 
160  _group->addChild(geode);
161  return _group;
162 }
163 
164 inline
165 osg::ref_ptr< osg::Group >
166 FEVV::Debug::createPyramid(const double &_x,
167  const double &_y,
168  const double &_z,
169  const double &_r,
170  const std::string &_name,
171  osg::ref_ptr< osg::Group > _group)
172 {
173  double r2 = _r / 2.0;
174 
175  osg::ref_ptr< osg::Geometry > pyramidGeometry = new osg::Geometry;
176  osg::ref_ptr< osg::Geode > geode = new osg::Geode;
177 
178  geode->addDrawable(pyramidGeometry);
179 
180  osg::ref_ptr< osg::Vec3Array > pyramidVertices = new osg::Vec3Array;
181  osg::ref_ptr< osg::Vec3Array > pyramidNormals = new osg::Vec3Array;
182 
183  osg::Vec3 p0 = osg::Vec3(_x - r2, _y - r2, _z - r2);
184  osg::Vec3 p1 = osg::Vec3(_x + r2, _y - r2, _z - r2);
185  osg::Vec3 p2 = osg::Vec3(_x + r2, _y + r2, _z - r2);
186  osg::Vec3 p3 = osg::Vec3(_x - r2, _y + r2, _z - r2);
187  osg::Vec3 p4 = osg::Vec3(_x, _y, _z + r2);
188 
189  pyramidVertices->push_back(p0);
190  pyramidVertices->push_back(p1);
191  pyramidVertices->push_back(p2);
192  pyramidVertices->push_back(p3);
193  pyramidVertices->push_back(p4);
194 
195  pyramidGeometry->setVertexArray(pyramidVertices);
196 
197  osg::ref_ptr< osg::DrawElementsUInt > pyramidBase =
198  new osg::DrawElementsUInt(osg::PrimitiveSet::QUADS, 0);
199  pyramidBase->push_back(3);
200  pyramidBase->push_back(2);
201  pyramidBase->push_back(1);
202  pyramidBase->push_back(0);
203  osg::Vec3 n0 = osg::Vec3(0.0f, 0.0f, -1.0f);
204  pyramidNormals->push_back(n0);
205  pyramidGeometry->addPrimitiveSet(pyramidBase);
206 
207  osg::ref_ptr< osg::DrawElementsUInt > pyramidFaceOne =
208  new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0);
209  pyramidFaceOne->push_back(0);
210  pyramidFaceOne->push_back(1);
211  pyramidFaceOne->push_back(4);
212  osg::Vec3 n1 = ((p4 - p1) ^ (p0 - p1));
213  n1 = n1 / n1.length();
214  pyramidNormals->push_back(n1);
215  pyramidGeometry->addPrimitiveSet(pyramidFaceOne);
216 
217  osg::ref_ptr< osg::DrawElementsUInt > pyramidFaceTwo =
218  new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0);
219  pyramidFaceTwo->push_back(1);
220  pyramidFaceTwo->push_back(2);
221  pyramidFaceTwo->push_back(4);
222  osg::Vec3 n2 = ((p4 - p2) ^ (p1 - p2));
223  n2 = n2 / n2.length();
224  pyramidNormals->push_back(n2);
225  pyramidGeometry->addPrimitiveSet(pyramidFaceTwo);
226 
227  osg::ref_ptr< osg::DrawElementsUInt > pyramidFaceThree =
228  new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0);
229  pyramidFaceThree->push_back(2);
230  pyramidFaceThree->push_back(3);
231  pyramidFaceThree->push_back(4);
232  osg::Vec3 n3 = ((p4 - p3) ^ (p2 - p3));
233  n3 = n3 / n3.length();
234  pyramidNormals->push_back(n3);
235  pyramidGeometry->addPrimitiveSet(pyramidFaceThree);
236 
237  osg::ref_ptr< osg::DrawElementsUInt > pyramidFaceFour =
238  new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0);
239  pyramidFaceFour->push_back(3);
240  pyramidFaceFour->push_back(0);
241  pyramidFaceFour->push_back(4);
242  osg::Vec3 n4 = ((p4 - p0) ^ (p3 - p0));
243  n4 = n4 / n4.length();
244  pyramidNormals->push_back(n4);
245  pyramidGeometry->addPrimitiveSet(pyramidFaceFour);
246 
247  pyramidGeometry->setNormalArray(pyramidNormals,
248  osg::Array::BIND_PER_PRIMITIVE_SET);
249 
250  osg::ref_ptr< osg::Vec4Array > colors = new osg::Vec4Array;
251  colors->push_back(Helpers::ColorConverter(Color::Red()));
252  colors->push_back(Helpers::ColorConverter(Color::Green()));
253  colors->push_back(Helpers::ColorConverter(Color::Blue()));
254  colors->push_back(Helpers::ColorConverter(Color::SunFlower()));
255  colors->push_back(Helpers::ColorConverter(Color::Wisteria()));
256 
257  pyramidGeometry->setColorArray(colors, osg::Array::BIND_PER_PRIMITIVE_SET);
258 
259 #if(0) // TEMP
260  osg::ref_ptr< osg::Vec2Array > texcoords = new osg::Vec2Array(5);
261  (*texcoords)[0].set(0.00f, 0.0f); // tex coord for vertex 0
262  (*texcoords)[1].set(0.25f, 0.0f); // tex coord for vertex 1
263  (*texcoords)[2].set(0.50f, 0.0f); // ""
264  (*texcoords)[3].set(0.75f, 0.0f); // ""
265  (*texcoords)[4].set(0.50f, 1.0f); // ""
266  pyramidGeometry->setTexCoordArray(0, texcoords);
267 
268  osg::ref_ptr< osg::Texture2D > texture = new osg::Texture2D;
269 
270  // protect from being optimized away as static state
271  texture->setDataVariance(osg::Object::DYNAMIC);
272 
273  // load an image by reading a file
274  osg::ref_ptr< osg::Image > image =
275  osgDB::readImageFile("C:\\tmp\\textures_osg\\texture0.tga");
276  if(image)
277  {
278  // assign the texture to the image we read from file
279  texture->setImage(image);
280 
281  // ---
282 
283  // create a new StateSet with default settings
284  osg::ref_ptr< osg::StateSet > stateOne = new osg::StateSet();
285 
286  // assign texture unit 0 of our new StateSet to the texture we just created
287  // and enable the texture
288  stateOne->setTextureAttributeAndModes(0, texture, osg::StateAttribute::ON);
289 
290  // associate this state set with the Geode that contains the pyramid
291  geode->setStateSet(stateOne);
292  }
293  else
294  {
295  std::cout << "Couldn't find texture file." << std::endl;
296  }
297 #endif
298 
299  geode->setName(_name);
300 
301  _group->addChild(geode);
302  return _group;
303 }
304 
305 inline
306 osg::ref_ptr< osg::Group >
307 FEVV::Debug::createGizmo(osg::ref_ptr< osg::Group > _group)
308 {
309  const int size = 1;
310  const double weight = 0.03;
311 
313  0, 0, 0, size, 0, 0, weight, Color::Red(), "(Gizmo) Cylinder", _group);
315  0, 0, 0, 0, size, 0, weight, Color::Green(), "(Gizmo) Cylinder", _group);
317  0, 0, 0, 0, 0, size, weight, Color::Blue(), "(Gizmo) Cylinder", _group);
318 
319  if(_group->getName() == "")
320  {
321  _group->setName("Gizmo");
322  }
323 
324  return _group;
325 }
326 
327 inline
328 osg::ref_ptr< osg::Group >
329 FEVV::Debug::createUnitGrid(osg::ref_ptr< osg::Group > _group)
330 {
331  const int size = 10;
332  const Color color = Color::Clouds();
333 
334  for(int x_axis = -size; x_axis <= size; ++x_axis)
335  {
336  createLine(
337  x_axis, -size, 0, x_axis, size, 0, color, "(UnitGrid) Line", _group);
338  }
339 
340  for(int y_axis = -size; y_axis <= size; ++y_axis)
341  {
342  createLine(
343  -size, y_axis, 0, size, y_axis, 0, color, "(UnitGrid) Line", _group);
344  }
345 
346  if(_group->getName() == "")
347  {
348  _group->setName("UnitGrid");
349  }
350 
351  return _group;
352 }
353 
354 inline
355 osg::ref_ptr< osg::Group >
356 FEVV::Debug::createHud(osg::ref_ptr< osgText::Text > updateText,
357  osg::ref_ptr< osg::Group > _group)
358 {
359  std::string timesFont("fonts/times.ttf");
360 
361  // turn lighting off for the text and disable depth test to ensure its always
362  // ontop.
363  osg::Vec3 position(0.0f, 0.0f, 0.0f);
364 
365  // this displays what has been selected
366  osg::Geode *geode = new osg::Geode();
367  osg::StateSet *stateset = geode->getOrCreateStateSet();
368  stateset->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
369  stateset->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
370  geode->setName("(Hud) Hud");
371  geode->addDrawable(updateText);
372 
373  updateText->setCharacterSize(20.0f);
374  updateText->setFont(timesFont);
375  updateText->setColor(osg::Vec4(1.0f, 1.0f, 0.0f, 1.0f));
376  updateText->setText("");
377  updateText->setPosition(position);
378  updateText->setDataVariance(osg::Object::DYNAMIC);
379 
380  _group->addChild(geode);
381 
382  if(_group->getName() == "")
383  {
384  _group->setName("Hud");
385  }
386 
387  return _group;
388 }
389 
390 inline
391 void
392 FEVV::Debug::print_osg_tree_from_node(osg::Node *nd, int level)
393 {
394  std::string blanks(level*2, ' ');
395 
396  osg::Geode *geode = dynamic_cast< osg::Geode * >(nd);
397  if(geode)
398  {
399  // the node is a geode
400  std::cout << blanks << "geode " << (void *)geode << geode->getName() << std::endl;
401  }
402  else
403  {
404  // the node is a group
405  osg::Group *gp = dynamic_cast< osg::Group * >(nd);
406  if(gp)
407  {
408  std::cout << blanks << "group " << gp->getName() << std::endl;
409  for(unsigned int ic = 0; ic < gp->getNumChildren(); ic++)
410  print_osg_tree_from_node(gp->getChild(ic), level + 1);
411  }
412  else
413  {
414  std::cout << blanks << "unknown node " << nd << std::endl;
415  }
416  }
417 }
FEVV::Debug::createBox
osg::ref_ptr< osg::Group > createBox(const double &_x, const double &_y, const double &_z, const double &_r, const Color &_color=Color::Orange(), const std::string &_name="Box", osg::ref_ptr< osg::Group > _group=new osg::Group)
Definition: OSGDebug.inl:105
FEVV::Debug::createHud
osg::ref_ptr< osg::Group > createHud(osg::ref_ptr< osgText::Text > updateText, osg::ref_ptr< osg::Group > _group=new osg::Group)
Definition: OSGDebug.inl:356
FEVV::Debug::print_osg_tree_from_node
void print_osg_tree_from_node(osg::Node *nd, int level=0)
Definition: OSGDebug.inl:392
FEVV::Debug::createLine
osg::ref_ptr< osg::Group > createLine(const double &_x, const double &_y, const double &_z, const double &_x2, const double &_y2, const double &_z2, const Color &_color=Color::Clouds(), const std::string &_name="Line", osg::ref_ptr< osg::Group > _group=new osg::Group)
Definition: OSGDebug.inl:25
FEVV::Debug::createCylinder
osg::ref_ptr< osg::Group > createCylinder(const double &_x, const double &_y, const double &_z, const double &_x2, const double &_y2, const double &_z2, const double &_r, const Color &_color=Color::Lime(), const std::string &_name="Cylinder", osg::ref_ptr< osg::Group > _group=new osg::Group)
Definition: OSGDebug.inl:61
OSGHelpers.h
FEVV::Color
Definition: Color.hpp:18
FEVV::Debug::createUnitGrid
osg::ref_ptr< osg::Group > createUnitGrid(osg::ref_ptr< osg::Group > _group=new osg::Group)
Definition: OSGDebug.inl:329
FEVV::Operators::Geometry::acos
T acos(T cosine)
Safe call to the std::acos function.
Definition: AngleOperations.hpp:64
FEVV::Debug::createGizmo
osg::ref_ptr< osg::Group > createGizmo(osg::ref_ptr< osg::Group > _group=new osg::Group)
Definition: OSGDebug.inl:307
FEVV::Helpers::ColorConverter
osg::Vec4 ColorConverter(const Color &_color)
Definition: OSGHelpers.h:59
FEVV::Debug::createBall
osg::ref_ptr< osg::Group > createBall(const double &_x, const double &_y, const double &_z, const double &_r, const Color &_color=Color::Amethyst(), const std::string &_name="Ball", osg::ref_ptr< osg::Group > _group=new osg::Group)
Definition: OSGDebug.inl:135
FEVV::Debug::createPyramid
osg::ref_ptr< osg::Group > createPyramid(const double &_x, const double &_y, const double &_z, const double &_r, const std::string &_name="Pyramid", osg::ref_ptr< osg::Group > _group=new osg::Group)
Definition: OSGDebug.inl:166