texgen.c revision cdc920a0
1c1f859d4Smrg/*
2c1f859d4Smrg * Mesa 3-D graphics library
34a49301eSmrg * Version:  7.5
4c1f859d4Smrg *
5c1f859d4Smrg * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
64a49301eSmrg * Copyright (C) 2009  VMware, Inc.  All Rights Reserved.
7c1f859d4Smrg *
8c1f859d4Smrg * Permission is hereby granted, free of charge, to any person obtaining a
9c1f859d4Smrg * copy of this software and associated documentation files (the "Software"),
10c1f859d4Smrg * to deal in the Software without restriction, including without limitation
11c1f859d4Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12c1f859d4Smrg * and/or sell copies of the Software, and to permit persons to whom the
13c1f859d4Smrg * Software is furnished to do so, subject to the following conditions:
14c1f859d4Smrg *
15c1f859d4Smrg * The above copyright notice and this permission notice shall be included
16c1f859d4Smrg * in all copies or substantial portions of the Software.
17c1f859d4Smrg *
18c1f859d4Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19c1f859d4Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20c1f859d4Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
21c1f859d4Smrg * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22c1f859d4Smrg * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23c1f859d4Smrg * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24c1f859d4Smrg */
25c1f859d4Smrg
26c1f859d4Smrg/**
27c1f859d4Smrg * \file texgen.c
28c1f859d4Smrg *
29c1f859d4Smrg * glTexGen-related functions
30c1f859d4Smrg */
31c1f859d4Smrg
32c1f859d4Smrg
33c1f859d4Smrg#include "main/glheader.h"
34c1f859d4Smrg#include "main/context.h"
35c1f859d4Smrg#include "main/enums.h"
36c1f859d4Smrg#include "main/macros.h"
37c1f859d4Smrg#include "main/texgen.h"
384a49301eSmrg#include "main/texstate.h"
394a49301eSmrg#include "math/m_matrix.h"
40cdc920a0Smrg#include "main/dispatch.h"
41c1f859d4Smrg
42c1f859d4Smrg
434a49301eSmrg#if FEATURE_texgen
444a49301eSmrg
454a49301eSmrg
464a49301eSmrg/**
474a49301eSmrg * Return texgen state for given coordinate
484a49301eSmrg */
494a49301eSmrgstatic struct gl_texgen *
504a49301eSmrgget_texgen(struct gl_texture_unit *texUnit, GLenum coord)
514a49301eSmrg{
524a49301eSmrg   switch (coord) {
534a49301eSmrg   case GL_S:
544a49301eSmrg      return &texUnit->GenS;
554a49301eSmrg   case GL_T:
564a49301eSmrg      return &texUnit->GenT;
574a49301eSmrg   case GL_R:
584a49301eSmrg      return &texUnit->GenR;
594a49301eSmrg   case GL_Q:
604a49301eSmrg      return &texUnit->GenQ;
614a49301eSmrg   default:
624a49301eSmrg      return NULL;
634a49301eSmrg   }
644a49301eSmrg}
654a49301eSmrg
66c1f859d4Smrg
67c1f859d4Smrgvoid GLAPIENTRY
68c1f859d4Smrg_mesa_TexGenfv( GLenum coord, GLenum pname, const GLfloat *params )
69c1f859d4Smrg{
70c1f859d4Smrg   struct gl_texture_unit *texUnit;
714a49301eSmrg   struct gl_texgen *texgen;
724a49301eSmrg   GET_CURRENT_CONTEXT(ctx);
73c1f859d4Smrg   ASSERT_OUTSIDE_BEGIN_END(ctx);
74c1f859d4Smrg
75c1f859d4Smrg   if (MESA_VERBOSE&(VERBOSE_API|VERBOSE_TEXTURE))
76c1f859d4Smrg      _mesa_debug(ctx, "glTexGen %s %s %.1f(%s)...\n",
77c1f859d4Smrg                  _mesa_lookup_enum_by_nr(coord),
78c1f859d4Smrg                  _mesa_lookup_enum_by_nr(pname),
79c1f859d4Smrg                  *params,
80c1f859d4Smrg		  _mesa_lookup_enum_by_nr((GLenum) (GLint) *params));
81c1f859d4Smrg
82c1f859d4Smrg   if (ctx->Texture.CurrentUnit >= ctx->Const.MaxTextureCoordUnits) {
83c1f859d4Smrg      _mesa_error(ctx, GL_INVALID_OPERATION, "glTexGen(current unit)");
84c1f859d4Smrg      return;
85c1f859d4Smrg   }
86c1f859d4Smrg
874a49301eSmrg   texUnit = _mesa_get_current_tex_unit(ctx);
88c1f859d4Smrg
894a49301eSmrg   texgen = get_texgen(texUnit, coord);
904a49301eSmrg   if (!texgen) {
914a49301eSmrg      _mesa_error(ctx, GL_INVALID_ENUM, "glTexGen(coord)");
924a49301eSmrg      return;
934a49301eSmrg   }
944a49301eSmrg
954a49301eSmrg   switch (pname) {
964a49301eSmrg   case GL_TEXTURE_GEN_MODE:
974a49301eSmrg      {
984a49301eSmrg         GLenum mode = (GLenum) (GLint) params[0];
994a49301eSmrg         GLbitfield bit = 0x0;
1004a49301eSmrg         if (texgen->Mode == mode)
1014a49301eSmrg            return;
1024a49301eSmrg         switch (mode) {
1034a49301eSmrg         case GL_OBJECT_LINEAR:
1044a49301eSmrg            bit = TEXGEN_OBJ_LINEAR;
1054a49301eSmrg            break;
1064a49301eSmrg         case GL_EYE_LINEAR:
1074a49301eSmrg            bit = TEXGEN_EYE_LINEAR;
1084a49301eSmrg            break;
1094a49301eSmrg         case GL_SPHERE_MAP:
1104a49301eSmrg            if (coord == GL_S || coord == GL_T)
1114a49301eSmrg               bit = TEXGEN_SPHERE_MAP;
1124a49301eSmrg            break;
1134a49301eSmrg         case GL_REFLECTION_MAP_NV:
1144a49301eSmrg            if (coord != GL_Q)
1154a49301eSmrg               bit = TEXGEN_REFLECTION_MAP_NV;
1164a49301eSmrg            break;
1174a49301eSmrg         case GL_NORMAL_MAP_NV:
1184a49301eSmrg            if (coord != GL_Q)
1194a49301eSmrg               bit = TEXGEN_NORMAL_MAP_NV;
1204a49301eSmrg            break;
1214a49301eSmrg         default:
1224a49301eSmrg            ; /* nop */
1234a49301eSmrg         }
1244a49301eSmrg         if (!bit) {
1254a49301eSmrg            _mesa_error( ctx, GL_INVALID_ENUM, "glTexGenfv(param)" );
1264a49301eSmrg            return;
1274a49301eSmrg         }
1284a49301eSmrg         FLUSH_VERTICES(ctx, _NEW_TEXTURE);
1294a49301eSmrg         texgen->Mode = mode;
1304a49301eSmrg         texgen->_ModeBit = bit;
1314a49301eSmrg      }
1324a49301eSmrg      break;
1334a49301eSmrg
1344a49301eSmrg   case GL_OBJECT_PLANE:
1354a49301eSmrg      {
1364a49301eSmrg         if (TEST_EQ_4V(texgen->ObjectPlane, params))
1374a49301eSmrg            return;
1384a49301eSmrg         FLUSH_VERTICES(ctx, _NEW_TEXTURE);
1394a49301eSmrg         COPY_4FV(texgen->ObjectPlane, params);
1404a49301eSmrg      }
1414a49301eSmrg      break;
1424a49301eSmrg
1434a49301eSmrg   case GL_EYE_PLANE:
1444a49301eSmrg      {
1454a49301eSmrg         GLfloat tmp[4];
1464a49301eSmrg         /* Transform plane equation by the inverse modelview matrix */
1474a49301eSmrg         if (_math_matrix_is_dirty(ctx->ModelviewMatrixStack.Top)) {
1484a49301eSmrg            _math_matrix_analyse(ctx->ModelviewMatrixStack.Top);
1494a49301eSmrg         }
1504a49301eSmrg         _mesa_transform_vector(tmp, params,
1514a49301eSmrg                                ctx->ModelviewMatrixStack.Top->inv);
1524a49301eSmrg         if (TEST_EQ_4V(texgen->EyePlane, tmp))
1534a49301eSmrg            return;
1544a49301eSmrg         FLUSH_VERTICES(ctx, _NEW_TEXTURE);
1554a49301eSmrg         COPY_4FV(texgen->EyePlane, tmp);
1564a49301eSmrg      }
1574a49301eSmrg      break;
1584a49301eSmrg
1594a49301eSmrg   default:
1604a49301eSmrg      _mesa_error( ctx, GL_INVALID_ENUM, "glTexGenfv(pname)" );
1614a49301eSmrg      return;
162c1f859d4Smrg   }
163c1f859d4Smrg
164c1f859d4Smrg   if (ctx->Driver.TexGen)
165c1f859d4Smrg      ctx->Driver.TexGen( ctx, coord, pname, params );
166c1f859d4Smrg}
167c1f859d4Smrg
168c1f859d4Smrg
1694a49301eSmrgstatic void GLAPIENTRY
170c1f859d4Smrg_mesa_TexGeniv(GLenum coord, GLenum pname, const GLint *params )
171c1f859d4Smrg{
172c1f859d4Smrg   GLfloat p[4];
173c1f859d4Smrg   p[0] = (GLfloat) params[0];
174c1f859d4Smrg   if (pname == GL_TEXTURE_GEN_MODE) {
175c1f859d4Smrg      p[1] = p[2] = p[3] = 0.0F;
176c1f859d4Smrg   }
177c1f859d4Smrg   else {
178c1f859d4Smrg      p[1] = (GLfloat) params[1];
179c1f859d4Smrg      p[2] = (GLfloat) params[2];
180c1f859d4Smrg      p[3] = (GLfloat) params[3];
181c1f859d4Smrg   }
182c1f859d4Smrg   _mesa_TexGenfv(coord, pname, p);
183c1f859d4Smrg}
184c1f859d4Smrg
185c1f859d4Smrg
1864a49301eSmrgstatic void GLAPIENTRY
187c1f859d4Smrg_mesa_TexGend(GLenum coord, GLenum pname, GLdouble param )
188c1f859d4Smrg{
1894a49301eSmrg   GLfloat p[4];
1904a49301eSmrg   p[0] = (GLfloat) param;
1914a49301eSmrg   p[1] = p[2] = p[3] = 0.0F;
1924a49301eSmrg   _mesa_TexGenfv( coord, pname, p );
193c1f859d4Smrg}
194c1f859d4Smrg
195c1f859d4Smrg
1964a49301eSmrgstatic void GLAPIENTRY
197c1f859d4Smrg_mesa_TexGendv(GLenum coord, GLenum pname, const GLdouble *params )
198c1f859d4Smrg{
199c1f859d4Smrg   GLfloat p[4];
200c1f859d4Smrg   p[0] = (GLfloat) params[0];
201c1f859d4Smrg   if (pname == GL_TEXTURE_GEN_MODE) {
202c1f859d4Smrg      p[1] = p[2] = p[3] = 0.0F;
203c1f859d4Smrg   }
204c1f859d4Smrg   else {
205c1f859d4Smrg      p[1] = (GLfloat) params[1];
206c1f859d4Smrg      p[2] = (GLfloat) params[2];
207c1f859d4Smrg      p[3] = (GLfloat) params[3];
208c1f859d4Smrg   }
209c1f859d4Smrg   _mesa_TexGenfv( coord, pname, p );
210c1f859d4Smrg}
211c1f859d4Smrg
212c1f859d4Smrg
213cdc920a0Smrgvoid GLAPIENTRY
214c1f859d4Smrg_mesa_TexGenf( GLenum coord, GLenum pname, GLfloat param )
215c1f859d4Smrg{
2164a49301eSmrg   GLfloat p[4];
2174a49301eSmrg   p[0] = param;
2184a49301eSmrg   p[1] = p[2] = p[3] = 0.0F;
2194a49301eSmrg   _mesa_TexGenfv(coord, pname, p);
220c1f859d4Smrg}
221c1f859d4Smrg
222c1f859d4Smrg
223c1f859d4Smrgvoid GLAPIENTRY
224c1f859d4Smrg_mesa_TexGeni( GLenum coord, GLenum pname, GLint param )
225c1f859d4Smrg{
2264a49301eSmrg   GLint p[4];
2274a49301eSmrg   p[0] = param;
2284a49301eSmrg   p[1] = p[2] = p[3] = 0;
2294a49301eSmrg   _mesa_TexGeniv( coord, pname, p );
230c1f859d4Smrg}
231c1f859d4Smrg
232c1f859d4Smrg
233c1f859d4Smrg
2344a49301eSmrgstatic void GLAPIENTRY
235c1f859d4Smrg_mesa_GetTexGendv( GLenum coord, GLenum pname, GLdouble *params )
236c1f859d4Smrg{
2374a49301eSmrg   struct gl_texture_unit *texUnit;
2384a49301eSmrg   struct gl_texgen *texgen;
239c1f859d4Smrg   GET_CURRENT_CONTEXT(ctx);
240c1f859d4Smrg   ASSERT_OUTSIDE_BEGIN_END(ctx);
241c1f859d4Smrg
242c1f859d4Smrg   if (ctx->Texture.CurrentUnit >= ctx->Const.MaxTextureCoordUnits) {
243c1f859d4Smrg      _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexGendv(current unit)");
244c1f859d4Smrg      return;
245c1f859d4Smrg   }
246c1f859d4Smrg
2474a49301eSmrg   texUnit = _mesa_get_current_tex_unit(ctx);
248c1f859d4Smrg
2494a49301eSmrg   texgen = get_texgen(texUnit, coord);
2504a49301eSmrg   if (!texgen) {
2514a49301eSmrg      _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexGendv(coord)");
2524a49301eSmrg      return;
2534a49301eSmrg   }
2544a49301eSmrg
2554a49301eSmrg   switch (pname) {
2564a49301eSmrg   case GL_TEXTURE_GEN_MODE:
2574a49301eSmrg      params[0] = ENUM_TO_DOUBLE(texgen->Mode);
2584a49301eSmrg      break;
2594a49301eSmrg   case GL_OBJECT_PLANE:
2604a49301eSmrg      COPY_4V(params, texgen->ObjectPlane);
2614a49301eSmrg      break;
2624a49301eSmrg   case GL_EYE_PLANE:
2634a49301eSmrg      COPY_4V(params, texgen->EyePlane);
2644a49301eSmrg      break;
2654a49301eSmrg   default:
2664a49301eSmrg      _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexGendv(pname)" );
267c1f859d4Smrg   }
268c1f859d4Smrg}
269c1f859d4Smrg
270c1f859d4Smrg
271c1f859d4Smrg
272cdc920a0Smrgvoid GLAPIENTRY
273c1f859d4Smrg_mesa_GetTexGenfv( GLenum coord, GLenum pname, GLfloat *params )
274c1f859d4Smrg{
2754a49301eSmrg   struct gl_texture_unit *texUnit;
2764a49301eSmrg   struct gl_texgen *texgen;
277c1f859d4Smrg   GET_CURRENT_CONTEXT(ctx);
278c1f859d4Smrg   ASSERT_OUTSIDE_BEGIN_END(ctx);
279c1f859d4Smrg
280c1f859d4Smrg   if (ctx->Texture.CurrentUnit >= ctx->Const.MaxTextureCoordUnits) {
281c1f859d4Smrg      _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexGenfv(current unit)");
282c1f859d4Smrg      return;
283c1f859d4Smrg   }
284c1f859d4Smrg
2854a49301eSmrg   texUnit = _mesa_get_current_tex_unit(ctx);
286c1f859d4Smrg
2874a49301eSmrg   texgen = get_texgen(texUnit, coord);
2884a49301eSmrg   if (!texgen) {
2894a49301eSmrg      _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexGenfv(coord)");
2904a49301eSmrg      return;
2914a49301eSmrg   }
2924a49301eSmrg
2934a49301eSmrg   switch (pname) {
2944a49301eSmrg   case GL_TEXTURE_GEN_MODE:
2954a49301eSmrg      params[0] = ENUM_TO_FLOAT(texgen->Mode);
2964a49301eSmrg      break;
2974a49301eSmrg   case GL_OBJECT_PLANE:
2984a49301eSmrg      COPY_4V(params, texgen->ObjectPlane);
2994a49301eSmrg      break;
3004a49301eSmrg   case GL_EYE_PLANE:
3014a49301eSmrg      COPY_4V(params, texgen->EyePlane);
3024a49301eSmrg      break;
3034a49301eSmrg   default:
3044a49301eSmrg      _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexGenfv(pname)" );
305c1f859d4Smrg   }
306c1f859d4Smrg}
307c1f859d4Smrg
308c1f859d4Smrg
309c1f859d4Smrg
3104a49301eSmrgstatic void GLAPIENTRY
311c1f859d4Smrg_mesa_GetTexGeniv( GLenum coord, GLenum pname, GLint *params )
312c1f859d4Smrg{
3134a49301eSmrg   struct gl_texture_unit *texUnit;
3144a49301eSmrg   struct gl_texgen *texgen;
315c1f859d4Smrg   GET_CURRENT_CONTEXT(ctx);
316c1f859d4Smrg   ASSERT_OUTSIDE_BEGIN_END(ctx);
317c1f859d4Smrg
318c1f859d4Smrg   if (ctx->Texture.CurrentUnit >= ctx->Const.MaxTextureCoordUnits) {
319c1f859d4Smrg      _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexGeniv(current unit)");
320c1f859d4Smrg      return;
321c1f859d4Smrg   }
322c1f859d4Smrg
3234a49301eSmrg   texUnit = _mesa_get_current_tex_unit(ctx);
324c1f859d4Smrg
3254a49301eSmrg   texgen = get_texgen(texUnit, coord);
3264a49301eSmrg   if (!texgen) {
3274a49301eSmrg      _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexGeniv(coord)");
3284a49301eSmrg      return;
329c1f859d4Smrg   }
3304a49301eSmrg
3314a49301eSmrg   switch (pname) {
3324a49301eSmrg   case GL_TEXTURE_GEN_MODE:
3334a49301eSmrg      params[0] = texgen->Mode;
3344a49301eSmrg      break;
3354a49301eSmrg   case GL_OBJECT_PLANE:
3364a49301eSmrg      params[0] = (GLint) texgen->ObjectPlane[0];
3374a49301eSmrg      params[1] = (GLint) texgen->ObjectPlane[1];
3384a49301eSmrg      params[2] = (GLint) texgen->ObjectPlane[2];
3394a49301eSmrg      params[3] = (GLint) texgen->ObjectPlane[3];
3404a49301eSmrg      break;
3414a49301eSmrg   case GL_EYE_PLANE:
3424a49301eSmrg      params[0] = (GLint) texgen->EyePlane[0];
3434a49301eSmrg      params[1] = (GLint) texgen->EyePlane[1];
3444a49301eSmrg      params[2] = (GLint) texgen->EyePlane[2];
3454a49301eSmrg      params[3] = (GLint) texgen->EyePlane[3];
3464a49301eSmrg      break;
3474a49301eSmrg   default:
3484a49301eSmrg      _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexGeniv(pname)" );
3494a49301eSmrg   }
3504a49301eSmrg}
3514a49301eSmrg
3524a49301eSmrg
3534a49301eSmrgvoid
3544a49301eSmrg_mesa_init_texgen_dispatch(struct _glapi_table *disp)
3554a49301eSmrg{
3564a49301eSmrg   SET_GetTexGendv(disp, _mesa_GetTexGendv);
3574a49301eSmrg   SET_GetTexGenfv(disp, _mesa_GetTexGenfv);
3584a49301eSmrg   SET_GetTexGeniv(disp, _mesa_GetTexGeniv);
3594a49301eSmrg   SET_TexGend(disp, _mesa_TexGend);
3604a49301eSmrg   SET_TexGendv(disp, _mesa_TexGendv);
3614a49301eSmrg   SET_TexGenf(disp, _mesa_TexGenf);
3624a49301eSmrg   SET_TexGenfv(disp, _mesa_TexGenfv);
3634a49301eSmrg   SET_TexGeni(disp, _mesa_TexGeni);
3644a49301eSmrg   SET_TexGeniv(disp, _mesa_TexGeniv);
365c1f859d4Smrg}
366c1f859d4Smrg
367c1f859d4Smrg
3684a49301eSmrg#endif /* FEATURE_texgen */
369