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