Stencil 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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_STENCIL
  14. #define OSG_STENCIL 1
  15. #include <osg/StateAttribute>
  16. namespace osg {
  17. #ifndef GL_INCR_WRAP
  18. #define GL_INCR_WRAP 0x8507
  19. #define GL_DECR_WRAP 0x8508
  20. #endif
  21. /** Encapsulate OpenGL glStencilFunc/Op/Mask functions.
  22. *
  23. * All functionality except INCR_WRAP and DECR_WRAP is supported by OpenGL 1.1.
  24. * INCR_WRAP an DECR_WRAP are available since OpenGL 1.4 or when
  25. * GL_EXT_stencil_wrap extension is present.
  26. *
  27. * If INCR_WRAP or DECR_WRAP values are used while they are detected to be not supported,
  28. * the INCR or DECR values are sent to OpenGL instead.
  29. *
  30. * OpenGL 2.0 introduced two side stenciling that is available through
  31. * osg::StencilTwoSided class.
  32. */
  33. class OSG_EXPORT Stencil : public StateAttribute
  34. {
  35. public :
  36. Stencil();
  37. /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
  38. Stencil(const Stencil& stencil,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
  39. StateAttribute(stencil,copyop),
  40. _func(stencil._func),
  41. _funcRef(stencil._funcRef),
  42. _funcMask(stencil._funcMask),
  43. _sfail(stencil._sfail),
  44. _zfail(stencil._zfail),
  45. _zpass(stencil._zpass),
  46. _writeMask(stencil._writeMask) {}
  47. META_StateAttribute(osg, Stencil, STENCIL);
  48. /** return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs.*/
  49. virtual int compare(const StateAttribute& sa) const
  50. {
  51. // check the types are equal and then create the rhs variable
  52. // used by the COMPARE_StateAttribute_Parameter macros below.
  53. COMPARE_StateAttribute_Types(Stencil,sa)
  54. // compare each parameter in turn against the rhs.
  55. COMPARE_StateAttribute_Parameter(_func)
  56. COMPARE_StateAttribute_Parameter(_funcRef)
  57. COMPARE_StateAttribute_Parameter(_funcMask)
  58. COMPARE_StateAttribute_Parameter(_sfail)
  59. COMPARE_StateAttribute_Parameter(_zfail)
  60. COMPARE_StateAttribute_Parameter(_zpass)
  61. COMPARE_StateAttribute_Parameter(_writeMask)
  62. return 0; // passed all the above comparison macros, must be equal.
  63. }
  64. virtual bool getModeUsage(StateAttribute::ModeUsage& usage) const
  65. {
  66. usage.usesMode(GL_STENCIL_TEST);
  67. return true;
  68. }
  69. enum Function
  70. {
  71. NEVER = GL_NEVER,
  72. LESS = GL_LESS,
  73. EQUAL = GL_EQUAL,
  74. LEQUAL = GL_LEQUAL,
  75. GREATER = GL_GREATER,
  76. NOTEQUAL = GL_NOTEQUAL,
  77. GEQUAL = GL_GEQUAL,
  78. ALWAYS = GL_ALWAYS
  79. };
  80. inline void setFunction(Function func,int ref,unsigned int mask)
  81. {
  82. _func = func;
  83. _funcRef = ref;
  84. _funcMask = mask;
  85. }
  86. inline void setFunction(Function func) { _func = func; }
  87. inline Function getFunction() const { return _func; }
  88. inline void setFunctionRef(int ref) { _funcRef=ref; }
  89. inline int getFunctionRef() const { return _funcRef; }
  90. inline void setFunctionMask(unsigned int mask) { _funcMask=mask; }
  91. inline unsigned int getFunctionMask() const { return _funcMask; }
  92. enum Operation
  93. {
  94. KEEP = GL_KEEP,
  95. ZERO = GL_ZERO,
  96. REPLACE = GL_REPLACE,
  97. INCR = GL_INCR,
  98. DECR = GL_DECR,
  99. INVERT = GL_INVERT,
  100. INCR_WRAP = GL_INCR_WRAP,
  101. DECR_WRAP = GL_DECR_WRAP
  102. };
  103. /** set the operations to apply when the various stencil and depth
  104. * tests fail or pass. First parameter is to control the operation
  105. * when the stencil test fails. The second parameter is to control the
  106. * operation when the stencil test passes, but depth test fails. The
  107. * third parameter controls the operation when both the stencil test
  108. * and depth pass. Ordering of parameter is the same as if using
  109. * glStencilOp(,,).*/
  110. inline void setOperation(Operation sfail, Operation zfail, Operation zpass)
  111. {
  112. _sfail = sfail;
  113. _zfail = zfail;
  114. _zpass = zpass;
  115. }
  116. /** set the operation when the stencil test fails.*/
  117. inline void setStencilFailOperation(Operation sfail) { _sfail = sfail; }
  118. /** get the operation when the stencil test fails.*/
  119. inline Operation getStencilFailOperation() const { return _sfail; }
  120. /** set the operation when the stencil test passes but the depth test fails.*/
  121. inline void setStencilPassAndDepthFailOperation(Operation zfail) { _zfail=zfail; }
  122. /** get the operation when the stencil test passes but the depth test fails.*/
  123. inline Operation getStencilPassAndDepthFailOperation() const { return _zfail; }
  124. /** set the operation when both the stencil test and the depth test pass.*/
  125. inline void setStencilPassAndDepthPassOperation(Operation zpass) { _zpass=zpass; }
  126. /** get the operation when both the stencil test and the depth test pass.*/
  127. inline Operation getStencilPassAndDepthPassOperation() const { return _zpass; }
  128. inline void setWriteMask(unsigned int mask) { _writeMask = mask; }
  129. inline unsigned int getWriteMask() const { return _writeMask; }
  130. virtual void apply(State& state) const;
  131. protected:
  132. virtual ~Stencil();
  133. Function _func;
  134. int _funcRef;
  135. unsigned int _funcMask;
  136. Operation _sfail;
  137. Operation _zfail;
  138. Operation _zpass;
  139. unsigned int _writeMask;
  140. };
  141. }
  142. #endif