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