StencilTwoSided 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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_STENCILTWOSIDED
  14. #define OSG_STENCILTWOSIDED 1
  15. #include <osg/Stencil>
  16. namespace osg {
  17. #ifndef GL_STENCIL_TEST_TWO_SIDE
  18. #define GL_STENCIL_TEST_TWO_SIDE 0x8910
  19. #endif
  20. /** Provides OpenGL two sided stencil functionality, also known as separate stencil.
  21. * It enables to specify different stencil function for front and back facing polygons.
  22. * Two sided stenciling is used usually to eliminate the need of two rendering passes
  23. * when using standard stenciling functions. See also \sa osg::Stencil.
  24. *
  25. * Two sided stenciling is available since OpenGL 2.0. It is also supported by
  26. * EXT_stencil_two_side extension especially on Nvidia cards.
  27. * Another extension introduced by ATI is ATI_separate_stencil. However, ATI's extension
  28. * is limited to have reference and mask value the same for both faces.
  29. * ATI's extension is currently not supported by the current implementation.
  30. *
  31. * osg::StencilTwoSided does nothing if OpenGL 2.0 or EXT_stencil_two_side are not available.
  32. */
  33. class OSG_EXPORT StencilTwoSided : public StateAttribute
  34. {
  35. public :
  36. StencilTwoSided();
  37. /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
  38. StencilTwoSided(const StencilTwoSided& stencil,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
  39. META_StateAttribute(osg, StencilTwoSided, STENCIL);
  40. /** return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs.*/
  41. virtual int compare(const StateAttribute& sa) const;
  42. virtual bool getModeUsage(StateAttribute::ModeUsage& usage) const
  43. {
  44. usage.usesMode(GL_STENCIL_TEST);
  45. return true;
  46. }
  47. enum Face
  48. {
  49. FRONT = 0,
  50. BACK = 1
  51. };
  52. enum Function
  53. {
  54. NEVER = GL_NEVER,
  55. LESS = GL_LESS,
  56. EQUAL = GL_EQUAL,
  57. LEQUAL = GL_LEQUAL,
  58. GREATER = GL_GREATER,
  59. NOTEQUAL = GL_NOTEQUAL,
  60. GEQUAL = GL_GEQUAL,
  61. ALWAYS = GL_ALWAYS
  62. };
  63. inline void setFunction(Face face, Function func,int ref,unsigned int mask)
  64. {
  65. _func[face] = func;
  66. _funcRef[face] = ref;
  67. _funcMask[face] = mask;
  68. }
  69. inline void setFunction(Face face, Function func) { _func[face] = func; }
  70. inline Function getFunction(Face face) const { return _func[face]; }
  71. inline void setFunctionRef(Face face, int ref) { _funcRef[face]=ref; }
  72. inline int getFunctionRef(Face face) const { return _funcRef[face]; }
  73. inline void setFunctionMask(Face face, unsigned int mask) { _funcMask[face]=mask; }
  74. inline unsigned int getFunctionMask(Face face) const { return _funcMask[face]; }
  75. enum Operation
  76. {
  77. KEEP = GL_KEEP,
  78. ZERO = GL_ZERO,
  79. REPLACE = GL_REPLACE,
  80. INCR = GL_INCR,
  81. DECR = GL_DECR,
  82. INVERT = GL_INVERT,
  83. INCR_WRAP = GL_INCR_WRAP,
  84. DECR_WRAP = GL_DECR_WRAP
  85. };
  86. /** set the operations to apply when the various stencil and depth
  87. * tests fail or pass. First parameter is to control the operation
  88. * when the stencil test fails. The second parameter is to control the
  89. * operation when the stencil test passes, but depth test fails. The
  90. * third parameter controls the operation when both the stencil test
  91. * and depth pass. Ordering of parameter is the same as if using
  92. * glStencilOp(,,).*/
  93. inline void setOperation(Face face, Operation sfail, Operation zfail, Operation zpass)
  94. {
  95. _sfail[face] = sfail;
  96. _zfail[face] = zfail;
  97. _zpass[face] = zpass;
  98. }
  99. /** set the operation when the stencil test fails.*/
  100. inline void setStencilFailOperation(Face face, Operation sfail) { _sfail[face] = sfail; }
  101. /** get the operation when the stencil test fails.*/
  102. inline Operation getStencilFailOperation(Face face) const { return _sfail[face]; }
  103. /** set the operation when the stencil test passes but the depth test fails.*/
  104. inline void setStencilPassAndDepthFailOperation(Face face, Operation zfail) { _zfail[face]=zfail; }
  105. /** get the operation when the stencil test passes but the depth test fails.*/
  106. inline Operation getStencilPassAndDepthFailOperation(Face face) const { return _zfail[face]; }
  107. /** set the operation when both the stencil test and the depth test pass.*/
  108. inline void setStencilPassAndDepthPassOperation(Face face, Operation zpass) { _zpass[face]=zpass; }
  109. /** get the operation when both the stencil test and the depth test pass.*/
  110. inline Operation getStencilPassAndDepthPassOperation(Face face) const { return _zpass[face]; }
  111. inline void setWriteMask(Face face, unsigned int mask) { _writeMask[face] = mask; }
  112. inline unsigned int getWriteMask(Face face) const { return _writeMask[face]; }
  113. virtual void apply(State& state) const;
  114. protected:
  115. virtual ~StencilTwoSided();
  116. Function _func[2];
  117. int _funcRef[2];
  118. unsigned int _funcMask[2];
  119. Operation _sfail[2];
  120. Operation _zfail[2];
  121. Operation _zpass[2];
  122. unsigned int _writeMask[2];
  123. };
  124. }
  125. #endif