BlendFunc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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_BLENDFUNC
  14. #define OSG_BLENDFUNC 1
  15. #include <osg/StateAttribute>
  16. #ifndef GL_VERSION_1_2
  17. #define GL_CONSTANT_COLOR 0x8001
  18. #define GL_ONE_MINUS_CONSTANT_COLOR 0x8002
  19. #define GL_CONSTANT_ALPHA 0x8003
  20. #define GL_ONE_MINUS_CONSTANT_ALPHA 0x8004
  21. #define GL_BLEND_COLOR 0x8005
  22. #endif
  23. #ifndef GL_VERSION_1_4
  24. #define GL_BLEND_DST_RGB 0x80C8
  25. #define GL_BLEND_SRC_RGB 0x80C9
  26. #define GL_BLEND_DST_ALPHA 0x80CA
  27. #define GL_BLEND_SRC_ALPHA 0x80CB
  28. #endif
  29. namespace osg {
  30. /** Encapsulates OpenGL blend/transparency state.
  31. *
  32. * Blending combines incoming fragment with a fragment
  33. * already present in the target buffer.
  34. *
  35. * OpenGL 1.1 supports following source and destination blending factors:
  36. * GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
  37. * GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA,
  38. * GL_ZERO, GL_ONE.
  39. *
  40. * Moreover, there are three source-only blending factors:
  41. * GL_DST_COLOR, GL_ONE_MINUS_DST_COLOR, GL_SRC_ALPHA_SATURATE
  42. * and two destination-only blending factors:
  43. * GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR.
  44. * OpenGL 1.4 allowed to use these five blending factors
  45. * as both - source and destination blending factors.
  46. *
  47. * Following four source and destination blending factors
  48. * were added by Imaging subset of OpenGL 1.2
  49. * and made mandatory by OpenGL 1.4:
  50. * GL_CONSTANT_COLOR, GL_ONE_MINUS_CONSTANT_COLOR,
  51. * GL_CONSTANT_ALPHA, GL_ONE_MINUS_CONSTANT_ALPHA
  52. *
  53. * OpenGL 1.4 further provides glBlendFuncSeparate
  54. * (promoted from GL_EXT_blend_func_separate).
  55. * It makes possible to set blending functions for RGB and Alpha separately.
  56. * Before, it was possible to set just one blending function for RGBA.
  57. */
  58. class OSG_EXPORT BlendFunc : public StateAttribute
  59. {
  60. public :
  61. BlendFunc();
  62. BlendFunc(GLenum source, GLenum destination);
  63. BlendFunc(GLenum source, GLenum destination, GLenum source_alpha, GLenum destination_alpha);
  64. /** Copy constructor using CopyOp to manage deep vs shallow copy. */
  65. BlendFunc(const BlendFunc& trans,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
  66. StateAttribute(trans,copyop),
  67. _source_factor(trans._source_factor),
  68. _destination_factor(trans._destination_factor),
  69. _source_factor_alpha(trans._source_factor_alpha),
  70. _destination_factor_alpha(trans._destination_factor_alpha) {}
  71. META_StateAttribute(osg, BlendFunc,BLENDFUNC);
  72. /** Return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */
  73. virtual int compare(const StateAttribute& sa) const
  74. {
  75. // Check for equal types, then create the rhs variable
  76. // used by the COMPARE_StateAttribute_Parameter macros below.
  77. COMPARE_StateAttribute_Types(BlendFunc,sa)
  78. // Compare each parameter in turn against the rhs.
  79. COMPARE_StateAttribute_Parameter(_source_factor)
  80. COMPARE_StateAttribute_Parameter(_destination_factor)
  81. COMPARE_StateAttribute_Parameter(_source_factor_alpha)
  82. COMPARE_StateAttribute_Parameter(_destination_factor_alpha)
  83. return 0; // Passed all the above comparison macros, so must be equal.
  84. }
  85. virtual bool getModeUsage(StateAttribute::ModeUsage& usage) const
  86. {
  87. usage.usesMode(GL_BLEND);
  88. return true;
  89. }
  90. enum BlendFuncMode {
  91. DST_ALPHA = GL_DST_ALPHA,
  92. DST_COLOR = GL_DST_COLOR,
  93. ONE = GL_ONE,
  94. ONE_MINUS_DST_ALPHA = GL_ONE_MINUS_DST_ALPHA,
  95. ONE_MINUS_DST_COLOR = GL_ONE_MINUS_DST_COLOR,
  96. ONE_MINUS_SRC_ALPHA = GL_ONE_MINUS_SRC_ALPHA,
  97. ONE_MINUS_SRC_COLOR = GL_ONE_MINUS_SRC_COLOR,
  98. SRC_ALPHA = GL_SRC_ALPHA,
  99. SRC_ALPHA_SATURATE = GL_SRC_ALPHA_SATURATE,
  100. SRC_COLOR = GL_SRC_COLOR,
  101. CONSTANT_COLOR = GL_CONSTANT_COLOR,
  102. ONE_MINUS_CONSTANT_COLOR = GL_ONE_MINUS_CONSTANT_COLOR,
  103. CONSTANT_ALPHA = GL_CONSTANT_ALPHA,
  104. ONE_MINUS_CONSTANT_ALPHA = GL_ONE_MINUS_CONSTANT_ALPHA,
  105. ZERO = GL_ZERO
  106. };
  107. inline void setFunction( GLenum source, GLenum destination )
  108. {
  109. _source_factor = source;
  110. _destination_factor = destination;
  111. _source_factor_alpha = source;
  112. _destination_factor_alpha = destination;
  113. }
  114. inline void setFunction( GLenum source_rgb, GLenum destination_rgb, GLenum source_alpha, GLenum destination_alpha )
  115. {
  116. _source_factor = source_rgb;
  117. _destination_factor = destination_rgb;
  118. _source_factor_alpha = source_alpha;
  119. _destination_factor_alpha = destination_alpha;
  120. }
  121. void setSource(GLenum source) { _source_factor = _source_factor_alpha = source; }
  122. inline GLenum getSource() const { return _source_factor; }
  123. void setSourceRGB(GLenum source) { _source_factor = source; }
  124. inline GLenum getSourceRGB() const { return _source_factor; }
  125. void setSourceAlpha(GLenum source) { _source_factor_alpha = source; }
  126. inline GLenum getSourceAlpha() const { return _source_factor_alpha; }
  127. void setDestination(GLenum destination) { _destination_factor = _destination_factor_alpha = destination; }
  128. inline GLenum getDestination() const { return _destination_factor; }
  129. void setDestinationRGB(GLenum destination) { _destination_factor = destination; }
  130. inline GLenum getDestinationRGB() const { return _destination_factor; }
  131. void setDestinationAlpha(GLenum destination) { _destination_factor_alpha = destination; }
  132. inline GLenum getDestinationAlpha() const { return _destination_factor_alpha; }
  133. virtual void apply(State& state) const;
  134. protected :
  135. virtual ~BlendFunc();
  136. GLenum _source_factor;
  137. GLenum _destination_factor;
  138. GLenum _source_factor_alpha;
  139. GLenum _destination_factor_alpha;
  140. };
  141. }
  142. #endif