2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * 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
);
43 if (MESA_VERBOSE
& VERBOSE_API
)
44 _mesa_debug(ctx
, "glShadeModel %s\n", _mesa_lookup_enum_by_nr(mode
));
46 if (mode
!= GL_FLAT
&& mode
!= GL_SMOOTH
) {
47 _mesa_error(ctx
, GL_INVALID_ENUM
, "glShadeModel");
51 if (ctx
->Light
.ShadeModel
== mode
)
54 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
55 ctx
->Light
.ShadeModel
= mode
;
57 if (ctx
->Driver
.ShadeModel
)
58 ctx
->Driver
.ShadeModel( ctx
, mode
);
63 * Set the provoking vertex (the vertex which specifies the prim's
64 * color when flat shading) to either the first or last vertex of the
68 _mesa_ProvokingVertex(GLenum mode
)
70 GET_CURRENT_CONTEXT(ctx
);
72 if (MESA_VERBOSE
&VERBOSE_API
)
73 _mesa_debug(ctx
, "glProvokingVertexEXT 0x%x\n", mode
);
76 case GL_FIRST_VERTEX_CONVENTION_EXT
:
77 case GL_LAST_VERTEX_CONVENTION_EXT
:
80 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProvokingVertexEXT(0x%x)", mode
);
84 if (ctx
->Light
.ProvokingVertex
== mode
)
87 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
88 ctx
->Light
.ProvokingVertex
= mode
;
93 * Helper function called by _mesa_Lightfv and _mesa_PopAttrib to set
95 * For GL_POSITION and GL_SPOT_DIRECTION the params position/direction
96 * will have already been transformed by the modelview matrix!
97 * Also, all error checking should have already been done.
100 _mesa_light(struct gl_context
*ctx
, GLuint lnum
, GLenum pname
, const GLfloat
*params
)
102 struct gl_light
*light
;
104 ASSERT(lnum
< MAX_LIGHTS
);
105 light
= &ctx
->Light
.Light
[lnum
];
109 if (TEST_EQ_4V(light
->Ambient
, params
))
111 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
112 COPY_4V( light
->Ambient
, params
);
115 if (TEST_EQ_4V(light
->Diffuse
, params
))
117 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
118 COPY_4V( light
->Diffuse
, params
);
121 if (TEST_EQ_4V(light
->Specular
, params
))
123 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
124 COPY_4V( light
->Specular
, params
);
127 /* NOTE: position has already been transformed by ModelView! */
128 if (TEST_EQ_4V(light
->EyePosition
, params
))
130 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
131 COPY_4V(light
->EyePosition
, params
);
132 if (light
->EyePosition
[3] != 0.0F
)
133 light
->_Flags
|= LIGHT_POSITIONAL
;
135 light
->_Flags
&= ~LIGHT_POSITIONAL
;
137 case GL_SPOT_DIRECTION
:
138 /* NOTE: Direction already transformed by inverse ModelView! */
139 if (TEST_EQ_3V(light
->SpotDirection
, params
))
141 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
142 COPY_3V(light
->SpotDirection
, params
);
144 case GL_SPOT_EXPONENT
:
145 ASSERT(params
[0] >= 0.0);
146 ASSERT(params
[0] <= ctx
->Const
.MaxSpotExponent
);
147 if (light
->SpotExponent
== params
[0])
149 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
150 light
->SpotExponent
= params
[0];
153 ASSERT(params
[0] == 180.0 || (params
[0] >= 0.0 && params
[0] <= 90.0));
154 if (light
->SpotCutoff
== params
[0])
156 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
157 light
->SpotCutoff
= params
[0];
158 light
->_CosCutoff
= (GLfloat
) (cos(light
->SpotCutoff
* DEG2RAD
));
159 if (light
->_CosCutoff
< 0)
160 light
->_CosCutoff
= 0;
161 if (light
->SpotCutoff
!= 180.0F
)
162 light
->_Flags
|= LIGHT_SPOT
;
164 light
->_Flags
&= ~LIGHT_SPOT
;
166 case GL_CONSTANT_ATTENUATION
:
167 ASSERT(params
[0] >= 0.0);
168 if (light
->ConstantAttenuation
== params
[0])
170 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
171 light
->ConstantAttenuation
= params
[0];
173 case GL_LINEAR_ATTENUATION
:
174 ASSERT(params
[0] >= 0.0);
175 if (light
->LinearAttenuation
== params
[0])
177 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
178 light
->LinearAttenuation
= params
[0];
180 case GL_QUADRATIC_ATTENUATION
:
181 ASSERT(params
[0] >= 0.0);
182 if (light
->QuadraticAttenuation
== params
[0])
184 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
185 light
->QuadraticAttenuation
= params
[0];
188 _mesa_problem(ctx
, "Unexpected pname in _mesa_light()");
192 if (ctx
->Driver
.Lightfv
)
193 ctx
->Driver
.Lightfv( ctx
, GL_LIGHT0
+ lnum
, pname
, params
);
198 _mesa_Lightf( GLenum light
, GLenum pname
, GLfloat param
)
202 fparam
[1] = fparam
[2] = fparam
[3] = 0.0F
;
203 _mesa_Lightfv( light
, pname
, fparam
);
208 _mesa_Lightfv( GLenum light
, GLenum pname
, const GLfloat
*params
)
210 GET_CURRENT_CONTEXT(ctx
);
211 GLint i
= (GLint
) (light
- GL_LIGHT0
);
214 if (i
< 0 || i
>= (GLint
) ctx
->Const
.MaxLights
) {
215 _mesa_error( ctx
, GL_INVALID_ENUM
, "glLight(light=0x%x)", light
);
219 /* do particular error checks, transformations */
227 /* transform position by ModelView matrix */
228 TRANSFORM_POINT(temp
, ctx
->ModelviewMatrixStack
.Top
->m
, params
);
231 case GL_SPOT_DIRECTION
:
232 /* transform direction by inverse modelview */
233 if (_math_matrix_is_dirty(ctx
->ModelviewMatrixStack
.Top
)) {
234 _math_matrix_analyse(ctx
->ModelviewMatrixStack
.Top
);
236 TRANSFORM_DIRECTION(temp
, params
, ctx
->ModelviewMatrixStack
.Top
->m
);
239 case GL_SPOT_EXPONENT
:
240 if (params
[0] < 0.0 || params
[0] > ctx
->Const
.MaxSpotExponent
) {
241 _mesa_error(ctx
, GL_INVALID_VALUE
, "glLight");
246 if ((params
[0] < 0.0 || params
[0] > 90.0) && params
[0] != 180.0) {
247 _mesa_error(ctx
, GL_INVALID_VALUE
, "glLight");
251 case GL_CONSTANT_ATTENUATION
:
252 if (params
[0] < 0.0) {
253 _mesa_error(ctx
, GL_INVALID_VALUE
, "glLight");
257 case GL_LINEAR_ATTENUATION
:
258 if (params
[0] < 0.0) {
259 _mesa_error(ctx
, GL_INVALID_VALUE
, "glLight");
263 case GL_QUADRATIC_ATTENUATION
:
264 if (params
[0] < 0.0) {
265 _mesa_error(ctx
, GL_INVALID_VALUE
, "glLight");
270 _mesa_error(ctx
, GL_INVALID_ENUM
, "glLight(pname=0x%x)", pname
);
274 _mesa_light(ctx
, i
, pname
, params
);
279 _mesa_Lighti( GLenum light
, GLenum pname
, GLint param
)
283 iparam
[1] = iparam
[2] = iparam
[3] = 0;
284 _mesa_Lightiv( light
, pname
, iparam
);
289 _mesa_Lightiv( GLenum light
, GLenum pname
, const GLint
*params
)
297 fparam
[0] = INT_TO_FLOAT( params
[0] );
298 fparam
[1] = INT_TO_FLOAT( params
[1] );
299 fparam
[2] = INT_TO_FLOAT( params
[2] );
300 fparam
[3] = INT_TO_FLOAT( params
[3] );
303 fparam
[0] = (GLfloat
) params
[0];
304 fparam
[1] = (GLfloat
) params
[1];
305 fparam
[2] = (GLfloat
) params
[2];
306 fparam
[3] = (GLfloat
) params
[3];
308 case GL_SPOT_DIRECTION
:
309 fparam
[0] = (GLfloat
) params
[0];
310 fparam
[1] = (GLfloat
) params
[1];
311 fparam
[2] = (GLfloat
) params
[2];
313 case GL_SPOT_EXPONENT
:
315 case GL_CONSTANT_ATTENUATION
:
316 case GL_LINEAR_ATTENUATION
:
317 case GL_QUADRATIC_ATTENUATION
:
318 fparam
[0] = (GLfloat
) params
[0];
321 /* error will be caught later in gl_Lightfv */
325 _mesa_Lightfv( light
, pname
, fparam
);
331 _mesa_GetLightfv( GLenum light
, GLenum pname
, GLfloat
*params
)
333 GET_CURRENT_CONTEXT(ctx
);
334 GLint l
= (GLint
) (light
- GL_LIGHT0
);
336 if (l
< 0 || l
>= (GLint
) ctx
->Const
.MaxLights
) {
337 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetLightfv" );
343 COPY_4V( params
, ctx
->Light
.Light
[l
].Ambient
);
346 COPY_4V( params
, ctx
->Light
.Light
[l
].Diffuse
);
349 COPY_4V( params
, ctx
->Light
.Light
[l
].Specular
);
352 COPY_4V( params
, ctx
->Light
.Light
[l
].EyePosition
);
354 case GL_SPOT_DIRECTION
:
355 COPY_3V( params
, ctx
->Light
.Light
[l
].SpotDirection
);
357 case GL_SPOT_EXPONENT
:
358 params
[0] = ctx
->Light
.Light
[l
].SpotExponent
;
361 params
[0] = ctx
->Light
.Light
[l
].SpotCutoff
;
363 case GL_CONSTANT_ATTENUATION
:
364 params
[0] = ctx
->Light
.Light
[l
].ConstantAttenuation
;
366 case GL_LINEAR_ATTENUATION
:
367 params
[0] = ctx
->Light
.Light
[l
].LinearAttenuation
;
369 case GL_QUADRATIC_ATTENUATION
:
370 params
[0] = ctx
->Light
.Light
[l
].QuadraticAttenuation
;
373 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetLightfv" );
380 _mesa_GetLightiv( GLenum light
, GLenum pname
, GLint
*params
)
382 GET_CURRENT_CONTEXT(ctx
);
383 GLint l
= (GLint
) (light
- GL_LIGHT0
);
385 if (l
< 0 || l
>= (GLint
) ctx
->Const
.MaxLights
) {
386 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetLightiv" );
392 params
[0] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Ambient
[0]);
393 params
[1] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Ambient
[1]);
394 params
[2] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Ambient
[2]);
395 params
[3] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Ambient
[3]);
398 params
[0] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Diffuse
[0]);
399 params
[1] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Diffuse
[1]);
400 params
[2] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Diffuse
[2]);
401 params
[3] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Diffuse
[3]);
404 params
[0] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Specular
[0]);
405 params
[1] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Specular
[1]);
406 params
[2] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Specular
[2]);
407 params
[3] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Specular
[3]);
410 params
[0] = (GLint
) ctx
->Light
.Light
[l
].EyePosition
[0];
411 params
[1] = (GLint
) ctx
->Light
.Light
[l
].EyePosition
[1];
412 params
[2] = (GLint
) ctx
->Light
.Light
[l
].EyePosition
[2];
413 params
[3] = (GLint
) ctx
->Light
.Light
[l
].EyePosition
[3];
415 case GL_SPOT_DIRECTION
:
416 params
[0] = (GLint
) ctx
->Light
.Light
[l
].SpotDirection
[0];
417 params
[1] = (GLint
) ctx
->Light
.Light
[l
].SpotDirection
[1];
418 params
[2] = (GLint
) ctx
->Light
.Light
[l
].SpotDirection
[2];
420 case GL_SPOT_EXPONENT
:
421 params
[0] = (GLint
) ctx
->Light
.Light
[l
].SpotExponent
;
424 params
[0] = (GLint
) ctx
->Light
.Light
[l
].SpotCutoff
;
426 case GL_CONSTANT_ATTENUATION
:
427 params
[0] = (GLint
) ctx
->Light
.Light
[l
].ConstantAttenuation
;
429 case GL_LINEAR_ATTENUATION
:
430 params
[0] = (GLint
) ctx
->Light
.Light
[l
].LinearAttenuation
;
432 case GL_QUADRATIC_ATTENUATION
:
433 params
[0] = (GLint
) ctx
->Light
.Light
[l
].QuadraticAttenuation
;
436 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetLightiv" );
443 /**********************************************************************/
444 /*** Light Model ***/
445 /**********************************************************************/
449 _mesa_LightModelfv( GLenum pname
, const GLfloat
*params
)
453 GET_CURRENT_CONTEXT(ctx
);
456 case GL_LIGHT_MODEL_AMBIENT
:
457 if (TEST_EQ_4V( ctx
->Light
.Model
.Ambient
, params
))
459 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
460 COPY_4V( ctx
->Light
.Model
.Ambient
, params
);
462 case GL_LIGHT_MODEL_LOCAL_VIEWER
:
463 if (ctx
->API
!= API_OPENGL_COMPAT
)
465 newbool
= (params
[0]!=0.0);
466 if (ctx
->Light
.Model
.LocalViewer
== newbool
)
468 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
469 ctx
->Light
.Model
.LocalViewer
= newbool
;
471 case GL_LIGHT_MODEL_TWO_SIDE
:
472 newbool
= (params
[0]!=0.0);
473 if (ctx
->Light
.Model
.TwoSide
== newbool
)
475 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
476 ctx
->Light
.Model
.TwoSide
= newbool
;
478 case GL_LIGHT_MODEL_COLOR_CONTROL
:
479 if (ctx
->API
!= API_OPENGL_COMPAT
)
481 if (params
[0] == (GLfloat
) GL_SINGLE_COLOR
)
482 newenum
= GL_SINGLE_COLOR
;
483 else if (params
[0] == (GLfloat
) GL_SEPARATE_SPECULAR_COLOR
)
484 newenum
= GL_SEPARATE_SPECULAR_COLOR
;
486 _mesa_error( ctx
, GL_INVALID_ENUM
, "glLightModel(param=0x0%x)",
490 if (ctx
->Light
.Model
.ColorControl
== newenum
)
492 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
493 ctx
->Light
.Model
.ColorControl
= newenum
;
499 if (ctx
->Driver
.LightModelfv
)
500 ctx
->Driver
.LightModelfv( ctx
, pname
, params
);
505 _mesa_error( ctx
, GL_INVALID_ENUM
, "glLightModel(pname=0x%x)", pname
);
511 _mesa_LightModeliv( GLenum pname
, const GLint
*params
)
516 case GL_LIGHT_MODEL_AMBIENT
:
517 fparam
[0] = INT_TO_FLOAT( params
[0] );
518 fparam
[1] = INT_TO_FLOAT( params
[1] );
519 fparam
[2] = INT_TO_FLOAT( params
[2] );
520 fparam
[3] = INT_TO_FLOAT( params
[3] );
522 case GL_LIGHT_MODEL_LOCAL_VIEWER
:
523 case GL_LIGHT_MODEL_TWO_SIDE
:
524 case GL_LIGHT_MODEL_COLOR_CONTROL
:
525 fparam
[0] = (GLfloat
) params
[0];
528 /* Error will be caught later in gl_LightModelfv */
529 ASSIGN_4V(fparam
, 0.0F
, 0.0F
, 0.0F
, 0.0F
);
531 _mesa_LightModelfv( pname
, fparam
);
536 _mesa_LightModeli( GLenum pname
, GLint param
)
540 iparam
[1] = iparam
[2] = iparam
[3] = 0;
541 _mesa_LightModeliv( pname
, iparam
);
546 _mesa_LightModelf( GLenum pname
, GLfloat param
)
550 fparam
[1] = fparam
[2] = fparam
[3] = 0.0F
;
551 _mesa_LightModelfv( pname
, fparam
);
556 /********** MATERIAL **********/
560 * Given a face and pname value (ala glColorMaterial), compute a bitmask
561 * of the targeted material values.
564 _mesa_material_bitmask( struct gl_context
*ctx
, GLenum face
, GLenum pname
,
565 GLuint legal
, const char *where
)
569 /* Make a bitmask indicating what material attribute(s) we're updating */
572 bitmask
|= MAT_BIT_FRONT_EMISSION
| MAT_BIT_BACK_EMISSION
;
575 bitmask
|= MAT_BIT_FRONT_AMBIENT
| MAT_BIT_BACK_AMBIENT
;
578 bitmask
|= MAT_BIT_FRONT_DIFFUSE
| MAT_BIT_BACK_DIFFUSE
;
581 bitmask
|= MAT_BIT_FRONT_SPECULAR
| MAT_BIT_BACK_SPECULAR
;
584 bitmask
|= MAT_BIT_FRONT_SHININESS
| MAT_BIT_BACK_SHININESS
;
586 case GL_AMBIENT_AND_DIFFUSE
:
587 bitmask
|= MAT_BIT_FRONT_AMBIENT
| MAT_BIT_BACK_AMBIENT
;
588 bitmask
|= MAT_BIT_FRONT_DIFFUSE
| MAT_BIT_BACK_DIFFUSE
;
590 case GL_COLOR_INDEXES
:
591 bitmask
|= MAT_BIT_FRONT_INDEXES
| MAT_BIT_BACK_INDEXES
;
594 _mesa_error( ctx
, GL_INVALID_ENUM
, "%s", where
);
598 if (face
==GL_FRONT
) {
599 bitmask
&= FRONT_MATERIAL_BITS
;
601 else if (face
==GL_BACK
) {
602 bitmask
&= BACK_MATERIAL_BITS
;
604 else if (face
!= GL_FRONT_AND_BACK
) {
605 _mesa_error( ctx
, GL_INVALID_ENUM
, "%s", where
);
609 if (bitmask
& ~legal
) {
610 _mesa_error( ctx
, GL_INVALID_ENUM
, "%s", where
);
619 /* Update derived values following a change in ctx->Light.Material
622 _mesa_update_material( struct gl_context
*ctx
, GLuint bitmask
)
624 struct gl_light
*light
, *list
= &ctx
->Light
.EnabledList
;
625 GLfloat (*mat
)[4] = ctx
->Light
.Material
.Attrib
;
627 if (MESA_VERBOSE
& VERBOSE_MATERIAL
)
628 _mesa_debug(ctx
, "_mesa_update_material, mask 0x%x\n", bitmask
);
633 /* update material ambience */
634 if (bitmask
& MAT_BIT_FRONT_AMBIENT
) {
635 foreach (light
, list
) {
636 SCALE_3V( light
->_MatAmbient
[0], light
->Ambient
,
637 mat
[MAT_ATTRIB_FRONT_AMBIENT
]);
641 if (bitmask
& MAT_BIT_BACK_AMBIENT
) {
642 foreach (light
, list
) {
643 SCALE_3V( light
->_MatAmbient
[1], light
->Ambient
,
644 mat
[MAT_ATTRIB_BACK_AMBIENT
]);
648 /* update BaseColor = emission + scene's ambience * material's ambience */
649 if (bitmask
& (MAT_BIT_FRONT_EMISSION
| MAT_BIT_FRONT_AMBIENT
)) {
650 COPY_3V( ctx
->Light
._BaseColor
[0], mat
[MAT_ATTRIB_FRONT_EMISSION
] );
651 ACC_SCALE_3V( ctx
->Light
._BaseColor
[0], mat
[MAT_ATTRIB_FRONT_AMBIENT
],
652 ctx
->Light
.Model
.Ambient
);
655 if (bitmask
& (MAT_BIT_BACK_EMISSION
| MAT_BIT_BACK_AMBIENT
)) {
656 COPY_3V( ctx
->Light
._BaseColor
[1], mat
[MAT_ATTRIB_BACK_EMISSION
] );
657 ACC_SCALE_3V( ctx
->Light
._BaseColor
[1], mat
[MAT_ATTRIB_BACK_AMBIENT
],
658 ctx
->Light
.Model
.Ambient
);
661 /* update material diffuse values */
662 if (bitmask
& MAT_BIT_FRONT_DIFFUSE
) {
663 foreach (light
, list
) {
664 SCALE_3V( light
->_MatDiffuse
[0], light
->Diffuse
,
665 mat
[MAT_ATTRIB_FRONT_DIFFUSE
] );
669 if (bitmask
& MAT_BIT_BACK_DIFFUSE
) {
670 foreach (light
, list
) {
671 SCALE_3V( light
->_MatDiffuse
[1], light
->Diffuse
,
672 mat
[MAT_ATTRIB_BACK_DIFFUSE
] );
676 /* update material specular values */
677 if (bitmask
& MAT_BIT_FRONT_SPECULAR
) {
678 foreach (light
, list
) {
679 SCALE_3V( light
->_MatSpecular
[0], light
->Specular
,
680 mat
[MAT_ATTRIB_FRONT_SPECULAR
]);
684 if (bitmask
& MAT_BIT_BACK_SPECULAR
) {
685 foreach (light
, list
) {
686 SCALE_3V( light
->_MatSpecular
[1], light
->Specular
,
687 mat
[MAT_ATTRIB_BACK_SPECULAR
]);
694 * Update the current materials from the given rgba color
695 * according to the bitmask in _ColorMaterialBitmask, which is
696 * set by glColorMaterial().
699 _mesa_update_color_material( struct gl_context
*ctx
, const GLfloat color
[4] )
701 const GLbitfield bitmask
= ctx
->Light
._ColorMaterialBitmask
;
702 struct gl_material
*mat
= &ctx
->Light
.Material
;
705 for (i
= 0 ; i
< MAT_ATTRIB_MAX
; i
++)
706 if (bitmask
& (1<<i
))
707 COPY_4FV( mat
->Attrib
[i
], color
);
709 _mesa_update_material( ctx
, bitmask
);
714 _mesa_ColorMaterial( GLenum face
, GLenum mode
)
716 GET_CURRENT_CONTEXT(ctx
);
718 GLuint legal
= (MAT_BIT_FRONT_EMISSION
| MAT_BIT_BACK_EMISSION
|
719 MAT_BIT_FRONT_SPECULAR
| MAT_BIT_BACK_SPECULAR
|
720 MAT_BIT_FRONT_DIFFUSE
| MAT_BIT_BACK_DIFFUSE
|
721 MAT_BIT_FRONT_AMBIENT
| MAT_BIT_BACK_AMBIENT
);
723 if (MESA_VERBOSE
&VERBOSE_API
)
724 _mesa_debug(ctx
, "glColorMaterial %s %s\n",
725 _mesa_lookup_enum_by_nr(face
),
726 _mesa_lookup_enum_by_nr(mode
));
728 bitmask
= _mesa_material_bitmask(ctx
, face
, mode
, legal
, "glColorMaterial");
730 return; /* error was recorded */
732 if (ctx
->Light
._ColorMaterialBitmask
== bitmask
&&
733 ctx
->Light
.ColorMaterialFace
== face
&&
734 ctx
->Light
.ColorMaterialMode
== mode
)
737 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
738 ctx
->Light
._ColorMaterialBitmask
= bitmask
;
739 ctx
->Light
.ColorMaterialFace
= face
;
740 ctx
->Light
.ColorMaterialMode
= mode
;
742 if (ctx
->Light
.ColorMaterialEnabled
) {
743 FLUSH_CURRENT( ctx
, 0 );
744 _mesa_update_color_material(ctx
,ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
]);
747 if (ctx
->Driver
.ColorMaterial
)
748 ctx
->Driver
.ColorMaterial( ctx
, face
, mode
);
753 _mesa_GetMaterialfv( GLenum face
, GLenum pname
, GLfloat
*params
)
755 GET_CURRENT_CONTEXT(ctx
);
757 GLfloat (*mat
)[4] = ctx
->Light
.Material
.Attrib
;
758 FLUSH_VERTICES(ctx
, 0); /* update materials */
760 FLUSH_CURRENT(ctx
, 0); /* update ctx->Light.Material from vertex buffer */
762 if (face
==GL_FRONT
) {
765 else if (face
==GL_BACK
) {
769 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetMaterialfv(face)" );
775 COPY_4FV( params
, mat
[MAT_ATTRIB_AMBIENT(f
)] );
778 COPY_4FV( params
, mat
[MAT_ATTRIB_DIFFUSE(f
)] );
781 COPY_4FV( params
, mat
[MAT_ATTRIB_SPECULAR(f
)] );
784 COPY_4FV( params
, mat
[MAT_ATTRIB_EMISSION(f
)] );
787 *params
= mat
[MAT_ATTRIB_SHININESS(f
)][0];
789 case GL_COLOR_INDEXES
:
790 if (ctx
->API
!= API_OPENGL_COMPAT
) {
791 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetMaterialfv(pname)" );
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
;
811 ASSERT(ctx
->API
== API_OPENGL_COMPAT
);
813 FLUSH_VERTICES(ctx
, 0); /* update materials */
814 FLUSH_CURRENT(ctx
, 0); /* update ctx->Light.Material from vertex buffer */
816 if (face
==GL_FRONT
) {
819 else if (face
==GL_BACK
) {
823 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetMaterialiv(face)" );
828 params
[0] = FLOAT_TO_INT( mat
[MAT_ATTRIB_AMBIENT(f
)][0] );
829 params
[1] = FLOAT_TO_INT( mat
[MAT_ATTRIB_AMBIENT(f
)][1] );
830 params
[2] = FLOAT_TO_INT( mat
[MAT_ATTRIB_AMBIENT(f
)][2] );
831 params
[3] = FLOAT_TO_INT( mat
[MAT_ATTRIB_AMBIENT(f
)][3] );
834 params
[0] = FLOAT_TO_INT( mat
[MAT_ATTRIB_DIFFUSE(f
)][0] );
835 params
[1] = FLOAT_TO_INT( mat
[MAT_ATTRIB_DIFFUSE(f
)][1] );
836 params
[2] = FLOAT_TO_INT( mat
[MAT_ATTRIB_DIFFUSE(f
)][2] );
837 params
[3] = FLOAT_TO_INT( mat
[MAT_ATTRIB_DIFFUSE(f
)][3] );
840 params
[0] = FLOAT_TO_INT( mat
[MAT_ATTRIB_SPECULAR(f
)][0] );
841 params
[1] = FLOAT_TO_INT( mat
[MAT_ATTRIB_SPECULAR(f
)][1] );
842 params
[2] = FLOAT_TO_INT( mat
[MAT_ATTRIB_SPECULAR(f
)][2] );
843 params
[3] = FLOAT_TO_INT( mat
[MAT_ATTRIB_SPECULAR(f
)][3] );
846 params
[0] = FLOAT_TO_INT( mat
[MAT_ATTRIB_EMISSION(f
)][0] );
847 params
[1] = FLOAT_TO_INT( mat
[MAT_ATTRIB_EMISSION(f
)][1] );
848 params
[2] = FLOAT_TO_INT( mat
[MAT_ATTRIB_EMISSION(f
)][2] );
849 params
[3] = FLOAT_TO_INT( mat
[MAT_ATTRIB_EMISSION(f
)][3] );
852 *params
= IROUND( mat
[MAT_ATTRIB_SHININESS(f
)][0] );
854 case GL_COLOR_INDEXES
:
855 params
[0] = IROUND( mat
[MAT_ATTRIB_INDEXES(f
)][0] );
856 params
[1] = IROUND( mat
[MAT_ATTRIB_INDEXES(f
)][1] );
857 params
[2] = IROUND( mat
[MAT_ATTRIB_INDEXES(f
)][2] );
860 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetMaterialfv(pname)" );
867 * Examine current lighting parameters to determine if the optimized lighting
868 * function can be used.
869 * Also, precompute some lighting values such as the products of light
870 * source and material ambient, diffuse and specular coefficients.
873 _mesa_update_lighting( struct gl_context
*ctx
)
875 GLbitfield flags
= 0;
876 struct gl_light
*light
;
877 ctx
->Light
._NeedEyeCoords
= GL_FALSE
;
879 if (!ctx
->Light
.Enabled
)
882 foreach(light
, &ctx
->Light
.EnabledList
) {
883 flags
|= light
->_Flags
;
886 ctx
->Light
._NeedVertices
=
887 ((flags
& (LIGHT_POSITIONAL
|LIGHT_SPOT
)) ||
888 ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
||
889 ctx
->Light
.Model
.LocalViewer
);
891 ctx
->Light
._NeedEyeCoords
= ((flags
& LIGHT_POSITIONAL
) ||
892 ctx
->Light
.Model
.LocalViewer
);
894 /* XXX: This test is overkill & needs to be fixed both for software and
895 * hardware t&l drivers. The above should be sufficient & should
896 * be tested to verify this.
898 if (ctx
->Light
._NeedVertices
)
899 ctx
->Light
._NeedEyeCoords
= GL_TRUE
;
901 /* Precompute some shading values. Although we reference
902 * Light.Material here, we can get away without flushing
903 * FLUSH_UPDATE_CURRENT, as when any outstanding material changes
904 * are flushed, they will update the derived state at that time.
906 if (ctx
->Light
.Model
.TwoSide
)
907 _mesa_update_material(ctx
,
908 MAT_BIT_FRONT_EMISSION
|
909 MAT_BIT_FRONT_AMBIENT
|
910 MAT_BIT_FRONT_DIFFUSE
|
911 MAT_BIT_FRONT_SPECULAR
|
912 MAT_BIT_BACK_EMISSION
|
913 MAT_BIT_BACK_AMBIENT
|
914 MAT_BIT_BACK_DIFFUSE
|
915 MAT_BIT_BACK_SPECULAR
);
917 _mesa_update_material(ctx
,
918 MAT_BIT_FRONT_EMISSION
|
919 MAT_BIT_FRONT_AMBIENT
|
920 MAT_BIT_FRONT_DIFFUSE
|
921 MAT_BIT_FRONT_SPECULAR
);
926 * Update state derived from light position, spot direction.
930 * _TNL_NEW_NEED_EYE_COORDS
932 * Update on (_NEW_MODELVIEW | _NEW_LIGHT) when lighting is enabled.
933 * Also update on lighting space changes.
936 compute_light_positions( struct gl_context
*ctx
)
938 struct gl_light
*light
;
939 static const GLfloat eye_z
[3] = { 0, 0, 1 };
941 if (!ctx
->Light
.Enabled
)
944 if (ctx
->_NeedEyeCoords
) {
945 COPY_3V( ctx
->_EyeZDir
, eye_z
);
948 TRANSFORM_NORMAL( ctx
->_EyeZDir
, eye_z
, ctx
->ModelviewMatrixStack
.Top
->m
);
951 foreach (light
, &ctx
->Light
.EnabledList
) {
953 if (ctx
->_NeedEyeCoords
) {
954 /* _Position is in eye coordinate space */
955 COPY_4FV( light
->_Position
, light
->EyePosition
);
958 /* _Position is in object coordinate space */
959 TRANSFORM_POINT( light
->_Position
, ctx
->ModelviewMatrixStack
.Top
->inv
,
960 light
->EyePosition
);
963 if (!(light
->_Flags
& LIGHT_POSITIONAL
)) {
964 /* VP (VP) = Normalize( Position ) */
965 COPY_3V( light
->_VP_inf_norm
, light
->_Position
);
966 NORMALIZE_3FV( light
->_VP_inf_norm
);
968 if (!ctx
->Light
.Model
.LocalViewer
) {
969 /* _h_inf_norm = Normalize( V_to_P + <0,0,1> ) */
970 ADD_3V( light
->_h_inf_norm
, light
->_VP_inf_norm
, ctx
->_EyeZDir
);
971 NORMALIZE_3FV( light
->_h_inf_norm
);
973 light
->_VP_inf_spot_attenuation
= 1.0;
976 /* positional light w/ homogeneous coordinate, divide by W */
977 GLfloat wInv
= (GLfloat
)1.0 / light
->_Position
[3];
978 light
->_Position
[0] *= wInv
;
979 light
->_Position
[1] *= wInv
;
980 light
->_Position
[2] *= wInv
;
983 if (light
->_Flags
& LIGHT_SPOT
) {
984 /* Note: we normalize the spot direction now */
986 if (ctx
->_NeedEyeCoords
) {
987 COPY_3V( light
->_NormSpotDirection
, light
->SpotDirection
);
988 NORMALIZE_3FV( light
->_NormSpotDirection
);
992 COPY_3V(spotDir
, light
->SpotDirection
);
993 NORMALIZE_3FV(spotDir
);
994 TRANSFORM_NORMAL( light
->_NormSpotDirection
,
996 ctx
->ModelviewMatrixStack
.Top
->m
);
999 NORMALIZE_3FV( light
->_NormSpotDirection
);
1001 if (!(light
->_Flags
& LIGHT_POSITIONAL
)) {
1002 GLfloat PV_dot_dir
= - DOT3(light
->_VP_inf_norm
,
1003 light
->_NormSpotDirection
);
1005 if (PV_dot_dir
> light
->_CosCutoff
) {
1006 light
->_VP_inf_spot_attenuation
=
1007 powf(PV_dot_dir
, light
->SpotExponent
);
1010 light
->_VP_inf_spot_attenuation
= 0;
1020 update_modelview_scale( struct gl_context
*ctx
)
1022 ctx
->_ModelViewInvScale
= 1.0F
;
1023 if (!_math_matrix_is_length_preserving(ctx
->ModelviewMatrixStack
.Top
)) {
1024 const GLfloat
*m
= ctx
->ModelviewMatrixStack
.Top
->inv
;
1025 GLfloat f
= m
[2] * m
[2] + m
[6] * m
[6] + m
[10] * m
[10];
1026 if (f
< 1e-12) f
= 1.0;
1027 if (ctx
->_NeedEyeCoords
)
1028 ctx
->_ModelViewInvScale
= (GLfloat
) INV_SQRTF(f
);
1030 ctx
->_ModelViewInvScale
= (GLfloat
) sqrtf(f
);
1036 * Bring up to date any state that relies on _NeedEyeCoords.
1039 _mesa_update_tnl_spaces( struct gl_context
*ctx
, GLuint new_state
)
1041 const GLuint oldneedeyecoords
= ctx
->_NeedEyeCoords
;
1044 ctx
->_NeedEyeCoords
= GL_FALSE
;
1046 if (ctx
->_ForceEyeCoords
||
1047 (ctx
->Texture
._GenFlags
& TEXGEN_NEED_EYE_COORD
) ||
1048 ctx
->Point
._Attenuated
||
1049 ctx
->Light
._NeedEyeCoords
)
1050 ctx
->_NeedEyeCoords
= GL_TRUE
;
1052 if (ctx
->Light
.Enabled
&&
1053 !_math_matrix_is_length_preserving(ctx
->ModelviewMatrixStack
.Top
))
1054 ctx
->_NeedEyeCoords
= GL_TRUE
;
1056 /* Check if the truth-value interpretations of the bitfields have
1059 if (oldneedeyecoords
!= ctx
->_NeedEyeCoords
) {
1060 /* Recalculate all state that depends on _NeedEyeCoords.
1062 update_modelview_scale(ctx
);
1063 compute_light_positions( ctx
);
1065 if (ctx
->Driver
.LightingSpaceChange
)
1066 ctx
->Driver
.LightingSpaceChange( ctx
);
1069 GLuint new_state2
= ctx
->NewState
;
1071 /* Recalculate that same state only if it has been invalidated
1072 * by other statechanges.
1074 if (new_state2
& _NEW_MODELVIEW
)
1075 update_modelview_scale(ctx
);
1077 if (new_state2
& (_NEW_LIGHT
|_NEW_MODELVIEW
))
1078 compute_light_positions( ctx
);
1084 * Drivers may need this if the hardware tnl unit doesn't support the
1085 * light-in-modelspace optimization. It's also useful for debugging.
1088 _mesa_allow_light_in_model( struct gl_context
*ctx
, GLboolean flag
)
1090 ctx
->_ForceEyeCoords
= !flag
;
1091 ctx
->NewState
|= _NEW_POINT
; /* one of the bits from
1092 * _MESA_NEW_NEED_EYE_COORDS.
1098 /**********************************************************************/
1099 /***** Initialization *****/
1100 /**********************************************************************/
1103 * Initialize the n-th light data structure.
1105 * \param l pointer to the gl_light structure to be initialized.
1106 * \param n number of the light.
1107 * \note The defaults for light 0 are different than the other lights.
1110 init_light( struct gl_light
*l
, GLuint n
)
1112 make_empty_list( l
);
1114 ASSIGN_4V( l
->Ambient
, 0.0, 0.0, 0.0, 1.0 );
1116 ASSIGN_4V( l
->Diffuse
, 1.0, 1.0, 1.0, 1.0 );
1117 ASSIGN_4V( l
->Specular
, 1.0, 1.0, 1.0, 1.0 );
1120 ASSIGN_4V( l
->Diffuse
, 0.0, 0.0, 0.0, 1.0 );
1121 ASSIGN_4V( l
->Specular
, 0.0, 0.0, 0.0, 1.0 );
1123 ASSIGN_4V( l
->EyePosition
, 0.0, 0.0, 1.0, 0.0 );
1124 ASSIGN_3V( l
->SpotDirection
, 0.0, 0.0, -1.0 );
1125 l
->SpotExponent
= 0.0;
1126 l
->SpotCutoff
= 180.0;
1127 l
->_CosCutoff
= 0.0; /* KW: -ve values not admitted */
1128 l
->ConstantAttenuation
= 1.0;
1129 l
->LinearAttenuation
= 0.0;
1130 l
->QuadraticAttenuation
= 0.0;
1131 l
->Enabled
= GL_FALSE
;
1136 * Initialize the light model data structure.
1138 * \param lm pointer to the gl_lightmodel structure to be initialized.
1141 init_lightmodel( struct gl_lightmodel
*lm
)
1143 ASSIGN_4V( lm
->Ambient
, 0.2F
, 0.2F
, 0.2F
, 1.0F
);
1144 lm
->LocalViewer
= GL_FALSE
;
1145 lm
->TwoSide
= GL_FALSE
;
1146 lm
->ColorControl
= GL_SINGLE_COLOR
;
1151 * Initialize the material data structure.
1153 * \param m pointer to the gl_material structure to be initialized.
1156 init_material( struct gl_material
*m
)
1158 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_FRONT_AMBIENT
], 0.2F
, 0.2F
, 0.2F
, 1.0F
);
1159 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
], 0.8F
, 0.8F
, 0.8F
, 1.0F
);
1160 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_FRONT_SPECULAR
], 0.0F
, 0.0F
, 0.0F
, 1.0F
);
1161 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_FRONT_EMISSION
], 0.0F
, 0.0F
, 0.0F
, 1.0F
);
1162 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_FRONT_SHININESS
], 0.0F
, 0.0F
, 0.0F
, 0.0F
);
1163 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_FRONT_INDEXES
], 0.0F
, 1.0F
, 1.0F
, 0.0F
);
1165 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_BACK_AMBIENT
], 0.2F
, 0.2F
, 0.2F
, 1.0F
);
1166 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_BACK_DIFFUSE
], 0.8F
, 0.8F
, 0.8F
, 1.0F
);
1167 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_BACK_SPECULAR
], 0.0F
, 0.0F
, 0.0F
, 1.0F
);
1168 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_BACK_EMISSION
], 0.0F
, 0.0F
, 0.0F
, 1.0F
);
1169 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_BACK_SHININESS
], 0.0F
, 0.0F
, 0.0F
, 0.0F
);
1170 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_BACK_INDEXES
], 0.0F
, 1.0F
, 1.0F
, 0.0F
);
1175 * Initialize all lighting state for the given context.
1178 _mesa_init_lighting( struct gl_context
*ctx
)
1182 /* Lighting group */
1183 for (i
= 0; i
< MAX_LIGHTS
; i
++) {
1184 init_light( &ctx
->Light
.Light
[i
], i
);
1186 make_empty_list( &ctx
->Light
.EnabledList
);
1188 init_lightmodel( &ctx
->Light
.Model
);
1189 init_material( &ctx
->Light
.Material
);
1190 ctx
->Light
.ShadeModel
= GL_SMOOTH
;
1191 ctx
->Light
.ProvokingVertex
= GL_LAST_VERTEX_CONVENTION_EXT
;
1192 ctx
->Light
.Enabled
= GL_FALSE
;
1193 ctx
->Light
.ColorMaterialFace
= GL_FRONT_AND_BACK
;
1194 ctx
->Light
.ColorMaterialMode
= GL_AMBIENT_AND_DIFFUSE
;
1195 ctx
->Light
._ColorMaterialBitmask
= _mesa_material_bitmask( ctx
,
1197 GL_AMBIENT_AND_DIFFUSE
, ~0,
1200 ctx
->Light
.ColorMaterialEnabled
= GL_FALSE
;
1201 ctx
->Light
.ClampVertexColor
= ctx
->API
== API_OPENGL_COMPAT
;
1202 ctx
->Light
._ClampVertexColor
= ctx
->API
== API_OPENGL_COMPAT
;
1205 ctx
->Light
._NeedEyeCoords
= GL_FALSE
;
1206 ctx
->_NeedEyeCoords
= GL_FALSE
;
1207 ctx
->_ForceEyeCoords
= GL_FALSE
;
1208 ctx
->_ModelViewInvScale
= 1.0;
1213 * Deallocate malloc'd lighting state attached to given context.
1216 _mesa_free_lighting_data( struct gl_context
*ctx
)