2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
25 * Keith Whitwell <keith@tungstengraphics.com>
29 #include "main/glheader.h"
30 #include "main/colormac.h"
31 #include "main/macros.h"
32 #include "main/imports.h"
33 #include "main/mtypes.h"
35 #include "math/m_xform.h"
37 #include "t_context.h"
38 #include "t_pipeline.h"
41 struct normal_stage_data {
42 normal_func NormalTransform;
46 #define NORMAL_STAGE_DATA(stage) ((struct normal_stage_data *)stage->privatePtr)
50 run_normal_stage(struct gl_context *ctx, struct tnl_pipeline_stage *stage)
52 struct normal_stage_data *store = NORMAL_STAGE_DATA(stage);
53 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
54 const GLfloat *lengths;
56 if (!store->NormalTransform)
59 /* We can only use the display list's saved normal lengths if we've
60 * got a transformation matrix with uniform scaling.
62 if (_math_matrix_is_general_scale(ctx->ModelviewMatrixStack.Top))
65 lengths = VB->NormalLengthPtr;
67 store->NormalTransform( ctx->ModelviewMatrixStack.Top,
68 ctx->_ModelViewInvScale,
69 VB->AttribPtr[_TNL_ATTRIB_NORMAL], /* input normals */
71 &store->normal ); /* resulting normals */
73 if (VB->AttribPtr[_TNL_ATTRIB_NORMAL]->count > 1) {
74 store->normal.stride = 4 * sizeof(GLfloat);
77 store->normal.stride = 0;
80 VB->AttribPtr[_TNL_ATTRIB_NORMAL] = &store->normal;
82 VB->NormalLengthPtr = NULL; /* no longer valid */
88 * Examine current GL state and set the store->NormalTransform pointer
89 * to point to the appropriate normal transformation routine.
92 validate_normal_stage(struct gl_context *ctx, struct tnl_pipeline_stage *stage)
94 struct normal_stage_data *store = NORMAL_STAGE_DATA(stage);
96 if (ctx->VertexProgram._Current ||
97 (!ctx->Light.Enabled &&
98 !(ctx->Texture._GenFlags & TEXGEN_NEED_NORMALS))) {
99 store->NormalTransform = NULL;
103 if (ctx->_NeedEyeCoords) {
104 /* Eye coordinates are needed, for whatever reasons.
105 * Do lighting in eye coordinates, as the GL spec says.
107 GLuint transform = NORM_TRANSFORM_NO_ROT;
109 if (_math_matrix_has_rotation(ctx->ModelviewMatrixStack.Top)) {
110 /* need to do full (3x3) matrix transform */
111 transform = NORM_TRANSFORM;
114 if (ctx->Transform.Normalize) {
115 store->NormalTransform = _mesa_normal_tab[transform | NORM_NORMALIZE];
117 else if (ctx->Transform.RescaleNormals &&
118 ctx->_ModelViewInvScale != 1.0) {
119 store->NormalTransform = _mesa_normal_tab[transform | NORM_RESCALE];
122 store->NormalTransform = _mesa_normal_tab[transform];
126 /* We don't need eye coordinates.
127 * Do lighting in object coordinates. Thus, we don't need to fully
128 * transform normal vectors (just leave them in object coordinates)
129 * but we still need to do normalization/rescaling if enabled.
131 if (ctx->Transform.Normalize) {
132 store->NormalTransform = _mesa_normal_tab[NORM_NORMALIZE];
134 else if (!ctx->Transform.RescaleNormals &&
135 ctx->_ModelViewInvScale != 1.0) {
136 store->NormalTransform = _mesa_normal_tab[NORM_RESCALE];
139 store->NormalTransform = NULL;
146 * Allocate stage's private data (storage for transformed normals).
149 alloc_normal_data(struct gl_context *ctx, struct tnl_pipeline_stage *stage)
151 TNLcontext *tnl = TNL_CONTEXT(ctx);
152 struct normal_stage_data *store;
154 stage->privatePtr = malloc(sizeof(*store));
155 store = NORMAL_STAGE_DATA(stage);
159 _mesa_vector4f_alloc( &store->normal, 0, tnl->vb.Size, 32 );
165 * Free stage's private data.
168 free_normal_data(struct tnl_pipeline_stage *stage)
170 struct normal_stage_data *store = NORMAL_STAGE_DATA(stage);
172 _mesa_vector4f_free( &store->normal );
174 stage->privatePtr = NULL;
179 const struct tnl_pipeline_stage _tnl_normal_transform_stage =
181 "normal transform", /* name */
182 NULL, /* privatePtr */
183 alloc_normal_data, /* create */
184 free_normal_data, /* destroy */
185 validate_normal_stage, /* validate */
186 run_normal_stage /* run */