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