[PROPSYS]
[reactos.git] / reactos / dll / opengl / mesa / main / texgen.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.5
4 *
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 /**
27 * \file texgen.c
28 *
29 * glTexGen-related functions
30 */
31
32 #include <precomp.h>
33
34 #if FEATURE_texgen
35
36
37 /**
38 * Return texgen state for given coordinate
39 */
40 static struct gl_texgen *
41 get_texgen(struct gl_texture_unit *texUnit, GLenum coord)
42 {
43 switch (coord) {
44 case GL_S:
45 return &texUnit->GenS;
46 case GL_T:
47 return &texUnit->GenT;
48 case GL_R:
49 return &texUnit->GenR;
50 case GL_Q:
51 return &texUnit->GenQ;
52 default:
53 return NULL;
54 }
55 }
56
57
58 void GLAPIENTRY
59 _mesa_TexGenfv( GLenum coord, GLenum pname, const GLfloat *params )
60 {
61 struct gl_texture_unit *texUnit;
62 struct gl_texgen *texgen;
63 GET_CURRENT_CONTEXT(ctx);
64 ASSERT_OUTSIDE_BEGIN_END(ctx);
65
66 if (MESA_VERBOSE&(VERBOSE_API|VERBOSE_TEXTURE))
67 _mesa_debug(ctx, "glTexGen %s %s %.1f(%s)...\n",
68 _mesa_lookup_enum_by_nr(coord),
69 _mesa_lookup_enum_by_nr(pname),
70 *params,
71 _mesa_lookup_enum_by_nr((GLenum) (GLint) *params));
72
73 texUnit = &ctx->Texture.Unit;
74
75 texgen = get_texgen(texUnit, coord);
76 if (!texgen) {
77 _mesa_error(ctx, GL_INVALID_ENUM, "glTexGen(coord)");
78 return;
79 }
80
81 switch (pname) {
82 case GL_TEXTURE_GEN_MODE:
83 {
84 GLenum mode = (GLenum) (GLint) params[0];
85 GLbitfield bit = 0x0;
86 if (texgen->Mode == mode)
87 return;
88 switch (mode) {
89 case GL_OBJECT_LINEAR:
90 bit = TEXGEN_OBJ_LINEAR;
91 break;
92 case GL_EYE_LINEAR:
93 bit = TEXGEN_EYE_LINEAR;
94 break;
95 case GL_SPHERE_MAP:
96 if (coord == GL_S || coord == GL_T)
97 bit = TEXGEN_SPHERE_MAP;
98 break;
99 case GL_REFLECTION_MAP_NV:
100 if (coord != GL_Q)
101 bit = TEXGEN_REFLECTION_MAP_NV;
102 break;
103 case GL_NORMAL_MAP_NV:
104 if (coord != GL_Q)
105 bit = TEXGEN_NORMAL_MAP_NV;
106 break;
107 default:
108 ; /* nop */
109 }
110 if (!bit) {
111 _mesa_error( ctx, GL_INVALID_ENUM, "glTexGenfv(param)" );
112 return;
113 }
114 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
115 texgen->Mode = mode;
116 texgen->_ModeBit = bit;
117 }
118 break;
119
120 case GL_OBJECT_PLANE:
121 {
122 if (TEST_EQ_4V(texgen->ObjectPlane, params))
123 return;
124 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
125 COPY_4FV(texgen->ObjectPlane, params);
126 }
127 break;
128
129 case GL_EYE_PLANE:
130 {
131 GLfloat tmp[4];
132 /* Transform plane equation by the inverse modelview matrix */
133 if (_math_matrix_is_dirty(ctx->ModelviewMatrixStack.Top)) {
134 _math_matrix_analyse(ctx->ModelviewMatrixStack.Top);
135 }
136 _mesa_transform_vector(tmp, params,
137 ctx->ModelviewMatrixStack.Top->inv);
138 if (TEST_EQ_4V(texgen->EyePlane, tmp))
139 return;
140 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
141 COPY_4FV(texgen->EyePlane, tmp);
142 }
143 break;
144
145 default:
146 _mesa_error( ctx, GL_INVALID_ENUM, "glTexGenfv(pname)" );
147 return;
148 }
149
150 if (ctx->Driver.TexGen)
151 ctx->Driver.TexGen( ctx, coord, pname, params );
152 }
153
154
155 static void GLAPIENTRY
156 _mesa_TexGeniv(GLenum coord, GLenum pname, const GLint *params )
157 {
158 GLfloat p[4];
159 p[0] = (GLfloat) params[0];
160 if (pname == GL_TEXTURE_GEN_MODE) {
161 p[1] = p[2] = p[3] = 0.0F;
162 }
163 else {
164 p[1] = (GLfloat) params[1];
165 p[2] = (GLfloat) params[2];
166 p[3] = (GLfloat) params[3];
167 }
168 _mesa_TexGenfv(coord, pname, p);
169 }
170
171
172 static void GLAPIENTRY
173 _mesa_TexGend(GLenum coord, GLenum pname, GLdouble param )
174 {
175 GLfloat p[4];
176 p[0] = (GLfloat) param;
177 p[1] = p[2] = p[3] = 0.0F;
178 _mesa_TexGenfv( coord, pname, p );
179 }
180
181 #if FEATURE_ES1
182
183 void GLAPIENTRY
184 _es_GetTexGenfv(GLenum coord, GLenum pname, GLfloat *params)
185 {
186 ASSERT(coord == GL_TEXTURE_GEN_STR_OES);
187 _mesa_GetTexGenfv(GL_S, pname, params);
188 }
189
190
191 void GLAPIENTRY
192 _es_TexGenf(GLenum coord, GLenum pname, GLfloat param)
193 {
194 ASSERT(coord == GL_TEXTURE_GEN_STR_OES);
195 /* set S, T, and R at the same time */
196 _mesa_TexGenf(GL_S, pname, param);
197 _mesa_TexGenf(GL_T, pname, param);
198 _mesa_TexGenf(GL_R, pname, param);
199 }
200
201
202 void GLAPIENTRY
203 _es_TexGenfv(GLenum coord, GLenum pname, const GLfloat *params)
204 {
205 ASSERT(coord == GL_TEXTURE_GEN_STR_OES);
206 /* set S, T, and R at the same time */
207 _mesa_TexGenfv(GL_S, pname, params);
208 _mesa_TexGenfv(GL_T, pname, params);
209 _mesa_TexGenfv(GL_R, pname, params);
210 }
211
212 #endif
213
214 static void GLAPIENTRY
215 _mesa_TexGendv(GLenum coord, GLenum pname, const GLdouble *params )
216 {
217 GLfloat p[4];
218 p[0] = (GLfloat) params[0];
219 if (pname == GL_TEXTURE_GEN_MODE) {
220 p[1] = p[2] = p[3] = 0.0F;
221 }
222 else {
223 p[1] = (GLfloat) params[1];
224 p[2] = (GLfloat) params[2];
225 p[3] = (GLfloat) params[3];
226 }
227 _mesa_TexGenfv( coord, pname, p );
228 }
229
230
231 void GLAPIENTRY
232 _mesa_TexGenf( GLenum coord, GLenum pname, GLfloat param )
233 {
234 GLfloat p[4];
235 p[0] = param;
236 p[1] = p[2] = p[3] = 0.0F;
237 _mesa_TexGenfv(coord, pname, p);
238 }
239
240
241 void GLAPIENTRY
242 _mesa_TexGeni( GLenum coord, GLenum pname, GLint param )
243 {
244 GLint p[4];
245 p[0] = param;
246 p[1] = p[2] = p[3] = 0;
247 _mesa_TexGeniv( coord, pname, p );
248 }
249
250
251
252 static void GLAPIENTRY
253 _mesa_GetTexGendv( GLenum coord, GLenum pname, GLdouble *params )
254 {
255 struct gl_texture_unit *texUnit;
256 struct gl_texgen *texgen;
257 GET_CURRENT_CONTEXT(ctx);
258 ASSERT_OUTSIDE_BEGIN_END(ctx);
259
260 texUnit = &ctx->Texture.Unit;
261
262 texgen = get_texgen(texUnit, coord);
263 if (!texgen) {
264 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexGendv(coord)");
265 return;
266 }
267
268 switch (pname) {
269 case GL_TEXTURE_GEN_MODE:
270 params[0] = ENUM_TO_DOUBLE(texgen->Mode);
271 break;
272 case GL_OBJECT_PLANE:
273 COPY_4V(params, texgen->ObjectPlane);
274 break;
275 case GL_EYE_PLANE:
276 COPY_4V(params, texgen->EyePlane);
277 break;
278 default:
279 _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexGendv(pname)" );
280 }
281 }
282
283
284
285 void GLAPIENTRY
286 _mesa_GetTexGenfv( GLenum coord, GLenum pname, GLfloat *params )
287 {
288 struct gl_texture_unit *texUnit;
289 struct gl_texgen *texgen;
290 GET_CURRENT_CONTEXT(ctx);
291 ASSERT_OUTSIDE_BEGIN_END(ctx);
292
293 texUnit = &ctx->Texture.Unit;
294
295 texgen = get_texgen(texUnit, coord);
296 if (!texgen) {
297 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexGenfv(coord)");
298 return;
299 }
300
301 switch (pname) {
302 case GL_TEXTURE_GEN_MODE:
303 params[0] = ENUM_TO_FLOAT(texgen->Mode);
304 break;
305 case GL_OBJECT_PLANE:
306 COPY_4V(params, texgen->ObjectPlane);
307 break;
308 case GL_EYE_PLANE:
309 COPY_4V(params, texgen->EyePlane);
310 break;
311 default:
312 _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexGenfv(pname)" );
313 }
314 }
315
316
317
318 static void GLAPIENTRY
319 _mesa_GetTexGeniv( GLenum coord, GLenum pname, GLint *params )
320 {
321 struct gl_texture_unit *texUnit;
322 struct gl_texgen *texgen;
323 GET_CURRENT_CONTEXT(ctx);
324 ASSERT_OUTSIDE_BEGIN_END(ctx);
325
326 texUnit = &ctx->Texture.Unit;
327
328 texgen = get_texgen(texUnit, coord);
329 if (!texgen) {
330 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexGeniv(coord)");
331 return;
332 }
333
334 switch (pname) {
335 case GL_TEXTURE_GEN_MODE:
336 params[0] = texgen->Mode;
337 break;
338 case GL_OBJECT_PLANE:
339 params[0] = (GLint) texgen->ObjectPlane[0];
340 params[1] = (GLint) texgen->ObjectPlane[1];
341 params[2] = (GLint) texgen->ObjectPlane[2];
342 params[3] = (GLint) texgen->ObjectPlane[3];
343 break;
344 case GL_EYE_PLANE:
345 params[0] = (GLint) texgen->EyePlane[0];
346 params[1] = (GLint) texgen->EyePlane[1];
347 params[2] = (GLint) texgen->EyePlane[2];
348 params[3] = (GLint) texgen->EyePlane[3];
349 break;
350 default:
351 _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexGeniv(pname)" );
352 }
353 }
354
355
356 void
357 _mesa_init_texgen_dispatch(struct _glapi_table *disp)
358 {
359 SET_GetTexGendv(disp, _mesa_GetTexGendv);
360 SET_GetTexGenfv(disp, _mesa_GetTexGenfv);
361 SET_GetTexGeniv(disp, _mesa_GetTexGeniv);
362 SET_TexGend(disp, _mesa_TexGend);
363 SET_TexGendv(disp, _mesa_TexGendv);
364 SET_TexGenf(disp, _mesa_TexGenf);
365 SET_TexGenfv(disp, _mesa_TexGenfv);
366 SET_TexGeni(disp, _mesa_TexGeni);
367 SET_TexGeniv(disp, _mesa_TexGeniv);
368 }
369
370
371 #endif /* FEATURE_texgen */