SphereSegment 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  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
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. #ifndef OSGSIM_SPHERESEGMENT
  14. #define OSGSIM_SPHERESEGMENT 1
  15. #include <osgSim/Export>
  16. #include <osg/Vec3>
  17. #include <osg/Vec4>
  18. #include <osg/Geode>
  19. #include <osg/Matrixd>
  20. #include <osg/BlendFunc>
  21. #include <osg/Geometry>
  22. namespace osgSim{
  23. /**
  24. A SphereSegment is a Geode to represent an portion of a sphere (potentially
  25. the whole sphere). The sphere is aligned such that the line through the
  26. sphere's poles is parallel to the z axis. The sphere segment
  27. may be rendered various components switched on or off:
  28. - The specified area of the sphere surface.
  29. - An edge line around the boundary of the specified area
  30. of the sphere surface.
  31. - Four <i>spokes</i>, where a spoke is the line from
  32. the sphere's centre to a corner of the rendered area.
  33. - Four planar areas, where the planar areas are formed
  34. between the spokes.
  35. Caveats:
  36. - It's worth noting that the line through the sphere's poles is
  37. parallel to the z axis. This has implications when specifying the
  38. area to be rendered, and specifying areas where the centre of
  39. the rendered area <i>is</i> the Z axis may lead to unexpected
  40. geometry.
  41. - It's possible to render the whole sphere by specifying elevation
  42. and azimuth ranges round the full 360 degrees. When doing
  43. so you may consider switching the planes, spokes, and edge lines
  44. off, to avoid rendering artifacts, e.g. the upper and lower
  45. planes will be coincident.
  46. */
  47. class OSGSIM_EXPORT SphereSegment: public osg::Geode
  48. {
  49. public:
  50. /**
  51. DrawMask represents a bit field, the values of which may be OR'ed together
  52. to specify which parts of the sphere segment should be drawn. E.g.
  53. \code
  54. sphereSegment->setDrawMask(SphereSegment::DrawMask(SphereSegment::SURFACE|SphereSegment::SPOKES));
  55. \endcode
  56. */
  57. enum DrawMask{
  58. SURFACE = 0x00000001, ///< Draw the specified area on the sphere's surface
  59. SPOKES = 0x00000002, ///< Draw the spokes from the sphere's centre to the surface's corners
  60. EDGELINE = 0x00000008, ///< Draw the line round the edge of the area on the sphere's surface
  61. SIDES = 0x00000010, ///< Draw the planes from the sphere's centre to the edge of the sphere's surface
  62. ALL = 0x7fffffff ///< Draw every part of the sphere segment
  63. };
  64. /** Default constructor. */
  65. SphereSegment():osg::Geode(),
  66. _centre(0.0f,0.0f,0.0f), _radius(1.0f),
  67. _azMin(0.0f), _azMax(osg::PI/2.0f),
  68. _elevMin(0.0f), _elevMax(osg::PI/2.0f),
  69. _density(10),
  70. _drawMask(DrawMask(ALL))
  71. {
  72. init();
  73. }
  74. /**
  75. Construct by angle ranges. Note that the azimuth 'zero' is the Y axis; specifying
  76. an azimuth range from azMin -PI/2.0f to azMax PI/2.0f will cover the
  77. 'top half' of the circle in the XY plane. The elev angles are 'out' of the 'zero'
  78. XY plane with +ve angles above the plane, and -ve angles below.
  79. @param centre sphere centre
  80. @param radius radius of sphere
  81. @param azMin azimuth minimum
  82. @param azMax azimuth maximum
  83. @param elevMin elevation minimum
  84. @param elevMax elevation maximum
  85. @param density number of units to divide the azimuth and elevation ranges into
  86. */
  87. SphereSegment(const osg::Vec3& centre, float radius, float azMin, float azMax, float elevMin, float elevMax, int density);
  88. /**
  89. Construct by vector.
  90. @param centre sphere centre
  91. @param radius radius of sphere
  92. @param vec vector pointing from sphere centre to centre point
  93. of rendered area on sphere surface
  94. @param azRange azimuth range in radians (with centre along vec)
  95. @param elevRange elevation range in radians (with centre along vec)
  96. @param density number of units to divide the azimuth and elevation ranges into
  97. */
  98. SphereSegment(const osg::Vec3& centre, float radius, const osg::Vec3& vec, float azRange, float elevRange, int density);
  99. /** Copy constructor */
  100. SphereSegment(const SphereSegment& rhs, const osg::CopyOp& co);
  101. void traverse(osg::NodeVisitor& nv);
  102. /** Set the centre point of the SphereSegment */
  103. void setCentre(const osg::Vec3& c);
  104. /** Get the centre point of the SphereSegment */
  105. const osg::Vec3& getCentre() const;
  106. /** Set the radius of the SphereSegment */
  107. void setRadius(float r);
  108. /** Get the radius of the SphereSegment */
  109. float getRadius() const;
  110. /** Set the area of the sphere segment
  111. @param vec vector pointing from sphere centre to centre point
  112. of rendered area on sphere surface
  113. @param azRange azimuth range in radians (with centre along vec)
  114. @param elevRange elevation range in radians (with centre along vec)
  115. */
  116. void setArea(const osg::Vec3& vec, float azRange, float elevRange);
  117. /** Get the area of the sphere segment
  118. @param vec vector pointing from sphere centre to centre point
  119. of rendered area on sphere surface (normalized)
  120. @param azRange azimuth range in radians (with centre along vec)
  121. @param elevRange elevation range in radians (with centre along vec)
  122. */
  123. void getArea(osg::Vec3& vec, float& azRange, float& elevRange) const;
  124. /** Set the area of the sphere segment
  125. @param azMin azimuth minimum
  126. @param azMax azimuth maximum
  127. @param elevMin elevation minimum
  128. @param elevMax elevation maximum
  129. */
  130. void setArea(float azMin, float azMax, float elevMin, float elevMax);
  131. /** Get the area of the sphere segment
  132. @param azMin azimuth minimum
  133. @param azMax azimuth maximum
  134. @param elevMin elevation minimum
  135. @param elevMax elevation maximum
  136. */
  137. void getArea(float &azMin, float &azMax, float &elevMin, float &elevMax) const;
  138. /** Set the density of the sphere segment */
  139. void setDensity(int d);
  140. /** Get the density of the sphere segment */
  141. int getDensity() const;
  142. /**
  143. Specify the DrawMask.
  144. @param dm Bitmask specifying which parts of the sphere segment should be drawn.
  145. @see DrawMask
  146. */
  147. void setDrawMask(int dm);
  148. /** Get the DrawMask */
  149. int getDrawMask() const { return _drawMask; }
  150. /** Set the color of the surface. */
  151. void setSurfaceColor(const osg::Vec4& c);
  152. /** Get the color of the surface. */
  153. const osg::Vec4& getSurfaceColor() const { return (*_surfaceColor)[0]; }
  154. /** Set the color of the spokes. */
  155. void setSpokeColor(const osg::Vec4& c);
  156. /** Get the color of the spokes. */
  157. const osg::Vec4& getSpokeColor() const { return (*_spokeColor)[0]; }
  158. /** Set the color of the edge line. */
  159. void setEdgeLineColor(const osg::Vec4& c);
  160. /** Get the color of the edge line. */
  161. const osg::Vec4& getEdgeLineColor() const { return (*_edgeLineColor)[0]; }
  162. /** Set the color of the planes. */
  163. void setSideColor(const osg::Vec4& c);
  164. /** Get the color of the planes. */
  165. const osg::Vec4& getSideColor() const { return (*_sideColor)[0]; }
  166. /** Set color of all components. */
  167. void setAllColors(const osg::Vec4& c);
  168. META_Node(osgSim, SphereSegment);
  169. /** A list of vertex arrays representing a list of lines.*/
  170. typedef std::vector< osg::ref_ptr<osg::Vec3Array> > LineList;
  171. /** Compute the intersection lines between subgraph and this sphere segment.
  172. * The matrix is the transform that takes the subgraph into the same coordinate frame as the sphere segment.
  173. * The resulting intersections are in the coordinate frame of the sphere segment. */
  174. LineList computeIntersection(const osg::Matrixd& matrix, osg::Node* subgraph);
  175. /** Compute the intersection lines between specified drawable and this sphere segment.
  176. * The matrix is the transform that takes the subgraph into the same coordinate frame as the sphere segment.
  177. * The resulting intersections are in the coordinate frame of the sphere segment. */
  178. LineList computeIntersection(const osg::Matrixd& matrix, osg::Drawable* drawable);
  179. /** Compute the intersection lines between subgraph and this sphere segment.
  180. * The matrix is the transform that takes the subgraph into the same coordinate frame as the sphere segment.
  181. * The resulting intersections are in the coordinate frame of the sphere segment. */
  182. osg::Node* computeIntersectionSubgraph(const osg::Matrixd& matrix, osg::Node* subgraph);
  183. /** Compute the intersection lines between specified drawable and this sphere segment.
  184. * The matrix is the transform that takes the subgraph into the same coordinate frame as the sphere segment.
  185. * The resulting intersections are in the coordinate frame of the sphere segment. */
  186. osg::Node* computeIntersectionSubgraph(const osg::Matrixd& matrix, osg::Drawable* drawable);
  187. /** recompute the vertex positions of the rendering meshes/lines that represent the sphere segment.*/
  188. void updatePositions();
  189. /** recompute the primitives rendering meshes/lines thtat represent the sphere segment.*/
  190. void updatePrimitives();
  191. virtual void resizeGLObjectBuffers(unsigned int maxSize);
  192. virtual void releaseGLObjects(osg::State* state = 0) const;
  193. virtual osg::BoundingSphere computeBound() const;
  194. private:
  195. void init(); // Shared constructor code, generates the drawables
  196. void dirty(); // Force re-calling of gl functions and bounding boxes
  197. // Sphere segment geometry details
  198. osg::Vec3 _centre;
  199. float _radius;
  200. float _azMin, _azMax, _elevMin, _elevMax;
  201. int _density;
  202. // Draw details
  203. int _drawMask;
  204. osg::ref_ptr<osg::Vec4Array> _surfaceColor;
  205. osg::ref_ptr<osg::Vec4Array> _spokeColor;
  206. osg::ref_ptr<osg::Vec4Array> _edgeLineColor;
  207. osg::ref_ptr<osg::Vec4Array> _sideColor;
  208. osg::ref_ptr<osg::Vec3Array> _vertices;
  209. osg::ref_ptr<osg::Vec3Array> _normals;
  210. osg::ref_ptr<osg::Geometry> _surfaceGeometry;
  211. osg::ref_ptr<osg::Geometry> _spokesGeometry;
  212. osg::ref_ptr<osg::Geometry> _edgeLineGeometry;
  213. osg::ref_ptr<osg::Geometry> _sidesGeometry;
  214. osg::ref_ptr<osg::StateSet> _litOpaqueState;
  215. osg::ref_ptr<osg::StateSet> _unlitOpaqueState;
  216. osg::ref_ptr<osg::StateSet> _litTransparentState;
  217. osg::ref_ptr<osg::StateSet> _unlitTransparentState;
  218. osg::StateSet* getLitStateSet(const osg::Vec4& color) { return (color.a()<1.0) ? _litTransparentState.get() : _litOpaqueState.get(); }
  219. osg::StateSet* getUnlitStateSet(const osg::Vec4& color) { return (color.a()<1.0) ? _unlitTransparentState.get() : _unlitOpaqueState.get(); }
  220. };
  221. }
  222. #endif