1 /* $Id: light.c,v 1.4 1999/10/08 09:27:11 keithw Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999 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.
41 #include "GL/xf86glx.h"
50 #include "simple_list.h"
58 void gl_ShadeModel( GLcontext
*ctx
, GLenum mode
)
60 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glShadeModel");
62 if (MESA_VERBOSE
& VERBOSE_API
)
63 fprintf(stderr
, "glShadeModel %s\n", gl_lookup_enum_by_nr(mode
));
69 if (ctx
->Light
.ShadeModel
!=mode
) {
70 ctx
->Light
.ShadeModel
= mode
;
71 ctx
->TriangleCaps
^= DD_FLATSHADE
;
72 ctx
->NewState
|= NEW_RASTER_OPS
;
76 gl_error( ctx
, GL_INVALID_ENUM
, "glShadeModel" );
79 if (ctx
->Driver
.ShadeModel
)
80 (*ctx
->Driver
.ShadeModel
)( ctx
, mode
);
85 void gl_Lightfv( GLcontext
*ctx
,
86 GLenum light
, GLenum pname
, const GLfloat
*params
,
93 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glLight");
95 l
= (GLint
) (light
- GL_LIGHT0
);
97 if (l
<0 || l
>=MAX_LIGHTS
) {
98 gl_error( ctx
, GL_INVALID_ENUM
, "glLight" );
104 COPY_4V( ctx
->Light
.Light
[l
].Ambient
, params
);
107 COPY_4V( ctx
->Light
.Light
[l
].Diffuse
, params
);
110 COPY_4V( ctx
->Light
.Light
[l
].Specular
, params
);
113 /* transform position by ModelView matrix */
114 TRANSFORM_POINT( ctx
->Light
.Light
[l
].EyePosition
,
118 case GL_SPOT_DIRECTION
:
119 /* transform direction by inverse modelview */
120 if (ctx
->ModelView
.flags
& MAT_DIRTY_INVERSE
) {
121 gl_matrix_analyze( &ctx
->ModelView
);
123 TRANSFORM_NORMAL( ctx
->Light
.Light
[l
].EyeDirection
,
125 ctx
->ModelView
.inv
);
127 case GL_SPOT_EXPONENT
:
128 if (params
[0]<0.0 || params
[0]>128.0) {
129 gl_error( ctx
, GL_INVALID_VALUE
, "glLight" );
132 if (ctx
->Light
.Light
[l
].SpotExponent
!= params
[0]) {
133 ctx
->Light
.Light
[l
].SpotExponent
= params
[0];
134 gl_compute_spot_exp_table( &ctx
->Light
.Light
[l
] );
138 if ((params
[0]<0.0 || params
[0]>90.0) && params
[0]!=180.0) {
139 gl_error( ctx
, GL_INVALID_VALUE
, "glLight" );
142 ctx
->Light
.Light
[l
].SpotCutoff
= params
[0];
143 ctx
->Light
.Light
[l
].CosCutoff
= cos(params
[0]*DEG2RAD
);
144 if (ctx
->Light
.Light
[l
].CosCutoff
< 0)
145 ctx
->Light
.Light
[l
].CosCutoff
= 0;
147 case GL_CONSTANT_ATTENUATION
:
149 gl_error( ctx
, GL_INVALID_VALUE
, "glLight" );
152 ctx
->Light
.Light
[l
].ConstantAttenuation
= params
[0];
154 case GL_LINEAR_ATTENUATION
:
156 gl_error( ctx
, GL_INVALID_VALUE
, "glLight" );
159 ctx
->Light
.Light
[l
].LinearAttenuation
= params
[0];
161 case GL_QUADRATIC_ATTENUATION
:
163 gl_error( ctx
, GL_INVALID_VALUE
, "glLight" );
166 ctx
->Light
.Light
[l
].QuadraticAttenuation
= params
[0];
169 gl_error( ctx
, GL_INVALID_ENUM
, "glLight" );
173 if (ctx
->Driver
.Lightfv
)
174 ctx
->Driver
.Lightfv( ctx
, light
, pname
, params
, nparams
);
176 ctx
->NewState
|= NEW_LIGHTING
;
181 void gl_GetLightfv( GLcontext
*ctx
,
182 GLenum light
, GLenum pname
, GLfloat
*params
)
184 GLint l
= (GLint
) (light
- GL_LIGHT0
);
186 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glGetLight");
188 if (l
<0 || l
>=MAX_LIGHTS
) {
189 gl_error( ctx
, GL_INVALID_ENUM
, "glGetLightfv" );
195 COPY_4V( params
, ctx
->Light
.Light
[l
].Ambient
);
198 COPY_4V( params
, ctx
->Light
.Light
[l
].Diffuse
);
201 COPY_4V( params
, ctx
->Light
.Light
[l
].Specular
);
204 COPY_4V( params
, ctx
->Light
.Light
[l
].EyePosition
);
206 case GL_SPOT_DIRECTION
:
207 COPY_3V( params
, ctx
->Light
.Light
[l
].EyeDirection
);
209 case GL_SPOT_EXPONENT
:
210 params
[0] = ctx
->Light
.Light
[l
].SpotExponent
;
213 params
[0] = ctx
->Light
.Light
[l
].SpotCutoff
;
215 case GL_CONSTANT_ATTENUATION
:
216 params
[0] = ctx
->Light
.Light
[l
].ConstantAttenuation
;
218 case GL_LINEAR_ATTENUATION
:
219 params
[0] = ctx
->Light
.Light
[l
].LinearAttenuation
;
221 case GL_QUADRATIC_ATTENUATION
:
222 params
[0] = ctx
->Light
.Light
[l
].QuadraticAttenuation
;
225 gl_error( ctx
, GL_INVALID_ENUM
, "glGetLightfv" );
232 void gl_GetLightiv( GLcontext
*ctx
, GLenum light
, GLenum pname
, GLint
*params
)
234 GLint l
= (GLint
) (light
- GL_LIGHT0
);
236 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glGetLight");
238 if (l
<0 || l
>=MAX_LIGHTS
) {
239 gl_error( ctx
, GL_INVALID_ENUM
, "glGetLightiv" );
245 params
[0] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Ambient
[0]);
246 params
[1] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Ambient
[1]);
247 params
[2] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Ambient
[2]);
248 params
[3] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Ambient
[3]);
251 params
[0] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Diffuse
[0]);
252 params
[1] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Diffuse
[1]);
253 params
[2] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Diffuse
[2]);
254 params
[3] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Diffuse
[3]);
257 params
[0] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Specular
[0]);
258 params
[1] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Specular
[1]);
259 params
[2] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Specular
[2]);
260 params
[3] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Specular
[3]);
263 params
[0] = (GLint
) ctx
->Light
.Light
[l
].EyePosition
[0];
264 params
[1] = (GLint
) ctx
->Light
.Light
[l
].EyePosition
[1];
265 params
[2] = (GLint
) ctx
->Light
.Light
[l
].EyePosition
[2];
266 params
[3] = (GLint
) ctx
->Light
.Light
[l
].EyePosition
[3];
268 case GL_SPOT_DIRECTION
:
269 params
[0] = (GLint
) ctx
->Light
.Light
[l
].EyeDirection
[0];
270 params
[1] = (GLint
) ctx
->Light
.Light
[l
].EyeDirection
[1];
271 params
[2] = (GLint
) ctx
->Light
.Light
[l
].EyeDirection
[2];
273 case GL_SPOT_EXPONENT
:
274 params
[0] = (GLint
) ctx
->Light
.Light
[l
].SpotExponent
;
277 params
[0] = (GLint
) ctx
->Light
.Light
[l
].SpotCutoff
;
279 case GL_CONSTANT_ATTENUATION
:
280 params
[0] = (GLint
) ctx
->Light
.Light
[l
].ConstantAttenuation
;
282 case GL_LINEAR_ATTENUATION
:
283 params
[0] = (GLint
) ctx
->Light
.Light
[l
].LinearAttenuation
;
285 case GL_QUADRATIC_ATTENUATION
:
286 params
[0] = (GLint
) ctx
->Light
.Light
[l
].QuadraticAttenuation
;
289 gl_error( ctx
, GL_INVALID_ENUM
, "glGetLightiv" );
296 /**********************************************************************/
297 /*** Light Model ***/
298 /**********************************************************************/
301 void gl_LightModelfv( GLcontext
*ctx
, GLenum pname
, const GLfloat
*params
)
303 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glLightModel");
306 case GL_LIGHT_MODEL_AMBIENT
:
307 COPY_4V( ctx
->Light
.Model
.Ambient
, params
);
309 case GL_LIGHT_MODEL_LOCAL_VIEWER
:
311 ctx
->Light
.Model
.LocalViewer
= GL_FALSE
;
313 ctx
->Light
.Model
.LocalViewer
= GL_TRUE
;
315 case GL_LIGHT_MODEL_TWO_SIDE
:
317 ctx
->Light
.Model
.TwoSide
= GL_FALSE
;
319 ctx
->Light
.Model
.TwoSide
= GL_TRUE
;
321 case GL_LIGHT_MODEL_COLOR_CONTROL
:
322 ctx
->TriangleCaps
&= ~DD_SEPERATE_SPECULAR
;
323 ctx
->NewState
|= NEW_RASTER_OPS
;
324 if (params
[0] == (GLfloat
) GL_SINGLE_COLOR
)
325 ctx
->Light
.Model
.ColorControl
= GL_SINGLE_COLOR
;
326 else if (params
[0] == (GLfloat
) GL_SEPARATE_SPECULAR_COLOR
) {
327 ctx
->Light
.Model
.ColorControl
= GL_SEPARATE_SPECULAR_COLOR
;
328 ctx
->TriangleCaps
|= DD_SEPERATE_SPECULAR
;
330 gl_error( ctx
, GL_INVALID_ENUM
, "glLightModel(param)" );
333 gl_error( ctx
, GL_INVALID_ENUM
, "glLightModel" );
337 if (ctx
->Driver
.LightModelfv
)
338 ctx
->Driver
.LightModelfv( ctx
, pname
, params
);
340 ctx
->NewState
|= NEW_LIGHTING
;
346 /********** MATERIAL **********/
350 * Given a face and pname value (ala glColorMaterial), compute a bitmask
351 * of the targeted material values.
353 GLuint
gl_material_bitmask( GLcontext
*ctx
, GLenum face
, GLenum pname
,
359 /* Make a bitmask indicating what material attribute(s) we're updating */
362 bitmask
|= FRONT_EMISSION_BIT
| BACK_EMISSION_BIT
;
365 bitmask
|= FRONT_AMBIENT_BIT
| BACK_AMBIENT_BIT
;
368 bitmask
|= FRONT_DIFFUSE_BIT
| BACK_DIFFUSE_BIT
;
371 bitmask
|= FRONT_SPECULAR_BIT
| BACK_SPECULAR_BIT
;
374 bitmask
|= FRONT_SHININESS_BIT
| BACK_SHININESS_BIT
;
376 case GL_AMBIENT_AND_DIFFUSE
:
377 bitmask
|= FRONT_AMBIENT_BIT
| BACK_AMBIENT_BIT
;
378 bitmask
|= FRONT_DIFFUSE_BIT
| BACK_DIFFUSE_BIT
;
380 case GL_COLOR_INDEXES
:
381 bitmask
|= FRONT_INDEXES_BIT
| BACK_INDEXES_BIT
;
384 gl_error( ctx
, GL_INVALID_ENUM
, where
);
388 if (face
==GL_FRONT
) {
389 bitmask
&= FRONT_MATERIAL_BITS
;
391 else if (face
==GL_BACK
) {
392 bitmask
&= BACK_MATERIAL_BITS
;
394 else if (face
!= GL_FRONT_AND_BACK
) {
395 gl_error( ctx
, GL_INVALID_ENUM
, where
);
399 if (bitmask
& ~legal
) {
400 gl_error( ctx
, GL_INVALID_ENUM
, where
);
413 * Check if the global material has to be updated with info that was
414 * associated with a vertex via glMaterial.
415 * This function is used when any material values get changed between
416 * glBegin/glEnd either by calling glMaterial() or by calling glColor()
417 * when GL_COLOR_MATERIAL is enabled.
419 * KW: Added code here to keep the precomputed variables uptodate.
420 * This means we can use the faster shade functions when using
421 * GL_COLOR_MATERIAL, and we can also now use the precomputed
422 * values in the slower shading functions, which further offsets
423 * the cost of doing this here.
425 void gl_update_material( GLcontext
*ctx
,
426 struct gl_material
*src
,
429 struct gl_light
*light
, *list
= &ctx
->Light
.EnabledList
;
432 if (ctx
->Light
.ColorMaterialEnabled
)
433 bitmask
&= ~ctx
->Light
.ColorMaterialBitmask
;
438 if (bitmask
& FRONT_AMBIENT_BIT
) {
439 struct gl_material
*mat
= &ctx
->Light
.Material
[0];
440 SUB_3V( tmp
, src
[0].Ambient
, mat
->Ambient
);
441 ACC_SCALE_3V( ctx
->Light
.BaseColor
[0], ctx
->Light
.Model
.Ambient
, tmp
);
442 foreach (light
, list
) {
443 ACC_SCALE_3V( ctx
->Light
.BaseColor
[0], light
->Ambient
, tmp
);
445 COPY_4FV( mat
->Ambient
, src
[0].Ambient
);
447 if (bitmask
& BACK_AMBIENT_BIT
) {
448 struct gl_material
*mat
= &ctx
->Light
.Material
[1];
449 SUB_3V( tmp
, src
[1].Ambient
, mat
->Ambient
);
450 ACC_SCALE_3V( ctx
->Light
.BaseColor
[1], ctx
->Light
.Model
.Ambient
, tmp
);
451 foreach (light
, list
) {
452 ACC_SCALE_3V( ctx
->Light
.BaseColor
[0], light
->Ambient
, tmp
);
454 COPY_4FV( mat
->Ambient
, src
[1].Ambient
);
456 if (bitmask
& FRONT_DIFFUSE_BIT
) {
457 struct gl_material
*mat
= &ctx
->Light
.Material
[0];
458 SUB_3V( tmp
, src
[0].Diffuse
, mat
->Diffuse
);
459 foreach (light
, list
) {
460 ACC_SCALE_3V( light
->MatDiffuse
[0], light
->Diffuse
, tmp
);
462 COPY_4FV( mat
->Diffuse
, src
[0].Diffuse
);
463 FLOAT_COLOR_TO_UBYTE_COLOR(ctx
->Light
.BaseAlpha
[0], mat
->Diffuse
[3]);
465 if (bitmask
& BACK_DIFFUSE_BIT
) {
466 struct gl_material
*mat
= &ctx
->Light
.Material
[1];
467 SUB_3V( tmp
, src
[1].Diffuse
, mat
->Diffuse
);
468 foreach (light
, list
) {
469 ACC_SCALE_3V( light
->MatDiffuse
[1], light
->Diffuse
, tmp
);
471 COPY_4FV( mat
->Diffuse
, src
[1].Diffuse
);
472 FLOAT_COLOR_TO_UBYTE_COLOR(ctx
->Light
.BaseAlpha
[1], mat
->Diffuse
[3]);
474 if (bitmask
& FRONT_SPECULAR_BIT
) {
475 struct gl_material
*mat
= &ctx
->Light
.Material
[0];
476 SUB_3V( tmp
, src
[0].Specular
, mat
->Specular
);
477 foreach (light
, list
) {
478 if (light
->Flags
& LIGHT_SPECULAR
) {
479 ACC_SCALE_3V( light
->MatSpecular
[0], light
->Specular
, tmp
);
480 light
->IsMatSpecular
[0] =
481 (LEN_SQUARED_3FV(light
->MatSpecular
[0]) > 1e-16);
484 COPY_4FV( mat
->Specular
, src
[0].Specular
);
486 if (bitmask
& BACK_SPECULAR_BIT
) {
487 struct gl_material
*mat
= &ctx
->Light
.Material
[1];
488 SUB_3V( tmp
, src
[1].Specular
, mat
->Specular
);
489 foreach (light
, list
) {
490 if (light
->Flags
& LIGHT_SPECULAR
) {
491 ACC_SCALE_3V( light
->MatSpecular
[1], light
->Specular
, tmp
);
492 light
->IsMatSpecular
[1] =
493 (LEN_SQUARED_3FV(light
->MatSpecular
[1]) > 1e-16);
496 COPY_4FV( mat
->Specular
, src
[1].Specular
);
498 if (bitmask
& FRONT_EMISSION_BIT
) {
499 struct gl_material
*mat
= &ctx
->Light
.Material
[0];
500 SUB_3V( tmp
, src
[0].Emission
, mat
->Emission
);
501 ACC_3V( ctx
->Light
.BaseColor
[0], tmp
);
502 COPY_4FV( mat
->Emission
, src
[0].Emission
);
504 if (bitmask
& BACK_EMISSION_BIT
) {
505 struct gl_material
*mat
= &ctx
->Light
.Material
[1];
506 SUB_3V( tmp
, src
[1].Emission
, mat
->Emission
);
507 ACC_3V( ctx
->Light
.BaseColor
[1], tmp
);
508 COPY_4FV( mat
->Emission
, src
[1].Emission
);
510 if (bitmask
& FRONT_SHININESS_BIT
) {
511 GLfloat shininess
= ctx
->Light
.Material
[0].Shininess
= src
[0].Shininess
;
512 gl_compute_shine_table( ctx
, 0, shininess
);
513 gl_compute_shine_table( ctx
, 2, shininess
* .5 );
515 if (bitmask
& BACK_SHININESS_BIT
) {
516 GLfloat shininess
= ctx
->Light
.Material
[1].Shininess
= src
[1].Shininess
;
517 gl_compute_shine_table( ctx
, 1, shininess
);
518 gl_compute_shine_table( ctx
, 3, shininess
* .5 );
520 if (bitmask
& FRONT_INDEXES_BIT
) {
521 ctx
->Light
.Material
[0].AmbientIndex
= src
[0].AmbientIndex
;
522 ctx
->Light
.Material
[0].DiffuseIndex
= src
[0].DiffuseIndex
;
523 ctx
->Light
.Material
[0].SpecularIndex
= src
[0].SpecularIndex
;
525 if (bitmask
& BACK_INDEXES_BIT
) {
526 ctx
->Light
.Material
[1].AmbientIndex
= src
[1].AmbientIndex
;
527 ctx
->Light
.Material
[1].DiffuseIndex
= src
[1].DiffuseIndex
;
528 ctx
->Light
.Material
[1].SpecularIndex
= src
[1].SpecularIndex
;
538 void gl_update_color_material( GLcontext
*ctx
,
539 const GLubyte rgba
[4] )
541 struct gl_light
*light
, *list
= &ctx
->Light
.EnabledList
;
542 GLuint bitmask
= ctx
->Light
.ColorMaterialBitmask
;
543 GLfloat tmp
[4], color
[4];
545 UBYTE_RGBA_TO_FLOAT_RGBA( color
, rgba
);
547 if (bitmask
& FRONT_AMBIENT_BIT
) {
548 struct gl_material
*mat
= &ctx
->Light
.Material
[0];
549 SUB_3V( tmp
, color
, mat
->Ambient
);
550 ACC_SCALE_3V( ctx
->Light
.BaseColor
[0], ctx
->Light
.Model
.Ambient
, tmp
);
551 foreach (light
, list
) {
552 ACC_SCALE_3V( ctx
->Light
.BaseColor
[0], light
->Ambient
, tmp
);
554 COPY_4FV( mat
->Ambient
, color
);
557 if (bitmask
& BACK_AMBIENT_BIT
) {
558 struct gl_material
*mat
= &ctx
->Light
.Material
[1];
559 SUB_3V( tmp
, color
, 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
, color
);
567 if (bitmask
& FRONT_DIFFUSE_BIT
) {
568 struct gl_material
*mat
= &ctx
->Light
.Material
[0];
569 SUB_3V( tmp
, color
, mat
->Diffuse
);
570 foreach (light
, list
) {
571 ACC_SCALE_3V( light
->MatDiffuse
[0], light
->Diffuse
, tmp
);
573 COPY_4FV( mat
->Diffuse
, color
);
574 FLOAT_COLOR_TO_UBYTE_COLOR(ctx
->Light
.BaseAlpha
[0], mat
->Diffuse
[3]);
577 if (bitmask
& BACK_DIFFUSE_BIT
) {
578 struct gl_material
*mat
= &ctx
->Light
.Material
[1];
579 SUB_3V( tmp
, color
, mat
->Diffuse
);
580 foreach (light
, list
) {
581 ACC_SCALE_3V( light
->MatDiffuse
[1], light
->Diffuse
, tmp
);
583 COPY_4FV( mat
->Diffuse
, color
);
584 FLOAT_COLOR_TO_UBYTE_COLOR(ctx
->Light
.BaseAlpha
[1], mat
->Diffuse
[3]);
587 if (bitmask
& FRONT_SPECULAR_BIT
) {
588 struct gl_material
*mat
= &ctx
->Light
.Material
[0];
589 SUB_3V( tmp
, color
, mat
->Specular
);
590 foreach (light
, list
) {
591 if (light
->Flags
& LIGHT_SPECULAR
) {
592 ACC_SCALE_3V( light
->MatSpecular
[0], light
->Specular
, tmp
);
593 light
->IsMatSpecular
[0] =
594 (LEN_SQUARED_3FV(light
->MatSpecular
[0]) > 1e-16);
597 COPY_4FV( mat
->Specular
, color
);
599 if (bitmask
& BACK_SPECULAR_BIT
) {
600 struct gl_material
*mat
= &ctx
->Light
.Material
[1];
601 SUB_3V( tmp
, color
, mat
->Specular
);
602 foreach (light
, list
) {
603 if (light
->Flags
& LIGHT_SPECULAR
) {
604 ACC_SCALE_3V( light
->MatSpecular
[1], light
->Specular
, tmp
);
605 light
->IsMatSpecular
[1] =
606 (LEN_SQUARED_3FV(light
->MatSpecular
[1]) > 1e-16);
609 COPY_4FV( mat
->Specular
, color
);
611 if (bitmask
& FRONT_EMISSION_BIT
) {
612 struct gl_material
*mat
= &ctx
->Light
.Material
[0];
613 SUB_3V( tmp
, color
, mat
->Emission
);
614 ACC_3V( ctx
->Light
.BaseColor
[0], tmp
);
615 COPY_4FV( mat
->Emission
, color
);
617 if (bitmask
& BACK_EMISSION_BIT
) {
618 struct gl_material
*mat
= &ctx
->Light
.Material
[1];
619 SUB_3V( tmp
, color
, mat
->Emission
);
620 ACC_3V( ctx
->Light
.BaseColor
[1], tmp
);
621 COPY_4FV( mat
->Emission
, color
);
628 void gl_ColorMaterial( GLcontext
*ctx
, GLenum face
, GLenum mode
)
631 GLuint legal
= (FRONT_EMISSION_BIT
| BACK_EMISSION_BIT
|
632 FRONT_SPECULAR_BIT
| BACK_SPECULAR_BIT
|
633 FRONT_DIFFUSE_BIT
| BACK_DIFFUSE_BIT
|
634 FRONT_AMBIENT_BIT
| BACK_AMBIENT_BIT
);
636 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glColorMaterial");
638 bitmask
= gl_material_bitmask( ctx
, face
, mode
, legal
, "glColorMaterial" );
641 ctx
->Light
.ColorMaterialBitmask
= bitmask
;
642 ctx
->Light
.ColorMaterialFace
= face
;
643 ctx
->Light
.ColorMaterialMode
= mode
;
649 /* KW: This is now called directly (ie by name) from the glMaterial*
652 void gl_Materialfv( GLcontext
*ctx
,
653 GLenum face
, GLenum pname
, const GLfloat
*params
)
655 struct immediate
*IM
;
656 struct gl_material
*mat
;
660 bitmask
= gl_material_bitmask( ctx
, face
, pname
, ~0, "gl_Materialfv" );
667 if (!(IM
->Flag
[count
] & VERT_MATERIAL
)) {
668 IM
->Flag
[count
] |= VERT_MATERIAL
;
669 IM
->MaterialMask
[count
] = 0;
672 IM
->MaterialMask
[count
] |= bitmask
;
673 mat
= IM
->Material
[count
];
674 IM
->LastMaterial
= count
;
676 if (bitmask
& FRONT_AMBIENT_BIT
) {
677 COPY_4FV( mat
[0].Ambient
, params
);
679 if (bitmask
& BACK_AMBIENT_BIT
) {
680 COPY_4FV( mat
[1].Ambient
, params
);
682 if (bitmask
& FRONT_DIFFUSE_BIT
) {
683 COPY_4FV( mat
[0].Diffuse
, params
);
685 if (bitmask
& BACK_DIFFUSE_BIT
) {
686 COPY_4FV( mat
[1].Diffuse
, params
);
688 if (bitmask
& FRONT_SPECULAR_BIT
) {
689 COPY_4FV( mat
[0].Specular
, params
);
691 if (bitmask
& BACK_SPECULAR_BIT
) {
692 COPY_4FV( mat
[1].Specular
, params
);
694 if (bitmask
& FRONT_EMISSION_BIT
) {
695 COPY_4FV( mat
[0].Emission
, params
);
697 if (bitmask
& BACK_EMISSION_BIT
) {
698 COPY_4FV( mat
[1].Emission
, params
);
700 if (bitmask
& FRONT_SHININESS_BIT
) {
701 GLfloat shininess
= CLAMP( params
[0], 0.0F
, 128.0F
);
702 mat
[0].Shininess
= shininess
;
704 if (bitmask
& BACK_SHININESS_BIT
) {
705 GLfloat shininess
= CLAMP( params
[0], 0.0F
, 128.0F
);
706 mat
[1].Shininess
= shininess
;
708 if (bitmask
& FRONT_INDEXES_BIT
) {
709 mat
[0].AmbientIndex
= params
[0];
710 mat
[0].DiffuseIndex
= params
[1];
711 mat
[0].SpecularIndex
= params
[2];
713 if (bitmask
& BACK_INDEXES_BIT
) {
714 mat
[1].AmbientIndex
= params
[0];
715 mat
[1].DiffuseIndex
= params
[1];
716 mat
[1].SpecularIndex
= params
[2];
723 void gl_GetMaterialfv( GLcontext
*ctx
,
724 GLenum face
, GLenum pname
, GLfloat
*params
)
728 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glGetMaterialfv");
730 if (face
==GL_FRONT
) {
733 else if (face
==GL_BACK
) {
737 gl_error( ctx
, GL_INVALID_ENUM
, "glGetMaterialfv(face)" );
742 COPY_4FV( params
, ctx
->Light
.Material
[f
].Ambient
);
745 COPY_4FV( params
, ctx
->Light
.Material
[f
].Diffuse
);
748 COPY_4FV( params
, ctx
->Light
.Material
[f
].Specular
);
751 COPY_4FV( params
, ctx
->Light
.Material
[f
].Emission
);
754 *params
= ctx
->Light
.Material
[f
].Shininess
;
756 case GL_COLOR_INDEXES
:
757 params
[0] = ctx
->Light
.Material
[f
].AmbientIndex
;
758 params
[1] = ctx
->Light
.Material
[f
].DiffuseIndex
;
759 params
[2] = ctx
->Light
.Material
[f
].SpecularIndex
;
762 gl_error( ctx
, GL_INVALID_ENUM
, "glGetMaterialfv(pname)" );
768 void gl_GetMaterialiv( GLcontext
*ctx
,
769 GLenum face
, GLenum pname
, GLint
*params
)
773 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glGetMaterialiv");
775 if (face
==GL_FRONT
) {
778 else if (face
==GL_BACK
) {
782 gl_error( ctx
, GL_INVALID_ENUM
, "glGetMaterialiv(face)" );
787 params
[0] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Ambient
[0] );
788 params
[1] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Ambient
[1] );
789 params
[2] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Ambient
[2] );
790 params
[3] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Ambient
[3] );
793 params
[0] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Diffuse
[0] );
794 params
[1] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Diffuse
[1] );
795 params
[2] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Diffuse
[2] );
796 params
[3] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Diffuse
[3] );
799 params
[0] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Specular
[0] );
800 params
[1] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Specular
[1] );
801 params
[2] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Specular
[2] );
802 params
[3] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Specular
[3] );
805 params
[0] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Emission
[0] );
806 params
[1] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Emission
[1] );
807 params
[2] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Emission
[2] );
808 params
[3] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Emission
[3] );
811 *params
= ROUNDF( ctx
->Light
.Material
[f
].Shininess
);
813 case GL_COLOR_INDEXES
:
814 params
[0] = ROUNDF( ctx
->Light
.Material
[f
].AmbientIndex
);
815 params
[1] = ROUNDF( ctx
->Light
.Material
[f
].DiffuseIndex
);
816 params
[2] = ROUNDF( ctx
->Light
.Material
[f
].SpecularIndex
);
819 gl_error( ctx
, GL_INVALID_ENUM
, "glGetMaterialfv(pname)" );
826 /**********************************************************************/
827 /***** Lighting computation *****/
828 /**********************************************************************/
833 * When two-sided lighting is enabled we compute the color (or index)
834 * for both the front and back side of the primitive. Then, when the
835 * orientation of the facet is later learned, we can determine which
836 * color (or index) to use for rendering.
838 * KW: We now know orientation in advance and only shade for
839 * the side or sides which are actually required.
843 * V = vertex position
844 * P = light source position
849 * // light at infinity
850 * IF local_viewer THEN
851 * VP_inf_norm = unit vector from V to P // Precompute
854 * h_inf_norm = Normalize( VP + <0,0,1> ) // Precompute
859 * Normalize( v ) = normalized vector v
860 * Magnitude( v ) = length of vector v
866 * Whenever the spotlight exponent for a light changes we must call
867 * this function to recompute the exponent lookup table.
869 void gl_compute_spot_exp_table( struct gl_light
*l
)
872 double exponent
= l
->SpotExponent
;
876 l
->SpotExpTable
[0][0] = 0.0;
878 for (i
=EXP_TABLE_SIZE
-1;i
>0;i
--) {
880 tmp
= pow(i
/(double)(EXP_TABLE_SIZE
-1), exponent
);
881 if (tmp
< FLT_MIN
*100.0) {
886 l
->SpotExpTable
[i
][0] = tmp
;
888 for (i
=0;i
<EXP_TABLE_SIZE
-1;i
++) {
889 l
->SpotExpTable
[i
][1] = l
->SpotExpTable
[i
+1][0] - l
->SpotExpTable
[i
][0];
891 l
->SpotExpTable
[EXP_TABLE_SIZE
-1][1] = 0.0;
897 /* Calculate a new shine table. Doing this here saves a branch in
898 * lighting, and the cost of doing it early may be partially offset
899 * by keeping a MRU cache of shine tables for various shine values.
901 static void compute_shine_table( struct gl_shine_tab
*tab
, GLfloat shininess
)
904 GLfloat
*m
= tab
->tab
;
907 if (shininess
== 0) {
908 for (i
= 1 ; i
<= SHINE_TABLE_SIZE
; i
++)
911 for (i
= 1 ; i
<= SHINE_TABLE_SIZE
; i
++) {
912 double t
= pow( i
/(GLfloat
)SHINE_TABLE_SIZE
, shininess
);
914 if (t
> 1e-20) m
[i
] = t
;
918 tab
->shininess
= shininess
;
921 #define DISTSQR(a,b) ((a-b)*(a-b))
923 void gl_compute_shine_table( GLcontext
*ctx
, GLuint i
, GLfloat shininess
)
925 struct gl_shine_tab
*list
= ctx
->ShineTabList
;
926 struct gl_shine_tab
*s
;
929 if ( DISTSQR(s
->shininess
, shininess
) < 1e-4 )
935 if (s
->refcount
== 0) break;
937 compute_shine_table( s
, shininess
);
940 ctx
->ShineTable
[i
]->refcount
--;
941 ctx
->ShineTable
[i
] = s
;
942 move_to_tail( list
, s
);
949 void gl_reinit_light_attrib( GLcontext
*ctx
, struct gl_light_attrib
*l
)
953 if (ctx
->ShineTable
[0]->shininess
!= l
->Material
[0].Shininess
) {
954 gl_compute_shine_table( ctx
, 0, l
->Material
[0].Shininess
);
955 gl_compute_shine_table( ctx
, 2, l
->Material
[0].Shininess
* .5 );
958 if (ctx
->ShineTable
[1]->shininess
!= l
->Material
[1].Shininess
) {
959 gl_compute_shine_table( ctx
, 1, l
->Material
[1].Shininess
);
960 gl_compute_shine_table( ctx
, 3, l
->Material
[1].Shininess
* .5 );
963 make_empty_list( &l
->EnabledList
);
964 for (i
= 0 ; i
< MAX_LIGHTS
; i
++) {
965 if (l
->Light
[i
].Enabled
)
966 insert_at_tail( &l
->EnabledList
, &l
->Light
[i
] );
973 * Examine current lighting parameters to determine if the optimized lighting
974 * function can be used.
975 * Also, precompute some lighting values such as the products of light
976 * source and material ambient, diffuse and specular coefficients.
978 void gl_update_lighting( GLcontext
*ctx
)
980 struct gl_light
*light
;
982 ctx
->Light
.Flags
= 0;
984 foreach(light
, &ctx
->Light
.EnabledList
) {
988 if (light
->EyePosition
[3] != 0.0F
)
989 light
->Flags
|= LIGHT_POSITIONAL
;
991 if (LEN_SQUARED_3FV(light
->Specular
) > 1e-16)
992 light
->Flags
|= LIGHT_SPECULAR
;
994 if (light
->SpotCutoff
!= 180.0F
)
995 light
->Flags
|= LIGHT_SPOT
;
997 ctx
->Light
.Flags
|= light
->Flags
;
1000 ctx
->Light
.NeedVertices
=
1001 ((ctx
->Light
.Flags
& (LIGHT_POSITIONAL
|LIGHT_SPOT
)) ||
1002 (ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
) ||
1003 (ctx
->Light
.Model
.LocalViewer
&& (ctx
->Light
.Flags
& LIGHT_SPECULAR
)));
1006 /* Precompute some shading values.
1008 if (ctx
->Visual
->RGBAflag
)
1010 GLuint sides
= ((ctx
->TriangleCaps
& DD_TRI_LIGHT_TWOSIDE
) ? 2 : 1);
1012 for (side
=0; side
< sides
; side
++) {
1013 struct gl_material
*mat
= &ctx
->Light
.Material
[side
];
1015 COPY_3V(ctx
->Light
.BaseColor
[side
], mat
->Emission
);
1016 ACC_SCALE_3V(ctx
->Light
.BaseColor
[side
],
1017 ctx
->Light
.Model
.Ambient
,
1020 FLOAT_COLOR_TO_UBYTE_COLOR(ctx
->Light
.BaseAlpha
[side
],
1021 ctx
->Light
.Material
[side
].Diffuse
[3] );
1024 foreach (light
, &ctx
->Light
.EnabledList
) {
1025 for (side
=0; side
< sides
; side
++) {
1026 struct gl_material
*mat
= &ctx
->Light
.Material
[side
];
1027 SCALE_3V( light
->MatDiffuse
[side
], light
->Diffuse
, mat
->Diffuse
);
1028 SCALE_3V( light
->MatAmbient
[side
], light
->Ambient
, mat
->Ambient
);
1029 ACC_3V( ctx
->Light
.BaseColor
[side
], light
->MatAmbient
[side
] );
1030 if (light
->Flags
& LIGHT_SPECULAR
)
1032 SCALE_3V( light
->MatSpecular
[side
], light
->Specular
,
1034 light
->IsMatSpecular
[side
] =
1035 (LEN_SQUARED_3FV(light
->MatSpecular
[side
]) > 1e-16);
1038 light
->IsMatSpecular
[side
] = 0;
1044 static GLfloat ci
[3] = { .30, .59, .11 };
1046 foreach(light
, &ctx
->Light
.EnabledList
) {
1047 light
->dli
= DOT3(ci
, light
->Diffuse
);
1048 light
->sli
= DOT3(ci
, light
->Specular
);
1053 /* Need to seriously restrict the circumstances under which these
1054 * calc's are performed.
1056 void gl_compute_light_positions( GLcontext
*ctx
)
1058 struct gl_light
*light
;
1060 if (ctx
->Light
.NeedVertices
&& !ctx
->Light
.Model
.LocalViewer
) {
1061 GLfloat eye_z
[3] = { 0, 0, 1 };
1062 if (!ctx
->NeedEyeCoords
) {
1063 TRANSFORM_NORMAL( ctx
->EyeZDir
, eye_z
, ctx
->ModelView
.m
);
1065 COPY_3V( ctx
->EyeZDir
, eye_z
);
1069 foreach (light
, &ctx
->Light
.EnabledList
) {
1071 if (!ctx
->NeedEyeCoords
) {
1072 TRANSFORM_POINT( light
->Position
, ctx
->ModelView
.inv
,
1073 light
->EyePosition
);
1075 COPY_4FV( light
->Position
, light
->EyePosition
);
1078 if (!(light
->Flags
& LIGHT_POSITIONAL
))
1080 /* VP (VP) = Normalize( Position ) */
1081 COPY_3V( light
->VP_inf_norm
, light
->Position
);
1082 NORMALIZE_3FV( light
->VP_inf_norm
);
1084 if (!ctx
->Light
.Model
.LocalViewer
)
1086 /* h_inf_norm = Normalize( V_to_P + <0,0,1> ) */
1087 ADD_3V( light
->h_inf_norm
, light
->VP_inf_norm
, ctx
->EyeZDir
);
1088 NORMALIZE_3FV( light
->h_inf_norm
);
1091 light
->VP_inf_spot_attenuation
= 1.0;
1094 if (light
->Flags
& LIGHT_SPOT
)
1096 if (ctx
->NeedEyeNormals
) {
1097 COPY_3V( light
->NormDirection
, light
->EyeDirection
);
1099 TRANSFORM_NORMAL( light
->NormDirection
,
1100 light
->EyeDirection
,
1104 NORMALIZE_3FV( light
->NormDirection
);
1107 /* Unlikely occurrance?
1109 if (!(light
->Flags
& LIGHT_POSITIONAL
)) {
1110 GLfloat PV_dot_dir
= - DOT3(light
->VP_inf_norm
,
1111 light
->NormDirection
);
1113 if (PV_dot_dir
> light
->CosCutoff
) {
1114 double x
= PV_dot_dir
* (EXP_TABLE_SIZE
-1);
1116 light
->VP_inf_spot_attenuation
=
1117 (light
->SpotExpTable
[k
][0] +
1118 (x
-k
)*light
->SpotExpTable
[k
][1]);
1121 light
->VP_inf_spot_attenuation
= 0;
1131 void gl_update_normal_transform( GLcontext
*ctx
)
1133 GLuint new_flag
= 0;
1134 normal_func
*last
= ctx
->NormalTransform
;
1136 ctx
->vb_rescale_factor
= 1.0;
1138 if (ctx
->NeedEyeCoords
) {
1139 if (ctx
->NeedNormals
) {
1140 GLuint transform
= NORM_TRANSFORM_NO_ROT
;
1142 if (ctx
->ModelView
.flags
& (MAT_FLAG_GENERAL
|
1144 MAT_FLAG_GENERAL_3D
|
1145 MAT_FLAG_PERSPECTIVE
))
1146 transform
= NORM_TRANSFORM
;
1149 new_flag
= ctx
->NewState
& NEW_MODELVIEW
;
1150 ctx
->vb_rescale_factor
= ctx
->rescale_factor
;
1152 if (ctx
->Transform
.Normalize
)
1154 ctx
->NormalTransform
= gl_normal_tab
[transform
| NORM_NORMALIZE
];
1156 else if (ctx
->Transform
.RescaleNormals
&&
1157 ctx
->rescale_factor
!= 1.0)
1159 ctx
->NormalTransform
= gl_normal_tab
[transform
| NORM_RESCALE
];
1163 ctx
->NormalTransform
= gl_normal_tab
[transform
];
1166 ctx
->NormalTransform
= 0;
1170 if (ctx
->NeedNormals
) {
1171 ctx
->vb_rescale_factor
= 1.0/ctx
->rescale_factor
;
1173 if (ctx
->Transform
.Normalize
)
1175 ctx
->NormalTransform
= gl_normal_tab
[NORM_NORMALIZE
];
1177 else if (!ctx
->Transform
.RescaleNormals
&&
1178 ctx
->rescale_factor
!= 1.0)
1180 ctx
->NormalTransform
= gl_normal_tab
[NORM_RESCALE
];
1184 ctx
->NormalTransform
= 0;
1187 ctx
->NormalTransform
= 0;
1191 if (last
!= ctx
->NormalTransform
|| new_flag
)
1192 ctx
->NewState
|= NEW_NORMAL_TRANSFORM
;