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