1 /* $Id: light.c,v 1.44 2001/05/09 12:24:51 keithw Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2001 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"
42 #include "math/m_xform.h"
43 #include "math/m_matrix.h"
47 /* XXX this is a bit of a hack needed for compilation within XFree86 */
54 _mesa_ShadeModel( GLenum mode
)
56 GET_CURRENT_CONTEXT(ctx
);
57 ASSERT_OUTSIDE_BEGIN_END(ctx
);
59 if (MESA_VERBOSE
& VERBOSE_API
)
60 fprintf(stderr
, "glShadeModel %s\n", _mesa_lookup_enum_by_nr(mode
));
62 if (mode
!= GL_FLAT
&& mode
!= GL_SMOOTH
) {
63 _mesa_error( ctx
, GL_INVALID_ENUM
, "glShadeModel" );
67 if (ctx
->Light
.ShadeModel
== mode
)
70 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
71 ctx
->Light
.ShadeModel
= mode
;
72 ctx
->_TriangleCaps
^= DD_FLATSHADE
;
73 if (ctx
->Driver
.ShadeModel
)
74 (*ctx
->Driver
.ShadeModel
)( ctx
, mode
);
80 _mesa_Lightf( GLenum light
, GLenum pname
, GLfloat param
)
82 _mesa_Lightfv( light
, pname
, ¶m
);
87 _mesa_Lightfv( GLenum light
, GLenum pname
, const GLfloat
*params
)
89 GET_CURRENT_CONTEXT(ctx
);
90 GLint i
= (GLint
) (light
- GL_LIGHT0
);
91 struct gl_light
*l
= &ctx
->Light
.Light
[i
];
93 if (i
< 0 || i
>= (GLint
) ctx
->Const
.MaxLights
) {
94 _mesa_error( ctx
, GL_INVALID_ENUM
, "glLight" );
100 if (TEST_EQ_4V(l
->Ambient
, params
))
102 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
103 COPY_4V( l
->Ambient
, params
);
106 if (TEST_EQ_4V(l
->Diffuse
, params
))
108 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
109 COPY_4V( l
->Diffuse
, params
);
112 if (TEST_EQ_4V(l
->Specular
, params
))
114 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
115 COPY_4V( l
->Specular
, params
);
119 /* transform position by ModelView matrix */
120 TRANSFORM_POINT( tmp
, ctx
->ModelView
.m
, params
);
121 if (TEST_EQ_4V(l
->EyePosition
, tmp
))
123 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
124 COPY_4V(l
->EyePosition
, tmp
);
125 if (l
->EyePosition
[3] != 0.0F
)
126 l
->_Flags
|= LIGHT_POSITIONAL
;
128 l
->_Flags
&= ~LIGHT_POSITIONAL
;
131 case GL_SPOT_DIRECTION
: {
133 /* transform direction by inverse modelview */
134 if (ctx
->ModelView
.flags
& MAT_DIRTY_INVERSE
) {
135 _math_matrix_analyse( &ctx
->ModelView
);
137 TRANSFORM_NORMAL( tmp
, params
, ctx
->ModelView
.inv
);
138 if (TEST_EQ_3V(l
->EyeDirection
, tmp
))
140 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
141 COPY_3V(l
->EyeDirection
, tmp
);
144 case GL_SPOT_EXPONENT
:
145 if (params
[0]<0.0 || params
[0]>128.0) {
146 _mesa_error( ctx
, GL_INVALID_VALUE
, "glLight" );
149 if (l
->SpotExponent
== params
[0])
151 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
152 l
->SpotExponent
= params
[0];
153 _mesa_invalidate_spot_exp_table( l
);
156 if ((params
[0]<0.0 || params
[0]>90.0) && params
[0]!=180.0) {
157 _mesa_error( ctx
, GL_INVALID_VALUE
, "glLight" );
160 if (l
->SpotCutoff
== params
[0])
162 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
163 l
->SpotCutoff
= params
[0];
164 l
->_CosCutoff
= cos(params
[0]*DEG2RAD
);
165 if (l
->_CosCutoff
< 0)
167 if (l
->SpotCutoff
!= 180.0F
)
168 l
->_Flags
|= LIGHT_SPOT
;
170 l
->_Flags
&= ~LIGHT_SPOT
;
172 case GL_CONSTANT_ATTENUATION
:
174 _mesa_error( ctx
, GL_INVALID_VALUE
, "glLight" );
177 if (l
->ConstantAttenuation
== params
[0])
179 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
180 l
->ConstantAttenuation
= params
[0];
182 case GL_LINEAR_ATTENUATION
:
184 _mesa_error( ctx
, GL_INVALID_VALUE
, "glLight" );
187 if (l
->LinearAttenuation
== params
[0])
189 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
190 l
->LinearAttenuation
= params
[0];
192 case GL_QUADRATIC_ATTENUATION
:
194 _mesa_error( ctx
, GL_INVALID_VALUE
, "glLight" );
197 if (l
->QuadraticAttenuation
== params
[0])
199 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
200 l
->QuadraticAttenuation
= params
[0];
203 _mesa_error( ctx
, GL_INVALID_ENUM
, "glLight" );
207 if (ctx
->Driver
.Lightfv
)
208 ctx
->Driver
.Lightfv( ctx
, light
, pname
, params
);
213 _mesa_Lighti( GLenum light
, GLenum pname
, GLint param
)
215 _mesa_Lightiv( light
, pname
, ¶m
);
220 _mesa_Lightiv( GLenum light
, GLenum pname
, const GLint
*params
)
228 fparam
[0] = INT_TO_FLOAT( params
[0] );
229 fparam
[1] = INT_TO_FLOAT( params
[1] );
230 fparam
[2] = INT_TO_FLOAT( params
[2] );
231 fparam
[3] = INT_TO_FLOAT( params
[3] );
234 fparam
[0] = (GLfloat
) params
[0];
235 fparam
[1] = (GLfloat
) params
[1];
236 fparam
[2] = (GLfloat
) params
[2];
237 fparam
[3] = (GLfloat
) params
[3];
239 case GL_SPOT_DIRECTION
:
240 fparam
[0] = (GLfloat
) params
[0];
241 fparam
[1] = (GLfloat
) params
[1];
242 fparam
[2] = (GLfloat
) params
[2];
244 case GL_SPOT_EXPONENT
:
246 case GL_CONSTANT_ATTENUATION
:
247 case GL_LINEAR_ATTENUATION
:
248 case GL_QUADRATIC_ATTENUATION
:
249 fparam
[0] = (GLfloat
) params
[0];
252 /* error will be caught later in gl_Lightfv */
256 _mesa_Lightfv( light
, pname
, fparam
);
262 _mesa_GetLightfv( GLenum light
, GLenum pname
, GLfloat
*params
)
264 GET_CURRENT_CONTEXT(ctx
);
265 GLint l
= (GLint
) (light
- GL_LIGHT0
);
266 ASSERT_OUTSIDE_BEGIN_END(ctx
);
268 if (l
< 0 || l
>= (GLint
) ctx
->Const
.MaxLights
) {
269 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetLightfv" );
275 COPY_4V( params
, ctx
->Light
.Light
[l
].Ambient
);
278 COPY_4V( params
, ctx
->Light
.Light
[l
].Diffuse
);
281 COPY_4V( params
, ctx
->Light
.Light
[l
].Specular
);
284 COPY_4V( params
, ctx
->Light
.Light
[l
].EyePosition
);
286 case GL_SPOT_DIRECTION
:
287 COPY_3V( params
, ctx
->Light
.Light
[l
].EyeDirection
);
289 case GL_SPOT_EXPONENT
:
290 params
[0] = ctx
->Light
.Light
[l
].SpotExponent
;
293 params
[0] = ctx
->Light
.Light
[l
].SpotCutoff
;
295 case GL_CONSTANT_ATTENUATION
:
296 params
[0] = ctx
->Light
.Light
[l
].ConstantAttenuation
;
298 case GL_LINEAR_ATTENUATION
:
299 params
[0] = ctx
->Light
.Light
[l
].LinearAttenuation
;
301 case GL_QUADRATIC_ATTENUATION
:
302 params
[0] = ctx
->Light
.Light
[l
].QuadraticAttenuation
;
305 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetLightfv" );
313 _mesa_GetLightiv( GLenum light
, GLenum pname
, GLint
*params
)
315 GET_CURRENT_CONTEXT(ctx
);
316 GLint l
= (GLint
) (light
- GL_LIGHT0
);
317 ASSERT_OUTSIDE_BEGIN_END(ctx
);
319 if (l
< 0 || l
>= (GLint
) ctx
->Const
.MaxLights
) {
320 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetLightiv" );
326 params
[0] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Ambient
[0]);
327 params
[1] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Ambient
[1]);
328 params
[2] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Ambient
[2]);
329 params
[3] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Ambient
[3]);
332 params
[0] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Diffuse
[0]);
333 params
[1] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Diffuse
[1]);
334 params
[2] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Diffuse
[2]);
335 params
[3] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Diffuse
[3]);
338 params
[0] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Specular
[0]);
339 params
[1] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Specular
[1]);
340 params
[2] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Specular
[2]);
341 params
[3] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Specular
[3]);
344 params
[0] = (GLint
) ctx
->Light
.Light
[l
].EyePosition
[0];
345 params
[1] = (GLint
) ctx
->Light
.Light
[l
].EyePosition
[1];
346 params
[2] = (GLint
) ctx
->Light
.Light
[l
].EyePosition
[2];
347 params
[3] = (GLint
) ctx
->Light
.Light
[l
].EyePosition
[3];
349 case GL_SPOT_DIRECTION
:
350 params
[0] = (GLint
) ctx
->Light
.Light
[l
].EyeDirection
[0];
351 params
[1] = (GLint
) ctx
->Light
.Light
[l
].EyeDirection
[1];
352 params
[2] = (GLint
) ctx
->Light
.Light
[l
].EyeDirection
[2];
354 case GL_SPOT_EXPONENT
:
355 params
[0] = (GLint
) ctx
->Light
.Light
[l
].SpotExponent
;
358 params
[0] = (GLint
) ctx
->Light
.Light
[l
].SpotCutoff
;
360 case GL_CONSTANT_ATTENUATION
:
361 params
[0] = (GLint
) ctx
->Light
.Light
[l
].ConstantAttenuation
;
363 case GL_LINEAR_ATTENUATION
:
364 params
[0] = (GLint
) ctx
->Light
.Light
[l
].LinearAttenuation
;
366 case GL_QUADRATIC_ATTENUATION
:
367 params
[0] = (GLint
) ctx
->Light
.Light
[l
].QuadraticAttenuation
;
370 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetLightiv" );
377 /**********************************************************************/
378 /*** Light Model ***/
379 /**********************************************************************/
383 _mesa_LightModelfv( GLenum pname
, const GLfloat
*params
)
387 GET_CURRENT_CONTEXT(ctx
);
388 ASSERT_OUTSIDE_BEGIN_END(ctx
);
391 case GL_LIGHT_MODEL_AMBIENT
:
392 if (TEST_EQ_4V( ctx
->Light
.Model
.Ambient
, params
))
394 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
395 COPY_4V( ctx
->Light
.Model
.Ambient
, params
);
397 case GL_LIGHT_MODEL_LOCAL_VIEWER
:
398 newbool
= (params
[0]!=0.0);
399 if (ctx
->Light
.Model
.LocalViewer
== newbool
)
401 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
402 ctx
->Light
.Model
.LocalViewer
= newbool
;
404 case GL_LIGHT_MODEL_TWO_SIDE
:
405 newbool
= (params
[0]!=0.0);
406 if (ctx
->Light
.Model
.TwoSide
== newbool
)
408 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
409 ctx
->Light
.Model
.TwoSide
= newbool
;
411 case GL_LIGHT_MODEL_COLOR_CONTROL
:
412 if (params
[0] == (GLfloat
) GL_SINGLE_COLOR
)
413 newenum
= GL_SINGLE_COLOR
;
414 else if (params
[0] == (GLfloat
) GL_SEPARATE_SPECULAR_COLOR
)
415 newenum
= GL_SEPARATE_SPECULAR_COLOR
;
417 _mesa_error( ctx
, GL_INVALID_ENUM
, "glLightModel(param)" );
420 if (ctx
->Light
.Model
.ColorControl
== newenum
)
422 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
423 ctx
->Light
.Model
.ColorControl
= newenum
;
425 if ((ctx
->Light
.Enabled
&&
426 ctx
->Light
.Model
.ColorControl
==GL_SEPARATE_SPECULAR_COLOR
)
427 || ctx
->Fog
.ColorSumEnabled
)
428 ctx
->_TriangleCaps
|= DD_SEPARATE_SPECULAR
;
430 ctx
->_TriangleCaps
&= ~DD_SEPARATE_SPECULAR
;
434 _mesa_error( ctx
, GL_INVALID_ENUM
, "glLightModel" );
438 if (ctx
->Driver
.LightModelfv
)
439 ctx
->Driver
.LightModelfv( ctx
, pname
, params
);
444 _mesa_LightModeliv( GLenum pname
, const GLint
*params
)
449 case GL_LIGHT_MODEL_AMBIENT
:
450 fparam
[0] = INT_TO_FLOAT( params
[0] );
451 fparam
[1] = INT_TO_FLOAT( params
[1] );
452 fparam
[2] = INT_TO_FLOAT( params
[2] );
453 fparam
[3] = INT_TO_FLOAT( params
[3] );
455 case GL_LIGHT_MODEL_LOCAL_VIEWER
:
456 case GL_LIGHT_MODEL_TWO_SIDE
:
457 case GL_LIGHT_MODEL_COLOR_CONTROL
:
458 fparam
[0] = (GLfloat
) params
[0];
461 /* Error will be caught later in gl_LightModelfv */
464 _mesa_LightModelfv( pname
, fparam
);
469 _mesa_LightModeli( GLenum pname
, GLint param
)
471 _mesa_LightModeliv( pname
, ¶m
);
476 _mesa_LightModelf( GLenum pname
, GLfloat param
)
478 _mesa_LightModelfv( pname
, ¶m
);
483 /********** MATERIAL **********/
487 * Given a face and pname value (ala glColorMaterial), compute a bitmask
488 * of the targeted material values.
491 _mesa_material_bitmask( GLcontext
*ctx
, GLenum face
, GLenum pname
,
492 GLuint legal
, const char *where
)
496 /* Make a bitmask indicating what material attribute(s) we're updating */
499 bitmask
|= FRONT_EMISSION_BIT
| BACK_EMISSION_BIT
;
502 bitmask
|= FRONT_AMBIENT_BIT
| BACK_AMBIENT_BIT
;
505 bitmask
|= FRONT_DIFFUSE_BIT
| BACK_DIFFUSE_BIT
;
508 bitmask
|= FRONT_SPECULAR_BIT
| BACK_SPECULAR_BIT
;
511 bitmask
|= FRONT_SHININESS_BIT
| BACK_SHININESS_BIT
;
513 case GL_AMBIENT_AND_DIFFUSE
:
514 bitmask
|= FRONT_AMBIENT_BIT
| BACK_AMBIENT_BIT
;
515 bitmask
|= FRONT_DIFFUSE_BIT
| BACK_DIFFUSE_BIT
;
517 case GL_COLOR_INDEXES
:
518 bitmask
|= FRONT_INDEXES_BIT
| BACK_INDEXES_BIT
;
521 _mesa_error( ctx
, GL_INVALID_ENUM
, where
);
525 if (face
==GL_FRONT
) {
526 bitmask
&= FRONT_MATERIAL_BITS
;
528 else if (face
==GL_BACK
) {
529 bitmask
&= BACK_MATERIAL_BITS
;
531 else if (face
!= GL_FRONT_AND_BACK
) {
532 _mesa_error( ctx
, GL_INVALID_ENUM
, where
);
536 if (bitmask
& ~legal
) {
537 _mesa_error( ctx
, GL_INVALID_ENUM
, where
);
545 /* Perform a straight copy between pairs of materials.
547 void _mesa_copy_material_pairs( struct gl_material dst
[2],
548 const struct gl_material src
[2],
551 if (bitmask
& FRONT_EMISSION_BIT
) {
552 COPY_4FV( dst
[0].Emission
, src
[0].Emission
);
554 if (bitmask
& BACK_EMISSION_BIT
) {
555 COPY_4FV( dst
[1].Emission
, src
[1].Emission
);
557 if (bitmask
& FRONT_AMBIENT_BIT
) {
558 COPY_4FV( dst
[0].Ambient
, src
[0].Ambient
);
560 if (bitmask
& BACK_AMBIENT_BIT
) {
561 COPY_4FV( dst
[1].Ambient
, src
[1].Ambient
);
563 if (bitmask
& FRONT_DIFFUSE_BIT
) {
564 COPY_4FV( dst
[0].Diffuse
, src
[0].Diffuse
);
566 if (bitmask
& BACK_DIFFUSE_BIT
) {
567 COPY_4FV( dst
[1].Diffuse
, src
[1].Diffuse
);
569 if (bitmask
& FRONT_SPECULAR_BIT
) {
570 COPY_4FV( dst
[0].Specular
, src
[0].Specular
);
572 if (bitmask
& BACK_SPECULAR_BIT
) {
573 COPY_4FV( dst
[1].Specular
, src
[1].Specular
);
575 if (bitmask
& FRONT_SHININESS_BIT
) {
576 dst
[0].Shininess
= src
[0].Shininess
;
578 if (bitmask
& BACK_SHININESS_BIT
) {
579 dst
[1].Shininess
= src
[1].Shininess
;
581 if (bitmask
& FRONT_INDEXES_BIT
) {
582 dst
[0].AmbientIndex
= src
[0].AmbientIndex
;
583 dst
[0].DiffuseIndex
= src
[0].DiffuseIndex
;
584 dst
[0].SpecularIndex
= src
[0].SpecularIndex
;
586 if (bitmask
& BACK_INDEXES_BIT
) {
587 dst
[1].AmbientIndex
= src
[1].AmbientIndex
;
588 dst
[1].DiffuseIndex
= src
[1].DiffuseIndex
;
589 dst
[1].SpecularIndex
= src
[1].SpecularIndex
;
595 * Check if the global material has to be updated with info that was
596 * associated with a vertex via glMaterial.
597 * This function is used when any material values get changed between
598 * glBegin/glEnd either by calling glMaterial() or by calling glColor()
599 * when GL_COLOR_MATERIAL is enabled.
601 * src[0] is front material, src[1] is back material
603 * Additionally keeps the precomputed lighting state uptodate.
605 void _mesa_update_material( GLcontext
*ctx
,
606 const struct gl_material src
[2],
609 struct gl_light
*light
, *list
= &ctx
->Light
.EnabledList
;
611 if (ctx
->Light
.ColorMaterialEnabled
)
612 bitmask
&= ~ctx
->Light
.ColorMaterialBitmask
;
614 if (MESA_VERBOSE
&VERBOSE_IMMEDIATE
)
615 fprintf(stderr
, "_mesa_update_material, mask 0x%x\n", bitmask
);
620 /* update material emission */
621 if (bitmask
& FRONT_EMISSION_BIT
) {
622 struct gl_material
*mat
= &ctx
->Light
.Material
[0];
623 COPY_4FV( mat
->Emission
, src
[0].Emission
);
625 if (bitmask
& BACK_EMISSION_BIT
) {
626 struct gl_material
*mat
= &ctx
->Light
.Material
[1];
627 COPY_4FV( mat
->Emission
, src
[1].Emission
);
630 /* update material ambience */
631 if (bitmask
& FRONT_AMBIENT_BIT
) {
632 struct gl_material
*mat
= &ctx
->Light
.Material
[0];
633 COPY_4FV( mat
->Ambient
, src
[0].Ambient
);
634 foreach (light
, list
) {
635 SCALE_3V( light
->_MatAmbient
[0], light
->Ambient
, src
[0].Ambient
);
638 if (bitmask
& BACK_AMBIENT_BIT
) {
639 struct gl_material
*mat
= &ctx
->Light
.Material
[1];
640 COPY_4FV( mat
->Ambient
, src
[1].Ambient
);
641 foreach (light
, list
) {
642 SCALE_3V( light
->_MatAmbient
[1], light
->Ambient
, src
[1].Ambient
);
646 /* update BaseColor = emission + scene's ambience * material's ambience */
647 if (bitmask
& (FRONT_EMISSION_BIT
| FRONT_AMBIENT_BIT
)) {
648 struct gl_material
*mat
= &ctx
->Light
.Material
[0];
649 COPY_3V( ctx
->Light
._BaseColor
[0], mat
->Emission
);
650 ACC_SCALE_3V( ctx
->Light
._BaseColor
[0], mat
->Ambient
,
651 ctx
->Light
.Model
.Ambient
);
653 if (bitmask
& (BACK_EMISSION_BIT
| BACK_AMBIENT_BIT
)) {
654 struct gl_material
*mat
= &ctx
->Light
.Material
[1];
655 COPY_3V( ctx
->Light
._BaseColor
[1], mat
->Emission
);
656 ACC_SCALE_3V( ctx
->Light
._BaseColor
[1], mat
->Ambient
,
657 ctx
->Light
.Model
.Ambient
);
660 /* update material diffuse values */
661 if (bitmask
& FRONT_DIFFUSE_BIT
) {
662 struct gl_material
*mat
= &ctx
->Light
.Material
[0];
663 COPY_4FV( mat
->Diffuse
, src
[0].Diffuse
);
664 foreach (light
, list
) {
665 SCALE_3V( light
->_MatDiffuse
[0], light
->Diffuse
, mat
->Diffuse
);
667 ctx
->Light
._BaseAlpha
[0] = CLAMP( mat
->Diffuse
[3], 0.0, 1.0 );
669 if (bitmask
& BACK_DIFFUSE_BIT
) {
670 struct gl_material
*mat
= &ctx
->Light
.Material
[1];
671 COPY_4FV( mat
->Diffuse
, src
[1].Diffuse
);
672 foreach (light
, list
) {
673 SCALE_3V( light
->_MatDiffuse
[1], light
->Diffuse
, mat
->Diffuse
);
675 ctx
->Light
._BaseAlpha
[1] = CLAMP( mat
->Diffuse
[3], 0.0, 1.0 );
678 /* update material specular values */
679 if (bitmask
& FRONT_SPECULAR_BIT
) {
680 struct gl_material
*mat
= &ctx
->Light
.Material
[0];
681 COPY_4FV( mat
->Specular
, src
[0].Specular
);
682 foreach (light
, list
) {
683 SCALE_3V( light
->_MatSpecular
[0], light
->Specular
, mat
->Specular
);
686 if (bitmask
& BACK_SPECULAR_BIT
) {
687 struct gl_material
*mat
= &ctx
->Light
.Material
[1];
688 COPY_4FV( mat
->Specular
, src
[1].Specular
);
689 foreach (light
, list
) {
690 SCALE_3V( light
->_MatSpecular
[1], light
->Specular
, mat
->Specular
);
694 if (bitmask
& FRONT_SHININESS_BIT
) {
695 ctx
->Light
.Material
[0].Shininess
= src
[0].Shininess
;
696 _mesa_invalidate_shine_table( ctx
, 0 );
698 if (bitmask
& BACK_SHININESS_BIT
) {
699 ctx
->Light
.Material
[1].Shininess
= src
[1].Shininess
;
700 _mesa_invalidate_shine_table( ctx
, 1 );
703 if (bitmask
& FRONT_INDEXES_BIT
) {
704 ctx
->Light
.Material
[0].AmbientIndex
= src
[0].AmbientIndex
;
705 ctx
->Light
.Material
[0].DiffuseIndex
= src
[0].DiffuseIndex
;
706 ctx
->Light
.Material
[0].SpecularIndex
= src
[0].SpecularIndex
;
708 if (bitmask
& BACK_INDEXES_BIT
) {
709 ctx
->Light
.Material
[1].AmbientIndex
= src
[1].AmbientIndex
;
710 ctx
->Light
.Material
[1].DiffuseIndex
= src
[1].DiffuseIndex
;
711 ctx
->Light
.Material
[1].SpecularIndex
= src
[1].SpecularIndex
;
716 struct gl_material
*mat
= &ctx
->Light
.Material
[0];
717 fprintf(stderr
, "update_mat emission : %f %f %f\n",
721 fprintf(stderr
, "update_mat specular : %f %f %f\n",
725 fprintf(stderr
, "update_mat diffuse : %f %f %f\n",
729 fprintf(stderr
, "update_mat ambient : %f %f %f\n",
743 * Update the current materials from the given rgba color
744 * according to the bitmask in ColorMaterialBitmask, which is
745 * set by glColorMaterial().
747 void _mesa_update_color_material( GLcontext
*ctx
,
748 const GLfloat color
[4] )
750 struct gl_light
*light
, *list
= &ctx
->Light
.EnabledList
;
751 GLuint bitmask
= ctx
->Light
.ColorMaterialBitmask
;
753 if (MESA_VERBOSE
&VERBOSE_IMMEDIATE
)
754 fprintf(stderr
, "_mesa_update_color_material, mask 0x%x\n", bitmask
);
756 /* update emissive colors */
757 if (bitmask
& FRONT_EMISSION_BIT
) {
758 struct gl_material
*mat
= &ctx
->Light
.Material
[0];
759 COPY_4FV( mat
->Emission
, color
);
762 if (bitmask
& BACK_EMISSION_BIT
) {
763 struct gl_material
*mat
= &ctx
->Light
.Material
[1];
764 COPY_4FV( mat
->Emission
, color
);
767 /* update light->_MatAmbient = light's ambient * material's ambient */
768 if (bitmask
& FRONT_AMBIENT_BIT
) {
769 struct gl_material
*mat
= &ctx
->Light
.Material
[0];
770 foreach (light
, list
) {
771 SCALE_3V( light
->_MatAmbient
[0], light
->Ambient
, color
);
773 COPY_4FV( mat
->Ambient
, color
);
776 if (bitmask
& BACK_AMBIENT_BIT
) {
777 struct gl_material
*mat
= &ctx
->Light
.Material
[1];
778 foreach (light
, list
) {
779 SCALE_3V( light
->_MatAmbient
[1], light
->Ambient
, color
);
781 COPY_4FV( mat
->Ambient
, color
);
784 /* update BaseColor = emission + scene's ambience * material's ambience */
785 if (bitmask
& (FRONT_EMISSION_BIT
| FRONT_AMBIENT_BIT
)) {
786 struct gl_material
*mat
= &ctx
->Light
.Material
[0];
787 COPY_3V( ctx
->Light
._BaseColor
[0], mat
->Emission
);
788 ACC_SCALE_3V( ctx
->Light
._BaseColor
[0], mat
->Ambient
, ctx
->Light
.Model
.Ambient
);
791 if (bitmask
& (BACK_EMISSION_BIT
| BACK_AMBIENT_BIT
)) {
792 struct gl_material
*mat
= &ctx
->Light
.Material
[1];
793 COPY_3V( ctx
->Light
._BaseColor
[1], mat
->Emission
);
794 ACC_SCALE_3V( ctx
->Light
._BaseColor
[1], mat
->Ambient
, ctx
->Light
.Model
.Ambient
);
797 /* update light->_MatDiffuse = light's diffuse * material's diffuse */
798 if (bitmask
& FRONT_DIFFUSE_BIT
) {
799 struct gl_material
*mat
= &ctx
->Light
.Material
[0];
800 COPY_4FV( mat
->Diffuse
, color
);
801 foreach (light
, list
) {
802 SCALE_3V( light
->_MatDiffuse
[0], light
->Diffuse
, mat
->Diffuse
);
804 ctx
->Light
._BaseAlpha
[0] = CLAMP( mat
->Diffuse
[3], 0.0, 1.0 );
807 if (bitmask
& BACK_DIFFUSE_BIT
) {
808 struct gl_material
*mat
= &ctx
->Light
.Material
[1];
809 COPY_4FV( mat
->Diffuse
, color
);
810 foreach (light
, list
) {
811 SCALE_3V( light
->_MatDiffuse
[1], light
->Diffuse
, mat
->Diffuse
);
813 ctx
->Light
._BaseAlpha
[1] = CLAMP( mat
->Diffuse
[3], 0.0, 1.0 );
816 /* update light->_MatSpecular = light's specular * material's specular */
817 if (bitmask
& FRONT_SPECULAR_BIT
) {
818 struct gl_material
*mat
= &ctx
->Light
.Material
[0];
819 COPY_4FV( mat
->Specular
, color
);
820 foreach (light
, list
) {
821 ACC_SCALE_3V( light
->_MatSpecular
[0], light
->Specular
, mat
->Specular
);
825 if (bitmask
& BACK_SPECULAR_BIT
) {
826 struct gl_material
*mat
= &ctx
->Light
.Material
[1];
827 COPY_4FV( mat
->Specular
, color
);
828 foreach (light
, list
) {
829 ACC_SCALE_3V( light
->_MatSpecular
[1], light
->Specular
, mat
->Specular
);
835 struct gl_material
*mat
= &ctx
->Light
.Material
[0];
836 fprintf(stderr
, "update_color_mat emission : %f %f %f\n",
840 fprintf(stderr
, "update_color_mat specular : %f %f %f\n",
844 fprintf(stderr
, "update_color_mat diffuse : %f %f %f\n",
848 fprintf(stderr
, "update_color_mat ambient : %f %f %f\n",
859 _mesa_ColorMaterial( GLenum face
, GLenum mode
)
861 GET_CURRENT_CONTEXT(ctx
);
863 GLuint legal
= (FRONT_EMISSION_BIT
| BACK_EMISSION_BIT
|
864 FRONT_SPECULAR_BIT
| BACK_SPECULAR_BIT
|
865 FRONT_DIFFUSE_BIT
| BACK_DIFFUSE_BIT
|
866 FRONT_AMBIENT_BIT
| BACK_AMBIENT_BIT
);
867 ASSERT_OUTSIDE_BEGIN_END(ctx
);
869 if (MESA_VERBOSE
&VERBOSE_API
)
870 fprintf(stderr
, "glColorMaterial %s %s\n",
871 _mesa_lookup_enum_by_nr(face
),
872 _mesa_lookup_enum_by_nr(mode
));
874 bitmask
= _mesa_material_bitmask(ctx
, face
, mode
, legal
, "glColorMaterial");
876 if (ctx
->Light
.ColorMaterialBitmask
== bitmask
&&
877 ctx
->Light
.ColorMaterialFace
== face
&&
878 ctx
->Light
.ColorMaterialMode
== mode
)
881 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
882 ctx
->Light
.ColorMaterialBitmask
= bitmask
;
883 ctx
->Light
.ColorMaterialFace
= face
;
884 ctx
->Light
.ColorMaterialMode
= mode
;
886 if (ctx
->Light
.ColorMaterialEnabled
) {
887 FLUSH_CURRENT( ctx
, 0 );
888 _mesa_update_color_material( ctx
, ctx
->Current
.Color
);
897 _mesa_GetMaterialfv( GLenum face
, GLenum pname
, GLfloat
*params
)
899 GET_CURRENT_CONTEXT(ctx
);
901 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
); /* update materials */
903 if (face
==GL_FRONT
) {
906 else if (face
==GL_BACK
) {
910 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetMaterialfv(face)" );
915 COPY_4FV( params
, ctx
->Light
.Material
[f
].Ambient
);
918 COPY_4FV( params
, ctx
->Light
.Material
[f
].Diffuse
);
921 COPY_4FV( params
, ctx
->Light
.Material
[f
].Specular
);
924 COPY_4FV( params
, ctx
->Light
.Material
[f
].Emission
);
927 *params
= ctx
->Light
.Material
[f
].Shininess
;
929 case GL_COLOR_INDEXES
:
930 params
[0] = ctx
->Light
.Material
[f
].AmbientIndex
;
931 params
[1] = ctx
->Light
.Material
[f
].DiffuseIndex
;
932 params
[2] = ctx
->Light
.Material
[f
].SpecularIndex
;
935 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetMaterialfv(pname)" );
942 _mesa_GetMaterialiv( GLenum face
, GLenum pname
, GLint
*params
)
944 GET_CURRENT_CONTEXT(ctx
);
946 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
); /* update materials */
948 if (face
==GL_FRONT
) {
951 else if (face
==GL_BACK
) {
955 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetMaterialiv(face)" );
960 params
[0] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Ambient
[0] );
961 params
[1] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Ambient
[1] );
962 params
[2] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Ambient
[2] );
963 params
[3] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Ambient
[3] );
966 params
[0] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Diffuse
[0] );
967 params
[1] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Diffuse
[1] );
968 params
[2] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Diffuse
[2] );
969 params
[3] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Diffuse
[3] );
972 params
[0] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Specular
[0] );
973 params
[1] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Specular
[1] );
974 params
[2] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Specular
[2] );
975 params
[3] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Specular
[3] );
978 params
[0] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Emission
[0] );
979 params
[1] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Emission
[1] );
980 params
[2] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Emission
[2] );
981 params
[3] = FLOAT_TO_INT( ctx
->Light
.Material
[f
].Emission
[3] );
984 *params
= ROUNDF( ctx
->Light
.Material
[f
].Shininess
);
986 case GL_COLOR_INDEXES
:
987 params
[0] = ROUNDF( ctx
->Light
.Material
[f
].AmbientIndex
);
988 params
[1] = ROUNDF( ctx
->Light
.Material
[f
].DiffuseIndex
);
989 params
[2] = ROUNDF( ctx
->Light
.Material
[f
].SpecularIndex
);
992 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetMaterialfv(pname)" );
999 /**********************************************************************/
1000 /***** Lighting computation *****/
1001 /**********************************************************************/
1006 * When two-sided lighting is enabled we compute the color (or index)
1007 * for both the front and back side of the primitive. Then, when the
1008 * orientation of the facet is later learned, we can determine which
1009 * color (or index) to use for rendering.
1011 * KW: We now know orientation in advance and only shade for
1012 * the side or sides which are actually required.
1016 * V = vertex position
1017 * P = light source position
1022 * // light at infinity
1023 * IF local_viewer THEN
1024 * _VP_inf_norm = unit vector from V to P // Precompute
1026 * // eye at infinity
1027 * _h_inf_norm = Normalize( VP + <0,0,1> ) // Precompute
1032 * Normalize( v ) = normalized vector v
1033 * Magnitude( v ) = length of vector v
1039 * Whenever the spotlight exponent for a light changes we must call
1040 * this function to recompute the exponent lookup table.
1043 _mesa_invalidate_spot_exp_table( struct gl_light
*l
)
1045 l
->_SpotExpTable
[0][0] = -1;
1048 static void validate_spot_exp_table( struct gl_light
*l
)
1051 GLdouble exponent
= l
->SpotExponent
;
1055 l
->_SpotExpTable
[0][0] = 0.0;
1057 for (i
= EXP_TABLE_SIZE
- 1; i
> 0 ;i
--) {
1059 tmp
= pow(i
/ (GLdouble
) (EXP_TABLE_SIZE
- 1), exponent
);
1060 if (tmp
< FLT_MIN
* 100.0) {
1065 l
->_SpotExpTable
[i
][0] = tmp
;
1067 for (i
= 0; i
< EXP_TABLE_SIZE
- 1; i
++) {
1068 l
->_SpotExpTable
[i
][1] = (l
->_SpotExpTable
[i
+1][0] -
1069 l
->_SpotExpTable
[i
][0]);
1071 l
->_SpotExpTable
[EXP_TABLE_SIZE
-1][1] = 0.0;
1077 /* Calculate a new shine table. Doing this here saves a branch in
1078 * lighting, and the cost of doing it early may be partially offset
1079 * by keeping a MRU cache of shine tables for various shine values.
1082 _mesa_invalidate_shine_table( GLcontext
*ctx
, GLuint i
)
1084 if (ctx
->_ShineTable
[i
])
1085 ctx
->_ShineTable
[i
]->refcount
--;
1086 ctx
->_ShineTable
[i
] = 0;
1089 static void validate_shine_table( GLcontext
*ctx
, GLuint i
, GLfloat shininess
)
1091 struct gl_shine_tab
*list
= ctx
->_ShineTabList
;
1092 struct gl_shine_tab
*s
;
1095 if ( s
->shininess
== shininess
)
1103 if (s
->refcount
== 0)
1108 if (shininess
== 0.0) {
1109 for (j
= 1 ; j
<= SHINE_TABLE_SIZE
; j
++)
1113 for (j
= 1 ; j
< SHINE_TABLE_SIZE
; j
++) {
1114 GLdouble t
, x
= j
/ (GLfloat
) (SHINE_TABLE_SIZE
- 1);
1115 if (x
< 0.005) /* underflow check */
1117 t
= pow(x
, shininess
);
1123 m
[SHINE_TABLE_SIZE
] = 1.0;
1126 s
->shininess
= shininess
;
1129 if (ctx
->_ShineTable
[i
])
1130 ctx
->_ShineTable
[i
]->refcount
--;
1132 ctx
->_ShineTable
[i
] = s
;
1133 move_to_tail( list
, s
);
1138 _mesa_validate_all_lighting_tables( GLcontext
*ctx
)
1143 shininess
= ctx
->Light
.Material
[0].Shininess
;
1144 if (!ctx
->_ShineTable
[0] || ctx
->_ShineTable
[0]->shininess
!= shininess
)
1145 validate_shine_table( ctx
, 0, shininess
);
1147 shininess
= ctx
->Light
.Material
[1].Shininess
;
1148 if (!ctx
->_ShineTable
[1] || ctx
->_ShineTable
[1]->shininess
!= shininess
)
1149 validate_shine_table( ctx
, 1, shininess
);
1151 for (i
= 0 ; i
< MAX_LIGHTS
; i
++)
1152 if (ctx
->Light
.Light
[i
]._SpotExpTable
[0][0] == -1)
1153 validate_spot_exp_table( &ctx
->Light
.Light
[i
] );
1160 * Examine current lighting parameters to determine if the optimized lighting
1161 * function can be used.
1162 * Also, precompute some lighting values such as the products of light
1163 * source and material ambient, diffuse and specular coefficients.
1166 _mesa_update_lighting( GLcontext
*ctx
)
1168 struct gl_light
*light
;
1169 ctx
->_TriangleCaps
&= ~DD_TRI_LIGHT_TWOSIDE
;
1170 ctx
->_NeedEyeCoords
&= ~NEED_EYE_LIGHT
;
1171 ctx
->_NeedNormals
&= ~NEED_NORMALS_LIGHT
;
1172 ctx
->Light
._Flags
= 0;
1174 if (!ctx
->Light
.Enabled
)
1177 ctx
->_NeedNormals
|= NEED_NORMALS_LIGHT
;
1179 if (ctx
->Light
.Model
.TwoSide
)
1180 ctx
->_TriangleCaps
|= DD_TRI_LIGHT_TWOSIDE
;
1182 foreach(light
, &ctx
->Light
.EnabledList
) {
1183 ctx
->Light
._Flags
|= light
->_Flags
;
1186 ctx
->Light
._NeedVertices
=
1187 ((ctx
->Light
._Flags
& (LIGHT_POSITIONAL
|LIGHT_SPOT
)) ||
1188 ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
||
1189 ctx
->Light
.Model
.LocalViewer
);
1191 if ((ctx
->Light
._Flags
& LIGHT_POSITIONAL
) ||
1192 ctx
->Light
.Model
.LocalViewer
)
1193 ctx
->_NeedEyeCoords
|= NEED_EYE_LIGHT
;
1196 /* XXX: This test is overkill & needs to be fixed both for software and
1197 * hardware t&l drivers. The above should be sufficient & should
1198 * be tested to verify this.
1200 if (ctx
->Light
._NeedVertices
)
1201 ctx
->_NeedEyeCoords
|= NEED_EYE_LIGHT
;
1204 /* Precompute some shading values. Although we reference
1205 * Light.Material here, we can get away without flushing
1206 * FLUSH_UPDATE_CURRENT, as when any outstanding material changes
1207 * are flushed, they will update the derived state at that time.
1209 if (ctx
->Visual
.rgbMode
) {
1210 GLuint sides
= ctx
->Light
.Model
.TwoSide
? 2 : 1;
1212 for (side
=0; side
< sides
; side
++) {
1213 struct gl_material
*mat
= &ctx
->Light
.Material
[side
];
1215 COPY_3V(ctx
->Light
._BaseColor
[side
], mat
->Emission
);
1216 ACC_SCALE_3V(ctx
->Light
._BaseColor
[side
],
1217 ctx
->Light
.Model
.Ambient
,
1220 ctx
->Light
._BaseAlpha
[side
] =
1221 CLAMP( ctx
->Light
.Material
[side
].Diffuse
[3], 0.0, 1.0 );
1224 foreach (light
, &ctx
->Light
.EnabledList
) {
1225 for (side
=0; side
< sides
; side
++) {
1226 const struct gl_material
*mat
= &ctx
->Light
.Material
[side
];
1227 SCALE_3V( light
->_MatDiffuse
[side
], light
->Diffuse
, mat
->Diffuse
);
1228 SCALE_3V( light
->_MatAmbient
[side
], light
->Ambient
, mat
->Ambient
);
1229 SCALE_3V( light
->_MatSpecular
[side
], light
->Specular
,
1235 static const GLfloat ci
[3] = { .30, .59, .11 };
1236 foreach(light
, &ctx
->Light
.EnabledList
) {
1237 light
->_dli
= DOT3(ci
, light
->Diffuse
);
1238 light
->_sli
= DOT3(ci
, light
->Specular
);
1246 * _TNL_NEW_NEED_EYE_COORDS
1248 * Update on (_NEW_MODELVIEW | _NEW_LIGHT) when lighting is enabled.
1249 * Also update on lighting space changes.
1252 _mesa_compute_light_positions( GLcontext
*ctx
)
1254 struct gl_light
*light
;
1255 static const GLfloat eye_z
[3] = { 0, 0, 1 };
1257 if (!ctx
->Light
.Enabled
)
1260 if (ctx
->_NeedEyeCoords
) {
1261 COPY_3V( ctx
->_EyeZDir
, eye_z
);
1264 TRANSFORM_NORMAL( ctx
->_EyeZDir
, eye_z
, ctx
->ModelView
.m
);
1267 foreach (light
, &ctx
->Light
.EnabledList
) {
1269 if (ctx
->_NeedEyeCoords
) {
1270 COPY_4FV( light
->_Position
, light
->EyePosition
);
1273 TRANSFORM_POINT( light
->_Position
, ctx
->ModelView
.inv
,
1274 light
->EyePosition
);
1277 if (!(light
->_Flags
& LIGHT_POSITIONAL
)) {
1278 /* VP (VP) = Normalize( Position ) */
1279 COPY_3V( light
->_VP_inf_norm
, light
->_Position
);
1280 NORMALIZE_3FV( light
->_VP_inf_norm
);
1282 if (!ctx
->Light
.Model
.LocalViewer
) {
1283 /* _h_inf_norm = Normalize( V_to_P + <0,0,1> ) */
1284 ADD_3V( light
->_h_inf_norm
, light
->_VP_inf_norm
, ctx
->_EyeZDir
);
1285 NORMALIZE_3FV( light
->_h_inf_norm
);
1287 light
->_VP_inf_spot_attenuation
= 1.0;
1290 if (light
->_Flags
& LIGHT_SPOT
) {
1291 if (ctx
->_NeedEyeCoords
) {
1292 COPY_3V( light
->_NormDirection
, light
->EyeDirection
);
1295 TRANSFORM_NORMAL( light
->_NormDirection
,
1296 light
->EyeDirection
,
1300 NORMALIZE_3FV( light
->_NormDirection
);
1302 if (!(light
->_Flags
& LIGHT_POSITIONAL
)) {
1303 GLfloat PV_dot_dir
= - DOT3(light
->_VP_inf_norm
,
1304 light
->_NormDirection
);
1306 if (PV_dot_dir
> light
->_CosCutoff
) {
1307 double x
= PV_dot_dir
* (EXP_TABLE_SIZE
-1);
1309 light
->_VP_inf_spot_attenuation
=
1310 (light
->_SpotExpTable
[k
][0] +
1311 (x
-k
)*light
->_SpotExpTable
[k
][1]);
1314 light
->_VP_inf_spot_attenuation
= 0;