openscenegraph
TexEnvCombine
Go to the documentation of this file.
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
14#ifndef OSG_TEXENVCOMBINE
15#define OSG_TEXENVCOMBINE 1
16
17#include <osg/TexEnv>
18#include <osg/Vec3>
19#include <osg/Vec4>
20
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
46#endif
47
48#ifndef GL_ARB_texture_env_dot3
49#define GL_DOT3_RGB_ARB 0x86AE
50#define GL_DOT3_RGBA_ARB 0x86AF
51#endif
52
53#ifndef GL_TEXTURE0
54#define GL_TEXTURE0 0x84C0
55#endif
56
57namespace osg {
58
59/** TexEnvCombine encapsulates the OpenGL glTexEnvCombine (texture
60 * environment) state. */
61class OSG_EXPORT TexEnvCombine : public StateAttribute
62{
63 public :
64
65 TexEnvCombine();
66
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) {}
88
89
90 META_StateAttribute(osg, TexEnvCombine, TEXENV);
91
92
93 virtual bool isTextureAttribute() const { return true; }
94
95 /** Return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */
96 virtual int compare(const StateAttribute& sa) const
97 {
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)
101
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)
121
122 return 0; // Passed all the above comparison macros, so must be equal.
123 }
124
125
126 enum CombineParam
127 {
128 REPLACE = GL_REPLACE,
129 MODULATE = GL_MODULATE,
130 ADD = GL_ADD,
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
136 };
137
138 void setCombine_RGB(GLint cm);
139 void setCombine_Alpha(GLint cm);
140
141 GLint getCombine_RGB() const { return _combine_RGB; }
142 GLint getCombine_Alpha() const { return _combine_Alpha; }
143
144 enum SourceParam
145 {
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
158 };
159
160 void setSource0_RGB(GLint sp);
161 void setSource1_RGB(GLint sp);
162 void setSource2_RGB(GLint sp);
163
164 void setSource0_Alpha(GLint sp);
165 void setSource1_Alpha(GLint sp);
166 void setSource2_Alpha(GLint sp);
167
168 GLint getSource0_RGB() const { return _source0_RGB; }
169 GLint getSource1_RGB() const { return _source1_RGB; }
170 GLint getSource2_RGB() const { return _source2_RGB; }
171
172 GLint getSource0_Alpha() const { return _source0_Alpha; }
173 GLint getSource1_Alpha() const { return _source1_Alpha; }
174 GLint getSource2_Alpha() const { return _source2_Alpha; }
175
176 enum OperandParam
177 {
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
182 };
183
184 void setOperand0_RGB(GLint op);
185 void setOperand1_RGB(GLint op);
186 void setOperand2_RGB(GLint op);
187
188 void setOperand0_Alpha(GLint op);
189 void setOperand1_Alpha(GLint op);
190 void setOperand2_Alpha(GLint op);
191
192 GLint getOperand0_RGB() const { return _operand0_RGB; }
193 GLint getOperand1_RGB() const { return _operand1_RGB; }
194 GLint getOperand2_RGB() const { return _operand2_RGB; }
195
196 GLint getOperand0_Alpha() const { return _operand0_Alpha; }
197 GLint getOperand1_Alpha() const { return _operand1_Alpha; }
198 GLint getOperand2_Alpha() const { return _operand2_Alpha; }
199
200
201 void setScale_RGB(float scale);
202 void setScale_Alpha(float scale);
203
204 float getScale_RGB() const { return _scale_RGB; }
205 float getScale_Alpha() const { return _scale_Alpha; }
206
207 void setConstantColor( const Vec4& color ) { _constantColor = color; }
208 const Vec4& getConstantColor() const { return _constantColor; }
209
210 /** Set the constant color attribute to the given light direction
211 * for use with DOT3 combine operation. */
212 void setConstantColorAsLightDirection(const Vec3& direction)
213 {
214 _constantColor.set((direction.x()+1.0f)*0.5f,(direction.y()+1.0f)*0.5f,(direction.z()+1.0f)*0.5f,1.0f);
215 }
216
217 Vec3 getConstantColorAsLightDirection() const
218 {
219 return Vec3(_constantColor.x()*2.0f-1.0f, _constantColor.y()*2.0f-1.0f, _constantColor.z()*2.0f-1.0f);
220 }
221
222 virtual void apply(State& state) const;
223
224 protected :
225
226 virtual ~TexEnvCombine();
227
228 inline bool needsTexEnvCombiner(GLint value) const
229 {
230 switch(value)
231 {
232 case(CONSTANT):
233 case(PRIMARY_COLOR):
234 case(PREVIOUS):
235 case(TEXTURE):
236 return false;
237 }
238 return true;
239 }
240
241 void computeNeedForTexEnvCombiners()
242 {
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));
249 }
250
251
252
253
254
255 bool _needsTexEnvCrossbar;
256
257 GLint _combine_RGB;
258 GLint _combine_Alpha;
259
260 GLint _source0_RGB;
261 GLint _source1_RGB;
262 GLint _source2_RGB;
263
264 GLint _source0_Alpha;
265 GLint _source1_Alpha;
266 GLint _source2_Alpha;
267
268
269 GLint _operand0_RGB;
270 GLint _operand1_RGB;
271 GLint _operand2_RGB;
272
273 GLint _operand0_Alpha;
274 GLint _operand1_Alpha;
275 GLint _operand2_Alpha;
276
277
278 float _scale_RGB;
279 float _scale_Alpha;
280
281 osg::Vec4 _constantColor;
282
283};
284
285}
286
287#endif