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 "util/simple_list.h"
36 #include "math/m_matrix.h"
37 #include "util/bitscan.h"
41 _mesa_ShadeModel( GLenum mode
)
43 GET_CURRENT_CONTEXT(ctx
);
45 if (MESA_VERBOSE
& VERBOSE_API
)
46 _mesa_debug(ctx
, "glShadeModel %s\n", _mesa_enum_to_string(mode
));
48 if (ctx
->Light
.ShadeModel
== mode
)
51 if (mode
!= GL_FLAT
&& mode
!= GL_SMOOTH
) {
52 _mesa_error(ctx
, GL_INVALID_ENUM
, "glShadeModel");
56 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
57 ctx
->Light
.ShadeModel
= mode
;
59 if (ctx
->Driver
.ShadeModel
)
60 ctx
->Driver
.ShadeModel( ctx
, mode
);
65 * Set the provoking vertex (the vertex which specifies the prim's
66 * color when flat shading) to either the first or last vertex of the
70 _mesa_ProvokingVertex(GLenum mode
)
72 GET_CURRENT_CONTEXT(ctx
);
74 if (MESA_VERBOSE
&VERBOSE_API
)
75 _mesa_debug(ctx
, "glProvokingVertexEXT 0x%x\n", mode
);
78 case GL_FIRST_VERTEX_CONVENTION_EXT
:
79 case GL_LAST_VERTEX_CONVENTION_EXT
:
82 _mesa_error(ctx
, GL_INVALID_ENUM
, "glProvokingVertexEXT(0x%x)", mode
);
86 if (ctx
->Light
.ProvokingVertex
== mode
)
89 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
90 ctx
->Light
.ProvokingVertex
= mode
;
95 * Helper function called by _mesa_Lightfv and _mesa_PopAttrib to set
97 * For GL_POSITION and GL_SPOT_DIRECTION the params position/direction
98 * will have already been transformed by the modelview matrix!
99 * Also, all error checking should have already been done.
102 _mesa_light(struct gl_context
*ctx
, GLuint lnum
, GLenum pname
, const GLfloat
*params
)
104 struct gl_light
*light
;
106 assert(lnum
< MAX_LIGHTS
);
107 light
= &ctx
->Light
.Light
[lnum
];
111 if (TEST_EQ_4V(light
->Ambient
, params
))
113 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
114 COPY_4V( light
->Ambient
, params
);
117 if (TEST_EQ_4V(light
->Diffuse
, params
))
119 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
120 COPY_4V( light
->Diffuse
, params
);
123 if (TEST_EQ_4V(light
->Specular
, params
))
125 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
126 COPY_4V( light
->Specular
, params
);
129 /* NOTE: position has already been transformed by ModelView! */
130 if (TEST_EQ_4V(light
->EyePosition
, params
))
132 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
133 COPY_4V(light
->EyePosition
, params
);
134 if (light
->EyePosition
[3] != 0.0F
)
135 light
->_Flags
|= LIGHT_POSITIONAL
;
137 light
->_Flags
&= ~LIGHT_POSITIONAL
;
139 case GL_SPOT_DIRECTION
:
140 /* NOTE: Direction already transformed by inverse ModelView! */
141 if (TEST_EQ_3V(light
->SpotDirection
, params
))
143 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
144 COPY_3V(light
->SpotDirection
, params
);
146 case GL_SPOT_EXPONENT
:
147 assert(params
[0] >= 0.0F
);
148 assert(params
[0] <= ctx
->Const
.MaxSpotExponent
);
149 if (light
->SpotExponent
== params
[0])
151 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
152 light
->SpotExponent
= params
[0];
155 assert(params
[0] == 180.0F
|| (params
[0] >= 0.0F
&& params
[0] <= 90.0F
));
156 if (light
->SpotCutoff
== params
[0])
158 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
159 light
->SpotCutoff
= params
[0];
160 light
->_CosCutoff
= (cosf(light
->SpotCutoff
* M_PI
/ 180.0));
161 if (light
->_CosCutoff
< 0)
162 light
->_CosCutoff
= 0;
163 if (light
->SpotCutoff
!= 180.0F
)
164 light
->_Flags
|= LIGHT_SPOT
;
166 light
->_Flags
&= ~LIGHT_SPOT
;
168 case GL_CONSTANT_ATTENUATION
:
169 assert(params
[0] >= 0.0F
);
170 if (light
->ConstantAttenuation
== params
[0])
172 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
173 light
->ConstantAttenuation
= params
[0];
175 case GL_LINEAR_ATTENUATION
:
176 assert(params
[0] >= 0.0F
);
177 if (light
->LinearAttenuation
== params
[0])
179 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
180 light
->LinearAttenuation
= params
[0];
182 case GL_QUADRATIC_ATTENUATION
:
183 assert(params
[0] >= 0.0F
);
184 if (light
->QuadraticAttenuation
== params
[0])
186 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
187 light
->QuadraticAttenuation
= params
[0];
190 _mesa_problem(ctx
, "Unexpected pname in _mesa_light()");
194 if (ctx
->Driver
.Lightfv
)
195 ctx
->Driver
.Lightfv( ctx
, GL_LIGHT0
+ lnum
, pname
, params
);
200 _mesa_Lightf( GLenum light
, GLenum pname
, GLfloat param
)
204 fparam
[1] = fparam
[2] = fparam
[3] = 0.0F
;
205 _mesa_Lightfv( light
, pname
, fparam
);
210 _mesa_Lightfv( GLenum light
, GLenum pname
, const GLfloat
*params
)
212 GET_CURRENT_CONTEXT(ctx
);
213 GLint i
= (GLint
) (light
- GL_LIGHT0
);
216 if (i
< 0 || i
>= (GLint
) ctx
->Const
.MaxLights
) {
217 _mesa_error( ctx
, GL_INVALID_ENUM
, "glLight(light=0x%x)", light
);
221 /* do particular error checks, transformations */
229 /* transform position by ModelView matrix */
230 TRANSFORM_POINT(temp
, ctx
->ModelviewMatrixStack
.Top
->m
, params
);
233 case GL_SPOT_DIRECTION
:
234 /* transform direction by inverse modelview */
235 if (_math_matrix_is_dirty(ctx
->ModelviewMatrixStack
.Top
)) {
236 _math_matrix_analyse(ctx
->ModelviewMatrixStack
.Top
);
238 TRANSFORM_DIRECTION(temp
, params
, ctx
->ModelviewMatrixStack
.Top
->m
);
241 case GL_SPOT_EXPONENT
:
242 if (params
[0] < 0.0F
|| params
[0] > ctx
->Const
.MaxSpotExponent
) {
243 _mesa_error(ctx
, GL_INVALID_VALUE
, "glLight");
248 if ((params
[0] < 0.0F
|| params
[0] > 90.0F
) && params
[0] != 180.0F
) {
249 _mesa_error(ctx
, GL_INVALID_VALUE
, "glLight");
253 case GL_CONSTANT_ATTENUATION
:
254 case GL_LINEAR_ATTENUATION
:
255 case GL_QUADRATIC_ATTENUATION
:
256 if (params
[0] < 0.0F
) {
257 _mesa_error(ctx
, GL_INVALID_VALUE
, "glLight");
262 _mesa_error(ctx
, GL_INVALID_ENUM
, "glLight(pname=0x%x)", pname
);
266 _mesa_light(ctx
, i
, pname
, params
);
271 _mesa_Lighti( GLenum light
, GLenum pname
, GLint param
)
275 iparam
[1] = iparam
[2] = iparam
[3] = 0;
276 _mesa_Lightiv( light
, pname
, iparam
);
281 _mesa_Lightiv( GLenum light
, GLenum pname
, const GLint
*params
)
289 fparam
[0] = INT_TO_FLOAT( params
[0] );
290 fparam
[1] = INT_TO_FLOAT( params
[1] );
291 fparam
[2] = INT_TO_FLOAT( params
[2] );
292 fparam
[3] = INT_TO_FLOAT( params
[3] );
295 fparam
[0] = (GLfloat
) params
[0];
296 fparam
[1] = (GLfloat
) params
[1];
297 fparam
[2] = (GLfloat
) params
[2];
298 fparam
[3] = (GLfloat
) params
[3];
300 case GL_SPOT_DIRECTION
:
301 fparam
[0] = (GLfloat
) params
[0];
302 fparam
[1] = (GLfloat
) params
[1];
303 fparam
[2] = (GLfloat
) params
[2];
305 case GL_SPOT_EXPONENT
:
307 case GL_CONSTANT_ATTENUATION
:
308 case GL_LINEAR_ATTENUATION
:
309 case GL_QUADRATIC_ATTENUATION
:
310 fparam
[0] = (GLfloat
) params
[0];
313 /* error will be caught later in gl_Lightfv */
317 _mesa_Lightfv( light
, pname
, fparam
);
323 _mesa_GetLightfv( GLenum light
, GLenum pname
, GLfloat
*params
)
325 GET_CURRENT_CONTEXT(ctx
);
326 GLint l
= (GLint
) (light
- GL_LIGHT0
);
328 if (l
< 0 || l
>= (GLint
) ctx
->Const
.MaxLights
) {
329 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetLightfv" );
335 COPY_4V( params
, ctx
->Light
.Light
[l
].Ambient
);
338 COPY_4V( params
, ctx
->Light
.Light
[l
].Diffuse
);
341 COPY_4V( params
, ctx
->Light
.Light
[l
].Specular
);
344 COPY_4V( params
, ctx
->Light
.Light
[l
].EyePosition
);
346 case GL_SPOT_DIRECTION
:
347 COPY_3V( params
, ctx
->Light
.Light
[l
].SpotDirection
);
349 case GL_SPOT_EXPONENT
:
350 params
[0] = ctx
->Light
.Light
[l
].SpotExponent
;
353 params
[0] = ctx
->Light
.Light
[l
].SpotCutoff
;
355 case GL_CONSTANT_ATTENUATION
:
356 params
[0] = ctx
->Light
.Light
[l
].ConstantAttenuation
;
358 case GL_LINEAR_ATTENUATION
:
359 params
[0] = ctx
->Light
.Light
[l
].LinearAttenuation
;
361 case GL_QUADRATIC_ATTENUATION
:
362 params
[0] = ctx
->Light
.Light
[l
].QuadraticAttenuation
;
365 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetLightfv" );
372 _mesa_GetLightiv( GLenum light
, GLenum pname
, GLint
*params
)
374 GET_CURRENT_CONTEXT(ctx
);
375 GLint l
= (GLint
) (light
- GL_LIGHT0
);
377 if (l
< 0 || l
>= (GLint
) ctx
->Const
.MaxLights
) {
378 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetLightiv" );
384 params
[0] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Ambient
[0]);
385 params
[1] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Ambient
[1]);
386 params
[2] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Ambient
[2]);
387 params
[3] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Ambient
[3]);
390 params
[0] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Diffuse
[0]);
391 params
[1] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Diffuse
[1]);
392 params
[2] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Diffuse
[2]);
393 params
[3] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Diffuse
[3]);
396 params
[0] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Specular
[0]);
397 params
[1] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Specular
[1]);
398 params
[2] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Specular
[2]);
399 params
[3] = FLOAT_TO_INT(ctx
->Light
.Light
[l
].Specular
[3]);
402 params
[0] = (GLint
) ctx
->Light
.Light
[l
].EyePosition
[0];
403 params
[1] = (GLint
) ctx
->Light
.Light
[l
].EyePosition
[1];
404 params
[2] = (GLint
) ctx
->Light
.Light
[l
].EyePosition
[2];
405 params
[3] = (GLint
) ctx
->Light
.Light
[l
].EyePosition
[3];
407 case GL_SPOT_DIRECTION
:
408 params
[0] = (GLint
) ctx
->Light
.Light
[l
].SpotDirection
[0];
409 params
[1] = (GLint
) ctx
->Light
.Light
[l
].SpotDirection
[1];
410 params
[2] = (GLint
) ctx
->Light
.Light
[l
].SpotDirection
[2];
412 case GL_SPOT_EXPONENT
:
413 params
[0] = (GLint
) ctx
->Light
.Light
[l
].SpotExponent
;
416 params
[0] = (GLint
) ctx
->Light
.Light
[l
].SpotCutoff
;
418 case GL_CONSTANT_ATTENUATION
:
419 params
[0] = (GLint
) ctx
->Light
.Light
[l
].ConstantAttenuation
;
421 case GL_LINEAR_ATTENUATION
:
422 params
[0] = (GLint
) ctx
->Light
.Light
[l
].LinearAttenuation
;
424 case GL_QUADRATIC_ATTENUATION
:
425 params
[0] = (GLint
) ctx
->Light
.Light
[l
].QuadraticAttenuation
;
428 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetLightiv" );
435 /**********************************************************************/
436 /*** Light Model ***/
437 /**********************************************************************/
441 _mesa_LightModelfv( GLenum pname
, const GLfloat
*params
)
445 GET_CURRENT_CONTEXT(ctx
);
448 case GL_LIGHT_MODEL_AMBIENT
:
449 if (TEST_EQ_4V( ctx
->Light
.Model
.Ambient
, params
))
451 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
452 COPY_4V( ctx
->Light
.Model
.Ambient
, params
);
454 case GL_LIGHT_MODEL_LOCAL_VIEWER
:
455 if (ctx
->API
!= API_OPENGL_COMPAT
)
457 newbool
= (params
[0] != 0.0F
);
458 if (ctx
->Light
.Model
.LocalViewer
== newbool
)
460 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
461 ctx
->Light
.Model
.LocalViewer
= newbool
;
463 case GL_LIGHT_MODEL_TWO_SIDE
:
464 newbool
= (params
[0] != 0.0F
);
465 if (ctx
->Light
.Model
.TwoSide
== newbool
)
467 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
468 ctx
->Light
.Model
.TwoSide
= newbool
;
470 case GL_LIGHT_MODEL_COLOR_CONTROL
:
471 if (ctx
->API
!= API_OPENGL_COMPAT
)
473 if (params
[0] == (GLfloat
) GL_SINGLE_COLOR
)
474 newenum
= GL_SINGLE_COLOR
;
475 else if (params
[0] == (GLfloat
) GL_SEPARATE_SPECULAR_COLOR
)
476 newenum
= GL_SEPARATE_SPECULAR_COLOR
;
478 _mesa_error( ctx
, GL_INVALID_ENUM
, "glLightModel(param=0x0%x)",
482 if (ctx
->Light
.Model
.ColorControl
== newenum
)
484 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
485 ctx
->Light
.Model
.ColorControl
= newenum
;
491 if (ctx
->Driver
.LightModelfv
)
492 ctx
->Driver
.LightModelfv( ctx
, pname
, params
);
497 _mesa_error( ctx
, GL_INVALID_ENUM
, "glLightModel(pname=0x%x)", pname
);
503 _mesa_LightModeliv( GLenum pname
, const GLint
*params
)
508 case GL_LIGHT_MODEL_AMBIENT
:
509 fparam
[0] = INT_TO_FLOAT( params
[0] );
510 fparam
[1] = INT_TO_FLOAT( params
[1] );
511 fparam
[2] = INT_TO_FLOAT( params
[2] );
512 fparam
[3] = INT_TO_FLOAT( params
[3] );
514 case GL_LIGHT_MODEL_LOCAL_VIEWER
:
515 case GL_LIGHT_MODEL_TWO_SIDE
:
516 case GL_LIGHT_MODEL_COLOR_CONTROL
:
517 fparam
[0] = (GLfloat
) params
[0];
520 /* Error will be caught later in gl_LightModelfv */
521 ASSIGN_4V(fparam
, 0.0F
, 0.0F
, 0.0F
, 0.0F
);
523 _mesa_LightModelfv( pname
, fparam
);
528 _mesa_LightModeli( GLenum pname
, GLint param
)
532 iparam
[1] = iparam
[2] = iparam
[3] = 0;
533 _mesa_LightModeliv( pname
, iparam
);
538 _mesa_LightModelf( GLenum pname
, GLfloat param
)
542 fparam
[1] = fparam
[2] = fparam
[3] = 0.0F
;
543 _mesa_LightModelfv( pname
, fparam
);
548 /********** MATERIAL **********/
552 * Given a face and pname value (ala glColorMaterial), compute a bitmask
553 * of the targeted material values.
556 _mesa_material_bitmask( struct gl_context
*ctx
, GLenum face
, GLenum pname
,
557 GLuint legal
, const char *where
)
561 /* Make a bitmask indicating what material attribute(s) we're updating */
564 bitmask
|= MAT_BIT_FRONT_EMISSION
| MAT_BIT_BACK_EMISSION
;
567 bitmask
|= MAT_BIT_FRONT_AMBIENT
| MAT_BIT_BACK_AMBIENT
;
570 bitmask
|= MAT_BIT_FRONT_DIFFUSE
| MAT_BIT_BACK_DIFFUSE
;
573 bitmask
|= MAT_BIT_FRONT_SPECULAR
| MAT_BIT_BACK_SPECULAR
;
576 bitmask
|= MAT_BIT_FRONT_SHININESS
| MAT_BIT_BACK_SHININESS
;
578 case GL_AMBIENT_AND_DIFFUSE
:
579 bitmask
|= MAT_BIT_FRONT_AMBIENT
| MAT_BIT_BACK_AMBIENT
;
580 bitmask
|= MAT_BIT_FRONT_DIFFUSE
| MAT_BIT_BACK_DIFFUSE
;
582 case GL_COLOR_INDEXES
:
583 bitmask
|= MAT_BIT_FRONT_INDEXES
| MAT_BIT_BACK_INDEXES
;
586 _mesa_error( ctx
, GL_INVALID_ENUM
, "%s", where
);
590 if (face
==GL_FRONT
) {
591 bitmask
&= FRONT_MATERIAL_BITS
;
593 else if (face
==GL_BACK
) {
594 bitmask
&= BACK_MATERIAL_BITS
;
596 else if (face
!= GL_FRONT_AND_BACK
) {
597 _mesa_error( ctx
, GL_INVALID_ENUM
, "%s", where
);
601 if (bitmask
& ~legal
) {
602 _mesa_error( ctx
, GL_INVALID_ENUM
, "%s", where
);
611 /* Update derived values following a change in ctx->Light.Material
614 _mesa_update_material( struct gl_context
*ctx
, GLuint bitmask
)
616 GLfloat (*mat
)[4] = ctx
->Light
.Material
.Attrib
;
618 if (MESA_VERBOSE
& VERBOSE_MATERIAL
)
619 _mesa_debug(ctx
, "_mesa_update_material, mask 0x%x\n", bitmask
);
624 /* update material ambience */
625 if (bitmask
& MAT_BIT_FRONT_AMBIENT
) {
626 GLbitfield mask
= ctx
->Light
._EnabledLights
;
628 const int i
= u_bit_scan(&mask
);
629 struct gl_light
*light
= &ctx
->Light
.Light
[i
];
630 SCALE_3V( light
->_MatAmbient
[0], light
->Ambient
,
631 mat
[MAT_ATTRIB_FRONT_AMBIENT
]);
635 if (bitmask
& MAT_BIT_BACK_AMBIENT
) {
636 GLbitfield mask
= ctx
->Light
._EnabledLights
;
638 const int i
= u_bit_scan(&mask
);
639 struct gl_light
*light
= &ctx
->Light
.Light
[i
];
640 SCALE_3V( light
->_MatAmbient
[1], light
->Ambient
,
641 mat
[MAT_ATTRIB_BACK_AMBIENT
]);
645 /* update BaseColor = emission + scene's ambience * material's ambience */
646 if (bitmask
& (MAT_BIT_FRONT_EMISSION
| MAT_BIT_FRONT_AMBIENT
)) {
647 COPY_3V( ctx
->Light
._BaseColor
[0], mat
[MAT_ATTRIB_FRONT_EMISSION
] );
648 ACC_SCALE_3V( ctx
->Light
._BaseColor
[0], mat
[MAT_ATTRIB_FRONT_AMBIENT
],
649 ctx
->Light
.Model
.Ambient
);
652 if (bitmask
& (MAT_BIT_BACK_EMISSION
| MAT_BIT_BACK_AMBIENT
)) {
653 COPY_3V( ctx
->Light
._BaseColor
[1], mat
[MAT_ATTRIB_BACK_EMISSION
] );
654 ACC_SCALE_3V( ctx
->Light
._BaseColor
[1], mat
[MAT_ATTRIB_BACK_AMBIENT
],
655 ctx
->Light
.Model
.Ambient
);
658 /* update material diffuse values */
659 if (bitmask
& MAT_BIT_FRONT_DIFFUSE
) {
660 GLbitfield mask
= ctx
->Light
._EnabledLights
;
662 const int i
= u_bit_scan(&mask
);
663 struct gl_light
*light
= &ctx
->Light
.Light
[i
];
664 SCALE_3V( light
->_MatDiffuse
[0], light
->Diffuse
,
665 mat
[MAT_ATTRIB_FRONT_DIFFUSE
] );
669 if (bitmask
& MAT_BIT_BACK_DIFFUSE
) {
670 GLbitfield mask
= ctx
->Light
._EnabledLights
;
672 const int i
= u_bit_scan(&mask
);
673 struct gl_light
*light
= &ctx
->Light
.Light
[i
];
674 SCALE_3V( light
->_MatDiffuse
[1], light
->Diffuse
,
675 mat
[MAT_ATTRIB_BACK_DIFFUSE
] );
679 /* update material specular values */
680 if (bitmask
& MAT_BIT_FRONT_SPECULAR
) {
681 GLbitfield mask
= ctx
->Light
._EnabledLights
;
683 const int i
= u_bit_scan(&mask
);
684 struct gl_light
*light
= &ctx
->Light
.Light
[i
];
685 SCALE_3V( light
->_MatSpecular
[0], light
->Specular
,
686 mat
[MAT_ATTRIB_FRONT_SPECULAR
]);
690 if (bitmask
& MAT_BIT_BACK_SPECULAR
) {
691 GLbitfield mask
= ctx
->Light
._EnabledLights
;
693 const int i
= u_bit_scan(&mask
);
694 struct gl_light
*light
= &ctx
->Light
.Light
[i
];
695 SCALE_3V( light
->_MatSpecular
[1], light
->Specular
,
696 mat
[MAT_ATTRIB_BACK_SPECULAR
]);
703 * Update the current materials from the given rgba color
704 * according to the bitmask in _ColorMaterialBitmask, which is
705 * set by glColorMaterial().
708 _mesa_update_color_material( struct gl_context
*ctx
, const GLfloat color
[4] )
710 const GLbitfield bitmask
= ctx
->Light
._ColorMaterialBitmask
;
711 struct gl_material
*mat
= &ctx
->Light
.Material
;
714 for (i
= 0 ; i
< MAT_ATTRIB_MAX
; i
++)
715 if (bitmask
& (1<<i
))
716 COPY_4FV( mat
->Attrib
[i
], color
);
718 _mesa_update_material( ctx
, bitmask
);
723 _mesa_ColorMaterial( GLenum face
, GLenum mode
)
725 GET_CURRENT_CONTEXT(ctx
);
727 GLuint legal
= (MAT_BIT_FRONT_EMISSION
| MAT_BIT_BACK_EMISSION
|
728 MAT_BIT_FRONT_SPECULAR
| MAT_BIT_BACK_SPECULAR
|
729 MAT_BIT_FRONT_DIFFUSE
| MAT_BIT_BACK_DIFFUSE
|
730 MAT_BIT_FRONT_AMBIENT
| MAT_BIT_BACK_AMBIENT
);
732 if (MESA_VERBOSE
&VERBOSE_API
)
733 _mesa_debug(ctx
, "glColorMaterial %s %s\n",
734 _mesa_enum_to_string(face
),
735 _mesa_enum_to_string(mode
));
737 bitmask
= _mesa_material_bitmask(ctx
, face
, mode
, legal
, "glColorMaterial");
739 return; /* error was recorded */
741 if (ctx
->Light
._ColorMaterialBitmask
== bitmask
&&
742 ctx
->Light
.ColorMaterialFace
== face
&&
743 ctx
->Light
.ColorMaterialMode
== mode
)
746 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
747 ctx
->Light
._ColorMaterialBitmask
= bitmask
;
748 ctx
->Light
.ColorMaterialFace
= face
;
749 ctx
->Light
.ColorMaterialMode
= mode
;
751 if (ctx
->Light
.ColorMaterialEnabled
) {
752 FLUSH_CURRENT( ctx
, 0 );
753 _mesa_update_color_material(ctx
,ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
]);
756 if (ctx
->Driver
.ColorMaterial
)
757 ctx
->Driver
.ColorMaterial( ctx
, face
, mode
);
762 _mesa_GetMaterialfv( GLenum face
, GLenum pname
, GLfloat
*params
)
764 GET_CURRENT_CONTEXT(ctx
);
766 GLfloat (*mat
)[4] = ctx
->Light
.Material
.Attrib
;
767 FLUSH_VERTICES(ctx
, 0); /* update materials */
769 FLUSH_CURRENT(ctx
, 0); /* update ctx->Light.Material from vertex buffer */
771 if (face
==GL_FRONT
) {
774 else if (face
==GL_BACK
) {
778 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetMaterialfv(face)" );
784 COPY_4FV( params
, mat
[MAT_ATTRIB_AMBIENT(f
)] );
787 COPY_4FV( params
, mat
[MAT_ATTRIB_DIFFUSE(f
)] );
790 COPY_4FV( params
, mat
[MAT_ATTRIB_SPECULAR(f
)] );
793 COPY_4FV( params
, mat
[MAT_ATTRIB_EMISSION(f
)] );
796 *params
= mat
[MAT_ATTRIB_SHININESS(f
)][0];
798 case GL_COLOR_INDEXES
:
799 if (ctx
->API
!= API_OPENGL_COMPAT
) {
800 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetMaterialfv(pname)" );
803 params
[0] = mat
[MAT_ATTRIB_INDEXES(f
)][0];
804 params
[1] = mat
[MAT_ATTRIB_INDEXES(f
)][1];
805 params
[2] = mat
[MAT_ATTRIB_INDEXES(f
)][2];
808 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetMaterialfv(pname)" );
814 _mesa_GetMaterialiv( GLenum face
, GLenum pname
, GLint
*params
)
816 GET_CURRENT_CONTEXT(ctx
);
818 GLfloat (*mat
)[4] = ctx
->Light
.Material
.Attrib
;
820 assert(ctx
->API
== API_OPENGL_COMPAT
);
822 FLUSH_VERTICES(ctx
, 0); /* update materials */
823 FLUSH_CURRENT(ctx
, 0); /* update ctx->Light.Material from vertex buffer */
825 if (face
==GL_FRONT
) {
828 else if (face
==GL_BACK
) {
832 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetMaterialiv(face)" );
837 params
[0] = FLOAT_TO_INT( mat
[MAT_ATTRIB_AMBIENT(f
)][0] );
838 params
[1] = FLOAT_TO_INT( mat
[MAT_ATTRIB_AMBIENT(f
)][1] );
839 params
[2] = FLOAT_TO_INT( mat
[MAT_ATTRIB_AMBIENT(f
)][2] );
840 params
[3] = FLOAT_TO_INT( mat
[MAT_ATTRIB_AMBIENT(f
)][3] );
843 params
[0] = FLOAT_TO_INT( mat
[MAT_ATTRIB_DIFFUSE(f
)][0] );
844 params
[1] = FLOAT_TO_INT( mat
[MAT_ATTRIB_DIFFUSE(f
)][1] );
845 params
[2] = FLOAT_TO_INT( mat
[MAT_ATTRIB_DIFFUSE(f
)][2] );
846 params
[3] = FLOAT_TO_INT( mat
[MAT_ATTRIB_DIFFUSE(f
)][3] );
849 params
[0] = FLOAT_TO_INT( mat
[MAT_ATTRIB_SPECULAR(f
)][0] );
850 params
[1] = FLOAT_TO_INT( mat
[MAT_ATTRIB_SPECULAR(f
)][1] );
851 params
[2] = FLOAT_TO_INT( mat
[MAT_ATTRIB_SPECULAR(f
)][2] );
852 params
[3] = FLOAT_TO_INT( mat
[MAT_ATTRIB_SPECULAR(f
)][3] );
855 params
[0] = FLOAT_TO_INT( mat
[MAT_ATTRIB_EMISSION(f
)][0] );
856 params
[1] = FLOAT_TO_INT( mat
[MAT_ATTRIB_EMISSION(f
)][1] );
857 params
[2] = FLOAT_TO_INT( mat
[MAT_ATTRIB_EMISSION(f
)][2] );
858 params
[3] = FLOAT_TO_INT( mat
[MAT_ATTRIB_EMISSION(f
)][3] );
861 *params
= IROUND( mat
[MAT_ATTRIB_SHININESS(f
)][0] );
863 case GL_COLOR_INDEXES
:
864 params
[0] = IROUND( mat
[MAT_ATTRIB_INDEXES(f
)][0] );
865 params
[1] = IROUND( mat
[MAT_ATTRIB_INDEXES(f
)][1] );
866 params
[2] = IROUND( mat
[MAT_ATTRIB_INDEXES(f
)][2] );
869 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetMaterialfv(pname)" );
876 * Examine current lighting parameters to determine if the optimized lighting
877 * function can be used.
878 * Also, precompute some lighting values such as the products of light
879 * source and material ambient, diffuse and specular coefficients.
882 _mesa_update_lighting( struct gl_context
*ctx
)
884 GLbitfield flags
= 0;
885 ctx
->Light
._NeedEyeCoords
= GL_FALSE
;
887 if (!ctx
->Light
.Enabled
)
890 GLbitfield mask
= ctx
->Light
._EnabledLights
;
892 const int i
= u_bit_scan(&mask
);
893 struct gl_light
*light
= &ctx
->Light
.Light
[i
];
894 flags
|= light
->_Flags
;
897 ctx
->Light
._NeedVertices
=
898 ((flags
& (LIGHT_POSITIONAL
|LIGHT_SPOT
)) ||
899 ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
||
900 ctx
->Light
.Model
.LocalViewer
);
902 ctx
->Light
._NeedEyeCoords
= ((flags
& LIGHT_POSITIONAL
) ||
903 ctx
->Light
.Model
.LocalViewer
);
905 /* XXX: This test is overkill & needs to be fixed both for software and
906 * hardware t&l drivers. The above should be sufficient & should
907 * be tested to verify this.
909 if (ctx
->Light
._NeedVertices
)
910 ctx
->Light
._NeedEyeCoords
= GL_TRUE
;
912 /* Precompute some shading values. Although we reference
913 * Light.Material here, we can get away without flushing
914 * FLUSH_UPDATE_CURRENT, as when any outstanding material changes
915 * are flushed, they will update the derived state at that time.
917 if (ctx
->Light
.Model
.TwoSide
)
918 _mesa_update_material(ctx
,
919 MAT_BIT_FRONT_EMISSION
|
920 MAT_BIT_FRONT_AMBIENT
|
921 MAT_BIT_FRONT_DIFFUSE
|
922 MAT_BIT_FRONT_SPECULAR
|
923 MAT_BIT_BACK_EMISSION
|
924 MAT_BIT_BACK_AMBIENT
|
925 MAT_BIT_BACK_DIFFUSE
|
926 MAT_BIT_BACK_SPECULAR
);
928 _mesa_update_material(ctx
,
929 MAT_BIT_FRONT_EMISSION
|
930 MAT_BIT_FRONT_AMBIENT
|
931 MAT_BIT_FRONT_DIFFUSE
|
932 MAT_BIT_FRONT_SPECULAR
);
937 * Update state derived from light position, spot direction.
941 * _TNL_NEW_NEED_EYE_COORDS
943 * Update on (_NEW_MODELVIEW | _NEW_LIGHT) when lighting is enabled.
944 * Also update on lighting space changes.
947 compute_light_positions( struct gl_context
*ctx
)
949 static const GLfloat eye_z
[3] = { 0, 0, 1 };
951 if (!ctx
->Light
.Enabled
)
954 if (ctx
->_NeedEyeCoords
) {
955 COPY_3V( ctx
->_EyeZDir
, eye_z
);
958 TRANSFORM_NORMAL( ctx
->_EyeZDir
, eye_z
, ctx
->ModelviewMatrixStack
.Top
->m
);
961 GLbitfield mask
= ctx
->Light
._EnabledLights
;
963 const int i
= u_bit_scan(&mask
);
964 struct gl_light
*light
= &ctx
->Light
.Light
[i
];
966 if (ctx
->_NeedEyeCoords
) {
967 /* _Position is in eye coordinate space */
968 COPY_4FV( light
->_Position
, light
->EyePosition
);
971 /* _Position is in object coordinate space */
972 TRANSFORM_POINT( light
->_Position
, ctx
->ModelviewMatrixStack
.Top
->inv
,
973 light
->EyePosition
);
976 if (!(light
->_Flags
& LIGHT_POSITIONAL
)) {
977 /* VP (VP) = Normalize( Position ) */
978 COPY_3V( light
->_VP_inf_norm
, light
->_Position
);
979 NORMALIZE_3FV( light
->_VP_inf_norm
);
981 if (!ctx
->Light
.Model
.LocalViewer
) {
982 /* _h_inf_norm = Normalize( V_to_P + <0,0,1> ) */
983 ADD_3V( light
->_h_inf_norm
, light
->_VP_inf_norm
, ctx
->_EyeZDir
);
984 NORMALIZE_3FV( light
->_h_inf_norm
);
986 light
->_VP_inf_spot_attenuation
= 1.0;
989 /* positional light w/ homogeneous coordinate, divide by W */
990 GLfloat wInv
= 1.0F
/ light
->_Position
[3];
991 light
->_Position
[0] *= wInv
;
992 light
->_Position
[1] *= wInv
;
993 light
->_Position
[2] *= wInv
;
996 if (light
->_Flags
& LIGHT_SPOT
) {
997 /* Note: we normalize the spot direction now */
999 if (ctx
->_NeedEyeCoords
) {
1000 COPY_3V( light
->_NormSpotDirection
, light
->SpotDirection
);
1001 NORMALIZE_3FV( light
->_NormSpotDirection
);
1005 COPY_3V(spotDir
, light
->SpotDirection
);
1006 NORMALIZE_3FV(spotDir
);
1007 TRANSFORM_NORMAL( light
->_NormSpotDirection
,
1009 ctx
->ModelviewMatrixStack
.Top
->m
);
1012 NORMALIZE_3FV( light
->_NormSpotDirection
);
1014 if (!(light
->_Flags
& LIGHT_POSITIONAL
)) {
1015 GLfloat PV_dot_dir
= - DOT3(light
->_VP_inf_norm
,
1016 light
->_NormSpotDirection
);
1018 if (PV_dot_dir
> light
->_CosCutoff
) {
1019 light
->_VP_inf_spot_attenuation
=
1020 powf(PV_dot_dir
, light
->SpotExponent
);
1023 light
->_VP_inf_spot_attenuation
= 0;
1033 update_modelview_scale( struct gl_context
*ctx
)
1035 ctx
->_ModelViewInvScale
= 1.0F
;
1036 if (!_math_matrix_is_length_preserving(ctx
->ModelviewMatrixStack
.Top
)) {
1037 const GLfloat
*m
= ctx
->ModelviewMatrixStack
.Top
->inv
;
1038 GLfloat f
= m
[2] * m
[2] + m
[6] * m
[6] + m
[10] * m
[10];
1039 if (f
< 1e-12f
) f
= 1.0f
;
1040 if (ctx
->_NeedEyeCoords
)
1041 ctx
->_ModelViewInvScale
= 1.0f
/ sqrtf(f
);
1043 ctx
->_ModelViewInvScale
= 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 make_empty_list( l
);
1127 ASSIGN_4V( l
->Ambient
, 0.0, 0.0, 0.0, 1.0 );
1129 ASSIGN_4V( l
->Diffuse
, 1.0, 1.0, 1.0, 1.0 );
1130 ASSIGN_4V( l
->Specular
, 1.0, 1.0, 1.0, 1.0 );
1133 ASSIGN_4V( l
->Diffuse
, 0.0, 0.0, 0.0, 1.0 );
1134 ASSIGN_4V( l
->Specular
, 0.0, 0.0, 0.0, 1.0 );
1136 ASSIGN_4V( l
->EyePosition
, 0.0, 0.0, 1.0, 0.0 );
1137 ASSIGN_3V( l
->SpotDirection
, 0.0, 0.0, -1.0 );
1138 l
->SpotExponent
= 0.0;
1139 l
->SpotCutoff
= 180.0;
1140 l
->_CosCutoff
= 0.0; /* KW: -ve values not admitted */
1141 l
->ConstantAttenuation
= 1.0;
1142 l
->LinearAttenuation
= 0.0;
1143 l
->QuadraticAttenuation
= 0.0;
1144 l
->Enabled
= GL_FALSE
;
1149 * Initialize the light model data structure.
1151 * \param lm pointer to the gl_lightmodel structure to be initialized.
1154 init_lightmodel( struct gl_lightmodel
*lm
)
1156 ASSIGN_4V( lm
->Ambient
, 0.2F
, 0.2F
, 0.2F
, 1.0F
);
1157 lm
->LocalViewer
= GL_FALSE
;
1158 lm
->TwoSide
= GL_FALSE
;
1159 lm
->ColorControl
= GL_SINGLE_COLOR
;
1164 * Initialize the material data structure.
1166 * \param m pointer to the gl_material structure to be initialized.
1169 init_material( struct gl_material
*m
)
1171 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_FRONT_AMBIENT
], 0.2F
, 0.2F
, 0.2F
, 1.0F
);
1172 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
], 0.8F
, 0.8F
, 0.8F
, 1.0F
);
1173 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_FRONT_SPECULAR
], 0.0F
, 0.0F
, 0.0F
, 1.0F
);
1174 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_FRONT_EMISSION
], 0.0F
, 0.0F
, 0.0F
, 1.0F
);
1175 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_FRONT_SHININESS
], 0.0F
, 0.0F
, 0.0F
, 0.0F
);
1176 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_FRONT_INDEXES
], 0.0F
, 1.0F
, 1.0F
, 0.0F
);
1178 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_BACK_AMBIENT
], 0.2F
, 0.2F
, 0.2F
, 1.0F
);
1179 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_BACK_DIFFUSE
], 0.8F
, 0.8F
, 0.8F
, 1.0F
);
1180 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_BACK_SPECULAR
], 0.0F
, 0.0F
, 0.0F
, 1.0F
);
1181 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_BACK_EMISSION
], 0.0F
, 0.0F
, 0.0F
, 1.0F
);
1182 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_BACK_SHININESS
], 0.0F
, 0.0F
, 0.0F
, 0.0F
);
1183 ASSIGN_4V( m
->Attrib
[MAT_ATTRIB_BACK_INDEXES
], 0.0F
, 1.0F
, 1.0F
, 0.0F
);
1188 * Initialize all lighting state for the given context.
1191 _mesa_init_lighting( struct gl_context
*ctx
)
1195 /* Lighting group */
1196 ctx
->Light
._EnabledLights
= 0;
1197 for (i
= 0; i
< MAX_LIGHTS
; i
++) {
1198 init_light( &ctx
->Light
.Light
[i
], i
);
1200 make_empty_list( &ctx
->Light
.EnabledList
);
1202 init_lightmodel( &ctx
->Light
.Model
);
1203 init_material( &ctx
->Light
.Material
);
1204 ctx
->Light
.ShadeModel
= GL_SMOOTH
;
1205 ctx
->Light
.ProvokingVertex
= GL_LAST_VERTEX_CONVENTION_EXT
;
1206 ctx
->Light
.Enabled
= GL_FALSE
;
1207 ctx
->Light
.ColorMaterialFace
= GL_FRONT_AND_BACK
;
1208 ctx
->Light
.ColorMaterialMode
= GL_AMBIENT_AND_DIFFUSE
;
1209 ctx
->Light
._ColorMaterialBitmask
= _mesa_material_bitmask( ctx
,
1211 GL_AMBIENT_AND_DIFFUSE
, ~0,
1214 ctx
->Light
.ColorMaterialEnabled
= GL_FALSE
;
1215 ctx
->Light
.ClampVertexColor
= ctx
->API
== API_OPENGL_COMPAT
;
1216 ctx
->Light
._ClampVertexColor
= ctx
->API
== API_OPENGL_COMPAT
;
1219 ctx
->Light
._NeedEyeCoords
= GL_FALSE
;
1220 ctx
->_NeedEyeCoords
= GL_FALSE
;
1221 ctx
->_ForceEyeCoords
= GL_FALSE
;
1222 ctx
->_ModelViewInvScale
= 1.0;