2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 #include "simple_list.h"
35 #include "math/m_matrix.h"
39 _mesa_ShadeModel( GLenum mode
)
41 GET_CURRENT_CONTEXT(ctx
);
42 ASSERT_OUTSIDE_BEGIN_END(ctx
);
44 if (MESA_VERBOSE
& VERBOSE_API
)
45 _mesa_debug(ctx
, "glShadeModel %s\n", _mesa_lookup_enum_by_nr(mode
));
47 if (mode
!= GL_FLAT
&& mode
!= GL_SMOOTH
) {
48 _mesa_error(ctx
, GL_INVALID_ENUM
, "glShadeModel");
52 if (ctx
->Light
.ShadeModel
== mode
)
55 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
56 ctx
->Light
.ShadeModel
= mode
;
58 ctx
->_TriangleCaps
|= DD_FLATSHADE
;
60 ctx
->_TriangleCaps
&= ~DD_FLATSHADE
;
62 if (ctx
->Driver
.ShadeModel
)
63 ctx
->Driver
.ShadeModel( ctx
, mode
);
68 * Set the provoking vertex (the vertex which specifies the prim's
69 * color when flat shading) to either the first or last vertex of the
73 _mesa_ProvokingVertexEXT(GLenum mode
)
75 GET_CURRENT_CONTEXT(ctx
);
76 ASSERT_OUTSIDE_BEGIN_END(ctx
);
78 if (MESA_VERBOSE
&VERBOSE_API
)
79 _mesa_debug(ctx
, "glProvokingVertexEXT 0x%x\n", mode
);
82 case GL_FIRST_VERTEX_CONVENTION_EXT
:
83 case GL_LAST_VERTEX_CONVENTION_EXT
:
86 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProvokingVertexEXT(0x%x)", mode
);
90 if (ctx
->Light
.ProvokingVertex
== mode
)
93 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
94 ctx
->Light
.ProvokingVertex
= mode
;
99 * Helper function called by _mesa_Lightfv and _mesa_PopAttrib to set
101 * For GL_POSITION and GL_SPOT_DIRECTION the params position/direction
102 * will have already been transformed by the modelview matrix!
103 * Also, all error checking should have already been done.
106 _mesa_light(struct gl_context
*ctx
, GLuint lnum
, GLenum pname
, const GLfloat
*params
)
108 struct gl_light
*light
;
110 ASSERT(lnum
< MAX_LIGHTS
);
111 light
= &ctx
->Light
.Light
[lnum
];
115 if (TEST_EQ_4V(light
->Ambient
, params
))
117 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
118 COPY_4V( light
->Ambient
, params
);
121 if (TEST_EQ_4V(light
->Diffuse
, params
))
123 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
124 COPY_4V( light
->Diffuse
, params
);
127 if (TEST_EQ_4V(light
->Specular
, params
))
129 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
130 COPY_4V( light
->Specular
, params
);
133 /* NOTE: position has already been transformed by ModelView! */
134 if (TEST_EQ_4V(light
->EyePosition
, params
))
136 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
137 COPY_4V(light
->EyePosition
, params
);
138 if (light
->EyePosition
[3] != 0.0F
)
139 light
->_Flags
|= LIGHT_POSITIONAL
;
141 light
->_Flags
&= ~LIGHT_POSITIONAL
;
143 case GL_SPOT_DIRECTION
:
144 /* NOTE: Direction already transformed by inverse ModelView! */
145 if (TEST_EQ_3V(light
->SpotDirection
, params
))
147 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
148 COPY_3V(light
->SpotDirection
, params
);
150 case GL_SPOT_EXPONENT
:
151 ASSERT(params
[0] >= 0.0);
152 ASSERT(params
[0] <= ctx
->Const
.MaxSpotExponent
);
153 if (light
->SpotExponent
== params
[0])
155 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
156 light
->SpotExponent
= params
[0];
159 ASSERT(params
[0] == 180.0 || (params
[0] >= 0.0 && params
[0] <= 90.0));
160 if (light
->SpotCutoff
== params
[0])
162 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
163 light
->SpotCutoff
= params
[0];
164 light
->_CosCutoff
= (GLfloat
) (cos(light
->SpotCutoff
* DEG2RAD
));
165 if (light
->_CosCutoff
< 0)
166 light
->_CosCutoff
= 0;
167 if (light
->SpotCutoff
!= 180.0F
)
168 light
->_Flags
|= LIGHT_SPOT
;
170 light
->_Flags
&= ~LIGHT_SPOT
;
172 case GL_CONSTANT_ATTENUATION
:
173 ASSERT(params
[0] >= 0.0);
174 if (light
->ConstantAttenuation
== params
[0])
176 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
177 light
->ConstantAttenuation
= params
[0];
179 case GL_LINEAR_ATTENUATION
:
180 ASSERT(params
[0] >= 0.0);
181 if (light
->LinearAttenuation
== params
[0])
183 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
184 light
->LinearAttenuation
= params
[0];
186 case GL_QUADRATIC_ATTENUATION
:
187 ASSERT(params
[0] >= 0.0);
188 if (light
->QuadraticAttenuation
== params
[0])
190 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
191 light
->QuadraticAttenuation
= params
[0];
194 _mesa_problem(ctx
, "Unexpected pname in _mesa_light()");
198 if (ctx
->Driver
.Lightfv
)
199 ctx
->Driver
.Lightfv( ctx
, GL_LIGHT0
+ lnum
, pname
, params
);
204 _mesa_Lightf( GLenum light
, GLenum pname
, GLfloat param
)
208 fparam
[1] = fparam
[2] = fparam
[3] = 0.0F
;
209 _mesa_Lightfv( light
, pname
, fparam
);
214 _mesa_Lightfv( GLenum light
, GLenum pname
, const GLfloat
*params
)
216 GET_CURRENT_CONTEXT(ctx
);
217 GLint i
= (GLint
) (light
- GL_LIGHT0
);
219 ASSERT_OUTSIDE_BEGIN_END(ctx
);
221 if (i
< 0 || i
>= (GLint
) ctx
->Const
.MaxLights
) {
222 _mesa_error( ctx
, GL_INVALID_ENUM
, "glLight(light=0x%x)", light
);
226 /* do particular error checks, transformations */
234 /* transform position by ModelView matrix */
235 TRANSFORM_POINT(temp
, ctx
->ModelviewMatrixStack
.Top
->m
, params
);
238 case GL_SPOT_DIRECTION
:
239 /* transform direction by inverse modelview */
240 if (_math_matrix_is_dirty(ctx
->ModelviewMatrixStack
.Top
)) {
241 _math_matrix_analyse(ctx
->ModelviewMatrixStack
.Top
);
243 TRANSFORM_DIRECTION(temp
, params
, ctx
->ModelviewMatrixStack
.Top
->m
);
246 case GL_SPOT_EXPONENT
:
247 if (params
[0] < 0.0 || params
[0] > ctx
->Const
.MaxSpotExponent
) {
248 _mesa_error(ctx
, GL_INVALID_VALUE
, "glLight");
253 if ((params
[0] < 0.0 || params
[0] > 90.0) && params
[0] != 180.0) {
254 _mesa_error(ctx
, GL_INVALID_VALUE
, "glLight");
258 case GL_CONSTANT_ATTENUATION
:
259 if (params
[0] < 0.0) {
260 _mesa_error(ctx
, GL_INVALID_VALUE
, "glLight");
264 case GL_LINEAR_ATTENUATION
:
265 if (params
[0] < 0.0) {
266 _mesa_error(ctx
, GL_INVALID_VALUE
, "glLight");
270 case GL_QUADRATIC_ATTENUATION
:
271 if (params
[0] < 0.0) {
272 _mesa_error(ctx
, GL_INVALID_VALUE
, "glLight");
277 _mesa_error(ctx
, GL_INVALID_ENUM
, "glLight(pname=0x%x)", pname
);
281 _mesa_light(ctx
, i
, pname
, params
);
286 _mesa_Lighti( GLenum light
, GLenum pname
, GLint param
)
290 iparam
[1] = iparam
[2] = iparam
[3] = 0;
291 _mesa_Lightiv( light
, pname
, iparam
);
296 _mesa_Lightiv( GLenum light
, GLenum pname
, const GLint
*params
)
304 fparam
[0] = INT_TO_FLOAT( params
[0] );
305 fparam
[1] = INT_TO_FLOAT( params
[1] );
306 fparam
[2] = INT_TO_FLOAT( params
[2] );
307 fparam
[3] = INT_TO_FLOAT( params
[3] );
310 fparam
[0] = (GLfloat
) params
[0];
311 fparam
[1] = (GLfloat
) params
[1];
312 fparam
[2] = (GLfloat
) params
[2];
313 fparam
[3] = (GLfloat
) params
[3];
315 case GL_SPOT_DIRECTION
:
316 fparam
[0] = (GLfloat
) params
[0];
317 fparam
[1] = (GLfloat
) params
[1];
318 fparam
[2] = (GLfloat
) params
[2];
320 case GL_SPOT_EXPONENT
:
322 case GL_CONSTANT_ATTENUATION
:
323 case GL_LINEAR_ATTENUATION
:
324 case GL_QUADRATIC_ATTENUATION
:
325 fparam
[0] = (GLfloat
) params
[0];
328 /* error will be caught later in gl_Lightfv */
332 _mesa_Lightfv( light
, pname
, fparam
);
338 _mesa_GetLightfv( GLenum light
, GLenum pname
, GLfloat
*params
)
340 GET_CURRENT_CONTEXT(ctx
);
341 GLint l
= (GLint
) (light
- GL_LIGHT0
);
342 ASSERT_OUTSIDE_BEGIN_END(ctx
);
344 if (l
< 0 || l
>= (GLint
) ctx
->Const
.MaxLights
) {
345 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetLightfv" );
351 COPY_4V( params
, ctx
->Light
.Light
[l
].Ambient
);
354 COPY_4V( params
, ctx
->Light
.Light
[l
].Diffuse
);
357 COPY_4V( params
, ctx
->Light
.Light
[l
].Specular
);
360 COPY_4V( params
, ctx
->Light
.Light
[l
].EyePosition
);
362 case GL_SPOT_DIRECTION
:
363 COPY_3V( params
, ctx
->Light
.Light
[l
].SpotDirection
);
365 case GL_SPOT_EXPONENT
:
366 params
[0] = ctx
->Light
.Light
[l
].SpotExponent
;
369 params
[0] = ctx
->Light
.Light
[l
].SpotCutoff
;
371 case GL_CONSTANT_ATTENUATION
:
372 params
[0] = ctx
->Light
.Light
[l
].ConstantAttenuation
;
374 case GL_LINEAR_ATTENUATION
:
375 params
[0] = ctx
->Light
.Light
[l
].LinearAttenuation
;
377 case GL_QUADRATIC_ATTENUATION
:
378 params
[0] = ctx
->Light
.Light
[l
].QuadraticAttenuation
;
381 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetLightfv" );
388 _mesa_GetLightiv( GLenum light
, GLenum pname
, GLint
*params
)
390 GET_CURRENT_CONTEXT(ctx
);
391 GLint l
= (GLint
) (light
- GL_LIGHT0
);
392 ASSERT_OUTSIDE_BEGIN_END(ctx
);
394 if (l
< 0 || l
>= (GLint
) ctx
->Const
.MaxLights
) {
395 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetLightiv" );
401 params
[0] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Ambient
[0]);
402 params
[1] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Ambient
[1]);
403 params
[2] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Ambient
[2]);
404 params
[3] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Ambient
[3]);
407 params
[0] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Diffuse
[0]);
408 params
[1] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Diffuse
[1]);
409 params
[2] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Diffuse
[2]);
410 params
[3] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Diffuse
[3]);
413 params
[0] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Specular
[0]);
414 params
[1] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Specular
[1]);
415 params
[2] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Specular
[2]);
416 params
[3] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Specular
[3]);
419 params
[0] = (GLint
) ctx
->Light
.Light
[l
].EyePosition
[0];
420 params
[1] = (GLint
) ctx
->Light
.Light
[l
].EyePosition
[1];
421 params
[2] = (GLint
) ctx
->Light
.Light
[l
].EyePosition
[2];
422 params
[3] = (GLint
) ctx
->Light
.Light
[l
].EyePosition
[3];
424 case GL_SPOT_DIRECTION
:
425 params
[0] = (GLint
) ctx
->Light
.Light
[l
].SpotDirection
[0];
426 params
[1] = (GLint
) ctx
->Light
.Light
[l
].SpotDirection
[1];
427 params
[2] = (GLint
) ctx
->Light
.Light
[l
].SpotDirection
[2];
429 case GL_SPOT_EXPONENT
:
430 params
[0] = (GLint
) ctx
->Light
.Light
[l
].SpotExponent
;
433 params
[0] = (GLint
) ctx
->Light
.Light
[l
].SpotCutoff
;
435 case GL_CONSTANT_ATTENUATION
:
436 params
[0] = (GLint
) ctx
->Light
.Light
[l
].ConstantAttenuation
;
438 case GL_LINEAR_ATTENUATION
:
439 params
[0] = (GLint
) ctx
->Light
.Light
[l
].LinearAttenuation
;
441 case GL_QUADRATIC_ATTENUATION
:
442 params
[0] = (GLint
) ctx
->Light
.Light
[l
].QuadraticAttenuation
;
445 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetLightiv" );
452 /**********************************************************************/
453 /*** Light Model ***/
454 /**********************************************************************/
458 _mesa_LightModelfv( GLenum pname
, const GLfloat
*params
)
462 GET_CURRENT_CONTEXT(ctx
);
463 ASSERT_OUTSIDE_BEGIN_END(ctx
);
466 case GL_LIGHT_MODEL_AMBIENT
:
467 if (TEST_EQ_4V( ctx
->Light
.Model
.Ambient
, params
))
469 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
470 COPY_4V( ctx
->Light
.Model
.Ambient
, params
);
472 case GL_LIGHT_MODEL_LOCAL_VIEWER
:
473 newbool
= (params
[0]!=0.0);
474 if (ctx
->Light
.Model
.LocalViewer
== newbool
)
476 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
477 ctx
->Light
.Model
.LocalViewer
= newbool
;
479 case GL_LIGHT_MODEL_TWO_SIDE
:
480 newbool
= (params
[0]!=0.0);
481 if (ctx
->Light
.Model
.TwoSide
== newbool
)
483 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
484 ctx
->Light
.Model
.TwoSide
= newbool
;
485 if (ctx
->Light
.Enabled
&& ctx
->Light
.Model
.TwoSide
)
486 ctx
->_TriangleCaps
|= DD_TRI_LIGHT_TWOSIDE
;
488 ctx
->_TriangleCaps
&= ~DD_TRI_LIGHT_TWOSIDE
;
490 case GL_LIGHT_MODEL_COLOR_CONTROL
:
491 if (params
[0] == (GLfloat
) GL_SINGLE_COLOR
)
492 newenum
= GL_SINGLE_COLOR
;
493 else if (params
[0] == (GLfloat
) GL_SEPARATE_SPECULAR_COLOR
)
494 newenum
= GL_SEPARATE_SPECULAR_COLOR
;
496 _mesa_error( ctx
, GL_INVALID_ENUM
, "glLightModel(param=0x0%x)",
500 if (ctx
->Light
.Model
.ColorControl
== newenum
)
502 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
503 ctx
->Light
.Model
.ColorControl
= newenum
;
506 _mesa_error( ctx
, GL_INVALID_ENUM
, "glLightModel(pname=0x%x)", pname
);
510 if (ctx
->Driver
.LightModelfv
)
511 ctx
->Driver
.LightModelfv( ctx
, pname
, params
);
516 _mesa_LightModeliv( GLenum pname
, const GLint
*params
)
521 case GL_LIGHT_MODEL_AMBIENT
:
522 fparam
[0] = INT_TO_FLOAT( params
[0] );
523 fparam
[1] = INT_TO_FLOAT( params
[1] );
524 fparam
[2] = INT_TO_FLOAT( params
[2] );
525 fparam
[3] = INT_TO_FLOAT( params
[3] );
527 case GL_LIGHT_MODEL_LOCAL_VIEWER
:
528 case GL_LIGHT_MODEL_TWO_SIDE
:
529 case GL_LIGHT_MODEL_COLOR_CONTROL
:
530 fparam
[0] = (GLfloat
) params
[0];
533 /* Error will be caught later in gl_LightModelfv */
534 ASSIGN_4V(fparam
, 0.0F
, 0.0F
, 0.0F
, 0.0F
);
536 _mesa_LightModelfv( pname
, fparam
);
541 _mesa_LightModeli( GLenum pname
, GLint param
)
545 iparam
[1] = iparam
[2] = iparam
[3] = 0;
546 _mesa_LightModeliv( pname
, iparam
);
551 _mesa_LightModelf( GLenum pname
, GLfloat param
)
555 fparam
[1] = fparam
[2] = fparam
[3] = 0.0F
;
556 _mesa_LightModelfv( pname
, fparam
);
561 /********** MATERIAL **********/
565 * Given a face and pname value (ala glColorMaterial), compute a bitmask
566 * of the targeted material values.
569 _mesa_material_bitmask( struct gl_context
*ctx
, GLenum face
, GLenum pname
,
570 GLuint legal
, const char *where
)
574 /* Make a bitmask indicating what material attribute(s) we're updating */
577 bitmask
|= MAT_BIT_FRONT_EMISSION
| MAT_BIT_BACK_EMISSION
;
580 bitmask
|= MAT_BIT_FRONT_AMBIENT
| MAT_BIT_BACK_AMBIENT
;
583 bitmask
|= MAT_BIT_FRONT_DIFFUSE
| MAT_BIT_BACK_DIFFUSE
;
586 bitmask
|= MAT_BIT_FRONT_SPECULAR
| MAT_BIT_BACK_SPECULAR
;
589 bitmask
|= MAT_BIT_FRONT_SHININESS
| MAT_BIT_BACK_SHININESS
;
591 case GL_AMBIENT_AND_DIFFUSE
:
592 bitmask
|= MAT_BIT_FRONT_AMBIENT
| MAT_BIT_BACK_AMBIENT
;
593 bitmask
|= MAT_BIT_FRONT_DIFFUSE
| MAT_BIT_BACK_DIFFUSE
;
595 case GL_COLOR_INDEXES
:
596 bitmask
|= MAT_BIT_FRONT_INDEXES
| MAT_BIT_BACK_INDEXES
;
599 _mesa_error( ctx
, GL_INVALID_ENUM
, "%s", where
);
603 if (face
==GL_FRONT
) {
604 bitmask
&= FRONT_MATERIAL_BITS
;
606 else if (face
==GL_BACK
) {
607 bitmask
&= BACK_MATERIAL_BITS
;
609 else if (face
!= GL_FRONT_AND_BACK
) {
610 _mesa_error( ctx
, GL_INVALID_ENUM
, "%s", where
);
614 if (bitmask
& ~legal
) {
615 _mesa_error( ctx
, GL_INVALID_ENUM
, "%s", where
);
624 /* Update derived values following a change in ctx->Light.Material
627 _mesa_update_material( struct gl_context
*ctx
, GLuint bitmask
)
629 struct gl_light
*light
, *list
= &ctx
->Light
.EnabledList
;
630 GLfloat (*mat
)[4] = ctx
->Light
.Material
.Attrib
;
632 if (MESA_VERBOSE
& VERBOSE_MATERIAL
)
633 _mesa_debug(ctx
, "_mesa_update_material, mask 0x%x\n", bitmask
);
638 /* update material ambience */
639 if (bitmask
& MAT_BIT_FRONT_AMBIENT
) {
640 foreach (light
, list
) {
641 SCALE_3V( light
->_MatAmbient
[0], light
->Ambient
,
642 mat
[MAT_ATTRIB_FRONT_AMBIENT
]);
646 if (bitmask
& MAT_BIT_BACK_AMBIENT
) {
647 foreach (light
, list
) {
648 SCALE_3V( light
->_MatAmbient
[1], light
->Ambient
,
649 mat
[MAT_ATTRIB_BACK_AMBIENT
]);
653 /* update BaseColor = emission + scene's ambience * material's ambience */
654 if (bitmask
& (MAT_BIT_FRONT_EMISSION
| MAT_BIT_FRONT_AMBIENT
)) {
655 COPY_3V( ctx
->Light
._BaseColor
[0], mat
[MAT_ATTRIB_FRONT_EMISSION
] );
656 ACC_SCALE_3V( ctx
->Light
._BaseColor
[0], mat
[MAT_ATTRIB_FRONT_AMBIENT
],
657 ctx
->Light
.Model
.Ambient
);
660 if (bitmask
& (MAT_BIT_BACK_EMISSION
| MAT_BIT_BACK_AMBIENT
)) {
661 COPY_3V( ctx
->Light
._BaseColor
[1], mat
[MAT_ATTRIB_BACK_EMISSION
] );
662 ACC_SCALE_3V( ctx
->Light
._BaseColor
[1], mat
[MAT_ATTRIB_BACK_AMBIENT
],
663 ctx
->Light
.Model
.Ambient
);
666 /* update material diffuse values */
667 if (bitmask
& MAT_BIT_FRONT_DIFFUSE
) {
668 foreach (light
, list
) {
669 SCALE_3V( light
->_MatDiffuse
[0], light
->Diffuse
,
670 mat
[MAT_ATTRIB_FRONT_DIFFUSE
] );
674 if (bitmask
& MAT_BIT_BACK_DIFFUSE
) {
675 foreach (light
, list
) {
676 SCALE_3V( light
->_MatDiffuse
[1], light
->Diffuse
,
677 mat
[MAT_ATTRIB_BACK_DIFFUSE
] );
681 /* update material specular values */
682 if (bitmask
& MAT_BIT_FRONT_SPECULAR
) {
683 foreach (light
, list
) {
684 SCALE_3V( light
->_MatSpecular
[0], light
->Specular
,
685 mat
[MAT_ATTRIB_FRONT_SPECULAR
]);
689 if (bitmask
& MAT_BIT_BACK_SPECULAR
) {
690 foreach (light
, list
) {
691 SCALE_3V( light
->_MatSpecular
[1], light
->Specular
,
692 mat
[MAT_ATTRIB_BACK_SPECULAR
]);
699 * Update the current materials from the given rgba color
700 * according to the bitmask in ColorMaterialBitmask, which is
701 * set by glColorMaterial().
704 _mesa_update_color_material( struct gl_context
*ctx
, const GLfloat color
[4] )
706 GLuint bitmask
= ctx
->Light
.ColorMaterialBitmask
;
707 struct gl_material
*mat
= &ctx
->Light
.Material
;
710 for (i
= 0 ; i
< MAT_ATTRIB_MAX
; i
++)
711 if (bitmask
& (1<<i
))
712 COPY_4FV( mat
->Attrib
[i
], color
);
714 _mesa_update_material( ctx
, bitmask
);
719 _mesa_ColorMaterial( GLenum face
, GLenum mode
)
721 GET_CURRENT_CONTEXT(ctx
);
723 GLuint legal
= (MAT_BIT_FRONT_EMISSION
| MAT_BIT_BACK_EMISSION
|
724 MAT_BIT_FRONT_SPECULAR
| MAT_BIT_BACK_SPECULAR
|
725 MAT_BIT_FRONT_DIFFUSE
| MAT_BIT_BACK_DIFFUSE
|
726 MAT_BIT_FRONT_AMBIENT
| MAT_BIT_BACK_AMBIENT
);
727 ASSERT_OUTSIDE_BEGIN_END(ctx
);
729 if (MESA_VERBOSE
&VERBOSE_API
)
730 _mesa_debug(ctx
, "glColorMaterial %s %s\n",
731 _mesa_lookup_enum_by_nr(face
),
732 _mesa_lookup_enum_by_nr(mode
));
734 bitmask
= _mesa_material_bitmask(ctx
, face
, mode
, legal
, "glColorMaterial");
736 if (ctx
->Light
.ColorMaterialBitmask
== bitmask
&&
737 ctx
->Light
.ColorMaterialFace
== face
&&
738 ctx
->Light
.ColorMaterialMode
== mode
)
741 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
742 ctx
->Light
.ColorMaterialBitmask
= bitmask
;
743 ctx
->Light
.ColorMaterialFace
= face
;
744 ctx
->Light
.ColorMaterialMode
= mode
;
746 if (ctx
->Light
.ColorMaterialEnabled
) {
747 FLUSH_CURRENT( ctx
, 0 );
748 _mesa_update_color_material(ctx
,ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
]);
751 if (ctx
->Driver
.ColorMaterial
)
752 ctx
->Driver
.ColorMaterial( ctx
, face
, mode
);
757 _mesa_GetMaterialfv( GLenum face
, GLenum pname
, GLfloat
*params
)
759 GET_CURRENT_CONTEXT(ctx
);
761 GLfloat (*mat
)[4] = ctx
->Light
.Material
.Attrib
;
762 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
); /* update materials */
764 FLUSH_CURRENT(ctx
, 0); /* update ctx->Light.Material from vertex buffer */
766 if (face
==GL_FRONT
) {
769 else if (face
==GL_BACK
) {
773 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetMaterialfv(face)" );
779 COPY_4FV( params
, mat
[MAT_ATTRIB_AMBIENT(f
)] );
782 COPY_4FV( params
, mat
[MAT_ATTRIB_DIFFUSE(f
)] );
785 COPY_4FV( params
, mat
[MAT_ATTRIB_SPECULAR(f
)] );
788 COPY_4FV( params
, mat
[MAT_ATTRIB_EMISSION(f
)] );
791 *params
= mat
[MAT_ATTRIB_SHININESS(f
)][0];
793 case GL_COLOR_INDEXES
:
794 params
[0] = mat
[MAT_ATTRIB_INDEXES(f
)][0];
795 params
[1] = mat
[MAT_ATTRIB_INDEXES(f
)][1];
796 params
[2] = mat
[MAT_ATTRIB_INDEXES(f
)][2];
799 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetMaterialfv(pname)" );
805 _mesa_GetMaterialiv( GLenum face
, GLenum pname
, GLint
*params
)
807 GET_CURRENT_CONTEXT(ctx
);
809 GLfloat (*mat
)[4] = ctx
->Light
.Material
.Attrib
;
810 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
); /* update materials */
812 FLUSH_CURRENT(ctx
, 0); /* update ctx->Light.Material from vertex buffer */
814 if (face
==GL_FRONT
) {
817 else if (face
==GL_BACK
) {
821 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetMaterialiv(face)" );
826 params
[0] = FLOAT_TO_INT( mat
[MAT_ATTRIB_AMBIENT(f
)][0] );
827 params
[1] = FLOAT_TO_INT( mat
[MAT_ATTRIB_AMBIENT(f
)][1] );
828 params
[2] = FLOAT_TO_INT( mat
[MAT_ATTRIB_AMBIENT(f
)][2] );
829 params
[3] = FLOAT_TO_INT( mat
[MAT_ATTRIB_AMBIENT(f
)][3] );
832 params
[0] = FLOAT_TO_INT( mat
[MAT_ATTRIB_DIFFUSE(f
)][0] );
833 params
[1] = FLOAT_TO_INT( mat
[MAT_ATTRIB_DIFFUSE(f
)][1] );
834 params
[2] = FLOAT_TO_INT( mat
[MAT_ATTRIB_DIFFUSE(f
)][2] );
835 params
[3] = FLOAT_TO_INT( mat
[MAT_ATTRIB_DIFFUSE(f
)][3] );
838 params
[0] = FLOAT_TO_INT( mat
[MAT_ATTRIB_SPECULAR(f
)][0] );
839 params
[1] = FLOAT_TO_INT( mat
[MAT_ATTRIB_SPECULAR(f
)][1] );
840 params
[2] = FLOAT_TO_INT( mat
[MAT_ATTRIB_SPECULAR(f
)][2] );
841 params
[3] = FLOAT_TO_INT( mat
[MAT_ATTRIB_SPECULAR(f
)][3] );
844 params
[0] = FLOAT_TO_INT( mat
[MAT_ATTRIB_EMISSION(f
)][0] );
845 params
[1] = FLOAT_TO_INT( mat
[MAT_ATTRIB_EMISSION(f
)][1] );
846 params
[2] = FLOAT_TO_INT( mat
[MAT_ATTRIB_EMISSION(f
)][2] );
847 params
[3] = FLOAT_TO_INT( mat
[MAT_ATTRIB_EMISSION(f
)][3] );
850 *params
= IROUND( mat
[MAT_ATTRIB_SHININESS(f
)][0] );
852 case GL_COLOR_INDEXES
:
853 params
[0] = IROUND( mat
[MAT_ATTRIB_INDEXES(f
)][0] );
854 params
[1] = IROUND( mat
[MAT_ATTRIB_INDEXES(f
)][1] );
855 params
[2] = IROUND( mat
[MAT_ATTRIB_INDEXES(f
)][2] );
858 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetMaterialfv(pname)" );
865 * Examine current lighting parameters to determine if the optimized lighting
866 * function can be used.
867 * Also, precompute some lighting values such as the products of light
868 * source and material ambient, diffuse and specular coefficients.
871 _mesa_update_lighting( struct gl_context
*ctx
)
873 GLbitfield flags
= 0;
874 struct gl_light
*light
;
875 ctx
->Light
._NeedEyeCoords
= GL_FALSE
;
877 if (!ctx
->Light
.Enabled
)
880 foreach(light
, &ctx
->Light
.EnabledList
) {
881 flags
|= light
->_Flags
;
884 ctx
->Light
._NeedVertices
=
885 ((flags
& (LIGHT_POSITIONAL
|LIGHT_SPOT
)) ||
886 ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
||
887 ctx
->Light
.Model
.LocalViewer
);
889 ctx
->Light
._NeedEyeCoords
= ((flags
& LIGHT_POSITIONAL
) ||
890 ctx
->Light
.Model
.LocalViewer
);
892 /* XXX: This test is overkill & needs to be fixed both for software and
893 * hardware t&l drivers. The above should be sufficient & should
894 * be tested to verify this.
896 if (ctx
->Light
._NeedVertices
)
897 ctx
->Light
._NeedEyeCoords
= GL_TRUE
;
899 /* Precompute some shading values. Although we reference
900 * Light.Material here, we can get away without flushing
901 * FLUSH_UPDATE_CURRENT, as when any outstanding material changes
902 * are flushed, they will update the derived state at that time.
904 if (ctx
->Light
.Model
.TwoSide
)
905 _mesa_update_material(ctx
,
906 MAT_BIT_FRONT_EMISSION
|
907 MAT_BIT_FRONT_AMBIENT
|
908 MAT_BIT_FRONT_DIFFUSE
|
909 MAT_BIT_FRONT_SPECULAR
|
910 MAT_BIT_BACK_EMISSION
|
911 MAT_BIT_BACK_AMBIENT
|
912 MAT_BIT_BACK_DIFFUSE
|
913 MAT_BIT_BACK_SPECULAR
);
915 _mesa_update_material(ctx
,
916 MAT_BIT_FRONT_EMISSION
|
917 MAT_BIT_FRONT_AMBIENT
|
918 MAT_BIT_FRONT_DIFFUSE
|
919 MAT_BIT_FRONT_SPECULAR
);
924 * Update state derived from light position, spot direction.
928 * _TNL_NEW_NEED_EYE_COORDS
930 * Update on (_NEW_MODELVIEW | _NEW_LIGHT) when lighting is enabled.
931 * Also update on lighting space changes.
934 compute_light_positions( struct gl_context
*ctx
)
936 struct gl_light
*light
;
937 static const GLfloat eye_z
[3] = { 0, 0, 1 };
939 if (!ctx
->Light
.Enabled
)
942 if (ctx
->_NeedEyeCoords
) {
943 COPY_3V( ctx
->_EyeZDir
, eye_z
);
946 TRANSFORM_NORMAL( ctx
->_EyeZDir
, eye_z
, ctx
->ModelviewMatrixStack
.Top
->m
);
949 foreach (light
, &ctx
->Light
.EnabledList
) {
951 if (ctx
->_NeedEyeCoords
) {
952 /* _Position is in eye coordinate space */
953 COPY_4FV( light
->_Position
, light
->EyePosition
);
956 /* _Position is in object coordinate space */
957 TRANSFORM_POINT( light
->_Position
, ctx
->ModelviewMatrixStack
.Top
->inv
,
958 light
->EyePosition
);
961 if (!(light
->_Flags
& LIGHT_POSITIONAL
)) {
962 /* VP (VP) = Normalize( Position ) */
963 COPY_3V( light
->_VP_inf_norm
, light
->_Position
);
964 NORMALIZE_3FV( light
->_VP_inf_norm
);
966 if (!ctx
->Light
.Model
.LocalViewer
) {
967 /* _h_inf_norm = Normalize( V_to_P + <0,0,1> ) */
968 ADD_3V( light
->_h_inf_norm
, light
->_VP_inf_norm
, ctx
->_EyeZDir
);
969 NORMALIZE_3FV( light
->_h_inf_norm
);
971 light
->_VP_inf_spot_attenuation
= 1.0;
974 /* positional light w/ homogeneous coordinate, divide by W */
975 GLfloat wInv
= (GLfloat
)1.0 / light
->_Position
[3];
976 light
->_Position
[0] *= wInv
;
977 light
->_Position
[1] *= wInv
;
978 light
->_Position
[2] *= wInv
;
981 if (light
->_Flags
& LIGHT_SPOT
) {
982 /* Note: we normalize the spot direction now */
984 if (ctx
->_NeedEyeCoords
) {
985 COPY_3V( light
->_NormSpotDirection
, light
->SpotDirection
);
986 NORMALIZE_3FV( light
->_NormSpotDirection
);
990 COPY_3V(spotDir
, light
->SpotDirection
);
991 NORMALIZE_3FV(spotDir
);
992 TRANSFORM_NORMAL( light
->_NormSpotDirection
,
994 ctx
->ModelviewMatrixStack
.Top
->m
);
997 NORMALIZE_3FV( light
->_NormSpotDirection
);
999 if (!(light
->_Flags
& LIGHT_POSITIONAL
)) {
1000 GLfloat PV_dot_dir
= - DOT3(light
->_VP_inf_norm
,
1001 light
->_NormSpotDirection
);
1003 if (PV_dot_dir
> light
->_CosCutoff
) {
1004 light
->_VP_inf_spot_attenuation
=
1005 powf(PV_dot_dir
, light
->SpotExponent
);
1008 light
->_VP_inf_spot_attenuation
= 0;
1018 update_modelview_scale( struct gl_context
*ctx
)
1020 ctx
->_ModelViewInvScale
= 1.0F
;
1021 if (!_math_matrix_is_length_preserving(ctx
->ModelviewMatrixStack
.Top
)) {
1022 const GLfloat
*m
= ctx
->ModelviewMatrixStack
.Top
->inv
;
1023 GLfloat f
= m
[2] * m
[2] + m
[6] * m
[6] + m
[10] * m
[10];
1024 if (f
< 1e-12) f
= 1.0;
1025 if (ctx
->_NeedEyeCoords
)
1026 ctx
->_ModelViewInvScale
= (GLfloat
) INV_SQRTF(f
);
1028 ctx
->_ModelViewInvScale
= (GLfloat
) SQRTF(f
);
1034 * Bring up to date any state that relies on _NeedEyeCoords.
1037 _mesa_update_tnl_spaces( struct gl_context
*ctx
, GLuint new_state
)
1039 const GLuint oldneedeyecoords
= ctx
->_NeedEyeCoords
;
1042 ctx
->_NeedEyeCoords
= GL_FALSE
;
1044 if (ctx
->_ForceEyeCoords
||
1045 (ctx
->Texture
._GenFlags
& TEXGEN_NEED_EYE_COORD
) ||
1046 ctx
->Point
._Attenuated
||
1047 ctx
->Light
._NeedEyeCoords
)
1048 ctx
->_NeedEyeCoords
= GL_TRUE
;
1050 if (ctx
->Light
.Enabled
&&
1051 !_math_matrix_is_length_preserving(ctx
->ModelviewMatrixStack
.Top
))
1052 ctx
->_NeedEyeCoords
= GL_TRUE
;
1054 /* Check if the truth-value interpretations of the bitfields have
1057 if (oldneedeyecoords
!= ctx
->_NeedEyeCoords
) {
1058 /* Recalculate all state that depends on _NeedEyeCoords.
1060 update_modelview_scale(ctx
);
1061 compute_light_positions( ctx
);
1063 if (ctx
->Driver
.LightingSpaceChange
)
1064 ctx
->Driver
.LightingSpaceChange( ctx
);
1067 GLuint new_state2
= ctx
->NewState
;
1069 /* Recalculate that same state only if it has been invalidated
1070 * by other statechanges.
1072 if (new_state2
& _NEW_MODELVIEW
)
1073 update_modelview_scale(ctx
);
1075 if (new_state2
& (_NEW_LIGHT
|_NEW_MODELVIEW
))
1076 compute_light_positions( ctx
);
1082 * Drivers may need this if the hardware tnl unit doesn't support the
1083 * light-in-modelspace optimization. It's also useful for debugging.
1086 _mesa_allow_light_in_model( struct gl_context
*ctx
, GLboolean flag
)
1088 ctx
->_ForceEyeCoords
= !flag
;
1089 ctx
->NewState
|= _NEW_POINT
; /* one of the bits from
1090 * _MESA_NEW_NEED_EYE_COORDS.
1096 /**********************************************************************/
1097 /***** Initialization *****/
1098 /**********************************************************************/
1101 * Initialize the n-th light data structure.
1103 * \param l pointer to the gl_light structure to be initialized.
1104 * \param n number of the light.
1105 * \note The defaults for light 0 are different than the other lights.
1108 init_light( struct gl_light
*l
, GLuint n
)
1110 make_empty_list( l
);
1112 ASSIGN_4V( l
->Ambient
, 0.0, 0.0, 0.0, 1.0 );
1114 ASSIGN_4V( l
->Diffuse
, 1.0, 1.0, 1.0, 1.0 );
1115 ASSIGN_4V( l
->Specular
, 1.0, 1.0, 1.0, 1.0 );
1118 ASSIGN_4V( l
->Diffuse
, 0.0, 0.0, 0.0, 1.0 );
1119 ASSIGN_4V( l
->Specular
, 0.0, 0.0, 0.0, 1.0 );
1121 ASSIGN_4V( l
->EyePosition
, 0.0, 0.0, 1.0, 0.0 );
1122 ASSIGN_3V( l
->SpotDirection
, 0.0, 0.0, -1.0 );
1123 l
->SpotExponent
= 0.0;
1124 l
->SpotCutoff
= 180.0;
1125 l
->_CosCutoff
= 0.0; /* KW: -ve values not admitted */
1126 l
->ConstantAttenuation
= 1.0;
1127 l
->LinearAttenuation
= 0.0;
1128 l
->QuadraticAttenuation
= 0.0;
1129 l
->Enabled
= GL_FALSE
;
1134 * Initialize the light model data structure.
1136 * \param lm pointer to the gl_lightmodel structure to be initialized.
1139 init_lightmodel( struct gl_lightmodel
*lm
)
1141 ASSIGN_4V( lm
->Ambient
, 0.2F
, 0.2F
, 0.2F
, 1.0F
);
1142 lm
->LocalViewer
= GL_FALSE
;
1143 lm
->TwoSide
= GL_FALSE
;
1144 lm
->ColorControl
= GL_SINGLE_COLOR
;
1149 * Initialize the material data structure.
1151 * \param m pointer to the gl_material structure to be initialized.
1154 init_material( struct gl_material
*m
)
1156 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_FRONT_AMBIENT
], 0.2F
, 0.2F
, 0.2F
, 1.0F
);
1157 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
], 0.8F
, 0.8F
, 0.8F
, 1.0F
);
1158 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_FRONT_SPECULAR
], 0.0F
, 0.0F
, 0.0F
, 1.0F
);
1159 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_FRONT_EMISSION
], 0.0F
, 0.0F
, 0.0F
, 1.0F
);
1160 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_FRONT_SHININESS
], 0.0F
, 0.0F
, 0.0F
, 0.0F
);
1161 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_FRONT_INDEXES
], 0.0F
, 1.0F
, 1.0F
, 0.0F
);
1163 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_BACK_AMBIENT
], 0.2F
, 0.2F
, 0.2F
, 1.0F
);
1164 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_BACK_DIFFUSE
], 0.8F
, 0.8F
, 0.8F
, 1.0F
);
1165 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_BACK_SPECULAR
], 0.0F
, 0.0F
, 0.0F
, 1.0F
);
1166 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_BACK_EMISSION
], 0.0F
, 0.0F
, 0.0F
, 1.0F
);
1167 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_BACK_SHININESS
], 0.0F
, 0.0F
, 0.0F
, 0.0F
);
1168 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_BACK_INDEXES
], 0.0F
, 1.0F
, 1.0F
, 0.0F
);
1173 * Initialize all lighting state for the given context.
1176 _mesa_init_lighting( struct gl_context
*ctx
)
1180 /* Lighting group */
1181 for (i
= 0; i
< MAX_LIGHTS
; i
++) {
1182 init_light( &ctx
->Light
.Light
[i
], i
);
1184 make_empty_list( &ctx
->Light
.EnabledList
);
1186 init_lightmodel( &ctx
->Light
.Model
);
1187 init_material( &ctx
->Light
.Material
);
1188 ctx
->Light
.ShadeModel
= GL_SMOOTH
;
1189 ctx
->Light
.ProvokingVertex
= GL_LAST_VERTEX_CONVENTION_EXT
;
1190 ctx
->Light
.Enabled
= GL_FALSE
;
1191 ctx
->Light
.ColorMaterialFace
= GL_FRONT_AND_BACK
;
1192 ctx
->Light
.ColorMaterialMode
= GL_AMBIENT_AND_DIFFUSE
;
1193 ctx
->Light
.ColorMaterialBitmask
= _mesa_material_bitmask( ctx
,
1195 GL_AMBIENT_AND_DIFFUSE
, ~0,
1198 ctx
->Light
.ColorMaterialEnabled
= GL_FALSE
;
1199 ctx
->Light
.ClampVertexColor
= GL_TRUE
;
1202 ctx
->Light
._NeedEyeCoords
= GL_FALSE
;
1203 ctx
->_NeedEyeCoords
= GL_FALSE
;
1204 ctx
->_ForceEyeCoords
= GL_FALSE
;
1205 ctx
->_ModelViewInvScale
= 1.0;
1210 * Deallocate malloc'd lighting state attached to given context.
1213 _mesa_free_lighting_data( struct gl_context
*ctx
)