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