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