ClipPlane 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 OSG_CLIPPLANE
  14. #define OSG_CLIPPLANE 1
  15. #include <osg/Vec4d>
  16. #include <osg/Plane>
  17. #include <osg/StateAttribute>
  18. #ifndef GL_CLIP_PLANE0
  19. #define GL_CLIP_PLANE0 0x3000
  20. #define GL_CLIP_PLANE1 0x3001
  21. #define GL_CLIP_PLANE2 0x3002
  22. #define GL_CLIP_PLANE3 0x3003
  23. #define GL_CLIP_PLANE4 0x3004
  24. #define GL_CLIP_PLANE5 0x3005
  25. #endif
  26. namespace osg {
  27. /** Encapsulates OpenGL glClipPlane().
  28. */
  29. class OSG_EXPORT ClipPlane : public StateAttribute
  30. {
  31. public :
  32. ClipPlane();
  33. inline ClipPlane(unsigned int no):_clipPlaneNum(no) {}
  34. inline ClipPlane(unsigned int no,const Vec4d& plane):_clipPlaneNum(no) { setClipPlane(plane); }
  35. inline ClipPlane(unsigned int no,const Plane& plane):_clipPlaneNum(no) { setClipPlane(plane); }
  36. inline ClipPlane(unsigned int no,double a,double b,double c,double d): _clipPlaneNum(no) { setClipPlane(a,b,c,d); }
  37. /** Copy constructor using CopyOp to manage deep vs shallow copy. */
  38. ClipPlane(const ClipPlane& cp,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
  39. StateAttribute(cp,copyop)
  40. {
  41. _clipPlane[0]=cp._clipPlane[0];
  42. _clipPlane[1]=cp._clipPlane[1];
  43. _clipPlane[2]=cp._clipPlane[2];
  44. _clipPlane[3]=cp._clipPlane[3];
  45. _clipPlaneNum=cp._clipPlaneNum;
  46. }
  47. virtual osg::Object* cloneType() const { return new ClipPlane( _clipPlaneNum ); }
  48. virtual osg::Object* clone(const osg::CopyOp& copyop) const { return new ClipPlane(*this,copyop); }
  49. virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const ClipPlane *>(obj)!=NULL; }
  50. virtual const char* libraryName() const { return "osg"; }
  51. virtual const char* className() const { return "ClipPlane"; }
  52. virtual Type getType() const { return CLIPPLANE; }
  53. /** Return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */
  54. virtual int compare(const StateAttribute& sa) const
  55. {
  56. // Check for equal types, then create the rhs variable
  57. // used by the COMPARE_StateAttribute_Parameter macros below.
  58. COMPARE_StateAttribute_Types(ClipPlane,sa)
  59. // Compare each parameter in turn against the rhs.
  60. COMPARE_StateAttribute_Parameter(_clipPlaneNum)
  61. COMPARE_StateAttribute_Parameter(_clipPlane[0])
  62. COMPARE_StateAttribute_Parameter(_clipPlane[1])
  63. COMPARE_StateAttribute_Parameter(_clipPlane[2])
  64. COMPARE_StateAttribute_Parameter(_clipPlane[3])
  65. return 0; // Passed all the above comparison macros, so must be equal.
  66. }
  67. virtual unsigned int getMember() const { return _clipPlaneNum; }
  68. virtual bool getModeUsage(StateAttribute::ModeUsage& usage) const
  69. {
  70. usage.usesMode((GLMode)(GL_CLIP_PLANE0+_clipPlaneNum));
  71. return true;
  72. }
  73. /** Set the clip plane with the given Plane. */
  74. void setClipPlane(const Plane& plane)
  75. {
  76. _clipPlane.set(plane[0],plane[1],plane[2],plane[3]);
  77. }
  78. /** Defines the plane as [ a b c d ]. */
  79. void setClipPlane(double a,double b,double c,double d)
  80. {
  81. _clipPlane.set(a,b,c,d);
  82. }
  83. /** Set the clip plane with the given Vec4. */
  84. inline void setClipPlane(const Vec4d& plane) { _clipPlane = plane; }
  85. /** Gets the clip plane as a Vec4d. */
  86. const Vec4d& getClipPlane() const { return _clipPlane; }
  87. /** Sets the clip plane number. */
  88. void setClipPlaneNum(unsigned int num);
  89. /** Gets the clip plane number. */
  90. unsigned int getClipPlaneNum() const;
  91. /** Applies the clip plane's state to the OpenGL state machine. */
  92. virtual void apply(State& state) const;
  93. protected :
  94. virtual ~ClipPlane();
  95. Vec4d _clipPlane;
  96. unsigned int _clipPlaneNum;
  97. };
  98. }
  99. #endif