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