1 /* $Id: light.c,v 1.12 2000/01/31 23:33:53 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2000 Brian Paul All Rights Reserved.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39 #include "simple_list.h"
46 /* XXX this is a bit of a hack needed for compilation within XFree86 */
53 _mesa_ShadeModel( GLenum mode
)
55 GET_CURRENT_CONTEXT(ctx
);
56 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glShadeModel");
58 if (MESA_VERBOSE
& VERBOSE_API
)
59 fprintf(stderr
, "glShadeModel %s\n", gl_lookup_enum_by_nr(mode
));
61 if (mode
== GL_FLAT
|| mode
== GL_SMOOTH
) {
62 if (ctx
->Light
.ShadeModel
!= mode
) {
63 ctx
->Light
.ShadeModel
= mode
;
64 if (ctx
->Light
.ShadeModel
== GL_FLAT
)
65 SET_BITS(ctx
->TriangleCaps
, DD_FLATSHADE
);
67 CLEAR_BITS(ctx
->TriangleCaps
, DD_FLATSHADE
);
68 ctx
->NewState
|= NEW_RASTER_OPS
;
69 if (ctx
->Driver
.ShadeModel
)
70 (*ctx
->Driver
.ShadeModel
)( ctx
, mode
);
74 gl_error( ctx
, GL_INVALID_ENUM
, "glShadeModel" );
81 _mesa_Lightf( GLenum light
, GLenum pname
, GLfloat param
)
83 _mesa_Lightfv( light
, pname
, ¶m
);
88 _mesa_Lightfv( GLenum light
, GLenum pname
, const GLfloat
*params
)
90 GET_CURRENT_CONTEXT(ctx
);
94 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glLight");
96 l
= (GLint
) (light
- GL_LIGHT0
);
98 if (l
< 0 || l
>= MAX_LIGHTS
) {
99 gl_error( ctx
, GL_INVALID_ENUM
, "glLight" );
105 COPY_4V( ctx
->Light
.Light
[l
].Ambient
, params
);
109 COPY_4V( ctx
->Light
.Light
[l
].Diffuse
, params
);
113 COPY_4V( ctx
->Light
.Light
[l
].Specular
, params
);
117 /* transform position by ModelView matrix */
118 TRANSFORM_POINT( ctx
->Light
.Light
[l
].EyePosition
,
123 case GL_SPOT_DIRECTION
:
124 /* transform direction by inverse modelview */
125 if (ctx
->ModelView
.flags
& MAT_DIRTY_INVERSE
) {
126 gl_matrix_analyze( &ctx
->ModelView
);
128 TRANSFORM_NORMAL( ctx
->Light
.Light
[l
].EyeDirection
,
130 ctx
->ModelView
.inv
);
133 case GL_SPOT_EXPONENT
:
134 if (params
[0]<0.0 || params
[0]>128.0) {
135 gl_error( ctx
, GL_INVALID_VALUE
, "glLight" );
138 if (ctx
->Light
.Light
[l
].SpotExponent
!= params
[0]) {
139 ctx
->Light
.Light
[l
].SpotExponent
= params
[0];
140 gl_compute_spot_exp_table( &ctx
->Light
.Light
[l
] );
145 if ((params
[0]<0.0 || params
[0]>90.0) && params
[0]!=180.0) {
146 gl_error( ctx
, GL_INVALID_VALUE
, "glLight" );
149 ctx
->Light
.Light
[l
].SpotCutoff
= params
[0];
150 ctx
->Light
.Light
[l
].CosCutoff
= cos(params
[0]*DEG2RAD
);
151 if (ctx
->Light
.Light
[l
].CosCutoff
< 0)
152 ctx
->Light
.Light
[l
].CosCutoff
= 0;
155 case GL_CONSTANT_ATTENUATION
:
157 gl_error( ctx
, GL_INVALID_VALUE
, "glLight" );
160 ctx
->Light
.Light
[l
].ConstantAttenuation
= params
[0];
163 case GL_LINEAR_ATTENUATION
:
165 gl_error( ctx
, GL_INVALID_VALUE
, "glLight" );
168 ctx
->Light
.Light
[l
].LinearAttenuation
= params
[0];
171 case GL_QUADRATIC_ATTENUATION
:
173 gl_error( ctx
, GL_INVALID_VALUE
, "glLight" );
176 ctx
->Light
.Light
[l
].QuadraticAttenuation
= params
[0];
180 gl_error( ctx
, GL_INVALID_ENUM
, "glLight" );
184 if (ctx
->Driver
.Lightfv
)
185 ctx
->Driver
.Lightfv( ctx
, light
, pname
, params
, nParams
);
187 ctx
->NewState
|= NEW_LIGHTING
;
192 _mesa_Lighti( GLenum light
, GLenum pname
, GLint param
)
194 _mesa_Lightiv( light
, pname
, ¶m
);
199 _mesa_Lightiv( GLenum light
, GLenum pname
, const GLint
*params
)
207 fparam
[0] = INT_TO_FLOAT( params
[0] );
208 fparam
[1] = INT_TO_FLOAT( params
[1] );
209 fparam
[2] = INT_TO_FLOAT( params
[2] );
210 fparam
[3] = INT_TO_FLOAT( params
[3] );
213 fparam
[0] = (GLfloat
) params
[0];
214 fparam
[1] = (GLfloat
) params
[1];
215 fparam
[2] = (GLfloat
) params
[2];
216 fparam
[3] = (GLfloat
) params
[3];
218 case GL_SPOT_DIRECTION
:
219 fparam
[0] = (GLfloat
) params
[0];
220 fparam
[1] = (GLfloat
) params
[1];
221 fparam
[2] = (GLfloat
) params
[2];
223 case GL_SPOT_EXPONENT
:
225 case GL_CONSTANT_ATTENUATION
:
226 case GL_LINEAR_ATTENUATION
:
227 case GL_QUADRATIC_ATTENUATION
:
228 fparam
[0] = (GLfloat
) params
[0];
231 /* error will be caught later in gl_Lightfv */
235 _mesa_Lightfv( light
, pname
, fparam
);
241 _mesa_GetLightfv( GLenum light
, GLenum pname
, GLfloat
*params
)
243 GET_CURRENT_CONTEXT(ctx
);
244 GLint l
= (GLint
) (light
- GL_LIGHT0
);
246 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glGetLight");
248 if (l
<0 || l
>=MAX_LIGHTS
) {
249 gl_error( ctx
, GL_INVALID_ENUM
, "glGetLightfv" );
255 COPY_4V( params
, ctx
->Light
.Light
[l
].Ambient
);
258 COPY_4V( params
, ctx
->Light
.Light
[l
].Diffuse
);
261 COPY_4V( params
, ctx
->Light
.Light
[l
].Specular
);
264 COPY_4V( params
, ctx
->Light
.Light
[l
].EyePosition
);
266 case GL_SPOT_DIRECTION
:
267 COPY_3V( params
, ctx
->Light
.Light
[l
].EyeDirection
);
269 case GL_SPOT_EXPONENT
:
270 params
[0] = ctx
->Light
.Light
[l
].SpotExponent
;
273 params
[0] = ctx
->Light
.Light
[l
].SpotCutoff
;
275 case GL_CONSTANT_ATTENUATION
:
276 params
[0] = ctx
->Light
.Light
[l
].ConstantAttenuation
;
278 case GL_LINEAR_ATTENUATION
:
279 params
[0] = ctx
->Light
.Light
[l
].LinearAttenuation
;
281 case GL_QUADRATIC_ATTENUATION
:
282 params
[0] = ctx
->Light
.Light
[l
].QuadraticAttenuation
;
285 gl_error( ctx
, GL_INVALID_ENUM
, "glGetLightfv" );
293 _mesa_GetLightiv( GLenum light
, GLenum pname
, GLint
*params
)
295 GET_CURRENT_CONTEXT(ctx
);
296 GLint l
= (GLint
) (light
- GL_LIGHT0
);
298 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glGetLight");
300 if (l
<0 || l
>=MAX_LIGHTS
) {
301 gl_error( ctx
, GL_INVALID_ENUM
, "glGetLightiv" );
307 params
[0] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Ambient
[0]);
308 params
[1] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Ambient
[1]);
309 params
[2] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Ambient
[2]);
310 params
[3] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Ambient
[3]);
313 params
[0] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Diffuse
[0]);
314 params
[1] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Diffuse
[1]);
315 params
[2] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Diffuse
[2]);
316 params
[3] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Diffuse
[3]);
319 params
[0] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Specular
[0]);
320 params
[1] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Specular
[1]);
321 params
[2] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Specular
[2]);
322 params
[3] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Specular
[3]);
325 params
[0] = (GLint
) ctx
->Light
.Light
[l
].EyePosition
[0];
326 params
[1] = (GLint
) ctx
->Light
.Light
[l
].EyePosition
[1];
327 params
[2] = (GLint
) ctx
->Light
.Light
[l
].EyePosition
[2];
328 params
[3] = (GLint
) ctx
->Light
.Light
[l
].EyePosition
[3];
330 case GL_SPOT_DIRECTION
:
331 params
[0] = (GLint
) ctx
->Light
.Light
[l
].EyeDirection
[0];
332 params
[1] = (GLint
) ctx
->Light
.Light
[l
].EyeDirection
[1];
333 params
[2] = (GLint
) ctx
->Light
.Light
[l
].EyeDirection
[2];
335 case GL_SPOT_EXPONENT
:
336 params
[0] = (GLint
) ctx
->Light
.Light
[l
].SpotExponent
;
339 params
[0] = (GLint
) ctx
->Light
.Light
[l
].SpotCutoff
;
341 case GL_CONSTANT_ATTENUATION
:
342 params
[0] = (GLint
) ctx
->Light
.Light
[l
].ConstantAttenuation
;
344 case GL_LINEAR_ATTENUATION
:
345 params
[0] = (GLint
) ctx
->Light
.Light
[l
].LinearAttenuation
;
347 case GL_QUADRATIC_ATTENUATION
:
348 params
[0] = (GLint
) ctx
->Light
.Light
[l
].QuadraticAttenuation
;
351 gl_error( ctx
, GL_INVALID_ENUM
, "glGetLightiv" );
358 /**********************************************************************/
359 /*** Light Model ***/
360 /**********************************************************************/
364 _mesa_LightModelfv( GLenum pname
, const GLfloat
*params
)
366 GET_CURRENT_CONTEXT(ctx
);
367 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glLightModelfv");
370 case GL_LIGHT_MODEL_AMBIENT
:
371 COPY_4V( ctx
->Light
.Model
.Ambient
, params
);
373 case GL_LIGHT_MODEL_LOCAL_VIEWER
:
375 ctx
->Light
.Model
.LocalViewer
= GL_FALSE
;
377 ctx
->Light
.Model
.LocalViewer
= GL_TRUE
;
379 case GL_LIGHT_MODEL_TWO_SIDE
:
381 ctx
->Light
.Model
.TwoSide
= GL_FALSE
;
383 ctx
->Light
.Model
.TwoSide
= GL_TRUE
;
385 case GL_LIGHT_MODEL_COLOR_CONTROL
:
386 if (params
[0] == (GLfloat
) GL_SINGLE_COLOR
) {
387 ctx
->Light
.Model
.ColorControl
= GL_SINGLE_COLOR
;
388 CLEAR_BITS(ctx
->TriangleCaps
, DD_SEPERATE_SPECULAR
);
390 else if (params
[0] == (GLfloat
) GL_SEPARATE_SPECULAR_COLOR
) {
391 ctx
->Light
.Model
.ColorControl
= GL_SEPARATE_SPECULAR_COLOR
;
392 SET_BITS(ctx
->TriangleCaps
, DD_SEPERATE_SPECULAR
);
395 gl_error( ctx
, GL_INVALID_ENUM
, "glLightModel(param)" );
397 ctx
->NewState
|= NEW_RASTER_OPS
;
400 gl_error( ctx
, GL_INVALID_ENUM
, "glLightModel" );
404 if (ctx
->Driver
.LightModelfv
)
405 ctx
->Driver
.LightModelfv( ctx
, pname
, params
);
407 ctx
->NewState
|= NEW_LIGHTING
;
412 _mesa_LightModeliv( GLenum pname
, const GLint
*params
)
415 GET_CURRENT_CONTEXT(ctx
);
416 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glLightModeliv");
419 case GL_LIGHT_MODEL_AMBIENT
:
420 fparam
[0] = INT_TO_FLOAT( params
[0] );
421 fparam
[1] = INT_TO_FLOAT( params
[1] );
422 fparam
[2] = INT_TO_FLOAT( params
[2] );
423 fparam
[3] = INT_TO_FLOAT( params
[3] );
425 case GL_LIGHT_MODEL_LOCAL_VIEWER
:
426 case GL_LIGHT_MODEL_TWO_SIDE
:
427 case GL_LIGHT_MODEL_COLOR_CONTROL
:
428 fparam
[0] = (GLfloat
) params
[0];
431 /* Error will be caught later in gl_LightModelfv */
434 _mesa_LightModelfv( pname
, fparam
);
439 _mesa_LightModeli( GLenum pname
, GLint param
)
441 _mesa_LightModeliv( pname
, ¶m
);
446 _mesa_LightModelf( GLenum pname
, GLfloat param
)
448 _mesa_LightModelfv( pname
, ¶m
);
453 /********** MATERIAL **********/
457 * Given a face and pname value (ala glColorMaterial), compute a bitmask
458 * of the targeted material values.
460 GLuint
gl_material_bitmask( GLcontext
*ctx
, GLenum face
, GLenum pname
,
466 /* Make a bitmask indicating what material attribute(s) we're updating */
469 bitmask
|= FRONT_EMISSION_BIT
| BACK_EMISSION_BIT
;
472 bitmask
|= FRONT_AMBIENT_BIT
| BACK_AMBIENT_BIT
;
475 bitmask
|= FRONT_DIFFUSE_BIT
| BACK_DIFFUSE_BIT
;
478 bitmask
|= FRONT_SPECULAR_BIT
| BACK_SPECULAR_BIT
;
481 bitmask
|= FRONT_SHININESS_BIT
| BACK_SHININESS_BIT
;
483 case GL_AMBIENT_AND_DIFFUSE
:
484 bitmask
|= FRONT_AMBIENT_BIT
| BACK_AMBIENT_BIT
;
485 bitmask
|= FRONT_DIFFUSE_BIT
| BACK_DIFFUSE_BIT
;
487 case GL_COLOR_INDEXES
:
488 bitmask
|= FRONT_INDEXES_BIT
| BACK_INDEXES_BIT
;
491 gl_error( ctx
, GL_INVALID_ENUM
, where
);
495 if (face
==GL_FRONT
) {
496 bitmask
&= FRONT_MATERIAL_BITS
;
498 else if (face
==GL_BACK
) {
499 bitmask
&= BACK_MATERIAL_BITS
;
501 else if (face
!= GL_FRONT_AND_BACK
) {
502 gl_error( ctx
, GL_INVALID_ENUM
, where
);
506 if (bitmask
& ~legal
) {
507 gl_error( ctx
, GL_INVALID_ENUM
, where
);
520 * Check if the global material has to be updated with info that was
521 * associated with a vertex via glMaterial.
522 * This function is used when any material values get changed between
523 * glBegin/glEnd either by calling glMaterial() or by calling glColor()
524 * when GL_COLOR_MATERIAL is enabled.
526 * KW: Added code here to keep the precomputed variables uptodate.
527 * This means we can use the faster shade functions when using
528 * GL_COLOR_MATERIAL, and we can also now use the precomputed
529 * values in the slower shading functions, which further offsets
530 * the cost of doing this here.
532 void gl_update_material( GLcontext
*ctx
,
533 struct gl_material
*src
,
536 struct gl_light
*light
, *list
= &ctx
->Light
.EnabledList
;
539 if (ctx
->Light
.ColorMaterialEnabled
)
540 bitmask
&= ~ctx
->Light
.ColorMaterialBitmask
;
542 if (MESA_VERBOSE
&VERBOSE_IMMEDIATE
)
543 fprintf(stderr
, "gl_update_material, mask %x\n", bitmask
);
548 if (bitmask
& FRONT_AMBIENT_BIT
) {
549 struct gl_material
*mat
= &ctx
->Light
.Material
[0];
550 SUB_3V( tmp
, src
[0].Ambient
, mat
->Ambient
);
551 ACC_SCALE_3V( ctx
->Light
.BaseColor
[0], ctx
->Light
.Model
.Ambient
, tmp
);
552 foreach (light
, list
) {
553 ACC_SCALE_3V( ctx
->Light
.BaseColor
[0], light
->Ambient
, tmp
);
555 COPY_4FV( mat
->Ambient
, src
[0].Ambient
);
557 if (bitmask
& BACK_AMBIENT_BIT
) {
558 struct gl_material
*mat
= &ctx
->Light
.Material
[1];
559 SUB_3V( tmp
, src
[1].Ambient
, mat
->Ambient
);
560 ACC_SCALE_3V( ctx
->Light
.BaseColor
[1], ctx
->Light
.Model
.Ambient
, tmp
);
561 foreach (light
, list
) {
562 ACC_SCALE_3V( ctx
->Light
.BaseColor
[0], light
->Ambient
, tmp
);
564 COPY_4FV( mat
->Ambient
, src
[1].Ambient
);
566 if (bitmask
& FRONT_DIFFUSE_BIT
) {
567 struct gl_material
*mat
= &ctx
->Light
.Material
[0];
568 SUB_3V( tmp
, src
[0].Diffuse
, mat
->Diffuse
);
569 foreach (light
, list
) {
570 ACC_SCALE_3V( light
->MatDiffuse
[0], light
->Diffuse
, tmp
);
572 COPY_4FV( mat
->Diffuse
, src
[0].Diffuse
);
573 FLOAT_COLOR_TO_UBYTE_COLOR(ctx
->Light
.BaseAlpha
[0], mat
->Diffuse
[3]);
575 if (bitmask
& BACK_DIFFUSE_BIT
) {
576 struct gl_material
*mat
= &ctx
->Light
.Material
[1];
577 SUB_3V( tmp
, src
[1].Diffuse
, mat
->Diffuse
);
578 foreach (light
, list
) {
579 ACC_SCALE_3V( light
->MatDiffuse
[1], light
->Diffuse
, tmp
);
581 COPY_4FV( mat
->Diffuse
, src
[1].Diffuse
);
582 FLOAT_COLOR_TO_UBYTE_COLOR(ctx
->Light
.BaseAlpha
[1], mat
->Diffuse
[3]);
584 if (bitmask
& FRONT_SPECULAR_BIT
) {
585 struct gl_material
*mat
= &ctx
->Light
.Material
[0];
586 SUB_3V( tmp
, src
[0].Specular
, mat
->Specular
);
587 foreach (light
, list
) {
588 if (light
->Flags
& LIGHT_SPECULAR
) {
589 ACC_SCALE_3V( light
->MatSpecular
[0], light
->Specular
, tmp
);
590 light
->IsMatSpecular
[0] =
591 (LEN_SQUARED_3FV(light
->MatSpecular
[0]) > 1e-16);
594 COPY_4FV( mat
->Specular
, src
[0].Specular
);
596 if (bitmask
& BACK_SPECULAR_BIT
) {
597 struct gl_material
*mat
= &ctx
->Light
.Material
[1];
598 SUB_3V( tmp
, src
[1].Specular
, mat
->Specular
);
599 foreach (light
, list
) {
600 if (light
->Flags
& LIGHT_SPECULAR
) {
601 ACC_SCALE_3V( light
->MatSpecular
[1], light
->Specular
, tmp
);
602 light
->IsMatSpecular
[1] =
603 (LEN_SQUARED_3FV(light
->MatSpecular
[1]) > 1e-16);
606 COPY_4FV( mat
->Specular
, src
[1].Specular
);
608 if (bitmask
& FRONT_EMISSION_BIT
) {
609 struct gl_material
*mat
= &ctx
->Light
.Material
[0];
610 SUB_3V( tmp
, src
[0].Emission
, mat
->Emission
);
611 ACC_3V( ctx
->Light
.BaseColor
[0], tmp
);
612 COPY_4FV( mat
->Emission
, src
[0].Emission
);
614 if (bitmask
& BACK_EMISSION_BIT
) {
615 struct gl_material
*mat
= &ctx
->Light
.Material
[1];
616 SUB_3V( tmp
, src
[1].Emission
, mat
->Emission
);
617 ACC_3V( ctx
->Light
.BaseColor
[1], tmp
);
618 COPY_4FV( mat
->Emission
, src
[1].Emission
);
620 if (bitmask
& FRONT_SHININESS_BIT
) {
621 GLfloat shininess
= ctx
->Light
.Material
[0].Shininess
= src
[0].Shininess
;
622 gl_compute_shine_table( ctx
, 0, shininess
);
623 gl_compute_shine_table( ctx
, 2, shininess
* .5 );
625 if (bitmask
& BACK_SHININESS_BIT
) {
626 GLfloat shininess
= ctx
->Light
.Material
[1].Shininess
= src
[1].Shininess
;
627 gl_compute_shine_table( ctx
, 1, shininess
);
628 gl_compute_shine_table( ctx
, 3, shininess
* .5 );
630 if (bitmask
& FRONT_INDEXES_BIT
) {
631 ctx
->Light
.Material
[0].AmbientIndex
= src
[0].AmbientIndex
;
632 ctx
->Light
.Material
[0].DiffuseIndex
= src
[0].DiffuseIndex
;
633 ctx
->Light
.Material
[0].SpecularIndex
= src
[0].SpecularIndex
;
635 if (bitmask
& BACK_INDEXES_BIT
) {
636 ctx
->Light
.Material
[1].AmbientIndex
= src
[1].AmbientIndex
;
637 ctx
->Light
.Material
[1].DiffuseIndex
= src
[1].DiffuseIndex
;
638 ctx
->Light
.Material
[1].SpecularIndex
= src
[1].SpecularIndex
;
643 struct gl_material
*mat
= &ctx
->Light
.Material
[0];
644 fprintf(stderr
, "update_mat emission : %f %f %f\n",
648 fprintf(stderr
, "update_mat specular : %f %f %f\n",
652 fprintf(stderr
, "update_mat diffuse : %f %f %f\n",
656 fprintf(stderr
, "update_mat ambient : %f %f %f\n",
668 void gl_update_color_material( GLcontext
*ctx
,
669 const GLubyte rgba
[4] )
671 struct gl_light
*light
, *list
= &ctx
->Light
.EnabledList
;
672 GLuint bitmask
= ctx
->Light
.ColorMaterialBitmask
;
673 GLfloat tmp
[4], color
[4];
675 UBYTE_RGBA_TO_FLOAT_RGBA( color
, rgba
);
677 if (MESA_VERBOSE
&VERBOSE_IMMEDIATE
)
678 fprintf(stderr
, "gl_update_color_material, mask %x\n", bitmask
);
681 if (bitmask
& FRONT_AMBIENT_BIT
) {
682 struct gl_material
*mat
= &ctx
->Light
.Material
[0];
683 SUB_3V( tmp
, color
, mat
->Ambient
);
684 ACC_SCALE_3V( ctx
->Light
.BaseColor
[0], ctx
->Light
.Model
.Ambient
, tmp
);
685 foreach (light
, list
) {
686 ACC_SCALE_3V( ctx
->Light
.BaseColor
[0], light
->Ambient
, tmp
);
688 COPY_4FV( mat
->Ambient
, color
);
691 if (bitmask
& BACK_AMBIENT_BIT
) {
692 struct gl_material
*mat
= &ctx
->Light
.Material
[1];
693 SUB_3V( tmp
, color
, mat
->Ambient
);
694 ACC_SCALE_3V( ctx
->Light
.BaseColor
[1], ctx
->Light
.Model
.Ambient
, tmp
);
695 foreach (light
, list
) {
696 ACC_SCALE_3V( ctx
->Light
.BaseColor
[0], light
->Ambient
, tmp
);
698 COPY_4FV( mat
->Ambient
, color
);
701 if (bitmask
& FRONT_DIFFUSE_BIT
) {
702 struct gl_material
*mat
= &ctx
->Light
.Material
[0];
703 SUB_3V( tmp
, color
, mat
->Diffuse
);
704 foreach (light
, list
) {
705 ACC_SCALE_3V( light
->MatDiffuse
[0], light
->Diffuse
, tmp
);
707 COPY_4FV( mat
->Diffuse
, color
);
708 FLOAT_COLOR_TO_UBYTE_COLOR(ctx
->Light
.BaseAlpha
[0], mat
->Diffuse
[3]);
711 if (bitmask
& BACK_DIFFUSE_BIT
) {
712 struct gl_material
*mat
= &ctx
->Light
.Material
[1];
713 SUB_3V( tmp
, color
, mat
->Diffuse
);
714 foreach (light
, list
) {
715 ACC_SCALE_3V( light
->MatDiffuse
[1], light
->Diffuse
, tmp
);
717 COPY_4FV( mat
->Diffuse
, color
);
718 FLOAT_COLOR_TO_UBYTE_COLOR(ctx
->Light
.BaseAlpha
[1], mat
->Diffuse
[3]);
721 if (bitmask
& FRONT_SPECULAR_BIT
) {
722 struct gl_material
*mat
= &ctx
->Light
.Material
[0];
723 SUB_3V( tmp
, color
, mat
->Specular
);
724 foreach (light
, list
) {
725 if (light
->Flags
& LIGHT_SPECULAR
) {
726 ACC_SCALE_3V( light
->MatSpecular
[0], light
->Specular
, tmp
);
727 light
->IsMatSpecular
[0] =
728 (LEN_SQUARED_3FV(light
->MatSpecular
[0]) > 1e-16);
731 COPY_4FV( mat
->Specular
, color
);
733 if (bitmask
& BACK_SPECULAR_BIT
) {
734 struct gl_material
*mat
= &ctx
->Light
.Material
[1];
735 SUB_3V( tmp
, color
, mat
->Specular
);
736 foreach (light
, list
) {
737 if (light
->Flags
& LIGHT_SPECULAR
) {
738 ACC_SCALE_3V( light
->MatSpecular
[1], light
->Specular
, tmp
);
739 light
->IsMatSpecular
[1] =
740 (LEN_SQUARED_3FV(light
->MatSpecular
[1]) > 1e-16);
743 COPY_4FV( mat
->Specular
, color
);
745 if (bitmask
& FRONT_EMISSION_BIT
) {
746 struct gl_material
*mat
= &ctx
->Light
.Material
[0];
747 SUB_3V( tmp
, color
, mat
->Emission
);
748 ACC_3V( ctx
->Light
.BaseColor
[0], tmp
);
749 COPY_4FV( mat
->Emission
, color
);
751 if (bitmask
& BACK_EMISSION_BIT
) {
752 struct gl_material
*mat
= &ctx
->Light
.Material
[1];
753 SUB_3V( tmp
, color
, mat
->Emission
);
754 ACC_3V( ctx
->Light
.BaseColor
[1], tmp
);
755 COPY_4FV( mat
->Emission
, color
);
760 struct gl_material
*mat
= &ctx
->Light
.Material
[0];
761 fprintf(stderr
, "update_color_mat emission : %f %f %f\n",
765 fprintf(stderr
, "update_color_mat specular : %f %f %f\n",
769 fprintf(stderr
, "update_color_mat diffuse : %f %f %f\n",
773 fprintf(stderr
, "update_color_mat ambient : %f %f %f\n",
784 _mesa_ColorMaterial( GLenum face
, GLenum mode
)
786 GET_CURRENT_CONTEXT(ctx
);
788 GLuint legal
= (FRONT_EMISSION_BIT
| BACK_EMISSION_BIT
|
789 FRONT_SPECULAR_BIT
| BACK_SPECULAR_BIT
|
790 FRONT_DIFFUSE_BIT
| BACK_DIFFUSE_BIT
|
791 FRONT_AMBIENT_BIT
| BACK_AMBIENT_BIT
);
793 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glColorMaterial");
795 if (MESA_VERBOSE
&VERBOSE_API
)
796 fprintf(stderr
, "glColorMaterial %s %s\n",
797 gl_lookup_enum_by_nr(face
),
798 gl_lookup_enum_by_nr(mode
));
800 bitmask
= gl_material_bitmask( ctx
, face
, mode
, legal
, "glColorMaterial" );
803 ctx
->Light
.ColorMaterialBitmask
= bitmask
;
804 ctx
->Light
.ColorMaterialFace
= face
;
805 ctx
->Light
.ColorMaterialMode
= mode
;
808 if (ctx
->Light
.ColorMaterialEnabled
)
809 gl_update_color_material( ctx
, ctx
->Current
.ByteColor
);
816 _mesa_Materialf( GLenum face
, GLenum pname
, GLfloat param
)
818 _mesa_Materialfv( face
, pname
, ¶m
);
822 /* KW: This is now called directly (ie by name) from the glMaterial*
826 _mesa_Materialfv( GLenum face
, GLenum pname
, const GLfloat
*params
)
828 GET_CURRENT_CONTEXT(ctx
);
829 struct immediate
*IM
;
830 struct gl_material
*mat
;
834 bitmask
= gl_material_bitmask( ctx
, face
, pname
, ~0, "gl_Materialfv" );
843 (struct gl_material (*)[2]) MALLOC( sizeof(struct gl_material
) *
845 IM
->MaterialMask
= (GLuint
*) MALLOC( sizeof(GLuint
) * VB_SIZE
);
849 if (!(IM
->Flag
[count
] & VERT_MATERIAL
)) {
850 IM
->Flag
[count
] |= VERT_MATERIAL
;
851 IM
->MaterialMask
[count
] = 0;
855 IM
->MaterialMask
[count
] |= bitmask
;
856 mat
= IM
->Material
[count
];
858 if (bitmask
& FRONT_AMBIENT_BIT
) {
859 COPY_4FV( mat
[0].Ambient
, params
);
861 if (bitmask
& BACK_AMBIENT_BIT
) {
862 COPY_4FV( mat
[1].Ambient
, params
);
864 if (bitmask
& FRONT_DIFFUSE_BIT
) {
865 COPY_4FV( mat
[0].Diffuse
, params
);
867 if (bitmask
& BACK_DIFFUSE_BIT
) {
868 COPY_4FV( mat
[1].Diffuse
, params
);
870 if (bitmask
& FRONT_SPECULAR_BIT
) {
871 COPY_4FV( mat
[0].Specular
, params
);
873 if (bitmask
& BACK_SPECULAR_BIT
) {
874 COPY_4FV( mat
[1].Specular
, params
);
876 if (bitmask
& FRONT_EMISSION_BIT
) {
877 COPY_4FV( mat
[0].Emission
, params
);
879 if (bitmask
& BACK_EMISSION_BIT
) {
880 COPY_4FV( mat
[1].Emission
, params
);
882 if (bitmask
& FRONT_SHININESS_BIT
) {
883 GLfloat shininess
= CLAMP( params
[0], 0.0F
, 128.0F
);
884 mat
[0].Shininess
= shininess
;
886 if (bitmask
& BACK_SHININESS_BIT
) {
887 GLfloat shininess
= CLAMP( params
[0], 0.0F
, 128.0F
);
888 mat
[1].Shininess
= shininess
;
890 if (bitmask
& FRONT_INDEXES_BIT
) {
891 mat
[0].AmbientIndex
= params
[0];
892 mat
[0].DiffuseIndex
= params
[1];
893 mat
[0].SpecularIndex
= params
[2];
895 if (bitmask
& BACK_INDEXES_BIT
) {
896 mat
[1].AmbientIndex
= params
[0];
897 mat
[1].DiffuseIndex
= params
[1];
898 mat
[1].SpecularIndex
= params
[2];
904 _mesa_Materiali(GLenum face
, GLenum pname
, GLint param
)
906 _mesa_Materialiv(face
, pname
, ¶m
);
911 _mesa_Materialiv(GLenum face
, GLenum pname
, const GLint
*params
)
919 case GL_AMBIENT_AND_DIFFUSE
:
920 fparam
[0] = INT_TO_FLOAT( params
[0] );
921 fparam
[1] = INT_TO_FLOAT( params
[1] );
922 fparam
[2] = INT_TO_FLOAT( params
[2] );
923 fparam
[3] = INT_TO_FLOAT( params
[3] );
926 fparam
[0] = (GLfloat
) params
[0];
928 case GL_COLOR_INDEXES
:
929 fparam
[0] = (GLfloat
) params
[0];
930 fparam
[1] = (GLfloat
) params
[1];
931 fparam
[2] = (GLfloat
) params
[2];
934 /* Error will be caught later in gl_Materialfv */
937 _mesa_Materialfv(face
, pname
, fparam
);
942 _mesa_GetMaterialfv( GLenum face
, GLenum pname
, GLfloat
*params
)
944 GET_CURRENT_CONTEXT(ctx
);
947 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glGetMaterialfv");
949 if (face
==GL_FRONT
) {
952 else if (face
==GL_BACK
) {
956 gl_error( ctx
, GL_INVALID_ENUM
, "glGetMaterialfv(face)" );
961 COPY_4FV( params
, ctx
->Light
.Material
[f
].Ambient
);
964 COPY_4FV( params
, ctx
->Light
.Material
[f
].Diffuse
);
967 COPY_4FV( params
, ctx
->Light
.Material
[f
].Specular
);
970 COPY_4FV( params
, ctx
->Light
.Material
[f
].Emission
);
973 *params
= ctx
->Light
.Material
[f
].Shininess
;
975 case GL_COLOR_INDEXES
:
976 params
[0] = ctx
->Light
.Material
[f
].AmbientIndex
;
977 params
[1] = ctx
->Light
.Material
[f
].DiffuseIndex
;
978 params
[2] = ctx
->Light
.Material
[f
].SpecularIndex
;
981 gl_error( ctx
, GL_INVALID_ENUM
, "glGetMaterialfv(pname)" );
988 _mesa_GetMaterialiv( GLenum face
, GLenum pname
, GLint
*params
)
990 GET_CURRENT_CONTEXT(ctx
);
993 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glGetMaterialiv");
995 if (face
==GL_FRONT
) {
998 else if (face
==GL_BACK
) {
1002 gl_error( ctx
, GL_INVALID_ENUM
, "glGetMaterialiv(face)" );
1007 params
[0] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Ambient
[0] );
1008 params
[1] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Ambient
[1] );
1009 params
[2] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Ambient
[2] );
1010 params
[3] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Ambient
[3] );
1013 params
[0] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Diffuse
[0] );
1014 params
[1] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Diffuse
[1] );
1015 params
[2] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Diffuse
[2] );
1016 params
[3] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Diffuse
[3] );
1019 params
[0] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Specular
[0] );
1020 params
[1] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Specular
[1] );
1021 params
[2] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Specular
[2] );
1022 params
[3] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Specular
[3] );
1025 params
[0] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Emission
[0] );
1026 params
[1] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Emission
[1] );
1027 params
[2] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Emission
[2] );
1028 params
[3] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Emission
[3] );
1031 *params
= ROUNDF( ctx
->Light
.Material
[f
].Shininess
);
1033 case GL_COLOR_INDEXES
:
1034 params
[0] = ROUNDF( ctx
->Light
.Material
[f
].AmbientIndex
);
1035 params
[1] = ROUNDF( ctx
->Light
.Material
[f
].DiffuseIndex
);
1036 params
[2] = ROUNDF( ctx
->Light
.Material
[f
].SpecularIndex
);
1039 gl_error( ctx
, GL_INVALID_ENUM
, "glGetMaterialfv(pname)" );
1046 /**********************************************************************/
1047 /***** Lighting computation *****/
1048 /**********************************************************************/
1053 * When two-sided lighting is enabled we compute the color (or index)
1054 * for both the front and back side of the primitive. Then, when the
1055 * orientation of the facet is later learned, we can determine which
1056 * color (or index) to use for rendering.
1058 * KW: We now know orientation in advance and only shade for
1059 * the side or sides which are actually required.
1063 * V = vertex position
1064 * P = light source position
1069 * // light at infinity
1070 * IF local_viewer THEN
1071 * VP_inf_norm = unit vector from V to P // Precompute
1073 * // eye at infinity
1074 * h_inf_norm = Normalize( VP + <0,0,1> ) // Precompute
1079 * Normalize( v ) = normalized vector v
1080 * Magnitude( v ) = length of vector v
1086 * Whenever the spotlight exponent for a light changes we must call
1087 * this function to recompute the exponent lookup table.
1089 void gl_compute_spot_exp_table( struct gl_light
*l
)
1092 double exponent
= l
->SpotExponent
;
1096 l
->SpotExpTable
[0][0] = 0.0;
1098 for (i
=EXP_TABLE_SIZE
-1;i
>0;i
--) {
1100 tmp
= pow(i
/(double)(EXP_TABLE_SIZE
-1), exponent
);
1101 if (tmp
< FLT_MIN
*100.0) {
1106 l
->SpotExpTable
[i
][0] = tmp
;
1108 for (i
=0;i
<EXP_TABLE_SIZE
-1;i
++) {
1109 l
->SpotExpTable
[i
][1] = l
->SpotExpTable
[i
+1][0] - l
->SpotExpTable
[i
][0];
1111 l
->SpotExpTable
[EXP_TABLE_SIZE
-1][1] = 0.0;
1117 /* Calculate a new shine table. Doing this here saves a branch in
1118 * lighting, and the cost of doing it early may be partially offset
1119 * by keeping a MRU cache of shine tables for various shine values.
1121 static void compute_shine_table( struct gl_shine_tab
*tab
, GLfloat shininess
)
1124 GLfloat
*m
= tab
->tab
;
1127 if (shininess
== 0) {
1128 for (i
= 1 ; i
<= SHINE_TABLE_SIZE
; i
++)
1131 for (i
= 1 ; i
<= SHINE_TABLE_SIZE
; i
++) {
1132 double t
= pow( i
/(GLfloat
)SHINE_TABLE_SIZE
, shininess
);
1134 if (t
> 1e-20) m
[i
] = t
;
1138 tab
->shininess
= shininess
;
1141 #define DISTSQR(a,b) ((a-b)*(a-b))
1143 void gl_compute_shine_table( GLcontext
*ctx
, GLuint i
, GLfloat shininess
)
1145 struct gl_shine_tab
*list
= ctx
->ShineTabList
;
1146 struct gl_shine_tab
*s
;
1149 if ( DISTSQR(s
->shininess
, shininess
) < 1e-4 )
1155 if (s
->refcount
== 0) break;
1157 compute_shine_table( s
, shininess
);
1160 ctx
->ShineTable
[i
]->refcount
--;
1161 ctx
->ShineTable
[i
] = s
;
1162 move_to_tail( list
, s
);
1169 static void gl_reinit_light_attrib( GLcontext
*ctx
, struct gl_light_attrib
*l
)
1173 if (ctx
->ShineTable
[0]->shininess
!= l
->Material
[0].Shininess
) {
1174 gl_compute_shine_table( ctx
, 0, l
->Material
[0].Shininess
);
1175 gl_compute_shine_table( ctx
, 2, l
->Material
[0].Shininess
* .5 );
1178 if (ctx
->ShineTable
[1]->shininess
!= l
->Material
[1].Shininess
) {
1179 gl_compute_shine_table( ctx
, 1, l
->Material
[1].Shininess
);
1180 gl_compute_shine_table( ctx
, 3, l
->Material
[1].Shininess
* .5 );
1183 make_empty_list( &l
->EnabledList
);
1184 for (i
= 0 ; i
< MAX_LIGHTS
; i
++) {
1185 if (l
->Light
[i
].Enabled
)
1186 insert_at_tail( &l
->EnabledList
, &l
->Light
[i
] );
1193 * Examine current lighting parameters to determine if the optimized lighting
1194 * function can be used.
1195 * Also, precompute some lighting values such as the products of light
1196 * source and material ambient, diffuse and specular coefficients.
1198 void gl_update_lighting( GLcontext
*ctx
)
1200 struct gl_light
*light
;
1202 ctx
->Light
.Flags
= 0;
1204 foreach(light
, &ctx
->Light
.EnabledList
) {
1208 if (light
->EyePosition
[3] != 0.0F
)
1209 light
->Flags
|= LIGHT_POSITIONAL
;
1211 if (LEN_SQUARED_3FV(light
->Specular
) > 1e-16)
1212 light
->Flags
|= LIGHT_SPECULAR
;
1214 if (light
->SpotCutoff
!= 180.0F
)
1215 light
->Flags
|= LIGHT_SPOT
;
1217 ctx
->Light
.Flags
|= light
->Flags
;
1220 ctx
->Light
.NeedVertices
=
1221 ((ctx
->Light
.Flags
& (LIGHT_POSITIONAL
|LIGHT_SPOT
)) ||
1222 (ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
) ||
1223 (ctx
->Light
.Model
.LocalViewer
&& (ctx
->Light
.Flags
& LIGHT_SPECULAR
)));
1226 /* Precompute some shading values.
1228 if (ctx
->Visual
->RGBAflag
)
1230 GLuint sides
= ((ctx
->TriangleCaps
& DD_TRI_LIGHT_TWOSIDE
) ? 2 : 1);
1232 for (side
=0; side
< sides
; side
++) {
1233 struct gl_material
*mat
= &ctx
->Light
.Material
[side
];
1235 COPY_3V(ctx
->Light
.BaseColor
[side
], mat
->Emission
);
1236 ACC_SCALE_3V(ctx
->Light
.BaseColor
[side
],
1237 ctx
->Light
.Model
.Ambient
,
1240 FLOAT_COLOR_TO_UBYTE_COLOR(ctx
->Light
.BaseAlpha
[side
],
1241 ctx
->Light
.Material
[side
].Diffuse
[3] );
1244 foreach (light
, &ctx
->Light
.EnabledList
) {
1245 for (side
=0; side
< sides
; side
++) {
1246 struct gl_material
*mat
= &ctx
->Light
.Material
[side
];
1247 SCALE_3V( light
->MatDiffuse
[side
], light
->Diffuse
, mat
->Diffuse
);
1248 SCALE_3V( light
->MatAmbient
[side
], light
->Ambient
, mat
->Ambient
);
1249 ACC_3V( ctx
->Light
.BaseColor
[side
], light
->MatAmbient
[side
] );
1250 if (light
->Flags
& LIGHT_SPECULAR
)
1252 SCALE_3V( light
->MatSpecular
[side
], light
->Specular
,
1254 light
->IsMatSpecular
[side
] =
1255 (LEN_SQUARED_3FV(light
->MatSpecular
[side
]) > 1e-16);
1258 light
->IsMatSpecular
[side
] = 0;
1264 static GLfloat ci
[3] = { .30, .59, .11 };
1266 foreach(light
, &ctx
->Light
.EnabledList
) {
1267 light
->dli
= DOT3(ci
, light
->Diffuse
);
1268 light
->sli
= DOT3(ci
, light
->Specular
);
1273 /* Need to seriously restrict the circumstances under which these
1274 * calc's are performed.
1276 void gl_compute_light_positions( GLcontext
*ctx
)
1278 struct gl_light
*light
;
1280 if (ctx
->Light
.NeedVertices
&& !ctx
->Light
.Model
.LocalViewer
) {
1281 GLfloat eye_z
[3] = { 0, 0, 1 };
1282 if (!ctx
->NeedEyeCoords
) {
1283 TRANSFORM_NORMAL( ctx
->EyeZDir
, eye_z
, ctx
->ModelView
.m
);
1285 COPY_3V( ctx
->EyeZDir
, eye_z
);
1289 foreach (light
, &ctx
->Light
.EnabledList
) {
1291 if (!ctx
->NeedEyeCoords
) {
1292 TRANSFORM_POINT( light
->Position
, ctx
->ModelView
.inv
,
1293 light
->EyePosition
);
1295 COPY_4FV( light
->Position
, light
->EyePosition
);
1298 if (!(light
->Flags
& LIGHT_POSITIONAL
))
1300 /* VP (VP) = Normalize( Position ) */
1301 COPY_3V( light
->VP_inf_norm
, light
->Position
);
1302 NORMALIZE_3FV( light
->VP_inf_norm
);
1304 if (!ctx
->Light
.Model
.LocalViewer
)
1306 /* h_inf_norm = Normalize( V_to_P + <0,0,1> ) */
1307 ADD_3V( light
->h_inf_norm
, light
->VP_inf_norm
, ctx
->EyeZDir
);
1308 NORMALIZE_3FV( light
->h_inf_norm
);
1311 light
->VP_inf_spot_attenuation
= 1.0;
1314 if (light
->Flags
& LIGHT_SPOT
)
1316 if (ctx
->NeedEyeNormals
) {
1317 COPY_3V( light
->NormDirection
, light
->EyeDirection
);
1319 TRANSFORM_NORMAL( light
->NormDirection
,
1320 light
->EyeDirection
,
1324 NORMALIZE_3FV( light
->NormDirection
);
1327 /* Unlikely occurrance?
1329 if (!(light
->Flags
& LIGHT_POSITIONAL
)) {
1330 GLfloat PV_dot_dir
= - DOT3(light
->VP_inf_norm
,
1331 light
->NormDirection
);
1333 if (PV_dot_dir
> light
->CosCutoff
) {
1334 double x
= PV_dot_dir
* (EXP_TABLE_SIZE
-1);
1336 light
->VP_inf_spot_attenuation
=
1337 (light
->SpotExpTable
[k
][0] +
1338 (x
-k
)*light
->SpotExpTable
[k
][1]);
1341 light
->VP_inf_spot_attenuation
= 0;
1351 void gl_update_normal_transform( GLcontext
*ctx
)
1353 GLuint new_flag
= 0;
1354 normal_func
*last
= ctx
->NormalTransform
;
1356 ctx
->vb_rescale_factor
= 1.0;
1358 if (ctx
->NeedEyeCoords
) {
1359 if (ctx
->NeedNormals
) {
1360 GLuint transform
= NORM_TRANSFORM_NO_ROT
;
1362 if (ctx
->ModelView
.flags
& (MAT_FLAG_GENERAL
|
1364 MAT_FLAG_GENERAL_3D
|
1365 MAT_FLAG_PERSPECTIVE
))
1366 transform
= NORM_TRANSFORM
;
1369 new_flag
= ctx
->NewState
& NEW_MODELVIEW
;
1370 ctx
->vb_rescale_factor
= ctx
->rescale_factor
;
1372 if (ctx
->Transform
.Normalize
)
1374 ctx
->NormalTransform
= gl_normal_tab
[transform
| NORM_NORMALIZE
];
1376 else if (ctx
->Transform
.RescaleNormals
&&
1377 ctx
->rescale_factor
!= 1.0)
1379 ctx
->NormalTransform
= gl_normal_tab
[transform
| NORM_RESCALE
];
1383 ctx
->NormalTransform
= gl_normal_tab
[transform
];
1386 ctx
->NormalTransform
= 0;
1390 if (ctx
->NeedNormals
) {
1391 ctx
->vb_rescale_factor
= 1.0/ctx
->rescale_factor
;
1393 if (ctx
->Transform
.Normalize
)
1395 ctx
->NormalTransform
= gl_normal_tab
[NORM_NORMALIZE
];
1397 else if (!ctx
->Transform
.RescaleNormals
&&
1398 ctx
->rescale_factor
!= 1.0)
1400 ctx
->NormalTransform
= gl_normal_tab
[NORM_RESCALE
];
1404 ctx
->NormalTransform
= 0;
1407 ctx
->NormalTransform
= 0;
1411 if (last
!= ctx
->NormalTransform
|| new_flag
)
1412 ctx
->NewState
|= NEW_NORMAL_TRANSFORM
;