fixed bug in gl_validate_all_lighting_tables() found with CI mode conformance tests
[mesa.git] / src / mesa / main / light.c
1 /* $Id: light.c,v 1.36 2001/02/13 23:55:30 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999-2000 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 fprintf(stderr, "glShadeModel %s\n", gl_lookup_enum_by_nr(mode));
61
62 if (mode != GL_FLAT && mode != GL_SMOOTH) {
63 gl_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 >= ctx->Const.MaxLights) {
94 gl_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->ModelView.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->ModelView.flags & MAT_DIRTY_INVERSE) {
135 _math_matrix_analyse( &ctx->ModelView );
136 }
137 TRANSFORM_NORMAL( tmp, params, ctx->ModelView.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 gl_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 gl_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 gl_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 = 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 gl_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 gl_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 gl_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 gl_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 >= ctx->Const.MaxLights) {
269 gl_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 gl_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 >= ctx->Const.MaxLights) {
320 gl_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 gl_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 break;
411 case GL_LIGHT_MODEL_COLOR_CONTROL:
412 if (params[0] == (GLfloat) GL_SINGLE_COLOR)
413 newenum = GL_SINGLE_COLOR;
414 else if (params[0] == (GLfloat) GL_SEPARATE_SPECULAR_COLOR)
415 newenum = GL_SEPARATE_SPECULAR_COLOR;
416 else {
417 gl_error( ctx, GL_INVALID_ENUM, "glLightModel(param)" );
418 return;
419 }
420 if (ctx->Light.Model.ColorControl == newenum)
421 return;
422 FLUSH_VERTICES(ctx, _NEW_LIGHT);
423 ctx->Light.Model.ColorControl = newenum;
424 ctx->_TriangleCaps ^= DD_SEPERATE_SPECULAR;
425 break;
426 default:
427 gl_error( ctx, GL_INVALID_ENUM, "glLightModel" );
428 break;
429 }
430
431 if (ctx->Driver.LightModelfv)
432 ctx->Driver.LightModelfv( ctx, pname, params );
433 }
434
435
436 void
437 _mesa_LightModeliv( GLenum pname, const GLint *params )
438 {
439 GLfloat fparam[4];
440
441 switch (pname) {
442 case GL_LIGHT_MODEL_AMBIENT:
443 fparam[0] = INT_TO_FLOAT( params[0] );
444 fparam[1] = INT_TO_FLOAT( params[1] );
445 fparam[2] = INT_TO_FLOAT( params[2] );
446 fparam[3] = INT_TO_FLOAT( params[3] );
447 break;
448 case GL_LIGHT_MODEL_LOCAL_VIEWER:
449 case GL_LIGHT_MODEL_TWO_SIDE:
450 case GL_LIGHT_MODEL_COLOR_CONTROL:
451 fparam[0] = (GLfloat) params[0];
452 break;
453 default:
454 /* Error will be caught later in gl_LightModelfv */
455 ;
456 }
457 _mesa_LightModelfv( pname, fparam );
458 }
459
460
461 void
462 _mesa_LightModeli( GLenum pname, GLint param )
463 {
464 _mesa_LightModeliv( pname, &param );
465 }
466
467
468 void
469 _mesa_LightModelf( GLenum pname, GLfloat param )
470 {
471 _mesa_LightModelfv( pname, &param );
472 }
473
474
475
476 /********** MATERIAL **********/
477
478
479 /*
480 * Given a face and pname value (ala glColorMaterial), compute a bitmask
481 * of the targeted material values.
482 */
483 GLuint gl_material_bitmask( GLcontext *ctx, GLenum face, GLenum pname,
484 GLuint legal,
485 const char *where )
486 {
487 GLuint bitmask = 0;
488
489 /* Make a bitmask indicating what material attribute(s) we're updating */
490 switch (pname) {
491 case GL_EMISSION:
492 bitmask |= FRONT_EMISSION_BIT | BACK_EMISSION_BIT;
493 break;
494 case GL_AMBIENT:
495 bitmask |= FRONT_AMBIENT_BIT | BACK_AMBIENT_BIT;
496 break;
497 case GL_DIFFUSE:
498 bitmask |= FRONT_DIFFUSE_BIT | BACK_DIFFUSE_BIT;
499 break;
500 case GL_SPECULAR:
501 bitmask |= FRONT_SPECULAR_BIT | BACK_SPECULAR_BIT;
502 break;
503 case GL_SHININESS:
504 bitmask |= FRONT_SHININESS_BIT | BACK_SHININESS_BIT;
505 break;
506 case GL_AMBIENT_AND_DIFFUSE:
507 bitmask |= FRONT_AMBIENT_BIT | BACK_AMBIENT_BIT;
508 bitmask |= FRONT_DIFFUSE_BIT | BACK_DIFFUSE_BIT;
509 break;
510 case GL_COLOR_INDEXES:
511 bitmask |= FRONT_INDEXES_BIT | BACK_INDEXES_BIT;
512 break;
513 default:
514 gl_error( ctx, GL_INVALID_ENUM, where );
515 return 0;
516 }
517
518 if (face==GL_FRONT) {
519 bitmask &= FRONT_MATERIAL_BITS;
520 }
521 else if (face==GL_BACK) {
522 bitmask &= BACK_MATERIAL_BITS;
523 }
524 else if (face != GL_FRONT_AND_BACK) {
525 gl_error( ctx, GL_INVALID_ENUM, where );
526 return 0;
527 }
528
529 if (bitmask & ~legal) {
530 gl_error( ctx, GL_INVALID_ENUM, where );
531 return 0;
532 }
533
534 return bitmask;
535 }
536
537
538
539 /*
540 * Check if the global material has to be updated with info that was
541 * associated with a vertex via glMaterial.
542 * This function is used when any material values get changed between
543 * glBegin/glEnd either by calling glMaterial() or by calling glColor()
544 * when GL_COLOR_MATERIAL is enabled.
545 *
546 * src[0] is front material, src[1] is back material
547 *
548 * KW: Added code here to keep the precomputed variables uptodate.
549 * This means we can use the faster shade functions when using
550 * GL_COLOR_MATERIAL, and we can also now use the precomputed
551 * values in the slower shading functions, which further offsets
552 * the cost of doing this here.
553 */
554 void gl_update_material( GLcontext *ctx,
555 const struct gl_material src[2],
556 GLuint bitmask )
557 {
558 struct gl_light *light, *list = &ctx->Light.EnabledList;
559
560 if (ctx->Light.ColorMaterialEnabled)
561 bitmask &= ~ctx->Light.ColorMaterialBitmask;
562
563 if (MESA_VERBOSE&VERBOSE_IMMEDIATE)
564 fprintf(stderr, "gl_update_material, mask 0x%x\n", bitmask);
565
566 if (!bitmask)
567 return;
568
569 /* update material emission */
570 if (bitmask & FRONT_EMISSION_BIT) {
571 struct gl_material *mat = &ctx->Light.Material[0];
572 COPY_4FV( mat->Emission, src[0].Emission );
573 }
574 if (bitmask & BACK_EMISSION_BIT) {
575 struct gl_material *mat = &ctx->Light.Material[1];
576 COPY_4FV( mat->Emission, src[1].Emission );
577 }
578
579 /* update material ambience */
580 if (bitmask & FRONT_AMBIENT_BIT) {
581 struct gl_material *mat = &ctx->Light.Material[0];
582 COPY_4FV( mat->Ambient, src[0].Ambient );
583 foreach (light, list) {
584 SCALE_3V( light->_MatAmbient[0], light->Ambient, src[0].Ambient);
585 }
586 }
587 if (bitmask & BACK_AMBIENT_BIT) {
588 struct gl_material *mat = &ctx->Light.Material[1];
589 COPY_4FV( mat->Ambient, src[1].Ambient );
590 foreach (light, list) {
591 SCALE_3V( light->_MatAmbient[1], light->Ambient, src[1].Ambient);
592 }
593 }
594
595 /* update BaseColor = emission + scene's ambience * material's ambience */
596 if (bitmask & (FRONT_EMISSION_BIT | FRONT_AMBIENT_BIT)) {
597 struct gl_material *mat = &ctx->Light.Material[0];
598 COPY_3V( ctx->Light._BaseColor[0], mat->Emission );
599 ACC_SCALE_3V( ctx->Light._BaseColor[0], mat->Ambient,
600 ctx->Light.Model.Ambient );
601 }
602 if (bitmask & (BACK_EMISSION_BIT | BACK_AMBIENT_BIT)) {
603 struct gl_material *mat = &ctx->Light.Material[1];
604 COPY_3V( ctx->Light._BaseColor[1], mat->Emission );
605 ACC_SCALE_3V( ctx->Light._BaseColor[1], mat->Ambient,
606 ctx->Light.Model.Ambient );
607 }
608
609 /* update material diffuse values */
610 if (bitmask & FRONT_DIFFUSE_BIT) {
611 struct gl_material *mat = &ctx->Light.Material[0];
612 COPY_4FV( mat->Diffuse, src[0].Diffuse );
613 /* fprintf(stderr, "FRONT_DIFFUSE %f %f %f %f\n", */
614 /* mat->Diffuse[0], mat->Diffuse[1], */
615 /* mat->Diffuse[2], mat->Diffuse[3]); */
616 foreach (light, list) {
617 SCALE_3V( light->_MatDiffuse[0], light->Diffuse, mat->Diffuse );
618 }
619 UNCLAMPED_FLOAT_TO_CHAN(ctx->Light._BaseAlpha[0], mat->Diffuse[3]);
620 }
621 if (bitmask & BACK_DIFFUSE_BIT) {
622 struct gl_material *mat = &ctx->Light.Material[1];
623 COPY_4FV( mat->Diffuse, src[1].Diffuse );
624 /* fprintf(stderr, "BACK_DIFFUSE %f %f %f %f\n", */
625 /* mat->Diffuse[0], mat->Diffuse[1], */
626 /* mat->Diffuse[2], mat->Diffuse[3]); */
627 foreach (light, list) {
628 SCALE_3V( light->_MatDiffuse[1], light->Diffuse, mat->Diffuse );
629 }
630 UNCLAMPED_FLOAT_TO_CHAN(ctx->Light._BaseAlpha[1], mat->Diffuse[3]);
631 }
632
633 /* update material specular values */
634 if (bitmask & FRONT_SPECULAR_BIT) {
635 struct gl_material *mat = &ctx->Light.Material[0];
636 COPY_4FV( mat->Specular, src[0].Specular );
637 foreach (light, list) {
638 SCALE_3V( light->_MatSpecular[0], light->Specular, mat->Specular);
639 }
640 }
641 if (bitmask & BACK_SPECULAR_BIT) {
642 struct gl_material *mat = &ctx->Light.Material[1];
643 COPY_4FV( mat->Specular, src[1].Specular );
644 foreach (light, list) {
645 SCALE_3V( light->_MatSpecular[1], light->Specular, mat->Specular);
646 }
647 }
648
649 if (bitmask & FRONT_SHININESS_BIT) {
650 /* fprintf(stderr, "FRONT_SHININESS_BIT %f\n", src[0].Shininess); */
651 ctx->Light.Material[0].Shininess = src[0].Shininess;
652 gl_invalidate_shine_table( ctx, 0 );
653 }
654 if (bitmask & BACK_SHININESS_BIT) {
655 ctx->Light.Material[1].Shininess = src[1].Shininess;
656 gl_invalidate_shine_table( ctx, 1 );
657 }
658
659 if (bitmask & FRONT_INDEXES_BIT) {
660 ctx->Light.Material[0].AmbientIndex = src[0].AmbientIndex;
661 ctx->Light.Material[0].DiffuseIndex = src[0].DiffuseIndex;
662 ctx->Light.Material[0].SpecularIndex = src[0].SpecularIndex;
663 }
664 if (bitmask & BACK_INDEXES_BIT) {
665 ctx->Light.Material[1].AmbientIndex = src[1].AmbientIndex;
666 ctx->Light.Material[1].DiffuseIndex = src[1].DiffuseIndex;
667 ctx->Light.Material[1].SpecularIndex = src[1].SpecularIndex;
668 }
669
670 if (0)
671 {
672 struct gl_material *mat = &ctx->Light.Material[0];
673 fprintf(stderr, "update_mat emission : %f %f %f\n",
674 mat->Emission[0],
675 mat->Emission[1],
676 mat->Emission[2]);
677 fprintf(stderr, "update_mat specular : %f %f %f\n",
678 mat->Specular[0],
679 mat->Specular[1],
680 mat->Specular[2]);
681 fprintf(stderr, "update_mat diffuse : %f %f %f\n",
682 mat->Diffuse[0],
683 mat->Diffuse[1],
684 mat->Diffuse[2]);
685 fprintf(stderr, "update_mat ambient : %f %f %f\n",
686 mat->Ambient[0],
687 mat->Ambient[1],
688 mat->Ambient[2]);
689 }
690 }
691
692
693
694
695 /*
696 * Update the current materials from the given rgba color
697 * according to the bitmask in ColorMaterialBitmask, which is
698 * set by glColorMaterial().
699 */
700 void gl_update_color_material( GLcontext *ctx,
701 const GLchan rgba[4] )
702 {
703 struct gl_light *light, *list = &ctx->Light.EnabledList;
704 GLuint bitmask = ctx->Light.ColorMaterialBitmask;
705 GLfloat color[4];
706
707 color[0] = CHAN_TO_FLOAT(rgba[0]);
708 color[1] = CHAN_TO_FLOAT(rgba[1]);
709 color[2] = CHAN_TO_FLOAT(rgba[2]);
710 color[3] = CHAN_TO_FLOAT(rgba[3]);
711
712 if (MESA_VERBOSE&VERBOSE_IMMEDIATE)
713 fprintf(stderr, "gl_update_color_material, mask 0x%x\n", bitmask);
714
715 /* update emissive colors */
716 if (bitmask & FRONT_EMISSION_BIT) {
717 struct gl_material *mat = &ctx->Light.Material[0];
718 COPY_4FV( mat->Emission, color );
719 }
720
721 if (bitmask & BACK_EMISSION_BIT) {
722 struct gl_material *mat = &ctx->Light.Material[1];
723 COPY_4FV( mat->Emission, color );
724 }
725
726 /* update light->_MatAmbient = light's ambient * material's ambient */
727 if (bitmask & FRONT_AMBIENT_BIT) {
728 struct gl_material *mat = &ctx->Light.Material[0];
729 foreach (light, list) {
730 SCALE_3V( light->_MatAmbient[0], light->Ambient, color);
731 }
732 COPY_4FV( mat->Ambient, color );
733 }
734
735 if (bitmask & BACK_AMBIENT_BIT) {
736 struct gl_material *mat = &ctx->Light.Material[1];
737 foreach (light, list) {
738 SCALE_3V( light->_MatAmbient[1], light->Ambient, color);
739 }
740 COPY_4FV( mat->Ambient, color );
741 }
742
743 /* update BaseColor = emission + scene's ambience * material's ambience */
744 if (bitmask & (FRONT_EMISSION_BIT | FRONT_AMBIENT_BIT)) {
745 struct gl_material *mat = &ctx->Light.Material[0];
746 COPY_3V( ctx->Light._BaseColor[0], mat->Emission );
747 ACC_SCALE_3V( ctx->Light._BaseColor[0], mat->Ambient, ctx->Light.Model.Ambient );
748 }
749
750 if (bitmask & (BACK_EMISSION_BIT | BACK_AMBIENT_BIT)) {
751 struct gl_material *mat = &ctx->Light.Material[1];
752 COPY_3V( ctx->Light._BaseColor[1], mat->Emission );
753 ACC_SCALE_3V( ctx->Light._BaseColor[1], mat->Ambient, ctx->Light.Model.Ambient );
754 }
755
756 /* update light->_MatDiffuse = light's diffuse * material's diffuse */
757 if (bitmask & FRONT_DIFFUSE_BIT) {
758 struct gl_material *mat = &ctx->Light.Material[0];
759 COPY_4FV( mat->Diffuse, color );
760 foreach (light, list) {
761 SCALE_3V( light->_MatDiffuse[0], light->Diffuse, mat->Diffuse );
762 }
763 UNCLAMPED_FLOAT_TO_CHAN(ctx->Light._BaseAlpha[0], mat->Diffuse[3]);
764 }
765
766 if (bitmask & BACK_DIFFUSE_BIT) {
767 struct gl_material *mat = &ctx->Light.Material[1];
768 COPY_4FV( mat->Diffuse, color );
769 foreach (light, list) {
770 SCALE_3V( light->_MatDiffuse[1], light->Diffuse, mat->Diffuse );
771 }
772 UNCLAMPED_FLOAT_TO_CHAN(ctx->Light._BaseAlpha[1], mat->Diffuse[3]);
773 }
774
775 /* update light->_MatSpecular = light's specular * material's specular */
776 if (bitmask & FRONT_SPECULAR_BIT) {
777 struct gl_material *mat = &ctx->Light.Material[0];
778 COPY_4FV( mat->Specular, color );
779 foreach (light, list) {
780 ACC_SCALE_3V( light->_MatSpecular[0], light->Specular, mat->Specular);
781 }
782 }
783
784 if (bitmask & BACK_SPECULAR_BIT) {
785 struct gl_material *mat = &ctx->Light.Material[1];
786 COPY_4FV( mat->Specular, color );
787 foreach (light, list) {
788 ACC_SCALE_3V( light->_MatSpecular[1], light->Specular, mat->Specular);
789 }
790 }
791
792 if (0)
793 {
794 struct gl_material *mat = &ctx->Light.Material[0];
795 fprintf(stderr, "update_color_mat emission : %f %f %f\n",
796 mat->Emission[0],
797 mat->Emission[1],
798 mat->Emission[2]);
799 fprintf(stderr, "update_color_mat specular : %f %f %f\n",
800 mat->Specular[0],
801 mat->Specular[1],
802 mat->Specular[2]);
803 fprintf(stderr, "update_color_mat diffuse : %f %f %f\n",
804 mat->Diffuse[0],
805 mat->Diffuse[1],
806 mat->Diffuse[2]);
807 fprintf(stderr, "update_color_mat ambient : %f %f %f\n",
808 mat->Ambient[0],
809 mat->Ambient[1],
810 mat->Ambient[2]);
811 }
812 }
813
814
815
816
817 void
818 _mesa_ColorMaterial( GLenum face, GLenum mode )
819 {
820 GET_CURRENT_CONTEXT(ctx);
821 GLuint bitmask;
822 GLuint legal = (FRONT_EMISSION_BIT | BACK_EMISSION_BIT |
823 FRONT_SPECULAR_BIT | BACK_SPECULAR_BIT |
824 FRONT_DIFFUSE_BIT | BACK_DIFFUSE_BIT |
825 FRONT_AMBIENT_BIT | BACK_AMBIENT_BIT);
826 ASSERT_OUTSIDE_BEGIN_END(ctx);
827
828 if (MESA_VERBOSE&VERBOSE_API)
829 fprintf(stderr, "glColorMaterial %s %s\n",
830 gl_lookup_enum_by_nr(face),
831 gl_lookup_enum_by_nr(mode));
832
833 bitmask = gl_material_bitmask( ctx, face, mode, legal, "glColorMaterial" );
834
835 if (ctx->Light.ColorMaterialBitmask == bitmask &&
836 ctx->Light.ColorMaterialFace == face &&
837 ctx->Light.ColorMaterialMode == mode)
838 return;
839
840 FLUSH_VERTICES(ctx, _NEW_LIGHT);
841 ctx->Light.ColorMaterialBitmask = bitmask;
842 ctx->Light.ColorMaterialFace = face;
843 ctx->Light.ColorMaterialMode = mode;
844
845 if (ctx->Light.ColorMaterialEnabled) {
846 FLUSH_CURRENT( ctx, 0 );
847 gl_update_color_material( ctx, ctx->Current.Color );
848 }
849 }
850
851
852
853
854
855 void
856 _mesa_GetMaterialfv( GLenum face, GLenum pname, GLfloat *params )
857 {
858 GET_CURRENT_CONTEXT(ctx);
859 GLuint f;
860 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); /* update materials */
861
862 if (face==GL_FRONT) {
863 f = 0;
864 }
865 else if (face==GL_BACK) {
866 f = 1;
867 }
868 else {
869 gl_error( ctx, GL_INVALID_ENUM, "glGetMaterialfv(face)" );
870 return;
871 }
872 switch (pname) {
873 case GL_AMBIENT:
874 COPY_4FV( params, ctx->Light.Material[f].Ambient );
875 break;
876 case GL_DIFFUSE:
877 COPY_4FV( params, ctx->Light.Material[f].Diffuse );
878 break;
879 case GL_SPECULAR:
880 COPY_4FV( params, ctx->Light.Material[f].Specular );
881 break;
882 case GL_EMISSION:
883 COPY_4FV( params, ctx->Light.Material[f].Emission );
884 break;
885 case GL_SHININESS:
886 *params = ctx->Light.Material[f].Shininess;
887 break;
888 case GL_COLOR_INDEXES:
889 params[0] = ctx->Light.Material[f].AmbientIndex;
890 params[1] = ctx->Light.Material[f].DiffuseIndex;
891 params[2] = ctx->Light.Material[f].SpecularIndex;
892 break;
893 default:
894 gl_error( ctx, GL_INVALID_ENUM, "glGetMaterialfv(pname)" );
895 }
896 }
897
898
899
900 void
901 _mesa_GetMaterialiv( GLenum face, GLenum pname, GLint *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 gl_error( ctx, GL_INVALID_ENUM, "glGetMaterialiv(face)" );
915 return;
916 }
917 switch (pname) {
918 case GL_AMBIENT:
919 params[0] = FLOAT_TO_INT( ctx->Light.Material[f].Ambient[0] );
920 params[1] = FLOAT_TO_INT( ctx->Light.Material[f].Ambient[1] );
921 params[2] = FLOAT_TO_INT( ctx->Light.Material[f].Ambient[2] );
922 params[3] = FLOAT_TO_INT( ctx->Light.Material[f].Ambient[3] );
923 break;
924 case GL_DIFFUSE:
925 params[0] = FLOAT_TO_INT( ctx->Light.Material[f].Diffuse[0] );
926 params[1] = FLOAT_TO_INT( ctx->Light.Material[f].Diffuse[1] );
927 params[2] = FLOAT_TO_INT( ctx->Light.Material[f].Diffuse[2] );
928 params[3] = FLOAT_TO_INT( ctx->Light.Material[f].Diffuse[3] );
929 break;
930 case GL_SPECULAR:
931 params[0] = FLOAT_TO_INT( ctx->Light.Material[f].Specular[0] );
932 params[1] = FLOAT_TO_INT( ctx->Light.Material[f].Specular[1] );
933 params[2] = FLOAT_TO_INT( ctx->Light.Material[f].Specular[2] );
934 params[3] = FLOAT_TO_INT( ctx->Light.Material[f].Specular[3] );
935 break;
936 case GL_EMISSION:
937 params[0] = FLOAT_TO_INT( ctx->Light.Material[f].Emission[0] );
938 params[1] = FLOAT_TO_INT( ctx->Light.Material[f].Emission[1] );
939 params[2] = FLOAT_TO_INT( ctx->Light.Material[f].Emission[2] );
940 params[3] = FLOAT_TO_INT( ctx->Light.Material[f].Emission[3] );
941 break;
942 case GL_SHININESS:
943 *params = ROUNDF( ctx->Light.Material[f].Shininess );
944 break;
945 case GL_COLOR_INDEXES:
946 params[0] = ROUNDF( ctx->Light.Material[f].AmbientIndex );
947 params[1] = ROUNDF( ctx->Light.Material[f].DiffuseIndex );
948 params[2] = ROUNDF( ctx->Light.Material[f].SpecularIndex );
949 break;
950 default:
951 gl_error( ctx, GL_INVALID_ENUM, "glGetMaterialfv(pname)" );
952 }
953 }
954
955
956
957
958 /**********************************************************************/
959 /***** Lighting computation *****/
960 /**********************************************************************/
961
962
963 /*
964 * Notes:
965 * When two-sided lighting is enabled we compute the color (or index)
966 * for both the front and back side of the primitive. Then, when the
967 * orientation of the facet is later learned, we can determine which
968 * color (or index) to use for rendering.
969 *
970 * KW: We now know orientation in advance and only shade for
971 * the side or sides which are actually required.
972 *
973 * Variables:
974 * n = normal vector
975 * V = vertex position
976 * P = light source position
977 * Pe = (0,0,0,1)
978 *
979 * Precomputed:
980 * IF P[3]==0 THEN
981 * // light at infinity
982 * IF local_viewer THEN
983 * _VP_inf_norm = unit vector from V to P // Precompute
984 * ELSE
985 * // eye at infinity
986 * _h_inf_norm = Normalize( VP + <0,0,1> ) // Precompute
987 * ENDIF
988 * ENDIF
989 *
990 * Functions:
991 * Normalize( v ) = normalized vector v
992 * Magnitude( v ) = length of vector v
993 */
994
995
996
997 /*
998 * Whenever the spotlight exponent for a light changes we must call
999 * this function to recompute the exponent lookup table.
1000 */
1001 void
1002 gl_invalidate_spot_exp_table( struct gl_light *l )
1003 {
1004 l->_SpotExpTable[0][0] = -1;
1005 }
1006
1007 static void validate_spot_exp_table( struct gl_light *l )
1008 {
1009 GLint i;
1010 GLdouble exponent = l->SpotExponent;
1011 GLdouble tmp = 0;
1012 GLint clamp = 0;
1013
1014 l->_SpotExpTable[0][0] = 0.0;
1015
1016 for (i = EXP_TABLE_SIZE - 1; i > 0 ;i--) {
1017 if (clamp == 0) {
1018 tmp = pow(i / (GLdouble) (EXP_TABLE_SIZE - 1), exponent);
1019 if (tmp < FLT_MIN * 100.0) {
1020 tmp = 0.0;
1021 clamp = 1;
1022 }
1023 }
1024 l->_SpotExpTable[i][0] = tmp;
1025 }
1026 for (i = 0; i < EXP_TABLE_SIZE - 1; i++) {
1027 l->_SpotExpTable[i][1] = (l->_SpotExpTable[i+1][0] -
1028 l->_SpotExpTable[i][0]);
1029 }
1030 l->_SpotExpTable[EXP_TABLE_SIZE-1][1] = 0.0;
1031 }
1032
1033
1034
1035
1036 /* Calculate a new shine table. Doing this here saves a branch in
1037 * lighting, and the cost of doing it early may be partially offset
1038 * by keeping a MRU cache of shine tables for various shine values.
1039 */
1040 void
1041 gl_invalidate_shine_table( GLcontext *ctx, GLuint i )
1042 {
1043 if (ctx->_ShineTable[i])
1044 ctx->_ShineTable[i]->refcount--;
1045 ctx->_ShineTable[i] = 0;
1046 }
1047
1048 static void validate_shine_table( GLcontext *ctx, GLuint i, GLfloat shininess )
1049 {
1050 struct gl_shine_tab *list = ctx->_ShineTabList;
1051 struct gl_shine_tab *s;
1052
1053 foreach(s, list)
1054 if ( s->shininess == shininess )
1055 break;
1056
1057 if (s == list) {
1058 GLint j;
1059 GLfloat *m;
1060
1061 foreach(s, list)
1062 if (s->refcount == 0)
1063 break;
1064
1065 m = s->tab;
1066 m[0] = 0.0;
1067 if (shininess == 0.0) {
1068 for (j = 1 ; j <= SHINE_TABLE_SIZE ; j++)
1069 m[j] = 1.0;
1070 }
1071 else {
1072 for (j = 1 ; j < SHINE_TABLE_SIZE ; j++) {
1073 GLdouble t, x = j / (GLfloat) (SHINE_TABLE_SIZE - 1);
1074 if (x < 0.005) /* underflow check */
1075 x = 0.005;
1076 t = pow(x, shininess);
1077 if (t > 1e-20)
1078 m[j] = t;
1079 else
1080 m[j] = 0.0;
1081 }
1082 m[SHINE_TABLE_SIZE] = 1.0;
1083 }
1084
1085 s->shininess = shininess;
1086 }
1087
1088 if (ctx->_ShineTable[i])
1089 ctx->_ShineTable[i]->refcount--;
1090
1091 ctx->_ShineTable[i] = s;
1092 move_to_tail( list, s );
1093 s->refcount++;
1094 }
1095
1096 void
1097 gl_validate_all_lighting_tables( GLcontext *ctx )
1098 {
1099 GLint i;
1100 GLfloat shininess;
1101
1102 shininess = ctx->Light.Material[0].Shininess;
1103 if (!ctx->_ShineTable[0] || ctx->_ShineTable[0]->shininess != shininess)
1104 validate_shine_table( ctx, 0, shininess );
1105
1106 shininess = ctx->Light.Material[1].Shininess;
1107 if (!ctx->_ShineTable[1] || ctx->_ShineTable[1]->shininess != shininess)
1108 validate_shine_table( ctx, 1, shininess );
1109
1110 for (i = 0 ; i < MAX_LIGHTS ; i++)
1111 if (ctx->Light.Light[i]._SpotExpTable[0][0] == -1)
1112 validate_spot_exp_table( &ctx->Light.Light[i] );
1113 }
1114
1115
1116
1117
1118 /*
1119 * Examine current lighting parameters to determine if the optimized lighting
1120 * function can be used.
1121 * Also, precompute some lighting values such as the products of light
1122 * source and material ambient, diffuse and specular coefficients.
1123 */
1124 void
1125 gl_update_lighting( GLcontext *ctx )
1126 {
1127 struct gl_light *light;
1128 ctx->_TriangleCaps &= ~DD_TRI_LIGHT_TWOSIDE;
1129 ctx->_NeedEyeCoords &= ~NEED_EYE_LIGHT;
1130 ctx->_NeedNormals &= ~NEED_NORMALS_LIGHT;
1131 ctx->Light._Flags = 0;
1132
1133 if (!ctx->Light.Enabled)
1134 return;
1135
1136 ctx->_NeedNormals |= NEED_NORMALS_LIGHT;
1137
1138 if (ctx->Light.Model.TwoSide)
1139 ctx->_TriangleCaps |= DD_TRI_LIGHT_TWOSIDE;
1140
1141 foreach(light, &ctx->Light.EnabledList) {
1142 ctx->Light._Flags |= light->_Flags;
1143 }
1144
1145 ctx->Light._NeedVertices =
1146 ((ctx->Light._Flags & (LIGHT_POSITIONAL|LIGHT_SPOT)) ||
1147 ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR ||
1148 ctx->Light.Model.LocalViewer);
1149
1150 if ((ctx->Light._Flags & LIGHT_POSITIONAL) ||
1151 ctx->Light.Model.LocalViewer)
1152 ctx->_NeedEyeCoords |= NEED_EYE_LIGHT;
1153
1154
1155 /* XXX: This test is overkill & needs to be fixed both for software and
1156 * hardware t&l drivers. The above should be sufficient & should
1157 * be tested to verify this.
1158 */
1159 if (ctx->Light._NeedVertices)
1160 ctx->_NeedEyeCoords |= NEED_EYE_LIGHT;
1161
1162
1163 /* Precompute some shading values. Although we reference
1164 * Light.Material here, we can get away without flushing
1165 * FLUSH_UPDATE_CURRENT, as when any outstanding material changes
1166 * are flushed, they will update the derived state at that time.
1167 */
1168 if (ctx->Visual.rgbMode) {
1169 GLuint sides = ctx->Light.Model.TwoSide ? 2 : 1;
1170 GLuint side;
1171 for (side=0; side < sides; side++) {
1172 struct gl_material *mat = &ctx->Light.Material[side];
1173
1174 COPY_3V(ctx->Light._BaseColor[side], mat->Emission);
1175 ACC_SCALE_3V(ctx->Light._BaseColor[side],
1176 ctx->Light.Model.Ambient,
1177 mat->Ambient);
1178
1179 UNCLAMPED_FLOAT_TO_CHAN(ctx->Light._BaseAlpha[side],
1180 ctx->Light.Material[side].Diffuse[3] );
1181 }
1182
1183 foreach (light, &ctx->Light.EnabledList) {
1184 for (side=0; side< sides; side++) {
1185 const struct gl_material *mat = &ctx->Light.Material[side];
1186 SCALE_3V( light->_MatDiffuse[side], light->Diffuse, mat->Diffuse );
1187 SCALE_3V( light->_MatAmbient[side], light->Ambient, mat->Ambient );
1188 SCALE_3V( light->_MatSpecular[side], light->Specular,
1189 mat->Specular);
1190 }
1191 }
1192 }
1193 else {
1194 static const GLfloat ci[3] = { .30, .59, .11 };
1195 foreach(light, &ctx->Light.EnabledList) {
1196 light->_dli = DOT3(ci, light->Diffuse);
1197 light->_sli = DOT3(ci, light->Specular);
1198 }
1199 }
1200 }
1201
1202
1203 /* _NEW_MODELVIEW
1204 * _NEW_LIGHT
1205 * _TNL_NEW_NEED_EYE_COORDS
1206 *
1207 * Update on (_NEW_MODELVIEW | _NEW_LIGHT) when lighting is enabled.
1208 * Also update on lighting space changes.
1209 */
1210 void
1211 gl_compute_light_positions( GLcontext *ctx )
1212 {
1213 struct gl_light *light;
1214 static const GLfloat eye_z[3] = { 0, 0, 1 };
1215
1216 if (!ctx->Light.Enabled)
1217 return;
1218
1219 if (ctx->_NeedEyeCoords) {
1220 COPY_3V( ctx->_EyeZDir, eye_z );
1221 }
1222 else {
1223 TRANSFORM_NORMAL( ctx->_EyeZDir, eye_z, ctx->ModelView.m );
1224 }
1225
1226 foreach (light, &ctx->Light.EnabledList) {
1227
1228 if (ctx->_NeedEyeCoords) {
1229 COPY_4FV( light->_Position, light->EyePosition );
1230 }
1231 else {
1232 TRANSFORM_POINT( light->_Position, ctx->ModelView.inv,
1233 light->EyePosition );
1234 }
1235
1236 if (!(light->_Flags & LIGHT_POSITIONAL)) {
1237 /* VP (VP) = Normalize( Position ) */
1238 COPY_3V( light->_VP_inf_norm, light->_Position );
1239 NORMALIZE_3FV( light->_VP_inf_norm );
1240
1241 if (!ctx->Light.Model.LocalViewer) {
1242 /* _h_inf_norm = Normalize( V_to_P + <0,0,1> ) */
1243 ADD_3V( light->_h_inf_norm, light->_VP_inf_norm, ctx->_EyeZDir);
1244 NORMALIZE_3FV( light->_h_inf_norm );
1245 }
1246 light->_VP_inf_spot_attenuation = 1.0;
1247 }
1248
1249 if (light->_Flags & LIGHT_SPOT) {
1250 if (ctx->_NeedEyeCoords) {
1251 COPY_3V( light->_NormDirection, light->EyeDirection );
1252 }
1253 else {
1254 TRANSFORM_NORMAL( light->_NormDirection,
1255 light->EyeDirection,
1256 ctx->ModelView.m);
1257 }
1258
1259 NORMALIZE_3FV( light->_NormDirection );
1260
1261 if (!(light->_Flags & LIGHT_POSITIONAL)) {
1262 GLfloat PV_dot_dir = - DOT3(light->_VP_inf_norm,
1263 light->_NormDirection);
1264
1265 if (PV_dot_dir > light->_CosCutoff) {
1266 double x = PV_dot_dir * (EXP_TABLE_SIZE-1);
1267 int k = (int) x;
1268 light->_VP_inf_spot_attenuation =
1269 (light->_SpotExpTable[k][0] +
1270 (x-k)*light->_SpotExpTable[k][1]);
1271 }
1272 else {
1273 light->_VP_inf_spot_attenuation = 0;
1274 }
1275 }
1276 }
1277 }
1278 }
1279
1280