Pteros  2.0
Molecular modeling library for human beings!
ShapeToGeometry.h
1 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2014 Robert Osfield
2  * Copyright (C) 2014 Pawel Ksiezopolski
3  *
4  * This library is open source and may be redistributed and/or modified under
5  * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
6  * (at your option) any later version. The full license is in LICENSE file
7  * included with this distribution, and on the openscenegraph.org website.
8  *
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
12  * OpenSceneGraph Public License for more details.
13  *
14 */
15 #ifndef SHAPE_TO_GEOMETRY
16 #define SHAPE_TO_GEOMETRY 1
17 #include <osg/Geometry>
18 #include <osg/Geode>
19 #include <osg/ShapeDrawable>
20 #include <osg/NodeVisitor>
21 
22 // arbitrary minima for rows & segments ( from shapedrawable.cpp )
23 const unsigned int MIN_NUM_ROWS = 3;
24 const unsigned int MIN_NUM_SEGMENTS = 5;
25 
26 // osg::GLBeginEndAdapter descendant that stores data for osg::Geometry creation
27 class FakeGLBeginEndAdapter : public osg::GLBeginEndAdapter
28 {
29 public:
30  FakeGLBeginEndAdapter();
31 
32  void PushMatrix();
33  void MultMatrixd(const GLdouble* m);
34  void Translated(GLdouble x, GLdouble y, GLdouble z);
35  void Scaled(GLdouble x, GLdouble y, GLdouble z);
36  void Rotated(GLdouble angle, GLdouble x, GLdouble y, GLdouble z);
37  void End();
38 
39  osg::ref_ptr<osg::Geometry> geometry;
40 };
41 
42 class ShapeToGeometryVisitor : public osg::ConstShapeVisitor
43 {
44 public:
45 
46  ShapeToGeometryVisitor(const osg::TessellationHints* hints)
47  : osg::ConstShapeVisitor(), _hints(hints)
48  {
49  }
50 
51  virtual void apply(const osg::Sphere&);
52  virtual void apply(const osg::Box&);
53  virtual void apply(const osg::Cone&);
54  virtual void apply(const osg::Cylinder&);
55  virtual void apply(const osg::Capsule&);
56  virtual void apply(const osg::InfinitePlane&);
57 
58  virtual void apply(const osg::TriangleMesh&);
59  virtual void apply(const osg::ConvexHull&);
60  virtual void apply(const osg::HeightField&);
61 
62  virtual void apply(const osg::CompositeShape&);
63 
64  osg::Geometry* getGeometry() { return gl.geometry.get(); }
65 
66 
67  const osg::TessellationHints* _hints;
68  FakeGLBeginEndAdapter gl;
69 protected:
70 
71  ShapeToGeometryVisitor& operator = (const ShapeToGeometryVisitor&) { return *this; }
72  enum SphereHalf { SphereTopHalf, SphereBottomHalf };
73 
74  // helpers for apply( Cylinder | Sphere | Capsule )
75  void drawCylinderBody(unsigned int numSegments, float radius, float height);
76  void drawHalfSphere(unsigned int numSegments, unsigned int numRows, float radius, SphereHalf which, float zOffset = 0.0f);
77 };
78 
79 osg::Geometry* convertShapeToGeometry(const osg::Shape& shape, const osg::TessellationHints* hints);
80 
81 osg::Geometry* convertShapeToGeometry(const osg::Shape& shape, const osg::TessellationHints* hints, const osg::Vec4& color);
82 
83 osg::Geode* convertShapeToGeode(const osg::Shape& shape, const osg::TessellationHints* hints);
84 
85 osg::Geode* convertShapeToGeode(const osg::Shape& shape, const osg::TessellationHints* hints, const osg::Vec4& color);
86 
87 
88 // example : how to use convertShapeToGeometry()
89 // osg::ref_ptr<osg::Capsule> shape = new osg::Capsule( osg::Vec3( 0.0, 0.0, 0.0 ), radius, height );
90 // osg::ref_ptr<osg::TessellationHints> tessHints = new osg::TessellationHints;
91 // tessHints->setDetailRatio(0.5f);
92 // tessHints->setCreateTextureCoords(true);
93 // osg::ref_ptr<osg::Geometry> capsuleGeometry = convertShapeToGeometry(*shape.get(), tessHints.get());
94 // osg::ref_ptr<osg::Geometry> redCapsuleGeometry = convertShapeToGeometry(*shape.get(), tessHints.get(), osg::Vec4(1.0,0.0,0.0,1.0) );
95 
96 
97 #endif