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