TexEnvCombine 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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_TEXENVCOMBINE
  14. #define OSG_TEXENVCOMBINE 1
  15. #include <osg/TexEnv>
  16. #include <osg/Vec3>
  17. #include <osg/Vec4>
  18. // If not defined by gl.h use the definition found in:
  19. // http://oss.sgi.com/projects/ogl-sample/registry/ARB/texture_env_combine.txt
  20. #ifndef GL_ARB_texture_env_combine
  21. #define GL_COMBINE_ARB 0x8570
  22. #define GL_COMBINE_RGB_ARB 0x8571
  23. #define GL_COMBINE_ALPHA_ARB 0x8572
  24. #define GL_SOURCE0_RGB_ARB 0x8580
  25. #define GL_SOURCE1_RGB_ARB 0x8581
  26. #define GL_SOURCE2_RGB_ARB 0x8582
  27. #define GL_SOURCE0_ALPHA_ARB 0x8588
  28. #define GL_SOURCE1_ALPHA_ARB 0x8589
  29. #define GL_SOURCE2_ALPHA_ARB 0x858A
  30. #define GL_OPERAND0_RGB_ARB 0x8590
  31. #define GL_OPERAND1_RGB_ARB 0x8591
  32. #define GL_OPERAND2_RGB_ARB 0x8592
  33. #define GL_OPERAND0_ALPHA_ARB 0x8598
  34. #define GL_OPERAND1_ALPHA_ARB 0x8599
  35. #define GL_OPERAND2_ALPHA_ARB 0x859A
  36. #define GL_RGB_SCALE_ARB 0x8573
  37. #define GL_ADD_SIGNED_ARB 0x8574
  38. #define GL_INTERPOLATE_ARB 0x8575
  39. #define GL_SUBTRACT_ARB 0x84E7
  40. #define GL_CONSTANT_ARB 0x8576
  41. #define GL_PRIMARY_COLOR_ARB 0x8577
  42. #define GL_PREVIOUS_ARB 0x8578
  43. #endif
  44. #ifndef GL_ARB_texture_env_dot3
  45. #define GL_DOT3_RGB_ARB 0x86AE
  46. #define GL_DOT3_RGBA_ARB 0x86AF
  47. #endif
  48. #ifndef GL_TEXTURE0
  49. #define GL_TEXTURE0 0x84C0
  50. #endif
  51. namespace osg {
  52. /** TexEnvCombine encapsulates the OpenGL glTexEnvCombine (texture
  53. * environment) state. */
  54. class OSG_EXPORT TexEnvCombine : public StateAttribute
  55. {
  56. public :
  57. TexEnvCombine();
  58. /** Copy constructor using CopyOp to manage deep vs shallow copy. */
  59. TexEnvCombine(const TexEnvCombine& texenv,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
  60. StateAttribute(texenv,copyop),
  61. _needsTexEnvCrossbar(texenv._needsTexEnvCrossbar),
  62. _combine_RGB(texenv._combine_RGB),
  63. _combine_Alpha(texenv._combine_Alpha),
  64. _source0_RGB(texenv._source0_RGB),
  65. _source1_RGB(texenv._source1_RGB),
  66. _source2_RGB(texenv._source2_RGB),
  67. _source0_Alpha(texenv._source0_Alpha),
  68. _source1_Alpha(texenv._source1_Alpha),
  69. _source2_Alpha(texenv._source2_Alpha),
  70. _operand0_RGB(texenv._operand0_RGB),
  71. _operand1_RGB(texenv._operand1_RGB),
  72. _operand2_RGB(texenv._operand2_RGB),
  73. _operand0_Alpha(texenv._operand0_Alpha),
  74. _operand1_Alpha(texenv._operand1_Alpha),
  75. _operand2_Alpha(texenv._operand2_Alpha),
  76. _scale_RGB(texenv._scale_RGB),
  77. _scale_Alpha(texenv._scale_Alpha),
  78. _constantColor(texenv._constantColor) {}
  79. META_StateAttribute(osg, TexEnvCombine, TEXENV);
  80. virtual bool isTextureAttribute() const { return true; }
  81. /** Return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */
  82. virtual int compare(const StateAttribute& sa) const
  83. {
  84. // Check for equal types, then create the rhs variable
  85. // used by the COMPARE_StateAttribute_parameter macros below.
  86. COMPARE_StateAttribute_Types(TexEnvCombine,sa)
  87. // Compare each parameter in turn against the rhs.
  88. COMPARE_StateAttribute_Parameter(_needsTexEnvCrossbar)
  89. COMPARE_StateAttribute_Parameter(_combine_RGB)
  90. COMPARE_StateAttribute_Parameter(_combine_Alpha)
  91. COMPARE_StateAttribute_Parameter(_source0_RGB)
  92. COMPARE_StateAttribute_Parameter(_source1_RGB)
  93. COMPARE_StateAttribute_Parameter(_source2_RGB)
  94. COMPARE_StateAttribute_Parameter(_source0_Alpha)
  95. COMPARE_StateAttribute_Parameter(_source1_Alpha)
  96. COMPARE_StateAttribute_Parameter(_source2_Alpha)
  97. COMPARE_StateAttribute_Parameter(_operand0_RGB)
  98. COMPARE_StateAttribute_Parameter(_operand1_RGB)
  99. COMPARE_StateAttribute_Parameter(_operand2_RGB)
  100. COMPARE_StateAttribute_Parameter(_operand0_Alpha)
  101. COMPARE_StateAttribute_Parameter(_operand1_Alpha)
  102. COMPARE_StateAttribute_Parameter(_operand2_Alpha)
  103. COMPARE_StateAttribute_Parameter(_scale_RGB)
  104. COMPARE_StateAttribute_Parameter(_scale_Alpha)
  105. COMPARE_StateAttribute_Parameter(_constantColor)
  106. return 0; // Passed all the above comparison macros, so must be equal.
  107. }
  108. enum CombineParam
  109. {
  110. REPLACE = GL_REPLACE,
  111. MODULATE = GL_MODULATE,
  112. ADD = GL_ADD,
  113. ADD_SIGNED = GL_ADD_SIGNED_ARB,
  114. INTERPOLATE = GL_INTERPOLATE_ARB,
  115. SUBTRACT = GL_SUBTRACT_ARB,
  116. DOT3_RGB = GL_DOT3_RGB_ARB,
  117. DOT3_RGBA = GL_DOT3_RGBA_ARB
  118. };
  119. void setCombine_RGB(GLint cm);
  120. void setCombine_Alpha(GLint cm);
  121. GLint getCombine_RGB() const { return _combine_RGB; }
  122. GLint getCombine_Alpha() const { return _combine_Alpha; }
  123. enum SourceParam
  124. {
  125. CONSTANT = GL_CONSTANT_ARB,
  126. PRIMARY_COLOR = GL_PRIMARY_COLOR_ARB,
  127. PREVIOUS = GL_PREVIOUS_ARB,
  128. TEXTURE = GL_TEXTURE,
  129. TEXTURE0 = GL_TEXTURE0,
  130. TEXTURE1 = GL_TEXTURE0+1,
  131. TEXTURE2 = GL_TEXTURE0+2,
  132. TEXTURE3 = GL_TEXTURE0+3,
  133. TEXTURE4 = GL_TEXTURE0+4,
  134. TEXTURE5 = GL_TEXTURE0+5,
  135. TEXTURE6 = GL_TEXTURE0+6,
  136. TEXTURE7 = GL_TEXTURE0+7
  137. };
  138. void setSource0_RGB(GLint sp);
  139. void setSource1_RGB(GLint sp);
  140. void setSource2_RGB(GLint sp);
  141. void setSource0_Alpha(GLint sp);
  142. void setSource1_Alpha(GLint sp);
  143. void setSource2_Alpha(GLint sp);
  144. GLint getSource0_RGB() const { return _source0_RGB; }
  145. GLint getSource1_RGB() const { return _source1_RGB; }
  146. GLint getSource2_RGB() const { return _source2_RGB; }
  147. GLint getSource0_Alpha() const { return _source0_Alpha; }
  148. GLint getSource1_Alpha() const { return _source1_Alpha; }
  149. GLint getSource2_Alpha() const { return _source2_Alpha; }
  150. enum OperandParam
  151. {
  152. SRC_COLOR = GL_SRC_COLOR,
  153. ONE_MINUS_SRC_COLOR = GL_ONE_MINUS_SRC_COLOR,
  154. SRC_ALPHA = GL_SRC_ALPHA,
  155. ONE_MINUS_SRC_ALPHA = GL_ONE_MINUS_SRC_ALPHA
  156. };
  157. void setOperand0_RGB(GLint op);
  158. void setOperand1_RGB(GLint op);
  159. void setOperand2_RGB(GLint op);
  160. void setOperand0_Alpha(GLint op);
  161. void setOperand1_Alpha(GLint op);
  162. void setOperand2_Alpha(GLint op);
  163. GLint getOperand0_RGB() const { return _operand0_RGB; }
  164. GLint getOperand1_RGB() const { return _operand1_RGB; }
  165. GLint getOperand2_RGB() const { return _operand2_RGB; }
  166. GLint getOperand0_Alpha() const { return _operand0_Alpha; }
  167. GLint getOperand1_Alpha() const { return _operand1_Alpha; }
  168. GLint getOperand2_Alpha() const { return _operand2_Alpha; }
  169. void setScale_RGB(float scale);
  170. void setScale_Alpha(float scale);
  171. float getScale_RGB() const { return _scale_RGB; }
  172. float getScale_Alpha() const { return _scale_Alpha; }
  173. void setConstantColor( const Vec4& color ) { _constantColor = color; }
  174. const Vec4& getConstantColor() const { return _constantColor; }
  175. /** Set the constant color attribute to the given light direction
  176. * for use with DOT3 combine operation. */
  177. void setConstantColorAsLightDirection(const Vec3& direction)
  178. {
  179. _constantColor.set((direction.x()+1.0f)*0.5f,(direction.y()+1.0f)*0.5f,(direction.z()+1.0f)*0.5f,1.0f);
  180. }
  181. Vec3 getConstantColorAsLightDirection() const
  182. {
  183. return Vec3(_constantColor.x()*2.0f-1.0f, _constantColor.y()*2.0f-1.0f, _constantColor.z()*2.0f-1.0f);
  184. }
  185. virtual void apply(State& state) const;
  186. protected :
  187. virtual ~TexEnvCombine();
  188. inline bool needsTexEnvCombiner(GLint value) const
  189. {
  190. switch(value)
  191. {
  192. case(CONSTANT):
  193. case(PRIMARY_COLOR):
  194. case(PREVIOUS):
  195. case(TEXTURE):
  196. return false;
  197. }
  198. return true;
  199. }
  200. void computeNeedForTexEnvCombiners()
  201. {
  202. _needsTexEnvCrossbar = (needsTexEnvCombiner(_source0_RGB) ||
  203. needsTexEnvCombiner(_source1_RGB) ||
  204. needsTexEnvCombiner(_source2_RGB) ||
  205. needsTexEnvCombiner(_source0_Alpha) ||
  206. needsTexEnvCombiner(_source1_Alpha) ||
  207. needsTexEnvCombiner(_source2_Alpha));
  208. }
  209. bool _needsTexEnvCrossbar;
  210. GLint _combine_RGB;
  211. GLint _combine_Alpha;
  212. GLint _source0_RGB;
  213. GLint _source1_RGB;
  214. GLint _source2_RGB;
  215. GLint _source0_Alpha;
  216. GLint _source1_Alpha;
  217. GLint _source2_Alpha;
  218. GLint _operand0_RGB;
  219. GLint _operand1_RGB;
  220. GLint _operand2_RGB;
  221. GLint _operand0_Alpha;
  222. GLint _operand1_Alpha;
  223. GLint _operand2_Alpha;
  224. float _scale_RGB;
  225. float _scale_Alpha;
  226. osg::Vec4 _constantColor;
  227. };
  228. }
  229. #endif