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.
34 #include "math/m_matrix.h"
35 #include "util/bitscan.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_enum_to_string(mode
));
46 if (ctx
->Light
.ShadeModel
== mode
)
49 if (mode
!= GL_FLAT
&& mode
!= GL_SMOOTH
) {
50 _mesa_error(ctx
, GL_INVALID_ENUM
, "glShadeModel");
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
);
75 if (ctx
->Light
.ProvokingVertex
== mode
)
79 case GL_FIRST_VERTEX_CONVENTION_EXT
:
80 case GL_LAST_VERTEX_CONVENTION_EXT
:
83 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProvokingVertexEXT(0x%x)", 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.0F
);
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.0F
|| (params
[0] >= 0.0F
&& params
[0] <= 90.0F
));
154 if (light
->SpotCutoff
== params
[0])
156 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
157 light
->SpotCutoff
= params
[0];
158 light
->_CosCutoff
= (cosf(light
->SpotCutoff
* M_PI
/ 180.0));
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.0F
);
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.0F
);
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.0F
);
182 if (light
->QuadraticAttenuation
== params
[0])
184 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
185 light
->QuadraticAttenuation
= params
[0];
188 unreachable("Unexpected pname in _mesa_light()");
191 if (ctx
->Driver
.Lightfv
)
192 ctx
->Driver
.Lightfv( ctx
, GL_LIGHT0
+ lnum
, pname
, params
);
197 _mesa_Lightf( GLenum light
, GLenum pname
, GLfloat param
)
201 fparam
[1] = fparam
[2] = fparam
[3] = 0.0F
;
202 _mesa_Lightfv( light
, pname
, fparam
);
207 _mesa_Lightfv( GLenum light
, GLenum pname
, const GLfloat
*params
)
209 GET_CURRENT_CONTEXT(ctx
);
210 GLint i
= (GLint
) (light
- GL_LIGHT0
);
213 if (i
< 0 || i
>= (GLint
) ctx
->Const
.MaxLights
) {
214 _mesa_error( ctx
, GL_INVALID_ENUM
, "glLight(light=0x%x)", light
);
218 /* do particular error checks, transformations */
226 /* transform position by ModelView matrix */
227 TRANSFORM_POINT(temp
, ctx
->ModelviewMatrixStack
.Top
->m
, params
);
230 case GL_SPOT_DIRECTION
:
231 /* transform direction by inverse modelview */
232 if (_math_matrix_is_dirty(ctx
->ModelviewMatrixStack
.Top
)) {
233 _math_matrix_analyse(ctx
->ModelviewMatrixStack
.Top
);
235 TRANSFORM_DIRECTION(temp
, params
, ctx
->ModelviewMatrixStack
.Top
->m
);
238 case GL_SPOT_EXPONENT
:
239 if (params
[0] < 0.0F
|| params
[0] > ctx
->Const
.MaxSpotExponent
) {
240 _mesa_error(ctx
, GL_INVALID_VALUE
, "glLight");
245 if ((params
[0] < 0.0F
|| params
[0] > 90.0F
) && params
[0] != 180.0F
) {
246 _mesa_error(ctx
, GL_INVALID_VALUE
, "glLight");
250 case GL_CONSTANT_ATTENUATION
:
251 case GL_LINEAR_ATTENUATION
:
252 case GL_QUADRATIC_ATTENUATION
:
253 if (params
[0] < 0.0F
) {
254 _mesa_error(ctx
, GL_INVALID_VALUE
, "glLight");
259 _mesa_error(ctx
, GL_INVALID_ENUM
, "glLight(pname=0x%x)", pname
);
263 _mesa_light(ctx
, i
, pname
, params
);
268 _mesa_Lighti( GLenum light
, GLenum pname
, GLint param
)
272 iparam
[1] = iparam
[2] = iparam
[3] = 0;
273 _mesa_Lightiv( light
, pname
, iparam
);
278 _mesa_Lightiv( GLenum light
, GLenum pname
, const GLint
*params
)
286 fparam
[0] = INT_TO_FLOAT( params
[0] );
287 fparam
[1] = INT_TO_FLOAT( params
[1] );
288 fparam
[2] = INT_TO_FLOAT( params
[2] );
289 fparam
[3] = INT_TO_FLOAT( params
[3] );
292 fparam
[0] = (GLfloat
) params
[0];
293 fparam
[1] = (GLfloat
) params
[1];
294 fparam
[2] = (GLfloat
) params
[2];
295 fparam
[3] = (GLfloat
) params
[3];
297 case GL_SPOT_DIRECTION
:
298 fparam
[0] = (GLfloat
) params
[0];
299 fparam
[1] = (GLfloat
) params
[1];
300 fparam
[2] = (GLfloat
) params
[2];
302 case GL_SPOT_EXPONENT
:
304 case GL_CONSTANT_ATTENUATION
:
305 case GL_LINEAR_ATTENUATION
:
306 case GL_QUADRATIC_ATTENUATION
:
307 fparam
[0] = (GLfloat
) params
[0];
310 /* error will be caught later in gl_Lightfv */
314 _mesa_Lightfv( light
, pname
, fparam
);
320 _mesa_GetLightfv( GLenum light
, GLenum pname
, GLfloat
*params
)
322 GET_CURRENT_CONTEXT(ctx
);
323 GLint l
= (GLint
) (light
- GL_LIGHT0
);
325 if (l
< 0 || l
>= (GLint
) ctx
->Const
.MaxLights
) {
326 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetLightfv" );
332 COPY_4V( params
, ctx
->Light
.Light
[l
].Ambient
);
335 COPY_4V( params
, ctx
->Light
.Light
[l
].Diffuse
);
338 COPY_4V( params
, ctx
->Light
.Light
[l
].Specular
);
341 COPY_4V( params
, ctx
->Light
.Light
[l
].EyePosition
);
343 case GL_SPOT_DIRECTION
:
344 COPY_3V( params
, ctx
->Light
.Light
[l
].SpotDirection
);
346 case GL_SPOT_EXPONENT
:
347 params
[0] = ctx
->Light
.Light
[l
].SpotExponent
;
350 params
[0] = ctx
->Light
.Light
[l
].SpotCutoff
;
352 case GL_CONSTANT_ATTENUATION
:
353 params
[0] = ctx
->Light
.Light
[l
].ConstantAttenuation
;
355 case GL_LINEAR_ATTENUATION
:
356 params
[0] = ctx
->Light
.Light
[l
].LinearAttenuation
;
358 case GL_QUADRATIC_ATTENUATION
:
359 params
[0] = ctx
->Light
.Light
[l
].QuadraticAttenuation
;
362 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetLightfv" );
369 _mesa_GetLightiv( GLenum light
, GLenum pname
, GLint
*params
)
371 GET_CURRENT_CONTEXT(ctx
);
372 GLint l
= (GLint
) (light
- GL_LIGHT0
);
374 if (l
< 0 || l
>= (GLint
) ctx
->Const
.MaxLights
) {
375 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetLightiv" );
381 params
[0] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Ambient
[0]);
382 params
[1] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Ambient
[1]);
383 params
[2] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Ambient
[2]);
384 params
[3] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Ambient
[3]);
387 params
[0] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Diffuse
[0]);
388 params
[1] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Diffuse
[1]);
389 params
[2] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Diffuse
[2]);
390 params
[3] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Diffuse
[3]);
393 params
[0] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Specular
[0]);
394 params
[1] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Specular
[1]);
395 params
[2] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Specular
[2]);
396 params
[3] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Specular
[3]);
399 params
[0] = (GLint
) ctx
->Light
.Light
[l
].EyePosition
[0];
400 params
[1] = (GLint
) ctx
->Light
.Light
[l
].EyePosition
[1];
401 params
[2] = (GLint
) ctx
->Light
.Light
[l
].EyePosition
[2];
402 params
[3] = (GLint
) ctx
->Light
.Light
[l
].EyePosition
[3];
404 case GL_SPOT_DIRECTION
:
405 params
[0] = (GLint
) ctx
->Light
.Light
[l
].SpotDirection
[0];
406 params
[1] = (GLint
) ctx
->Light
.Light
[l
].SpotDirection
[1];
407 params
[2] = (GLint
) ctx
->Light
.Light
[l
].SpotDirection
[2];
409 case GL_SPOT_EXPONENT
:
410 params
[0] = (GLint
) ctx
->Light
.Light
[l
].SpotExponent
;
413 params
[0] = (GLint
) ctx
->Light
.Light
[l
].SpotCutoff
;
415 case GL_CONSTANT_ATTENUATION
:
416 params
[0] = (GLint
) ctx
->Light
.Light
[l
].ConstantAttenuation
;
418 case GL_LINEAR_ATTENUATION
:
419 params
[0] = (GLint
) ctx
->Light
.Light
[l
].LinearAttenuation
;
421 case GL_QUADRATIC_ATTENUATION
:
422 params
[0] = (GLint
) ctx
->Light
.Light
[l
].QuadraticAttenuation
;
425 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetLightiv" );
432 /**********************************************************************/
433 /*** Light Model ***/
434 /**********************************************************************/
438 _mesa_LightModelfv( GLenum pname
, const GLfloat
*params
)
442 GET_CURRENT_CONTEXT(ctx
);
445 case GL_LIGHT_MODEL_AMBIENT
:
446 if (TEST_EQ_4V( ctx
->Light
.Model
.Ambient
, params
))
448 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
449 COPY_4V( ctx
->Light
.Model
.Ambient
, params
);
451 case GL_LIGHT_MODEL_LOCAL_VIEWER
:
452 if (ctx
->API
!= API_OPENGL_COMPAT
)
454 newbool
= (params
[0] != 0.0F
);
455 if (ctx
->Light
.Model
.LocalViewer
== newbool
)
457 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
458 ctx
->Light
.Model
.LocalViewer
= newbool
;
460 case GL_LIGHT_MODEL_TWO_SIDE
:
461 newbool
= (params
[0] != 0.0F
);
462 if (ctx
->Light
.Model
.TwoSide
== newbool
)
464 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
465 ctx
->Light
.Model
.TwoSide
= newbool
;
467 case GL_LIGHT_MODEL_COLOR_CONTROL
:
468 if (ctx
->API
!= API_OPENGL_COMPAT
)
470 if (params
[0] == (GLfloat
) GL_SINGLE_COLOR
)
471 newenum
= GL_SINGLE_COLOR
;
472 else if (params
[0] == (GLfloat
) GL_SEPARATE_SPECULAR_COLOR
)
473 newenum
= GL_SEPARATE_SPECULAR_COLOR
;
475 _mesa_error( ctx
, GL_INVALID_ENUM
, "glLightModel(param=0x0%x)",
479 if (ctx
->Light
.Model
.ColorControl
== newenum
)
481 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
482 ctx
->Light
.Model
.ColorControl
= newenum
;
488 if (ctx
->Driver
.LightModelfv
)
489 ctx
->Driver
.LightModelfv( ctx
, pname
, params
);
494 _mesa_error( ctx
, GL_INVALID_ENUM
, "glLightModel(pname=0x%x)", pname
);
500 _mesa_LightModeliv( GLenum pname
, const GLint
*params
)
505 case GL_LIGHT_MODEL_AMBIENT
:
506 fparam
[0] = INT_TO_FLOAT( params
[0] );
507 fparam
[1] = INT_TO_FLOAT( params
[1] );
508 fparam
[2] = INT_TO_FLOAT( params
[2] );
509 fparam
[3] = INT_TO_FLOAT( params
[3] );
511 case GL_LIGHT_MODEL_LOCAL_VIEWER
:
512 case GL_LIGHT_MODEL_TWO_SIDE
:
513 case GL_LIGHT_MODEL_COLOR_CONTROL
:
514 fparam
[0] = (GLfloat
) params
[0];
517 /* Error will be caught later in gl_LightModelfv */
518 ASSIGN_4V(fparam
, 0.0F
, 0.0F
, 0.0F
, 0.0F
);
520 _mesa_LightModelfv( pname
, fparam
);
525 _mesa_LightModeli( GLenum pname
, GLint param
)
529 iparam
[1] = iparam
[2] = iparam
[3] = 0;
530 _mesa_LightModeliv( pname
, iparam
);
535 _mesa_LightModelf( GLenum pname
, GLfloat param
)
539 fparam
[1] = fparam
[2] = fparam
[3] = 0.0F
;
540 _mesa_LightModelfv( pname
, fparam
);
545 /********** MATERIAL **********/
549 * Given a face and pname value (ala glColorMaterial), compute a bitmask
550 * of the targeted material values.
553 _mesa_material_bitmask( struct gl_context
*ctx
, GLenum face
, GLenum pname
,
554 GLuint legal
, const char *where
)
558 /* Make a bitmask indicating what material attribute(s) we're updating */
561 bitmask
|= MAT_BIT_FRONT_EMISSION
| MAT_BIT_BACK_EMISSION
;
564 bitmask
|= MAT_BIT_FRONT_AMBIENT
| MAT_BIT_BACK_AMBIENT
;
567 bitmask
|= MAT_BIT_FRONT_DIFFUSE
| MAT_BIT_BACK_DIFFUSE
;
570 bitmask
|= MAT_BIT_FRONT_SPECULAR
| MAT_BIT_BACK_SPECULAR
;
573 bitmask
|= MAT_BIT_FRONT_SHININESS
| MAT_BIT_BACK_SHININESS
;
575 case GL_AMBIENT_AND_DIFFUSE
:
576 bitmask
|= MAT_BIT_FRONT_AMBIENT
| MAT_BIT_BACK_AMBIENT
;
577 bitmask
|= MAT_BIT_FRONT_DIFFUSE
| MAT_BIT_BACK_DIFFUSE
;
579 case GL_COLOR_INDEXES
:
580 bitmask
|= MAT_BIT_FRONT_INDEXES
| MAT_BIT_BACK_INDEXES
;
583 _mesa_error( ctx
, GL_INVALID_ENUM
, "%s", where
);
587 if (face
==GL_FRONT
) {
588 bitmask
&= FRONT_MATERIAL_BITS
;
590 else if (face
==GL_BACK
) {
591 bitmask
&= BACK_MATERIAL_BITS
;
593 else if (face
!= GL_FRONT_AND_BACK
) {
594 _mesa_error( ctx
, GL_INVALID_ENUM
, "%s", where
);
598 if (bitmask
& ~legal
) {
599 _mesa_error( ctx
, GL_INVALID_ENUM
, "%s", where
);
608 /* Update derived values following a change in ctx->Light.Material
611 _mesa_update_material( struct gl_context
*ctx
, GLuint bitmask
)
613 GLfloat (*mat
)[4] = ctx
->Light
.Material
.Attrib
;
615 if (MESA_VERBOSE
& VERBOSE_MATERIAL
)
616 _mesa_debug(ctx
, "_mesa_update_material, mask 0x%x\n", bitmask
);
621 /* update material ambience */
622 if (bitmask
& MAT_BIT_FRONT_AMBIENT
) {
623 GLbitfield mask
= ctx
->Light
._EnabledLights
;
625 const int i
= u_bit_scan(&mask
);
626 struct gl_light
*light
= &ctx
->Light
.Light
[i
];
627 SCALE_3V( light
->_MatAmbient
[0], light
->Ambient
,
628 mat
[MAT_ATTRIB_FRONT_AMBIENT
]);
632 if (bitmask
& MAT_BIT_BACK_AMBIENT
) {
633 GLbitfield mask
= ctx
->Light
._EnabledLights
;
635 const int i
= u_bit_scan(&mask
);
636 struct gl_light
*light
= &ctx
->Light
.Light
[i
];
637 SCALE_3V( light
->_MatAmbient
[1], light
->Ambient
,
638 mat
[MAT_ATTRIB_BACK_AMBIENT
]);
642 /* update BaseColor = emission + scene's ambience * material's ambience */
643 if (bitmask
& (MAT_BIT_FRONT_EMISSION
| MAT_BIT_FRONT_AMBIENT
)) {
644 COPY_3V( ctx
->Light
._BaseColor
[0], mat
[MAT_ATTRIB_FRONT_EMISSION
] );
645 ACC_SCALE_3V( ctx
->Light
._BaseColor
[0], mat
[MAT_ATTRIB_FRONT_AMBIENT
],
646 ctx
->Light
.Model
.Ambient
);
649 if (bitmask
& (MAT_BIT_BACK_EMISSION
| MAT_BIT_BACK_AMBIENT
)) {
650 COPY_3V( ctx
->Light
._BaseColor
[1], mat
[MAT_ATTRIB_BACK_EMISSION
] );
651 ACC_SCALE_3V( ctx
->Light
._BaseColor
[1], mat
[MAT_ATTRIB_BACK_AMBIENT
],
652 ctx
->Light
.Model
.Ambient
);
655 /* update material diffuse values */
656 if (bitmask
& MAT_BIT_FRONT_DIFFUSE
) {
657 GLbitfield mask
= ctx
->Light
._EnabledLights
;
659 const int i
= u_bit_scan(&mask
);
660 struct gl_light
*light
= &ctx
->Light
.Light
[i
];
661 SCALE_3V( light
->_MatDiffuse
[0], light
->Diffuse
,
662 mat
[MAT_ATTRIB_FRONT_DIFFUSE
] );
666 if (bitmask
& MAT_BIT_BACK_DIFFUSE
) {
667 GLbitfield mask
= ctx
->Light
._EnabledLights
;
669 const int i
= u_bit_scan(&mask
);
670 struct gl_light
*light
= &ctx
->Light
.Light
[i
];
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 GLbitfield mask
= ctx
->Light
._EnabledLights
;
680 const int i
= u_bit_scan(&mask
);
681 struct gl_light
*light
= &ctx
->Light
.Light
[i
];
682 SCALE_3V( light
->_MatSpecular
[0], light
->Specular
,
683 mat
[MAT_ATTRIB_FRONT_SPECULAR
]);
687 if (bitmask
& MAT_BIT_BACK_SPECULAR
) {
688 GLbitfield mask
= ctx
->Light
._EnabledLights
;
690 const int i
= u_bit_scan(&mask
);
691 struct gl_light
*light
= &ctx
->Light
.Light
[i
];
692 SCALE_3V( light
->_MatSpecular
[1], light
->Specular
,
693 mat
[MAT_ATTRIB_BACK_SPECULAR
]);
700 * Update the current materials from the given rgba color
701 * according to the bitmask in _ColorMaterialBitmask, which is
702 * set by glColorMaterial().
705 _mesa_update_color_material( struct gl_context
*ctx
, const GLfloat color
[4] )
707 GLbitfield bitmask
= ctx
->Light
._ColorMaterialBitmask
;
708 struct gl_material
*mat
= &ctx
->Light
.Material
;
711 const int i
= u_bit_scan(&bitmask
);
713 COPY_4FV( mat
->Attrib
[i
], color
);
716 _mesa_update_material( ctx
, bitmask
);
721 _mesa_ColorMaterial( GLenum face
, GLenum mode
)
723 GET_CURRENT_CONTEXT(ctx
);
725 GLuint legal
= (MAT_BIT_FRONT_EMISSION
| MAT_BIT_BACK_EMISSION
|
726 MAT_BIT_FRONT_SPECULAR
| MAT_BIT_BACK_SPECULAR
|
727 MAT_BIT_FRONT_DIFFUSE
| MAT_BIT_BACK_DIFFUSE
|
728 MAT_BIT_FRONT_AMBIENT
| MAT_BIT_BACK_AMBIENT
);
730 if (MESA_VERBOSE
&VERBOSE_API
)
731 _mesa_debug(ctx
, "glColorMaterial %s %s\n",
732 _mesa_enum_to_string(face
),
733 _mesa_enum_to_string(mode
));
735 bitmask
= _mesa_material_bitmask(ctx
, face
, mode
, legal
, "glColorMaterial");
737 return; /* error was recorded */
739 if (ctx
->Light
._ColorMaterialBitmask
== bitmask
&&
740 ctx
->Light
.ColorMaterialFace
== face
&&
741 ctx
->Light
.ColorMaterialMode
== mode
)
744 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
745 ctx
->Light
._ColorMaterialBitmask
= bitmask
;
746 ctx
->Light
.ColorMaterialFace
= face
;
747 ctx
->Light
.ColorMaterialMode
= mode
;
749 if (ctx
->Light
.ColorMaterialEnabled
) {
750 FLUSH_CURRENT( ctx
, 0 );
751 _mesa_update_color_material(ctx
,ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
]);
754 if (ctx
->Driver
.ColorMaterial
)
755 ctx
->Driver
.ColorMaterial( ctx
, face
, mode
);
760 _mesa_GetMaterialfv( GLenum face
, GLenum pname
, GLfloat
*params
)
762 GET_CURRENT_CONTEXT(ctx
);
764 GLfloat (*mat
)[4] = ctx
->Light
.Material
.Attrib
;
765 FLUSH_VERTICES(ctx
, 0); /* update materials */
767 FLUSH_CURRENT(ctx
, 0); /* update ctx->Light.Material from vertex buffer */
769 if (face
==GL_FRONT
) {
772 else if (face
==GL_BACK
) {
776 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetMaterialfv(face)" );
782 COPY_4FV( params
, mat
[MAT_ATTRIB_AMBIENT(f
)] );
785 COPY_4FV( params
, mat
[MAT_ATTRIB_DIFFUSE(f
)] );
788 COPY_4FV( params
, mat
[MAT_ATTRIB_SPECULAR(f
)] );
791 COPY_4FV( params
, mat
[MAT_ATTRIB_EMISSION(f
)] );
794 *params
= mat
[MAT_ATTRIB_SHININESS(f
)][0];
796 case GL_COLOR_INDEXES
:
797 if (ctx
->API
!= API_OPENGL_COMPAT
) {
798 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetMaterialfv(pname)" );
801 params
[0] = mat
[MAT_ATTRIB_INDEXES(f
)][0];
802 params
[1] = mat
[MAT_ATTRIB_INDEXES(f
)][1];
803 params
[2] = mat
[MAT_ATTRIB_INDEXES(f
)][2];
806 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetMaterialfv(pname)" );
812 _mesa_GetMaterialiv( GLenum face
, GLenum pname
, GLint
*params
)
814 GET_CURRENT_CONTEXT(ctx
);
816 GLfloat (*mat
)[4] = ctx
->Light
.Material
.Attrib
;
818 assert(ctx
->API
== API_OPENGL_COMPAT
);
820 FLUSH_VERTICES(ctx
, 0); /* update materials */
821 FLUSH_CURRENT(ctx
, 0); /* update ctx->Light.Material from vertex buffer */
823 if (face
==GL_FRONT
) {
826 else if (face
==GL_BACK
) {
830 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetMaterialiv(face)" );
835 params
[0] = FLOAT_TO_INT( mat
[MAT_ATTRIB_AMBIENT(f
)][0] );
836 params
[1] = FLOAT_TO_INT( mat
[MAT_ATTRIB_AMBIENT(f
)][1] );
837 params
[2] = FLOAT_TO_INT( mat
[MAT_ATTRIB_AMBIENT(f
)][2] );
838 params
[3] = FLOAT_TO_INT( mat
[MAT_ATTRIB_AMBIENT(f
)][3] );
841 params
[0] = FLOAT_TO_INT( mat
[MAT_ATTRIB_DIFFUSE(f
)][0] );
842 params
[1] = FLOAT_TO_INT( mat
[MAT_ATTRIB_DIFFUSE(f
)][1] );
843 params
[2] = FLOAT_TO_INT( mat
[MAT_ATTRIB_DIFFUSE(f
)][2] );
844 params
[3] = FLOAT_TO_INT( mat
[MAT_ATTRIB_DIFFUSE(f
)][3] );
847 params
[0] = FLOAT_TO_INT( mat
[MAT_ATTRIB_SPECULAR(f
)][0] );
848 params
[1] = FLOAT_TO_INT( mat
[MAT_ATTRIB_SPECULAR(f
)][1] );
849 params
[2] = FLOAT_TO_INT( mat
[MAT_ATTRIB_SPECULAR(f
)][2] );
850 params
[3] = FLOAT_TO_INT( mat
[MAT_ATTRIB_SPECULAR(f
)][3] );
853 params
[0] = FLOAT_TO_INT( mat
[MAT_ATTRIB_EMISSION(f
)][0] );
854 params
[1] = FLOAT_TO_INT( mat
[MAT_ATTRIB_EMISSION(f
)][1] );
855 params
[2] = FLOAT_TO_INT( mat
[MAT_ATTRIB_EMISSION(f
)][2] );
856 params
[3] = FLOAT_TO_INT( mat
[MAT_ATTRIB_EMISSION(f
)][3] );
859 *params
= lroundf( mat
[MAT_ATTRIB_SHININESS(f
)][0] );
861 case GL_COLOR_INDEXES
:
862 params
[0] = lroundf( mat
[MAT_ATTRIB_INDEXES(f
)][0] );
863 params
[1] = lroundf( mat
[MAT_ATTRIB_INDEXES(f
)][1] );
864 params
[2] = lroundf( mat
[MAT_ATTRIB_INDEXES(f
)][2] );
867 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetMaterialfv(pname)" );
874 * Examine current lighting parameters to determine if the optimized lighting
875 * function can be used.
876 * Also, precompute some lighting values such as the products of light
877 * source and material ambient, diffuse and specular coefficients.
880 _mesa_update_lighting( struct gl_context
*ctx
)
882 GLbitfield flags
= 0;
883 ctx
->Light
._NeedEyeCoords
= GL_FALSE
;
885 if (!ctx
->Light
.Enabled
)
888 GLbitfield mask
= ctx
->Light
._EnabledLights
;
890 const int i
= u_bit_scan(&mask
);
891 struct gl_light
*light
= &ctx
->Light
.Light
[i
];
892 flags
|= light
->_Flags
;
895 ctx
->Light
._NeedVertices
=
896 ((flags
& (LIGHT_POSITIONAL
|LIGHT_SPOT
)) ||
897 ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
||
898 ctx
->Light
.Model
.LocalViewer
);
900 ctx
->Light
._NeedEyeCoords
= ((flags
& LIGHT_POSITIONAL
) ||
901 ctx
->Light
.Model
.LocalViewer
);
903 /* XXX: This test is overkill & needs to be fixed both for software and
904 * hardware t&l drivers. The above should be sufficient & should
905 * be tested to verify this.
907 if (ctx
->Light
._NeedVertices
)
908 ctx
->Light
._NeedEyeCoords
= GL_TRUE
;
910 /* Precompute some shading values. Although we reference
911 * Light.Material here, we can get away without flushing
912 * FLUSH_UPDATE_CURRENT, as when any outstanding material changes
913 * are flushed, they will update the derived state at that time.
915 if (ctx
->Light
.Model
.TwoSide
)
916 _mesa_update_material(ctx
,
917 MAT_BIT_FRONT_EMISSION
|
918 MAT_BIT_FRONT_AMBIENT
|
919 MAT_BIT_FRONT_DIFFUSE
|
920 MAT_BIT_FRONT_SPECULAR
|
921 MAT_BIT_BACK_EMISSION
|
922 MAT_BIT_BACK_AMBIENT
|
923 MAT_BIT_BACK_DIFFUSE
|
924 MAT_BIT_BACK_SPECULAR
);
926 _mesa_update_material(ctx
,
927 MAT_BIT_FRONT_EMISSION
|
928 MAT_BIT_FRONT_AMBIENT
|
929 MAT_BIT_FRONT_DIFFUSE
|
930 MAT_BIT_FRONT_SPECULAR
);
935 * Update state derived from light position, spot direction.
939 * _TNL_NEW_NEED_EYE_COORDS
941 * Update on (_NEW_MODELVIEW | _NEW_LIGHT) when lighting is enabled.
942 * Also update on lighting space changes.
945 compute_light_positions( struct gl_context
*ctx
)
947 static const GLfloat eye_z
[3] = { 0, 0, 1 };
949 if (!ctx
->Light
.Enabled
)
952 if (ctx
->_NeedEyeCoords
) {
953 COPY_3V( ctx
->_EyeZDir
, eye_z
);
956 TRANSFORM_NORMAL( ctx
->_EyeZDir
, eye_z
, ctx
->ModelviewMatrixStack
.Top
->m
);
959 GLbitfield mask
= ctx
->Light
._EnabledLights
;
961 const int i
= u_bit_scan(&mask
);
962 struct gl_light
*light
= &ctx
->Light
.Light
[i
];
964 if (ctx
->_NeedEyeCoords
) {
965 /* _Position is in eye coordinate space */
966 COPY_4FV( light
->_Position
, light
->EyePosition
);
969 /* _Position is in object coordinate space */
970 TRANSFORM_POINT( light
->_Position
, ctx
->ModelviewMatrixStack
.Top
->inv
,
971 light
->EyePosition
);
974 if (!(light
->_Flags
& LIGHT_POSITIONAL
)) {
975 /* VP (VP) = Normalize( Position ) */
976 COPY_3V( light
->_VP_inf_norm
, light
->_Position
);
977 NORMALIZE_3FV( light
->_VP_inf_norm
);
979 if (!ctx
->Light
.Model
.LocalViewer
) {
980 /* _h_inf_norm = Normalize( V_to_P + <0,0,1> ) */
981 ADD_3V( light
->_h_inf_norm
, light
->_VP_inf_norm
, ctx
->_EyeZDir
);
982 NORMALIZE_3FV( light
->_h_inf_norm
);
984 light
->_VP_inf_spot_attenuation
= 1.0;
987 /* positional light w/ homogeneous coordinate, divide by W */
988 GLfloat wInv
= 1.0F
/ light
->_Position
[3];
989 light
->_Position
[0] *= wInv
;
990 light
->_Position
[1] *= wInv
;
991 light
->_Position
[2] *= wInv
;
994 if (light
->_Flags
& LIGHT_SPOT
) {
995 /* Note: we normalize the spot direction now */
997 if (ctx
->_NeedEyeCoords
) {
998 COPY_3V( light
->_NormSpotDirection
, light
->SpotDirection
);
999 NORMALIZE_3FV( light
->_NormSpotDirection
);
1003 COPY_3V(spotDir
, light
->SpotDirection
);
1004 NORMALIZE_3FV(spotDir
);
1005 TRANSFORM_NORMAL( light
->_NormSpotDirection
,
1007 ctx
->ModelviewMatrixStack
.Top
->m
);
1010 NORMALIZE_3FV( light
->_NormSpotDirection
);
1012 if (!(light
->_Flags
& LIGHT_POSITIONAL
)) {
1013 GLfloat PV_dot_dir
= - DOT3(light
->_VP_inf_norm
,
1014 light
->_NormSpotDirection
);
1016 if (PV_dot_dir
> light
->_CosCutoff
) {
1017 light
->_VP_inf_spot_attenuation
=
1018 powf(PV_dot_dir
, light
->SpotExponent
);
1021 light
->_VP_inf_spot_attenuation
= 0;
1031 update_modelview_scale( struct gl_context
*ctx
)
1033 ctx
->_ModelViewInvScale
= 1.0F
;
1034 ctx
->_ModelViewInvScaleEyespace
= 1.0F
;
1035 if (!_math_matrix_is_length_preserving(ctx
->ModelviewMatrixStack
.Top
)) {
1036 const GLfloat
*m
= ctx
->ModelviewMatrixStack
.Top
->inv
;
1037 GLfloat f
= m
[2] * m
[2] + m
[6] * m
[6] + m
[10] * m
[10];
1038 if (f
< 1e-12f
) f
= 1.0f
;
1039 if (ctx
->_NeedEyeCoords
)
1040 ctx
->_ModelViewInvScale
= 1.0f
/ sqrtf(f
);
1042 ctx
->_ModelViewInvScale
= sqrtf(f
);
1043 ctx
->_ModelViewInvScaleEyespace
= 1.0f
/ sqrtf(f
);
1049 * Bring up to date any state that relies on _NeedEyeCoords.
1052 _mesa_update_tnl_spaces( struct gl_context
*ctx
, GLuint new_state
)
1054 const GLuint oldneedeyecoords
= ctx
->_NeedEyeCoords
;
1057 ctx
->_NeedEyeCoords
= GL_FALSE
;
1059 if (ctx
->_ForceEyeCoords
||
1060 (ctx
->Texture
._GenFlags
& TEXGEN_NEED_EYE_COORD
) ||
1061 ctx
->Point
._Attenuated
||
1062 ctx
->Light
._NeedEyeCoords
)
1063 ctx
->_NeedEyeCoords
= GL_TRUE
;
1065 if (ctx
->Light
.Enabled
&&
1066 !_math_matrix_is_length_preserving(ctx
->ModelviewMatrixStack
.Top
))
1067 ctx
->_NeedEyeCoords
= GL_TRUE
;
1069 /* Check if the truth-value interpretations of the bitfields have
1072 if (oldneedeyecoords
!= ctx
->_NeedEyeCoords
) {
1073 /* Recalculate all state that depends on _NeedEyeCoords.
1075 update_modelview_scale(ctx
);
1076 compute_light_positions( ctx
);
1078 if (ctx
->Driver
.LightingSpaceChange
)
1079 ctx
->Driver
.LightingSpaceChange( ctx
);
1082 GLuint new_state2
= ctx
->NewState
;
1084 /* Recalculate that same state only if it has been invalidated
1085 * by other statechanges.
1087 if (new_state2
& _NEW_MODELVIEW
)
1088 update_modelview_scale(ctx
);
1090 if (new_state2
& (_NEW_LIGHT
|_NEW_MODELVIEW
))
1091 compute_light_positions( ctx
);
1097 * Drivers may need this if the hardware tnl unit doesn't support the
1098 * light-in-modelspace optimization. It's also useful for debugging.
1101 _mesa_allow_light_in_model( struct gl_context
*ctx
, GLboolean flag
)
1103 ctx
->_ForceEyeCoords
= !flag
;
1104 ctx
->NewState
|= _NEW_POINT
; /* one of the bits from
1105 * _MESA_NEW_NEED_EYE_COORDS.
1111 /**********************************************************************/
1112 /***** Initialization *****/
1113 /**********************************************************************/
1116 * Initialize the n-th light data structure.
1118 * \param l pointer to the gl_light structure to be initialized.
1119 * \param n number of the light.
1120 * \note The defaults for light 0 are different than the other lights.
1123 init_light( struct gl_light
*l
, GLuint n
)
1125 ASSIGN_4V( l
->Ambient
, 0.0, 0.0, 0.0, 1.0 );
1127 ASSIGN_4V( l
->Diffuse
, 1.0, 1.0, 1.0, 1.0 );
1128 ASSIGN_4V( l
->Specular
, 1.0, 1.0, 1.0, 1.0 );
1131 ASSIGN_4V( l
->Diffuse
, 0.0, 0.0, 0.0, 1.0 );
1132 ASSIGN_4V( l
->Specular
, 0.0, 0.0, 0.0, 1.0 );
1134 ASSIGN_4V( l
->EyePosition
, 0.0, 0.0, 1.0, 0.0 );
1135 ASSIGN_3V( l
->SpotDirection
, 0.0, 0.0, -1.0 );
1136 l
->SpotExponent
= 0.0;
1137 l
->SpotCutoff
= 180.0;
1138 l
->_CosCutoff
= 0.0; /* KW: -ve values not admitted */
1139 l
->ConstantAttenuation
= 1.0;
1140 l
->LinearAttenuation
= 0.0;
1141 l
->QuadraticAttenuation
= 0.0;
1142 l
->Enabled
= GL_FALSE
;
1147 * Initialize the light model data structure.
1149 * \param lm pointer to the gl_lightmodel structure to be initialized.
1152 init_lightmodel( struct gl_lightmodel
*lm
)
1154 ASSIGN_4V( lm
->Ambient
, 0.2F
, 0.2F
, 0.2F
, 1.0F
);
1155 lm
->LocalViewer
= GL_FALSE
;
1156 lm
->TwoSide
= GL_FALSE
;
1157 lm
->ColorControl
= GL_SINGLE_COLOR
;
1162 * Initialize the material data structure.
1164 * \param m pointer to the gl_material structure to be initialized.
1167 init_material( struct gl_material
*m
)
1169 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_FRONT_AMBIENT
], 0.2F
, 0.2F
, 0.2F
, 1.0F
);
1170 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
], 0.8F
, 0.8F
, 0.8F
, 1.0F
);
1171 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_FRONT_SPECULAR
], 0.0F
, 0.0F
, 0.0F
, 1.0F
);
1172 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_FRONT_EMISSION
], 0.0F
, 0.0F
, 0.0F
, 1.0F
);
1173 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_FRONT_SHININESS
], 0.0F
, 0.0F
, 0.0F
, 0.0F
);
1174 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_FRONT_INDEXES
], 0.0F
, 1.0F
, 1.0F
, 0.0F
);
1176 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_BACK_AMBIENT
], 0.2F
, 0.2F
, 0.2F
, 1.0F
);
1177 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_BACK_DIFFUSE
], 0.8F
, 0.8F
, 0.8F
, 1.0F
);
1178 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_BACK_SPECULAR
], 0.0F
, 0.0F
, 0.0F
, 1.0F
);
1179 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_BACK_EMISSION
], 0.0F
, 0.0F
, 0.0F
, 1.0F
);
1180 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_BACK_SHININESS
], 0.0F
, 0.0F
, 0.0F
, 0.0F
);
1181 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_BACK_INDEXES
], 0.0F
, 1.0F
, 1.0F
, 0.0F
);
1186 * Initialize all lighting state for the given context.
1189 _mesa_init_lighting( struct gl_context
*ctx
)
1193 /* Lighting group */
1194 ctx
->Light
._EnabledLights
= 0;
1195 for (i
= 0; i
< MAX_LIGHTS
; i
++) {
1196 init_light( &ctx
->Light
.Light
[i
], i
);
1199 init_lightmodel( &ctx
->Light
.Model
);
1200 init_material( &ctx
->Light
.Material
);
1201 ctx
->Light
.ShadeModel
= GL_SMOOTH
;
1202 ctx
->Light
.ProvokingVertex
= GL_LAST_VERTEX_CONVENTION_EXT
;
1203 ctx
->Light
.Enabled
= GL_FALSE
;
1204 ctx
->Light
.ColorMaterialFace
= GL_FRONT_AND_BACK
;
1205 ctx
->Light
.ColorMaterialMode
= GL_AMBIENT_AND_DIFFUSE
;
1206 ctx
->Light
._ColorMaterialBitmask
= _mesa_material_bitmask( ctx
,
1208 GL_AMBIENT_AND_DIFFUSE
, ~0,
1211 ctx
->Light
.ColorMaterialEnabled
= GL_FALSE
;
1212 ctx
->Light
.ClampVertexColor
= ctx
->API
== API_OPENGL_COMPAT
;
1213 ctx
->Light
._ClampVertexColor
= ctx
->API
== API_OPENGL_COMPAT
;
1216 ctx
->Light
._NeedEyeCoords
= GL_FALSE
;
1217 ctx
->_NeedEyeCoords
= GL_FALSE
;
1218 ctx
->_ForceEyeCoords
= GL_FALSE
;
1219 ctx
->_ModelViewInvScale
= 1.0;
1220 ctx
->_ModelViewInvScaleEyespace
= 1.0;