mesa: Fix array out-of-bounds access by _mesa_LightModelf.
[mesa.git] / src / mesa / main / light.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.5
4 *
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27 #include "glheader.h"
28 #include "imports.h"
29 #include "context.h"
30 #include "enums.h"
31 #include "light.h"
32 #include "macros.h"
33 #include "simple_list.h"
34 #include "mtypes.h"
35 #include "math/m_matrix.h"
36
37
38 void GLAPIENTRY
39 _mesa_ShadeModel( GLenum mode )
40 {
41 GET_CURRENT_CONTEXT(ctx);
42 ASSERT_OUTSIDE_BEGIN_END(ctx);
43
44 if (MESA_VERBOSE & VERBOSE_API)
45 _mesa_debug(ctx, "glShadeModel %s\n", _mesa_lookup_enum_by_nr(mode));
46
47 if (mode != GL_FLAT && mode != GL_SMOOTH) {
48 _mesa_error(ctx, GL_INVALID_ENUM, "glShadeModel");
49 return;
50 }
51
52 if (ctx->Light.ShadeModel == mode)
53 return;
54
55 FLUSH_VERTICES(ctx, _NEW_LIGHT);
56 ctx->Light.ShadeModel = mode;
57 if (mode == GL_FLAT)
58 ctx->_TriangleCaps |= DD_FLATSHADE;
59 else
60 ctx->_TriangleCaps &= ~DD_FLATSHADE;
61
62 if (ctx->Driver.ShadeModel)
63 ctx->Driver.ShadeModel( ctx, mode );
64 }
65
66
67 /**
68 * Set the provoking vertex (the vertex which specifies the prim's
69 * color when flat shading) to either the first or last vertex of the
70 * triangle or line.
71 */
72 void GLAPIENTRY
73 _mesa_ProvokingVertexEXT(GLenum mode)
74 {
75 GET_CURRENT_CONTEXT(ctx);
76 ASSERT_OUTSIDE_BEGIN_END(ctx);
77
78 if (MESA_VERBOSE&VERBOSE_API)
79 _mesa_debug(ctx, "glProvokingVertexEXT 0x%x\n", mode);
80
81 switch (mode) {
82 case GL_FIRST_VERTEX_CONVENTION_EXT:
83 case GL_LAST_VERTEX_CONVENTION_EXT:
84 break;
85 default:
86 _mesa_error(ctx, GL_INVALID_ENUM, "glProvokingVertexEXT(0x%x)", mode);
87 return;
88 }
89
90 if (ctx->Light.ProvokingVertex == mode)
91 return;
92
93 FLUSH_VERTICES(ctx, _NEW_LIGHT);
94 ctx->Light.ProvokingVertex = mode;
95 }
96
97
98 /**
99 * Helper function called by _mesa_Lightfv and _mesa_PopAttrib to set
100 * per-light state.
101 * For GL_POSITION and GL_SPOT_DIRECTION the params position/direction
102 * will have already been transformed by the modelview matrix!
103 * Also, all error checking should have already been done.
104 */
105 void
106 _mesa_light(GLcontext *ctx, GLuint lnum, GLenum pname, const GLfloat *params)
107 {
108 struct gl_light *light;
109
110 ASSERT(lnum < MAX_LIGHTS);
111 light = &ctx->Light.Light[lnum];
112
113 switch (pname) {
114 case GL_AMBIENT:
115 if (TEST_EQ_4V(light->Ambient, params))
116 return;
117 FLUSH_VERTICES(ctx, _NEW_LIGHT);
118 COPY_4V( light->Ambient, params );
119 break;
120 case GL_DIFFUSE:
121 if (TEST_EQ_4V(light->Diffuse, params))
122 return;
123 FLUSH_VERTICES(ctx, _NEW_LIGHT);
124 COPY_4V( light->Diffuse, params );
125 break;
126 case GL_SPECULAR:
127 if (TEST_EQ_4V(light->Specular, params))
128 return;
129 FLUSH_VERTICES(ctx, _NEW_LIGHT);
130 COPY_4V( light->Specular, params );
131 break;
132 case GL_POSITION:
133 /* NOTE: position has already been transformed by ModelView! */
134 if (TEST_EQ_4V(light->EyePosition, params))
135 return;
136 FLUSH_VERTICES(ctx, _NEW_LIGHT);
137 COPY_4V(light->EyePosition, params);
138 if (light->EyePosition[3] != 0.0F)
139 light->_Flags |= LIGHT_POSITIONAL;
140 else
141 light->_Flags &= ~LIGHT_POSITIONAL;
142 break;
143 case GL_SPOT_DIRECTION:
144 /* NOTE: Direction already transformed by inverse ModelView! */
145 if (TEST_EQ_3V(light->SpotDirection, params))
146 return;
147 FLUSH_VERTICES(ctx, _NEW_LIGHT);
148 COPY_3V(light->SpotDirection, params);
149 break;
150 case GL_SPOT_EXPONENT:
151 ASSERT(params[0] >= 0.0);
152 ASSERT(params[0] <= ctx->Const.MaxSpotExponent);
153 if (light->SpotExponent == params[0])
154 return;
155 FLUSH_VERTICES(ctx, _NEW_LIGHT);
156 light->SpotExponent = params[0];
157 _mesa_invalidate_spot_exp_table(light);
158 break;
159 case GL_SPOT_CUTOFF:
160 ASSERT(params[0] == 180.0 || (params[0] >= 0.0 && params[0] <= 90.0));
161 if (light->SpotCutoff == params[0])
162 return;
163 FLUSH_VERTICES(ctx, _NEW_LIGHT);
164 light->SpotCutoff = params[0];
165 light->_CosCutoffNeg = (GLfloat) (_mesa_cos(light->SpotCutoff * DEG2RAD));
166 if (light->_CosCutoffNeg < 0)
167 light->_CosCutoff = 0;
168 else
169 light->_CosCutoff = light->_CosCutoffNeg;
170 if (light->SpotCutoff != 180.0F)
171 light->_Flags |= LIGHT_SPOT;
172 else
173 light->_Flags &= ~LIGHT_SPOT;
174 break;
175 case GL_CONSTANT_ATTENUATION:
176 ASSERT(params[0] >= 0.0);
177 if (light->ConstantAttenuation == params[0])
178 return;
179 FLUSH_VERTICES(ctx, _NEW_LIGHT);
180 light->ConstantAttenuation = params[0];
181 break;
182 case GL_LINEAR_ATTENUATION:
183 ASSERT(params[0] >= 0.0);
184 if (light->LinearAttenuation == params[0])
185 return;
186 FLUSH_VERTICES(ctx, _NEW_LIGHT);
187 light->LinearAttenuation = params[0];
188 break;
189 case GL_QUADRATIC_ATTENUATION:
190 ASSERT(params[0] >= 0.0);
191 if (light->QuadraticAttenuation == params[0])
192 return;
193 FLUSH_VERTICES(ctx, _NEW_LIGHT);
194 light->QuadraticAttenuation = params[0];
195 break;
196 default:
197 _mesa_problem(ctx, "Unexpected pname in _mesa_light()");
198 return;
199 }
200
201 if (ctx->Driver.Lightfv)
202 ctx->Driver.Lightfv( ctx, GL_LIGHT0 + lnum, pname, params );
203 }
204
205
206 void GLAPIENTRY
207 _mesa_Lightf( GLenum light, GLenum pname, GLfloat param )
208 {
209 _mesa_Lightfv( light, pname, &param );
210 }
211
212
213 void GLAPIENTRY
214 _mesa_Lightfv( GLenum light, GLenum pname, const GLfloat *params )
215 {
216 GET_CURRENT_CONTEXT(ctx);
217 GLint i = (GLint) (light - GL_LIGHT0);
218 GLfloat temp[4];
219 ASSERT_OUTSIDE_BEGIN_END(ctx);
220
221 if (i < 0 || i >= (GLint) ctx->Const.MaxLights) {
222 _mesa_error( ctx, GL_INVALID_ENUM, "glLight(light=0x%x)", light );
223 return;
224 }
225
226 /* do particular error checks, transformations */
227 switch (pname) {
228 case GL_AMBIENT:
229 case GL_DIFFUSE:
230 case GL_SPECULAR:
231 /* nothing */
232 break;
233 case GL_POSITION:
234 /* transform position by ModelView matrix */
235 TRANSFORM_POINT(temp, ctx->ModelviewMatrixStack.Top->m, params);
236 params = temp;
237 break;
238 case GL_SPOT_DIRECTION:
239 /* transform direction by inverse modelview */
240 if (_math_matrix_is_dirty(ctx->ModelviewMatrixStack.Top)) {
241 _math_matrix_analyse(ctx->ModelviewMatrixStack.Top);
242 }
243 TRANSFORM_DIRECTION(temp, params, ctx->ModelviewMatrixStack.Top->m);
244 params = temp;
245 break;
246 case GL_SPOT_EXPONENT:
247 if (params[0] < 0.0 || params[0] > ctx->Const.MaxSpotExponent) {
248 _mesa_error(ctx, GL_INVALID_VALUE, "glLight");
249 return;
250 }
251 break;
252 case GL_SPOT_CUTOFF:
253 if ((params[0] < 0.0 || params[0] > 90.0) && params[0] != 180.0) {
254 _mesa_error(ctx, GL_INVALID_VALUE, "glLight");
255 return;
256 }
257 break;
258 case GL_CONSTANT_ATTENUATION:
259 if (params[0] < 0.0) {
260 _mesa_error(ctx, GL_INVALID_VALUE, "glLight");
261 return;
262 }
263 break;
264 case GL_LINEAR_ATTENUATION:
265 if (params[0] < 0.0) {
266 _mesa_error(ctx, GL_INVALID_VALUE, "glLight");
267 return;
268 }
269 break;
270 case GL_QUADRATIC_ATTENUATION:
271 if (params[0] < 0.0) {
272 _mesa_error(ctx, GL_INVALID_VALUE, "glLight");
273 return;
274 }
275 break;
276 default:
277 _mesa_error(ctx, GL_INVALID_ENUM, "glLight(pname=0x%x)", pname);
278 return;
279 }
280
281 _mesa_light(ctx, i, pname, params);
282 }
283
284
285 void GLAPIENTRY
286 _mesa_Lighti( GLenum light, GLenum pname, GLint param )
287 {
288 _mesa_Lightiv( light, pname, &param );
289 }
290
291
292 void GLAPIENTRY
293 _mesa_Lightiv( GLenum light, GLenum pname, const GLint *params )
294 {
295 GLfloat fparam[4];
296
297 switch (pname) {
298 case GL_AMBIENT:
299 case GL_DIFFUSE:
300 case GL_SPECULAR:
301 fparam[0] = INT_TO_FLOAT( params[0] );
302 fparam[1] = INT_TO_FLOAT( params[1] );
303 fparam[2] = INT_TO_FLOAT( params[2] );
304 fparam[3] = INT_TO_FLOAT( params[3] );
305 break;
306 case GL_POSITION:
307 fparam[0] = (GLfloat) params[0];
308 fparam[1] = (GLfloat) params[1];
309 fparam[2] = (GLfloat) params[2];
310 fparam[3] = (GLfloat) params[3];
311 break;
312 case GL_SPOT_DIRECTION:
313 fparam[0] = (GLfloat) params[0];
314 fparam[1] = (GLfloat) params[1];
315 fparam[2] = (GLfloat) params[2];
316 break;
317 case GL_SPOT_EXPONENT:
318 case GL_SPOT_CUTOFF:
319 case GL_CONSTANT_ATTENUATION:
320 case GL_LINEAR_ATTENUATION:
321 case GL_QUADRATIC_ATTENUATION:
322 fparam[0] = (GLfloat) params[0];
323 break;
324 default:
325 /* error will be caught later in gl_Lightfv */
326 ;
327 }
328
329 _mesa_Lightfv( light, pname, fparam );
330 }
331
332
333
334 void GLAPIENTRY
335 _mesa_GetLightfv( GLenum light, GLenum pname, GLfloat *params )
336 {
337 GET_CURRENT_CONTEXT(ctx);
338 GLint l = (GLint) (light - GL_LIGHT0);
339 ASSERT_OUTSIDE_BEGIN_END(ctx);
340
341 if (l < 0 || l >= (GLint) ctx->Const.MaxLights) {
342 _mesa_error( ctx, GL_INVALID_ENUM, "glGetLightfv" );
343 return;
344 }
345
346 switch (pname) {
347 case GL_AMBIENT:
348 COPY_4V( params, ctx->Light.Light[l].Ambient );
349 break;
350 case GL_DIFFUSE:
351 COPY_4V( params, ctx->Light.Light[l].Diffuse );
352 break;
353 case GL_SPECULAR:
354 COPY_4V( params, ctx->Light.Light[l].Specular );
355 break;
356 case GL_POSITION:
357 COPY_4V( params, ctx->Light.Light[l].EyePosition );
358 break;
359 case GL_SPOT_DIRECTION:
360 COPY_3V( params, ctx->Light.Light[l].SpotDirection );
361 break;
362 case GL_SPOT_EXPONENT:
363 params[0] = ctx->Light.Light[l].SpotExponent;
364 break;
365 case GL_SPOT_CUTOFF:
366 params[0] = ctx->Light.Light[l].SpotCutoff;
367 break;
368 case GL_CONSTANT_ATTENUATION:
369 params[0] = ctx->Light.Light[l].ConstantAttenuation;
370 break;
371 case GL_LINEAR_ATTENUATION:
372 params[0] = ctx->Light.Light[l].LinearAttenuation;
373 break;
374 case GL_QUADRATIC_ATTENUATION:
375 params[0] = ctx->Light.Light[l].QuadraticAttenuation;
376 break;
377 default:
378 _mesa_error( ctx, GL_INVALID_ENUM, "glGetLightfv" );
379 break;
380 }
381 }
382
383
384 void GLAPIENTRY
385 _mesa_GetLightiv( GLenum light, GLenum pname, GLint *params )
386 {
387 GET_CURRENT_CONTEXT(ctx);
388 GLint l = (GLint) (light - GL_LIGHT0);
389 ASSERT_OUTSIDE_BEGIN_END(ctx);
390
391 if (l < 0 || l >= (GLint) ctx->Const.MaxLights) {
392 _mesa_error( ctx, GL_INVALID_ENUM, "glGetLightiv" );
393 return;
394 }
395
396 switch (pname) {
397 case GL_AMBIENT:
398 params[0] = FLOAT_TO_INT(ctx->Light.Light[l].Ambient[0]);
399 params[1] = FLOAT_TO_INT(ctx->Light.Light[l].Ambient[1]);
400 params[2] = FLOAT_TO_INT(ctx->Light.Light[l].Ambient[2]);
401 params[3] = FLOAT_TO_INT(ctx->Light.Light[l].Ambient[3]);
402 break;
403 case GL_DIFFUSE:
404 params[0] = FLOAT_TO_INT(ctx->Light.Light[l].Diffuse[0]);
405 params[1] = FLOAT_TO_INT(ctx->Light.Light[l].Diffuse[1]);
406 params[2] = FLOAT_TO_INT(ctx->Light.Light[l].Diffuse[2]);
407 params[3] = FLOAT_TO_INT(ctx->Light.Light[l].Diffuse[3]);
408 break;
409 case GL_SPECULAR:
410 params[0] = FLOAT_TO_INT(ctx->Light.Light[l].Specular[0]);
411 params[1] = FLOAT_TO_INT(ctx->Light.Light[l].Specular[1]);
412 params[2] = FLOAT_TO_INT(ctx->Light.Light[l].Specular[2]);
413 params[3] = FLOAT_TO_INT(ctx->Light.Light[l].Specular[3]);
414 break;
415 case GL_POSITION:
416 params[0] = (GLint) ctx->Light.Light[l].EyePosition[0];
417 params[1] = (GLint) ctx->Light.Light[l].EyePosition[1];
418 params[2] = (GLint) ctx->Light.Light[l].EyePosition[2];
419 params[3] = (GLint) ctx->Light.Light[l].EyePosition[3];
420 break;
421 case GL_SPOT_DIRECTION:
422 params[0] = (GLint) ctx->Light.Light[l].SpotDirection[0];
423 params[1] = (GLint) ctx->Light.Light[l].SpotDirection[1];
424 params[2] = (GLint) ctx->Light.Light[l].SpotDirection[2];
425 break;
426 case GL_SPOT_EXPONENT:
427 params[0] = (GLint) ctx->Light.Light[l].SpotExponent;
428 break;
429 case GL_SPOT_CUTOFF:
430 params[0] = (GLint) ctx->Light.Light[l].SpotCutoff;
431 break;
432 case GL_CONSTANT_ATTENUATION:
433 params[0] = (GLint) ctx->Light.Light[l].ConstantAttenuation;
434 break;
435 case GL_LINEAR_ATTENUATION:
436 params[0] = (GLint) ctx->Light.Light[l].LinearAttenuation;
437 break;
438 case GL_QUADRATIC_ATTENUATION:
439 params[0] = (GLint) ctx->Light.Light[l].QuadraticAttenuation;
440 break;
441 default:
442 _mesa_error( ctx, GL_INVALID_ENUM, "glGetLightiv" );
443 break;
444 }
445 }
446
447
448
449 /**********************************************************************/
450 /*** Light Model ***/
451 /**********************************************************************/
452
453
454 void GLAPIENTRY
455 _mesa_LightModelfv( GLenum pname, const GLfloat *params )
456 {
457 GLenum newenum;
458 GLboolean newbool;
459 GET_CURRENT_CONTEXT(ctx);
460 ASSERT_OUTSIDE_BEGIN_END(ctx);
461
462 switch (pname) {
463 case GL_LIGHT_MODEL_AMBIENT:
464 if (TEST_EQ_4V( ctx->Light.Model.Ambient, params ))
465 return;
466 FLUSH_VERTICES(ctx, _NEW_LIGHT);
467 COPY_4V( ctx->Light.Model.Ambient, params );
468 break;
469 case GL_LIGHT_MODEL_LOCAL_VIEWER:
470 newbool = (params[0]!=0.0);
471 if (ctx->Light.Model.LocalViewer == newbool)
472 return;
473 FLUSH_VERTICES(ctx, _NEW_LIGHT);
474 ctx->Light.Model.LocalViewer = newbool;
475 break;
476 case GL_LIGHT_MODEL_TWO_SIDE:
477 newbool = (params[0]!=0.0);
478 if (ctx->Light.Model.TwoSide == newbool)
479 return;
480 FLUSH_VERTICES(ctx, _NEW_LIGHT);
481 ctx->Light.Model.TwoSide = newbool;
482 if (ctx->Light.Enabled && ctx->Light.Model.TwoSide)
483 ctx->_TriangleCaps |= DD_TRI_LIGHT_TWOSIDE;
484 else
485 ctx->_TriangleCaps &= ~DD_TRI_LIGHT_TWOSIDE;
486 break;
487 case GL_LIGHT_MODEL_COLOR_CONTROL:
488 if (params[0] == (GLfloat) GL_SINGLE_COLOR)
489 newenum = GL_SINGLE_COLOR;
490 else if (params[0] == (GLfloat) GL_SEPARATE_SPECULAR_COLOR)
491 newenum = GL_SEPARATE_SPECULAR_COLOR;
492 else {
493 _mesa_error( ctx, GL_INVALID_ENUM, "glLightModel(param=0x0%x)",
494 (GLint) params[0] );
495 return;
496 }
497 if (ctx->Light.Model.ColorControl == newenum)
498 return;
499 FLUSH_VERTICES(ctx, _NEW_LIGHT);
500 ctx->Light.Model.ColorControl = newenum;
501 break;
502 default:
503 _mesa_error( ctx, GL_INVALID_ENUM, "glLightModel(pname=0x%x)", pname );
504 break;
505 }
506
507 if (ctx->Driver.LightModelfv)
508 ctx->Driver.LightModelfv( ctx, pname, params );
509 }
510
511
512 void GLAPIENTRY
513 _mesa_LightModeliv( GLenum pname, const GLint *params )
514 {
515 GLfloat fparam[4];
516
517 switch (pname) {
518 case GL_LIGHT_MODEL_AMBIENT:
519 fparam[0] = INT_TO_FLOAT( params[0] );
520 fparam[1] = INT_TO_FLOAT( params[1] );
521 fparam[2] = INT_TO_FLOAT( params[2] );
522 fparam[3] = INT_TO_FLOAT( params[3] );
523 break;
524 case GL_LIGHT_MODEL_LOCAL_VIEWER:
525 case GL_LIGHT_MODEL_TWO_SIDE:
526 case GL_LIGHT_MODEL_COLOR_CONTROL:
527 fparam[0] = (GLfloat) params[0];
528 break;
529 default:
530 /* Error will be caught later in gl_LightModelfv */
531 ASSIGN_4V(fparam, 0.0F, 0.0F, 0.0F, 0.0F);
532 }
533 _mesa_LightModelfv( pname, fparam );
534 }
535
536
537 void GLAPIENTRY
538 _mesa_LightModeli( GLenum pname, GLint param )
539 {
540 GLint iparam[4];
541 iparam[0] = param;
542 iparam[1] = iparam[2] = iparam[3] = 0;
543 _mesa_LightModeliv( pname, iparam );
544 }
545
546
547 void GLAPIENTRY
548 _mesa_LightModelf( GLenum pname, GLfloat param )
549 {
550 GLfloat fparam[4];
551 fparam[0] = param;
552 fparam[1] = fparam[2] = fparam[3] = 0.0F;
553 _mesa_LightModelfv( pname, fparam );
554 }
555
556
557
558 /********** MATERIAL **********/
559
560
561 /*
562 * Given a face and pname value (ala glColorMaterial), compute a bitmask
563 * of the targeted material values.
564 */
565 GLuint
566 _mesa_material_bitmask( GLcontext *ctx, GLenum face, GLenum pname,
567 GLuint legal, const char *where )
568 {
569 GLuint bitmask = 0;
570
571 /* Make a bitmask indicating what material attribute(s) we're updating */
572 switch (pname) {
573 case GL_EMISSION:
574 bitmask |= MAT_BIT_FRONT_EMISSION | MAT_BIT_BACK_EMISSION;
575 break;
576 case GL_AMBIENT:
577 bitmask |= MAT_BIT_FRONT_AMBIENT | MAT_BIT_BACK_AMBIENT;
578 break;
579 case GL_DIFFUSE:
580 bitmask |= MAT_BIT_FRONT_DIFFUSE | MAT_BIT_BACK_DIFFUSE;
581 break;
582 case GL_SPECULAR:
583 bitmask |= MAT_BIT_FRONT_SPECULAR | MAT_BIT_BACK_SPECULAR;
584 break;
585 case GL_SHININESS:
586 bitmask |= MAT_BIT_FRONT_SHININESS | MAT_BIT_BACK_SHININESS;
587 break;
588 case GL_AMBIENT_AND_DIFFUSE:
589 bitmask |= MAT_BIT_FRONT_AMBIENT | MAT_BIT_BACK_AMBIENT;
590 bitmask |= MAT_BIT_FRONT_DIFFUSE | MAT_BIT_BACK_DIFFUSE;
591 break;
592 case GL_COLOR_INDEXES:
593 bitmask |= MAT_BIT_FRONT_INDEXES | MAT_BIT_BACK_INDEXES;
594 break;
595 default:
596 _mesa_error( ctx, GL_INVALID_ENUM, where );
597 return 0;
598 }
599
600 if (face==GL_FRONT) {
601 bitmask &= FRONT_MATERIAL_BITS;
602 }
603 else if (face==GL_BACK) {
604 bitmask &= BACK_MATERIAL_BITS;
605 }
606 else if (face != GL_FRONT_AND_BACK) {
607 _mesa_error( ctx, GL_INVALID_ENUM, where );
608 return 0;
609 }
610
611 if (bitmask & ~legal) {
612 _mesa_error( ctx, GL_INVALID_ENUM, where );
613 return 0;
614 }
615
616 return bitmask;
617 }
618
619
620
621 /* Perform a straight copy between materials.
622 */
623 void
624 _mesa_copy_materials( struct gl_material *dst,
625 const struct gl_material *src,
626 GLuint bitmask )
627 {
628 int i;
629
630 for (i = 0 ; i < MAT_ATTRIB_MAX ; i++)
631 if (bitmask & (1<<i))
632 COPY_4FV( dst->Attrib[i], src->Attrib[i] );
633 }
634
635
636
637 /* Update derived values following a change in ctx->Light.Material
638 */
639 void
640 _mesa_update_material( GLcontext *ctx, GLuint bitmask )
641 {
642 struct gl_light *light, *list = &ctx->Light.EnabledList;
643 GLfloat (*mat)[4] = ctx->Light.Material.Attrib;
644
645 if (MESA_VERBOSE&VERBOSE_IMMEDIATE)
646 _mesa_debug(ctx, "_mesa_update_material, mask 0x%x\n", bitmask);
647
648 if (!bitmask)
649 return;
650
651 /* update material ambience */
652 if (bitmask & MAT_BIT_FRONT_AMBIENT) {
653 foreach (light, list) {
654 SCALE_3V( light->_MatAmbient[0], light->Ambient,
655 mat[MAT_ATTRIB_FRONT_AMBIENT]);
656 }
657 }
658
659 if (bitmask & MAT_BIT_BACK_AMBIENT) {
660 foreach (light, list) {
661 SCALE_3V( light->_MatAmbient[1], light->Ambient,
662 mat[MAT_ATTRIB_BACK_AMBIENT]);
663 }
664 }
665
666 /* update BaseColor = emission + scene's ambience * material's ambience */
667 if (bitmask & (MAT_BIT_FRONT_EMISSION | MAT_BIT_FRONT_AMBIENT)) {
668 COPY_3V( ctx->Light._BaseColor[0], mat[MAT_ATTRIB_FRONT_EMISSION] );
669 ACC_SCALE_3V( ctx->Light._BaseColor[0], mat[MAT_ATTRIB_FRONT_AMBIENT],
670 ctx->Light.Model.Ambient );
671 }
672
673 if (bitmask & (MAT_BIT_BACK_EMISSION | MAT_BIT_BACK_AMBIENT)) {
674 COPY_3V( ctx->Light._BaseColor[1], mat[MAT_ATTRIB_BACK_EMISSION] );
675 ACC_SCALE_3V( ctx->Light._BaseColor[1], mat[MAT_ATTRIB_BACK_AMBIENT],
676 ctx->Light.Model.Ambient );
677 }
678
679 /* update material diffuse values */
680 if (bitmask & MAT_BIT_FRONT_DIFFUSE) {
681 foreach (light, list) {
682 SCALE_3V( light->_MatDiffuse[0], light->Diffuse,
683 mat[MAT_ATTRIB_FRONT_DIFFUSE] );
684 }
685 }
686
687 if (bitmask & MAT_BIT_BACK_DIFFUSE) {
688 foreach (light, list) {
689 SCALE_3V( light->_MatDiffuse[1], light->Diffuse,
690 mat[MAT_ATTRIB_BACK_DIFFUSE] );
691 }
692 }
693
694 /* update material specular values */
695 if (bitmask & MAT_BIT_FRONT_SPECULAR) {
696 foreach (light, list) {
697 SCALE_3V( light->_MatSpecular[0], light->Specular,
698 mat[MAT_ATTRIB_FRONT_SPECULAR]);
699 }
700 }
701
702 if (bitmask & MAT_BIT_BACK_SPECULAR) {
703 foreach (light, list) {
704 SCALE_3V( light->_MatSpecular[1], light->Specular,
705 mat[MAT_ATTRIB_BACK_SPECULAR]);
706 }
707 }
708
709 if (bitmask & MAT_BIT_FRONT_SHININESS) {
710 _mesa_invalidate_shine_table( ctx, 0 );
711 }
712
713 if (bitmask & MAT_BIT_BACK_SHININESS) {
714 _mesa_invalidate_shine_table( ctx, 1 );
715 }
716 }
717
718
719 /*
720 * Update the current materials from the given rgba color
721 * according to the bitmask in ColorMaterialBitmask, which is
722 * set by glColorMaterial().
723 */
724 void
725 _mesa_update_color_material( GLcontext *ctx, const GLfloat color[4] )
726 {
727 GLuint bitmask = ctx->Light.ColorMaterialBitmask;
728 struct gl_material *mat = &ctx->Light.Material;
729 int i;
730
731 for (i = 0 ; i < MAT_ATTRIB_MAX ; i++)
732 if (bitmask & (1<<i))
733 COPY_4FV( mat->Attrib[i], color );
734
735 _mesa_update_material( ctx, bitmask );
736 }
737
738
739 void GLAPIENTRY
740 _mesa_ColorMaterial( GLenum face, GLenum mode )
741 {
742 GET_CURRENT_CONTEXT(ctx);
743 GLuint bitmask;
744 GLuint legal = (MAT_BIT_FRONT_EMISSION | MAT_BIT_BACK_EMISSION |
745 MAT_BIT_FRONT_SPECULAR | MAT_BIT_BACK_SPECULAR |
746 MAT_BIT_FRONT_DIFFUSE | MAT_BIT_BACK_DIFFUSE |
747 MAT_BIT_FRONT_AMBIENT | MAT_BIT_BACK_AMBIENT);
748 ASSERT_OUTSIDE_BEGIN_END(ctx);
749
750 if (MESA_VERBOSE&VERBOSE_API)
751 _mesa_debug(ctx, "glColorMaterial %s %s\n",
752 _mesa_lookup_enum_by_nr(face),
753 _mesa_lookup_enum_by_nr(mode));
754
755 bitmask = _mesa_material_bitmask(ctx, face, mode, legal, "glColorMaterial");
756
757 if (ctx->Light.ColorMaterialBitmask == bitmask &&
758 ctx->Light.ColorMaterialFace == face &&
759 ctx->Light.ColorMaterialMode == mode)
760 return;
761
762 FLUSH_VERTICES(ctx, _NEW_LIGHT);
763 ctx->Light.ColorMaterialBitmask = bitmask;
764 ctx->Light.ColorMaterialFace = face;
765 ctx->Light.ColorMaterialMode = mode;
766
767 if (ctx->Light.ColorMaterialEnabled) {
768 FLUSH_CURRENT( ctx, 0 );
769 _mesa_update_color_material(ctx,ctx->Current.Attrib[VERT_ATTRIB_COLOR0]);
770 }
771
772 if (ctx->Driver.ColorMaterial)
773 ctx->Driver.ColorMaterial( ctx, face, mode );
774 }
775
776
777 void GLAPIENTRY
778 _mesa_GetMaterialfv( GLenum face, GLenum pname, GLfloat *params )
779 {
780 GET_CURRENT_CONTEXT(ctx);
781 GLuint f;
782 GLfloat (*mat)[4] = ctx->Light.Material.Attrib;
783 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); /* update materials */
784
785 FLUSH_CURRENT(ctx, 0); /* update ctx->Light.Material from vertex buffer */
786
787 if (face==GL_FRONT) {
788 f = 0;
789 }
790 else if (face==GL_BACK) {
791 f = 1;
792 }
793 else {
794 _mesa_error( ctx, GL_INVALID_ENUM, "glGetMaterialfv(face)" );
795 return;
796 }
797
798 switch (pname) {
799 case GL_AMBIENT:
800 COPY_4FV( params, mat[MAT_ATTRIB_AMBIENT(f)] );
801 break;
802 case GL_DIFFUSE:
803 COPY_4FV( params, mat[MAT_ATTRIB_DIFFUSE(f)] );
804 break;
805 case GL_SPECULAR:
806 COPY_4FV( params, mat[MAT_ATTRIB_SPECULAR(f)] );
807 break;
808 case GL_EMISSION:
809 COPY_4FV( params, mat[MAT_ATTRIB_EMISSION(f)] );
810 break;
811 case GL_SHININESS:
812 *params = mat[MAT_ATTRIB_SHININESS(f)][0];
813 break;
814 case GL_COLOR_INDEXES:
815 params[0] = mat[MAT_ATTRIB_INDEXES(f)][0];
816 params[1] = mat[MAT_ATTRIB_INDEXES(f)][1];
817 params[2] = mat[MAT_ATTRIB_INDEXES(f)][2];
818 break;
819 default:
820 _mesa_error( ctx, GL_INVALID_ENUM, "glGetMaterialfv(pname)" );
821 }
822 }
823
824
825 void GLAPIENTRY
826 _mesa_GetMaterialiv( GLenum face, GLenum pname, GLint *params )
827 {
828 GET_CURRENT_CONTEXT(ctx);
829 GLuint f;
830 GLfloat (*mat)[4] = ctx->Light.Material.Attrib;
831 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); /* update materials */
832
833 FLUSH_CURRENT(ctx, 0); /* update ctx->Light.Material from vertex buffer */
834
835 if (face==GL_FRONT) {
836 f = 0;
837 }
838 else if (face==GL_BACK) {
839 f = 1;
840 }
841 else {
842 _mesa_error( ctx, GL_INVALID_ENUM, "glGetMaterialiv(face)" );
843 return;
844 }
845 switch (pname) {
846 case GL_AMBIENT:
847 params[0] = FLOAT_TO_INT( mat[MAT_ATTRIB_AMBIENT(f)][0] );
848 params[1] = FLOAT_TO_INT( mat[MAT_ATTRIB_AMBIENT(f)][1] );
849 params[2] = FLOAT_TO_INT( mat[MAT_ATTRIB_AMBIENT(f)][2] );
850 params[3] = FLOAT_TO_INT( mat[MAT_ATTRIB_AMBIENT(f)][3] );
851 break;
852 case GL_DIFFUSE:
853 params[0] = FLOAT_TO_INT( mat[MAT_ATTRIB_DIFFUSE(f)][0] );
854 params[1] = FLOAT_TO_INT( mat[MAT_ATTRIB_DIFFUSE(f)][1] );
855 params[2] = FLOAT_TO_INT( mat[MAT_ATTRIB_DIFFUSE(f)][2] );
856 params[3] = FLOAT_TO_INT( mat[MAT_ATTRIB_DIFFUSE(f)][3] );
857 break;
858 case GL_SPECULAR:
859 params[0] = FLOAT_TO_INT( mat[MAT_ATTRIB_SPECULAR(f)][0] );
860 params[1] = FLOAT_TO_INT( mat[MAT_ATTRIB_SPECULAR(f)][1] );
861 params[2] = FLOAT_TO_INT( mat[MAT_ATTRIB_SPECULAR(f)][2] );
862 params[3] = FLOAT_TO_INT( mat[MAT_ATTRIB_SPECULAR(f)][3] );
863 break;
864 case GL_EMISSION:
865 params[0] = FLOAT_TO_INT( mat[MAT_ATTRIB_EMISSION(f)][0] );
866 params[1] = FLOAT_TO_INT( mat[MAT_ATTRIB_EMISSION(f)][1] );
867 params[2] = FLOAT_TO_INT( mat[MAT_ATTRIB_EMISSION(f)][2] );
868 params[3] = FLOAT_TO_INT( mat[MAT_ATTRIB_EMISSION(f)][3] );
869 break;
870 case GL_SHININESS:
871 *params = IROUND( mat[MAT_ATTRIB_SHININESS(f)][0] );
872 break;
873 case GL_COLOR_INDEXES:
874 params[0] = IROUND( mat[MAT_ATTRIB_INDEXES(f)][0] );
875 params[1] = IROUND( mat[MAT_ATTRIB_INDEXES(f)][1] );
876 params[2] = IROUND( mat[MAT_ATTRIB_INDEXES(f)][2] );
877 break;
878 default:
879 _mesa_error( ctx, GL_INVALID_ENUM, "glGetMaterialfv(pname)" );
880 }
881 }
882
883
884
885 /**********************************************************************/
886 /***** Lighting computation *****/
887 /**********************************************************************/
888
889
890 /*
891 * Notes:
892 * When two-sided lighting is enabled we compute the color (or index)
893 * for both the front and back side of the primitive. Then, when the
894 * orientation of the facet is later learned, we can determine which
895 * color (or index) to use for rendering.
896 *
897 * KW: We now know orientation in advance and only shade for
898 * the side or sides which are actually required.
899 *
900 * Variables:
901 * n = normal vector
902 * V = vertex position
903 * P = light source position
904 * Pe = (0,0,0,1)
905 *
906 * Precomputed:
907 * IF P[3]==0 THEN
908 * // light at infinity
909 * IF local_viewer THEN
910 * _VP_inf_norm = unit vector from V to P // Precompute
911 * ELSE
912 * // eye at infinity
913 * _h_inf_norm = Normalize( VP + <0,0,1> ) // Precompute
914 * ENDIF
915 * ENDIF
916 *
917 * Functions:
918 * Normalize( v ) = normalized vector v
919 * Magnitude( v ) = length of vector v
920 */
921
922
923
924 /*
925 * Whenever the spotlight exponent for a light changes we must call
926 * this function to recompute the exponent lookup table.
927 */
928 void
929 _mesa_invalidate_spot_exp_table( struct gl_light *l )
930 {
931 l->_SpotExpTable[0][0] = -1;
932 }
933
934
935 static void
936 validate_spot_exp_table( struct gl_light *l )
937 {
938 GLint i;
939 GLdouble exponent = l->SpotExponent;
940 GLdouble tmp = 0;
941 GLint clamp = 0;
942
943 l->_SpotExpTable[0][0] = 0.0;
944
945 for (i = EXP_TABLE_SIZE - 1; i > 0 ;i--) {
946 if (clamp == 0) {
947 tmp = _mesa_pow(i / (GLdouble) (EXP_TABLE_SIZE - 1), exponent);
948 if (tmp < FLT_MIN * 100.0) {
949 tmp = 0.0;
950 clamp = 1;
951 }
952 }
953 l->_SpotExpTable[i][0] = (GLfloat) tmp;
954 }
955 for (i = 0; i < EXP_TABLE_SIZE - 1; i++) {
956 l->_SpotExpTable[i][1] = (l->_SpotExpTable[i+1][0] -
957 l->_SpotExpTable[i][0]);
958 }
959 l->_SpotExpTable[EXP_TABLE_SIZE-1][1] = 0.0;
960 }
961
962
963
964 /* Calculate a new shine table. Doing this here saves a branch in
965 * lighting, and the cost of doing it early may be partially offset
966 * by keeping a MRU cache of shine tables for various shine values.
967 */
968 void
969 _mesa_invalidate_shine_table( GLcontext *ctx, GLuint side )
970 {
971 ASSERT(side < 2);
972 if (ctx->_ShineTable[side])
973 ctx->_ShineTable[side]->refcount--;
974 ctx->_ShineTable[side] = NULL;
975 }
976
977
978 static void
979 validate_shine_table( GLcontext *ctx, GLuint side, GLfloat shininess )
980 {
981 struct gl_shine_tab *list = ctx->_ShineTabList;
982 struct gl_shine_tab *s;
983
984 ASSERT(side < 2);
985
986 foreach(s, list)
987 if ( s->shininess == shininess )
988 break;
989
990 if (s == list) {
991 GLint j;
992 GLfloat *m;
993
994 foreach(s, list)
995 if (s->refcount == 0)
996 break;
997
998 m = s->tab;
999 m[0] = 0.0;
1000 if (shininess == 0.0) {
1001 for (j = 1 ; j <= SHINE_TABLE_SIZE ; j++)
1002 m[j] = 1.0;
1003 }
1004 else {
1005 for (j = 1 ; j < SHINE_TABLE_SIZE ; j++) {
1006 GLdouble t, x = j / (GLfloat) (SHINE_TABLE_SIZE - 1);
1007 if (x < 0.005) /* underflow check */
1008 x = 0.005;
1009 t = _mesa_pow(x, shininess);
1010 if (t > 1e-20)
1011 m[j] = (GLfloat) t;
1012 else
1013 m[j] = 0.0;
1014 }
1015 m[SHINE_TABLE_SIZE] = 1.0;
1016 }
1017
1018 s->shininess = shininess;
1019 }
1020
1021 if (ctx->_ShineTable[side])
1022 ctx->_ShineTable[side]->refcount--;
1023
1024 ctx->_ShineTable[side] = s;
1025 move_to_tail( list, s );
1026 s->refcount++;
1027 }
1028
1029
1030 void
1031 _mesa_validate_all_lighting_tables( GLcontext *ctx )
1032 {
1033 GLuint i;
1034 GLfloat shininess;
1035
1036 shininess = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SHININESS][0];
1037 if (!ctx->_ShineTable[0] || ctx->_ShineTable[0]->shininess != shininess)
1038 validate_shine_table( ctx, 0, shininess );
1039
1040 shininess = ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_SHININESS][0];
1041 if (!ctx->_ShineTable[1] || ctx->_ShineTable[1]->shininess != shininess)
1042 validate_shine_table( ctx, 1, shininess );
1043
1044 for (i = 0; i < ctx->Const.MaxLights; i++)
1045 if (ctx->Light.Light[i]._SpotExpTable[0][0] == -1)
1046 validate_spot_exp_table( &ctx->Light.Light[i] );
1047 }
1048
1049
1050 /**
1051 * Examine current lighting parameters to determine if the optimized lighting
1052 * function can be used.
1053 * Also, precompute some lighting values such as the products of light
1054 * source and material ambient, diffuse and specular coefficients.
1055 */
1056 void
1057 _mesa_update_lighting( GLcontext *ctx )
1058 {
1059 struct gl_light *light;
1060 ctx->Light._NeedEyeCoords = GL_FALSE;
1061 ctx->Light._Flags = 0;
1062
1063 if (!ctx->Light.Enabled)
1064 return;
1065
1066 foreach(light, &ctx->Light.EnabledList) {
1067 ctx->Light._Flags |= light->_Flags;
1068 }
1069
1070 ctx->Light._NeedVertices =
1071 ((ctx->Light._Flags & (LIGHT_POSITIONAL|LIGHT_SPOT)) ||
1072 ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR ||
1073 ctx->Light.Model.LocalViewer);
1074
1075 ctx->Light._NeedEyeCoords = ((ctx->Light._Flags & LIGHT_POSITIONAL) ||
1076 ctx->Light.Model.LocalViewer);
1077
1078 /* XXX: This test is overkill & needs to be fixed both for software and
1079 * hardware t&l drivers. The above should be sufficient & should
1080 * be tested to verify this.
1081 */
1082 if (ctx->Light._NeedVertices)
1083 ctx->Light._NeedEyeCoords = GL_TRUE;
1084
1085 /* Precompute some shading values. Although we reference
1086 * Light.Material here, we can get away without flushing
1087 * FLUSH_UPDATE_CURRENT, as when any outstanding material changes
1088 * are flushed, they will update the derived state at that time.
1089 */
1090 if (ctx->Visual.rgbMode) {
1091 if (ctx->Light.Model.TwoSide)
1092 _mesa_update_material( ctx,
1093 MAT_BIT_FRONT_EMISSION |
1094 MAT_BIT_FRONT_AMBIENT |
1095 MAT_BIT_FRONT_DIFFUSE |
1096 MAT_BIT_FRONT_SPECULAR |
1097 MAT_BIT_BACK_EMISSION |
1098 MAT_BIT_BACK_AMBIENT |
1099 MAT_BIT_BACK_DIFFUSE |
1100 MAT_BIT_BACK_SPECULAR);
1101 else
1102 _mesa_update_material( ctx,
1103 MAT_BIT_FRONT_EMISSION |
1104 MAT_BIT_FRONT_AMBIENT |
1105 MAT_BIT_FRONT_DIFFUSE |
1106 MAT_BIT_FRONT_SPECULAR);
1107 }
1108 else {
1109 static const GLfloat ci[3] = { .30F, .59F, .11F };
1110 foreach(light, &ctx->Light.EnabledList) {
1111 light->_dli = DOT3(ci, light->Diffuse);
1112 light->_sli = DOT3(ci, light->Specular);
1113 }
1114 }
1115 }
1116
1117
1118 /**
1119 * Update state derived from light position, spot direction.
1120 * Called upon:
1121 * _NEW_MODELVIEW
1122 * _NEW_LIGHT
1123 * _TNL_NEW_NEED_EYE_COORDS
1124 *
1125 * Update on (_NEW_MODELVIEW | _NEW_LIGHT) when lighting is enabled.
1126 * Also update on lighting space changes.
1127 */
1128 static void
1129 compute_light_positions( GLcontext *ctx )
1130 {
1131 struct gl_light *light;
1132 static const GLfloat eye_z[3] = { 0, 0, 1 };
1133
1134 if (!ctx->Light.Enabled)
1135 return;
1136
1137 if (ctx->_NeedEyeCoords) {
1138 COPY_3V( ctx->_EyeZDir, eye_z );
1139 }
1140 else {
1141 TRANSFORM_NORMAL( ctx->_EyeZDir, eye_z, ctx->ModelviewMatrixStack.Top->m );
1142 }
1143
1144 foreach (light, &ctx->Light.EnabledList) {
1145
1146 if (ctx->_NeedEyeCoords) {
1147 /* _Position is in eye coordinate space */
1148 COPY_4FV( light->_Position, light->EyePosition );
1149 }
1150 else {
1151 /* _Position is in object coordinate space */
1152 TRANSFORM_POINT( light->_Position, ctx->ModelviewMatrixStack.Top->inv,
1153 light->EyePosition );
1154 }
1155
1156 if (!(light->_Flags & LIGHT_POSITIONAL)) {
1157 /* VP (VP) = Normalize( Position ) */
1158 COPY_3V( light->_VP_inf_norm, light->_Position );
1159 NORMALIZE_3FV( light->_VP_inf_norm );
1160
1161 if (!ctx->Light.Model.LocalViewer) {
1162 /* _h_inf_norm = Normalize( V_to_P + <0,0,1> ) */
1163 ADD_3V( light->_h_inf_norm, light->_VP_inf_norm, ctx->_EyeZDir);
1164 NORMALIZE_3FV( light->_h_inf_norm );
1165 }
1166 light->_VP_inf_spot_attenuation = 1.0;
1167 }
1168 else {
1169 /* positional light w/ homogeneous coordinate, divide by W */
1170 GLfloat wInv = (GLfloat)1.0 / light->_Position[3];
1171 light->_Position[0] *= wInv;
1172 light->_Position[1] *= wInv;
1173 light->_Position[2] *= wInv;
1174 }
1175
1176 if (light->_Flags & LIGHT_SPOT) {
1177 /* Note: we normalize the spot direction now */
1178
1179 if (ctx->_NeedEyeCoords) {
1180 COPY_3V( light->_NormSpotDirection, light->SpotDirection );
1181 NORMALIZE_3FV( light->_NormSpotDirection );
1182 }
1183 else {
1184 GLfloat spotDir[3];
1185 COPY_3V(spotDir, light->SpotDirection);
1186 NORMALIZE_3FV(spotDir);
1187 TRANSFORM_NORMAL( light->_NormSpotDirection,
1188 spotDir,
1189 ctx->ModelviewMatrixStack.Top->m);
1190 }
1191
1192 NORMALIZE_3FV( light->_NormSpotDirection );
1193
1194 if (!(light->_Flags & LIGHT_POSITIONAL)) {
1195 GLfloat PV_dot_dir = - DOT3(light->_VP_inf_norm,
1196 light->_NormSpotDirection);
1197
1198 if (PV_dot_dir > light->_CosCutoff) {
1199 double x = PV_dot_dir * (EXP_TABLE_SIZE-1);
1200 int k = (int) x;
1201 light->_VP_inf_spot_attenuation =
1202 (GLfloat) (light->_SpotExpTable[k][0] +
1203 (x-k)*light->_SpotExpTable[k][1]);
1204 }
1205 else {
1206 light->_VP_inf_spot_attenuation = 0;
1207 }
1208 }
1209 }
1210 }
1211 }
1212
1213
1214
1215 static void
1216 update_modelview_scale( GLcontext *ctx )
1217 {
1218 ctx->_ModelViewInvScale = 1.0F;
1219 if (!_math_matrix_is_length_preserving(ctx->ModelviewMatrixStack.Top)) {
1220 const GLfloat *m = ctx->ModelviewMatrixStack.Top->inv;
1221 GLfloat f = m[2] * m[2] + m[6] * m[6] + m[10] * m[10];
1222 if (f < 1e-12) f = 1.0;
1223 if (ctx->_NeedEyeCoords)
1224 ctx->_ModelViewInvScale = (GLfloat) INV_SQRTF(f);
1225 else
1226 ctx->_ModelViewInvScale = (GLfloat) SQRTF(f);
1227 }
1228 }
1229
1230
1231 /**
1232 * Bring up to date any state that relies on _NeedEyeCoords.
1233 */
1234 void
1235 _mesa_update_tnl_spaces( GLcontext *ctx, GLuint new_state )
1236 {
1237 const GLuint oldneedeyecoords = ctx->_NeedEyeCoords;
1238
1239 (void) new_state;
1240 ctx->_NeedEyeCoords = GL_FALSE;
1241
1242 if (ctx->_ForceEyeCoords ||
1243 (ctx->Texture._GenFlags & TEXGEN_NEED_EYE_COORD) ||
1244 ctx->Point._Attenuated ||
1245 ctx->Light._NeedEyeCoords)
1246 ctx->_NeedEyeCoords = GL_TRUE;
1247
1248 if (ctx->Light.Enabled &&
1249 !_math_matrix_is_length_preserving(ctx->ModelviewMatrixStack.Top))
1250 ctx->_NeedEyeCoords = GL_TRUE;
1251
1252 /* Check if the truth-value interpretations of the bitfields have
1253 * changed:
1254 */
1255 if (oldneedeyecoords != ctx->_NeedEyeCoords) {
1256 /* Recalculate all state that depends on _NeedEyeCoords.
1257 */
1258 update_modelview_scale(ctx);
1259 compute_light_positions( ctx );
1260
1261 if (ctx->Driver.LightingSpaceChange)
1262 ctx->Driver.LightingSpaceChange( ctx );
1263 }
1264 else {
1265 GLuint new_state2 = ctx->NewState;
1266
1267 /* Recalculate that same state only if it has been invalidated
1268 * by other statechanges.
1269 */
1270 if (new_state2 & _NEW_MODELVIEW)
1271 update_modelview_scale(ctx);
1272
1273 if (new_state2 & (_NEW_LIGHT|_NEW_MODELVIEW))
1274 compute_light_positions( ctx );
1275 }
1276 }
1277
1278
1279 /**
1280 * Drivers may need this if the hardware tnl unit doesn't support the
1281 * light-in-modelspace optimization. It's also useful for debugging.
1282 */
1283 void
1284 _mesa_allow_light_in_model( GLcontext *ctx, GLboolean flag )
1285 {
1286 ctx->_ForceEyeCoords = !flag;
1287 ctx->NewState |= _NEW_POINT; /* one of the bits from
1288 * _MESA_NEW_NEED_EYE_COORDS.
1289 */
1290 }
1291
1292
1293
1294 /**********************************************************************/
1295 /***** Initialization *****/
1296 /**********************************************************************/
1297
1298 /**
1299 * Initialize the n-th light data structure.
1300 *
1301 * \param l pointer to the gl_light structure to be initialized.
1302 * \param n number of the light.
1303 * \note The defaults for light 0 are different than the other lights.
1304 */
1305 static void
1306 init_light( struct gl_light *l, GLuint n )
1307 {
1308 make_empty_list( l );
1309
1310 ASSIGN_4V( l->Ambient, 0.0, 0.0, 0.0, 1.0 );
1311 if (n==0) {
1312 ASSIGN_4V( l->Diffuse, 1.0, 1.0, 1.0, 1.0 );
1313 ASSIGN_4V( l->Specular, 1.0, 1.0, 1.0, 1.0 );
1314 }
1315 else {
1316 ASSIGN_4V( l->Diffuse, 0.0, 0.0, 0.0, 1.0 );
1317 ASSIGN_4V( l->Specular, 0.0, 0.0, 0.0, 1.0 );
1318 }
1319 ASSIGN_4V( l->EyePosition, 0.0, 0.0, 1.0, 0.0 );
1320 ASSIGN_3V( l->SpotDirection, 0.0, 0.0, -1.0 );
1321 l->SpotExponent = 0.0;
1322 _mesa_invalidate_spot_exp_table( l );
1323 l->SpotCutoff = 180.0;
1324 l->_CosCutoffNeg = -1.0f;
1325 l->_CosCutoff = 0.0; /* KW: -ve values not admitted */
1326 l->ConstantAttenuation = 1.0;
1327 l->LinearAttenuation = 0.0;
1328 l->QuadraticAttenuation = 0.0;
1329 l->Enabled = GL_FALSE;
1330 }
1331
1332
1333 /**
1334 * Initialize the light model data structure.
1335 *
1336 * \param lm pointer to the gl_lightmodel structure to be initialized.
1337 */
1338 static void
1339 init_lightmodel( struct gl_lightmodel *lm )
1340 {
1341 ASSIGN_4V( lm->Ambient, 0.2F, 0.2F, 0.2F, 1.0F );
1342 lm->LocalViewer = GL_FALSE;
1343 lm->TwoSide = GL_FALSE;
1344 lm->ColorControl = GL_SINGLE_COLOR;
1345 }
1346
1347
1348 /**
1349 * Initialize the material data structure.
1350 *
1351 * \param m pointer to the gl_material structure to be initialized.
1352 */
1353 static void
1354 init_material( struct gl_material *m )
1355 {
1356 ASSIGN_4V( m->Attrib[MAT_ATTRIB_FRONT_AMBIENT], 0.2F, 0.2F, 0.2F, 1.0F );
1357 ASSIGN_4V( m->Attrib[MAT_ATTRIB_FRONT_DIFFUSE], 0.8F, 0.8F, 0.8F, 1.0F );
1358 ASSIGN_4V( m->Attrib[MAT_ATTRIB_FRONT_SPECULAR], 0.0F, 0.0F, 0.0F, 1.0F );
1359 ASSIGN_4V( m->Attrib[MAT_ATTRIB_FRONT_EMISSION], 0.0F, 0.0F, 0.0F, 1.0F );
1360 ASSIGN_4V( m->Attrib[MAT_ATTRIB_FRONT_SHININESS], 0.0F, 0.0F, 0.0F, 0.0F );
1361 ASSIGN_4V( m->Attrib[MAT_ATTRIB_FRONT_INDEXES], 0.0F, 1.0F, 1.0F, 0.0F );
1362
1363 ASSIGN_4V( m->Attrib[MAT_ATTRIB_BACK_AMBIENT], 0.2F, 0.2F, 0.2F, 1.0F );
1364 ASSIGN_4V( m->Attrib[MAT_ATTRIB_BACK_DIFFUSE], 0.8F, 0.8F, 0.8F, 1.0F );
1365 ASSIGN_4V( m->Attrib[MAT_ATTRIB_BACK_SPECULAR], 0.0F, 0.0F, 0.0F, 1.0F );
1366 ASSIGN_4V( m->Attrib[MAT_ATTRIB_BACK_EMISSION], 0.0F, 0.0F, 0.0F, 1.0F );
1367 ASSIGN_4V( m->Attrib[MAT_ATTRIB_BACK_SHININESS], 0.0F, 0.0F, 0.0F, 0.0F );
1368 ASSIGN_4V( m->Attrib[MAT_ATTRIB_BACK_INDEXES], 0.0F, 1.0F, 1.0F, 0.0F );
1369 }
1370
1371
1372 /**
1373 * Initialize all lighting state for the given context.
1374 */
1375 void
1376 _mesa_init_lighting( GLcontext *ctx )
1377 {
1378 GLuint i;
1379
1380 /* Lighting group */
1381 for (i = 0; i < MAX_LIGHTS; i++) {
1382 init_light( &ctx->Light.Light[i], i );
1383 }
1384 make_empty_list( &ctx->Light.EnabledList );
1385
1386 init_lightmodel( &ctx->Light.Model );
1387 init_material( &ctx->Light.Material );
1388 ctx->Light.ShadeModel = GL_SMOOTH;
1389 ctx->Light.ProvokingVertex = GL_LAST_VERTEX_CONVENTION_EXT;
1390 ctx->Light.Enabled = GL_FALSE;
1391 ctx->Light.ColorMaterialFace = GL_FRONT_AND_BACK;
1392 ctx->Light.ColorMaterialMode = GL_AMBIENT_AND_DIFFUSE;
1393 ctx->Light.ColorMaterialBitmask = _mesa_material_bitmask( ctx,
1394 GL_FRONT_AND_BACK,
1395 GL_AMBIENT_AND_DIFFUSE, ~0,
1396 NULL );
1397
1398 ctx->Light.ColorMaterialEnabled = GL_FALSE;
1399 ctx->Light.ClampVertexColor = GL_TRUE;
1400
1401 /* Lighting miscellaneous */
1402 ctx->_ShineTabList = MALLOC_STRUCT( gl_shine_tab );
1403 make_empty_list( ctx->_ShineTabList );
1404 /* Allocate 10 (arbitrary) shininess lookup tables */
1405 for (i = 0 ; i < 10 ; i++) {
1406 struct gl_shine_tab *s = MALLOC_STRUCT( gl_shine_tab );
1407 s->shininess = -1;
1408 s->refcount = 0;
1409 insert_at_tail( ctx->_ShineTabList, s );
1410 }
1411
1412 /* Miscellaneous */
1413 ctx->Light._NeedEyeCoords = GL_FALSE;
1414 ctx->_NeedEyeCoords = GL_FALSE;
1415 ctx->_ForceEyeCoords = GL_FALSE;
1416 ctx->_ModelViewInvScale = 1.0;
1417 }
1418
1419
1420 /**
1421 * Deallocate malloc'd lighting state attached to given context.
1422 */
1423 void
1424 _mesa_free_lighting_data( GLcontext *ctx )
1425 {
1426 struct gl_shine_tab *s, *tmps;
1427
1428 /* Free lighting shininess exponentiation table */
1429 foreach_s( s, tmps, ctx->_ShineTabList ) {
1430 _mesa_free( s );
1431 }
1432 _mesa_free( ctx->_ShineTabList );
1433 }