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