texgen.c revision b82757880545f8bce471ba8f13c16998888cd4b5
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
33#include "main/glheader.h"
34#include "main/context.h"
35#include "main/enums.h"
36#include "main/macros.h"
37#include "main/texgen.h"
38#include "math/m_matrix.h"
39
40
41/**
42 * Return texgen state for given coordinate
43 */
44static struct gl_texgen *
45get_texgen(struct gl_texture_unit *texUnit, GLenum coord)
46{
47   switch (coord) {
48   case GL_S:
49      return &texUnit->GenS;
50   case GL_T:
51      return &texUnit->GenT;
52   case GL_R:
53      return &texUnit->GenR;
54   case GL_Q:
55      return &texUnit->GenQ;
56   default:
57      return NULL;
58   }
59}
60
61
62void GLAPIENTRY
63_mesa_TexGenfv( GLenum coord, GLenum pname, const GLfloat *params )
64{
65   struct gl_texture_unit *texUnit;
66   struct gl_texgen *texgen;
67   GET_CURRENT_CONTEXT(ctx);
68   ASSERT_OUTSIDE_BEGIN_END(ctx);
69
70   if (MESA_VERBOSE&(VERBOSE_API|VERBOSE_TEXTURE))
71      _mesa_debug(ctx, "glTexGen %s %s %.1f(%s)...\n",
72                  _mesa_lookup_enum_by_nr(coord),
73                  _mesa_lookup_enum_by_nr(pname),
74                  *params,
75		  _mesa_lookup_enum_by_nr((GLenum) (GLint) *params));
76
77   if (ctx->Texture.CurrentUnit >= ctx->Const.MaxTextureCoordUnits) {
78      _mesa_error(ctx, GL_INVALID_OPERATION, "glTexGen(current unit)");
79      return;
80   }
81
82   texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
83
84   texgen = get_texgen(texUnit, coord);
85   if (!texgen) {
86      _mesa_error(ctx, GL_INVALID_ENUM, "glTexGen(coord)");
87      return;
88   }
89
90   switch (pname) {
91   case GL_TEXTURE_GEN_MODE:
92      {
93         GLenum mode = (GLenum) (GLint) params[0];
94         GLbitfield bit = 0x0;
95         if (texgen->Mode == mode)
96            return;
97         switch (mode) {
98         case GL_OBJECT_LINEAR:
99            bit = TEXGEN_OBJ_LINEAR;
100            break;
101         case GL_EYE_LINEAR:
102            bit = TEXGEN_EYE_LINEAR;
103            break;
104         case GL_SPHERE_MAP:
105            if (coord == GL_S || coord == GL_T)
106               bit = TEXGEN_SPHERE_MAP;
107            break;
108         case GL_REFLECTION_MAP_NV:
109            if (coord != GL_Q)
110               bit = TEXGEN_REFLECTION_MAP_NV;
111            break;
112         case GL_NORMAL_MAP_NV:
113            if (coord != GL_Q)
114               bit = TEXGEN_NORMAL_MAP_NV;
115            break;
116         default:
117            ; /* nop */
118         }
119         if (!bit) {
120            _mesa_error( ctx, GL_INVALID_ENUM, "glTexGenfv(param)" );
121            return;
122         }
123         FLUSH_VERTICES(ctx, _NEW_TEXTURE);
124         texgen->Mode = mode;
125         texgen->_ModeBit = bit;
126      }
127      break;
128
129   case GL_OBJECT_PLANE:
130      {
131         if (TEST_EQ_4V(texgen->ObjectPlane, params))
132            return;
133         FLUSH_VERTICES(ctx, _NEW_TEXTURE);
134         COPY_4FV(texgen->ObjectPlane, params);
135      }
136      break;
137
138   case GL_EYE_PLANE:
139      {
140         GLfloat tmp[4];
141         /* Transform plane equation by the inverse modelview matrix */
142         if (_math_matrix_is_dirty(ctx->ModelviewMatrixStack.Top)) {
143            _math_matrix_analyse(ctx->ModelviewMatrixStack.Top);
144         }
145         _mesa_transform_vector(tmp, params,
146                                ctx->ModelviewMatrixStack.Top->inv);
147         if (TEST_EQ_4V(texgen->EyePlane, tmp))
148            return;
149         FLUSH_VERTICES(ctx, _NEW_TEXTURE);
150         COPY_4FV(texgen->EyePlane, tmp);
151      }
152      break;
153
154   default:
155      _mesa_error( ctx, GL_INVALID_ENUM, "glTexGenfv(pname)" );
156      return;
157   }
158
159   if (ctx->Driver.TexGen)
160      ctx->Driver.TexGen( ctx, coord, pname, params );
161}
162
163
164void GLAPIENTRY
165_mesa_TexGeniv(GLenum coord, GLenum pname, const GLint *params )
166{
167   GLfloat p[4];
168   p[0] = (GLfloat) params[0];
169   if (pname == GL_TEXTURE_GEN_MODE) {
170      p[1] = p[2] = p[3] = 0.0F;
171   }
172   else {
173      p[1] = (GLfloat) params[1];
174      p[2] = (GLfloat) params[2];
175      p[3] = (GLfloat) params[3];
176   }
177   _mesa_TexGenfv(coord, pname, p);
178}
179
180
181void GLAPIENTRY
182_mesa_TexGend(GLenum coord, GLenum pname, GLdouble param )
183{
184   GLfloat p[4];
185   p[0] = (GLfloat) param;
186   p[1] = p[2] = p[3] = 0.0F;
187   _mesa_TexGenfv( coord, pname, p );
188}
189
190
191void GLAPIENTRY
192_mesa_TexGendv(GLenum coord, GLenum pname, const GLdouble *params )
193{
194   GLfloat p[4];
195   p[0] = (GLfloat) params[0];
196   if (pname == GL_TEXTURE_GEN_MODE) {
197      p[1] = p[2] = p[3] = 0.0F;
198   }
199   else {
200      p[1] = (GLfloat) params[1];
201      p[2] = (GLfloat) params[2];
202      p[3] = (GLfloat) params[3];
203   }
204   _mesa_TexGenfv( coord, pname, p );
205}
206
207
208void GLAPIENTRY
209_mesa_TexGenf( GLenum coord, GLenum pname, GLfloat param )
210{
211   GLfloat p[4];
212   p[0] = param;
213   p[1] = p[2] = p[3] = 0.0F;
214   _mesa_TexGenfv(coord, pname, p);
215}
216
217
218void GLAPIENTRY
219_mesa_TexGeni( GLenum coord, GLenum pname, GLint param )
220{
221   GLint p[4];
222   p[0] = param;
223   p[1] = p[2] = p[3] = 0;
224   _mesa_TexGeniv( coord, pname, p );
225}
226
227
228
229void GLAPIENTRY
230_mesa_GetTexGendv( GLenum coord, GLenum pname, GLdouble *params )
231{
232   struct gl_texture_unit *texUnit;
233   struct gl_texgen *texgen;
234   GET_CURRENT_CONTEXT(ctx);
235   ASSERT_OUTSIDE_BEGIN_END(ctx);
236
237   if (ctx->Texture.CurrentUnit >= ctx->Const.MaxTextureCoordUnits) {
238      _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexGendv(current unit)");
239      return;
240   }
241
242   texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
243
244   texgen = get_texgen(texUnit, coord);
245   if (!texgen) {
246      _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexGendv(coord)");
247      return;
248   }
249
250   switch (pname) {
251   case GL_TEXTURE_GEN_MODE:
252      params[0] = ENUM_TO_DOUBLE(texgen->Mode);
253      break;
254   case GL_OBJECT_PLANE:
255      COPY_4V(params, texgen->ObjectPlane);
256      break;
257   case GL_EYE_PLANE:
258      COPY_4V(params, texgen->EyePlane);
259      break;
260   default:
261      _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexGendv(pname)" );
262   }
263}
264
265
266
267void GLAPIENTRY
268_mesa_GetTexGenfv( GLenum coord, GLenum pname, GLfloat *params )
269{
270   struct gl_texture_unit *texUnit;
271   struct gl_texgen *texgen;
272   GET_CURRENT_CONTEXT(ctx);
273   ASSERT_OUTSIDE_BEGIN_END(ctx);
274
275   if (ctx->Texture.CurrentUnit >= ctx->Const.MaxTextureCoordUnits) {
276      _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexGenfv(current unit)");
277      return;
278   }
279
280   texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
281
282   texgen = get_texgen(texUnit, coord);
283   if (!texgen) {
284      _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexGenfv(coord)");
285      return;
286   }
287
288   switch (pname) {
289   case GL_TEXTURE_GEN_MODE:
290      params[0] = ENUM_TO_FLOAT(texgen->Mode);
291      break;
292   case GL_OBJECT_PLANE:
293      COPY_4V(params, texgen->ObjectPlane);
294      break;
295   case GL_EYE_PLANE:
296      COPY_4V(params, texgen->EyePlane);
297      break;
298   default:
299      _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexGenfv(pname)" );
300   }
301}
302
303
304
305void GLAPIENTRY
306_mesa_GetTexGeniv( GLenum coord, GLenum pname, GLint *params )
307{
308   struct gl_texture_unit *texUnit;
309   struct gl_texgen *texgen;
310   GET_CURRENT_CONTEXT(ctx);
311   ASSERT_OUTSIDE_BEGIN_END(ctx);
312
313   if (ctx->Texture.CurrentUnit >= ctx->Const.MaxTextureCoordUnits) {
314      _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexGeniv(current unit)");
315      return;
316   }
317
318   texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
319
320   texgen = get_texgen(texUnit, coord);
321   if (!texgen) {
322      _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexGeniv(coord)");
323      return;
324   }
325
326   switch (pname) {
327   case GL_TEXTURE_GEN_MODE:
328      params[0] = texgen->Mode;
329      break;
330   case GL_OBJECT_PLANE:
331      params[0] = (GLint) texgen->ObjectPlane[0];
332      params[1] = (GLint) texgen->ObjectPlane[1];
333      params[2] = (GLint) texgen->ObjectPlane[2];
334      params[3] = (GLint) texgen->ObjectPlane[3];
335      break;
336   case GL_EYE_PLANE:
337      params[0] = (GLint) texgen->EyePlane[0];
338      params[1] = (GLint) texgen->EyePlane[1];
339      params[2] = (GLint) texgen->EyePlane[2];
340      params[3] = (GLint) texgen->EyePlane[3];
341      break;
342   default:
343      _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexGeniv(pname)" );
344   }
345}
346
347
348