blob: a7fce7e0f2c42fa78538df6d71f1fd5d4febc878 [file] [log] [blame]
Brian Paul08836342001-03-03 20:33:27 +00001/* $Id: light.c,v 1.39 2001/03/03 20:33:27 brianp Exp $ */
jtgafb833d1999-08-19 00:55:39 +00002
3/*
4 * Mesa 3-D graphics library
Brian Paulba643a22000-10-28 18:34:48 +00005 * Version: 3.5
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00006 *
Brian Paul16a9efe2000-01-13 00:29:02 +00007 * Copyright (C) 1999-2000 Brian Paul All Rights Reserved.
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00008 *
jtgafb833d1999-08-19 00:55:39 +00009 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +000015 *
jtgafb833d1999-08-19 00:55:39 +000016 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +000018 *
jtgafb833d1999-08-19 00:55:39 +000019 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27
jtgafb833d1999-08-19 00:55:39 +000028#ifdef PC_HEADER
29#include "all.h"
30#else
Brian Paulfbd8f211999-11-11 01:22:25 +000031#include "glheader.h"
Brian Paulc893a012000-10-28 20:41:13 +000032#include "colormac.h"
jtgafb833d1999-08-19 00:55:39 +000033#include "context.h"
34#include "enums.h"
35#include "light.h"
36#include "macros.h"
Brian Paulfbd8f211999-11-11 01:22:25 +000037#include "mem.h"
jtgafb833d1999-08-19 00:55:39 +000038#include "mmath.h"
39#include "simple_list.h"
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +000040#include "mtypes.h"
Keith Whitwell23caf202000-11-16 21:05:34 +000041
42#include "math/m_xform.h"
43#include "math/m_matrix.h"
jtgafb833d1999-08-19 00:55:39 +000044#endif
45
46
Brian Paul075398b2000-01-31 23:33:53 +000047/* XXX this is a bit of a hack needed for compilation within XFree86 */
48#ifndef FLT_MIN
49#define FLT_MIN 1e-37
50#endif
51
jtgafb833d1999-08-19 00:55:39 +000052
Brian Paulfbd8f211999-11-11 01:22:25 +000053void
54_mesa_ShadeModel( GLenum mode )
jtgafb833d1999-08-19 00:55:39 +000055{
Brian Paulfbd8f211999-11-11 01:22:25 +000056 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +000057 ASSERT_OUTSIDE_BEGIN_END(ctx);
jtgafb833d1999-08-19 00:55:39 +000058
59 if (MESA_VERBOSE & VERBOSE_API)
Brian Paul08836342001-03-03 20:33:27 +000060 fprintf(stderr, "glShadeModel %s\n", _mesa_lookup_enum_by_nr(mode));
jtgafb833d1999-08-19 00:55:39 +000061
Keith Whitwellcab974c2000-12-26 05:09:27 +000062 if (mode != GL_FLAT && mode != GL_SMOOTH) {
Brian Paul08836342001-03-03 20:33:27 +000063 _mesa_error( ctx, GL_INVALID_ENUM, "glShadeModel" );
Keith Whitwellcab974c2000-12-26 05:09:27 +000064 return;
jtgafb833d1999-08-19 00:55:39 +000065 }
Keith Whitwellcab974c2000-12-26 05:09:27 +000066
67 if (ctx->Light.ShadeModel == mode)
68 return;
69
70 FLUSH_VERTICES(ctx, _NEW_LIGHT);
71 ctx->Light.ShadeModel = mode;
72 ctx->_TriangleCaps ^= DD_FLATSHADE;
73 if (ctx->Driver.ShadeModel)
74 (*ctx->Driver.ShadeModel)( ctx, mode );
jtgafb833d1999-08-19 00:55:39 +000075}
76
77
78
Brian Paulfbd8f211999-11-11 01:22:25 +000079void
80_mesa_Lightf( GLenum light, GLenum pname, GLfloat param )
jtgafb833d1999-08-19 00:55:39 +000081{
Brian Paulfbd8f211999-11-11 01:22:25 +000082 _mesa_Lightfv( light, pname, &param );
83}
jtgafb833d1999-08-19 00:55:39 +000084
Brian Paulfbd8f211999-11-11 01:22:25 +000085
86void
87_mesa_Lightfv( GLenum light, GLenum pname, const GLfloat *params )
88{
89 GET_CURRENT_CONTEXT(ctx);
Keith Whitwell1e1aac02000-11-13 20:02:56 +000090 GLint i = (GLint) (light - GL_LIGHT0);
91 struct gl_light *l = &ctx->Light.Light[i];
jtgafb833d1999-08-19 00:55:39 +000092
Brian Paula8644322000-11-27 18:22:13 +000093 if (i < 0 || i >= ctx->Const.MaxLights) {
Brian Paul08836342001-03-03 20:33:27 +000094 _mesa_error( ctx, GL_INVALID_ENUM, "glLight" );
jtgafb833d1999-08-19 00:55:39 +000095 return;
96 }
97
98 switch (pname) {
Keith Whitwellcab974c2000-12-26 05:09:27 +000099 case GL_AMBIENT:
100 if (TEST_EQ_4V(l->Ambient, params))
101 return;
102 FLUSH_VERTICES(ctx, _NEW_LIGHT);
103 COPY_4V( l->Ambient, params );
104 break;
105 case GL_DIFFUSE:
106 if (TEST_EQ_4V(l->Diffuse, params))
107 return;
108 FLUSH_VERTICES(ctx, _NEW_LIGHT);
109 COPY_4V( l->Diffuse, params );
110 break;
111 case GL_SPECULAR:
112 if (TEST_EQ_4V(l->Specular, params))
113 return;
114 FLUSH_VERTICES(ctx, _NEW_LIGHT);
115 COPY_4V( l->Specular, params );
116 break;
117 case GL_POSITION: {
118 GLfloat tmp[4];
119 /* transform position by ModelView matrix */
120 TRANSFORM_POINT( tmp, ctx->ModelView.m, params );
121 if (TEST_EQ_4V(l->EyePosition, tmp))
122 return;
123 FLUSH_VERTICES(ctx, _NEW_LIGHT);
124 COPY_4V(l->EyePosition, tmp);
125 if (l->EyePosition[3] != 0.0F)
126 l->_Flags |= LIGHT_POSITIONAL;
127 else
128 l->_Flags &= ~LIGHT_POSITIONAL;
129 break;
130 }
131 case GL_SPOT_DIRECTION: {
132 GLfloat tmp[4];
133 /* transform direction by inverse modelview */
134 if (ctx->ModelView.flags & MAT_DIRTY_INVERSE) {
135 _math_matrix_analyse( &ctx->ModelView );
136 }
137 TRANSFORM_NORMAL( tmp, params, ctx->ModelView.inv );
138 if (TEST_EQ_3V(l->EyeDirection, tmp))
139 return;
140 FLUSH_VERTICES(ctx, _NEW_LIGHT);
141 COPY_3V(l->EyeDirection, tmp);
142 break;
143 }
144 case GL_SPOT_EXPONENT:
145 if (params[0]<0.0 || params[0]>128.0) {
Brian Paul08836342001-03-03 20:33:27 +0000146 _mesa_error( ctx, GL_INVALID_VALUE, "glLight" );
Keith Whitwellcab974c2000-12-26 05:09:27 +0000147 return;
148 }
149 if (l->SpotExponent == params[0])
150 return;
151 FLUSH_VERTICES(ctx, _NEW_LIGHT);
152 l->SpotExponent = params[0];
Brian Paul08836342001-03-03 20:33:27 +0000153 _mesa_invalidate_spot_exp_table( l );
Keith Whitwellcab974c2000-12-26 05:09:27 +0000154 break;
155 case GL_SPOT_CUTOFF:
156 if ((params[0]<0.0 || params[0]>90.0) && params[0]!=180.0) {
Brian Paul08836342001-03-03 20:33:27 +0000157 _mesa_error( ctx, GL_INVALID_VALUE, "glLight" );
Keith Whitwellcab974c2000-12-26 05:09:27 +0000158 return;
159 }
160 if (l->SpotCutoff == params[0])
161 return;
162 FLUSH_VERTICES(ctx, _NEW_LIGHT);
163 l->SpotCutoff = params[0];
164 l->_CosCutoff = cos(params[0]*DEG2RAD);
165 if (l->_CosCutoff < 0)
166 l->_CosCutoff = 0;
167 if (l->SpotCutoff != 180.0F)
168 l->_Flags |= LIGHT_SPOT;
169 else
170 l->_Flags &= ~LIGHT_SPOT;
171 break;
172 case GL_CONSTANT_ATTENUATION:
173 if (params[0]<0.0) {
Brian Paul08836342001-03-03 20:33:27 +0000174 _mesa_error( ctx, GL_INVALID_VALUE, "glLight" );
Keith Whitwellcab974c2000-12-26 05:09:27 +0000175 return;
176 }
177 if (l->ConstantAttenuation == params[0])
178 return;
179 FLUSH_VERTICES(ctx, _NEW_LIGHT);
180 l->ConstantAttenuation = params[0];
181 break;
182 case GL_LINEAR_ATTENUATION:
183 if (params[0]<0.0) {
Brian Paul08836342001-03-03 20:33:27 +0000184 _mesa_error( ctx, GL_INVALID_VALUE, "glLight" );
Keith Whitwellcab974c2000-12-26 05:09:27 +0000185 return;
186 }
187 if (l->LinearAttenuation == params[0])
188 return;
189 FLUSH_VERTICES(ctx, _NEW_LIGHT);
190 l->LinearAttenuation = params[0];
191 break;
192 case GL_QUADRATIC_ATTENUATION:
193 if (params[0]<0.0) {
Brian Paul08836342001-03-03 20:33:27 +0000194 _mesa_error( ctx, GL_INVALID_VALUE, "glLight" );
Keith Whitwellcab974c2000-12-26 05:09:27 +0000195 return;
196 }
197 if (l->QuadraticAttenuation == params[0])
198 return;
199 FLUSH_VERTICES(ctx, _NEW_LIGHT);
200 l->QuadraticAttenuation = params[0];
201 break;
202 default:
Brian Paul08836342001-03-03 20:33:27 +0000203 _mesa_error( ctx, GL_INVALID_ENUM, "glLight" );
Keith Whitwellcab974c2000-12-26 05:09:27 +0000204 return;
jtgafb833d1999-08-19 00:55:39 +0000205 }
206
Keith Whitwell69cfdb21999-09-30 11:18:21 +0000207 if (ctx->Driver.Lightfv)
Keith Whitwell1e1aac02000-11-13 20:02:56 +0000208 ctx->Driver.Lightfv( ctx, light, pname, params );
jtgafb833d1999-08-19 00:55:39 +0000209}
210
211
Brian Paulfbd8f211999-11-11 01:22:25 +0000212void
213_mesa_Lighti( GLenum light, GLenum pname, GLint param )
jtgafb833d1999-08-19 00:55:39 +0000214{
Brian Paulfbd8f211999-11-11 01:22:25 +0000215 _mesa_Lightiv( light, pname, &param );
216}
217
218
219void
220_mesa_Lightiv( GLenum light, GLenum pname, const GLint *params )
221{
222 GLfloat fparam[4];
223
224 switch (pname) {
225 case GL_AMBIENT:
226 case GL_DIFFUSE:
227 case GL_SPECULAR:
228 fparam[0] = INT_TO_FLOAT( params[0] );
229 fparam[1] = INT_TO_FLOAT( params[1] );
230 fparam[2] = INT_TO_FLOAT( params[2] );
231 fparam[3] = INT_TO_FLOAT( params[3] );
232 break;
233 case GL_POSITION:
234 fparam[0] = (GLfloat) params[0];
235 fparam[1] = (GLfloat) params[1];
236 fparam[2] = (GLfloat) params[2];
237 fparam[3] = (GLfloat) params[3];
238 break;
239 case GL_SPOT_DIRECTION:
240 fparam[0] = (GLfloat) params[0];
241 fparam[1] = (GLfloat) params[1];
242 fparam[2] = (GLfloat) params[2];
243 break;
244 case GL_SPOT_EXPONENT:
245 case GL_SPOT_CUTOFF:
246 case GL_CONSTANT_ATTENUATION:
247 case GL_LINEAR_ATTENUATION:
248 case GL_QUADRATIC_ATTENUATION:
249 fparam[0] = (GLfloat) params[0];
250 break;
251 default:
252 /* error will be caught later in gl_Lightfv */
253 ;
254 }
255
256 _mesa_Lightfv( light, pname, fparam );
257}
258
259
260
261void
262_mesa_GetLightfv( GLenum light, GLenum pname, GLfloat *params )
263{
264 GET_CURRENT_CONTEXT(ctx);
jtgafb833d1999-08-19 00:55:39 +0000265 GLint l = (GLint) (light - GL_LIGHT0);
Keith Whitwellcab974c2000-12-26 05:09:27 +0000266 ASSERT_OUTSIDE_BEGIN_END(ctx);
jtgafb833d1999-08-19 00:55:39 +0000267
Brian Paula8644322000-11-27 18:22:13 +0000268 if (l < 0 || l >= ctx->Const.MaxLights) {
Brian Paul08836342001-03-03 20:33:27 +0000269 _mesa_error( ctx, GL_INVALID_ENUM, "glGetLightfv" );
jtgafb833d1999-08-19 00:55:39 +0000270 return;
271 }
272
273 switch (pname) {
274 case GL_AMBIENT:
275 COPY_4V( params, ctx->Light.Light[l].Ambient );
276 break;
277 case GL_DIFFUSE:
278 COPY_4V( params, ctx->Light.Light[l].Diffuse );
279 break;
280 case GL_SPECULAR:
281 COPY_4V( params, ctx->Light.Light[l].Specular );
282 break;
283 case GL_POSITION:
284 COPY_4V( params, ctx->Light.Light[l].EyePosition );
285 break;
286 case GL_SPOT_DIRECTION:
287 COPY_3V( params, ctx->Light.Light[l].EyeDirection );
288 break;
289 case GL_SPOT_EXPONENT:
290 params[0] = ctx->Light.Light[l].SpotExponent;
291 break;
292 case GL_SPOT_CUTOFF:
293 params[0] = ctx->Light.Light[l].SpotCutoff;
294 break;
295 case GL_CONSTANT_ATTENUATION:
296 params[0] = ctx->Light.Light[l].ConstantAttenuation;
297 break;
298 case GL_LINEAR_ATTENUATION:
299 params[0] = ctx->Light.Light[l].LinearAttenuation;
300 break;
301 case GL_QUADRATIC_ATTENUATION:
302 params[0] = ctx->Light.Light[l].QuadraticAttenuation;
303 break;
304 default:
Brian Paul08836342001-03-03 20:33:27 +0000305 _mesa_error( ctx, GL_INVALID_ENUM, "glGetLightfv" );
jtgafb833d1999-08-19 00:55:39 +0000306 break;
307 }
308}
309
310
311
Brian Paulfbd8f211999-11-11 01:22:25 +0000312void
313_mesa_GetLightiv( GLenum light, GLenum pname, GLint *params )
jtgafb833d1999-08-19 00:55:39 +0000314{
Brian Paulfbd8f211999-11-11 01:22:25 +0000315 GET_CURRENT_CONTEXT(ctx);
jtgafb833d1999-08-19 00:55:39 +0000316 GLint l = (GLint) (light - GL_LIGHT0);
Keith Whitwellcab974c2000-12-26 05:09:27 +0000317 ASSERT_OUTSIDE_BEGIN_END(ctx);
jtgafb833d1999-08-19 00:55:39 +0000318
Brian Paula8644322000-11-27 18:22:13 +0000319 if (l < 0 || l >= ctx->Const.MaxLights) {
Brian Paul08836342001-03-03 20:33:27 +0000320 _mesa_error( ctx, GL_INVALID_ENUM, "glGetLightiv" );
jtgafb833d1999-08-19 00:55:39 +0000321 return;
322 }
323
324 switch (pname) {
325 case GL_AMBIENT:
326 params[0] = FLOAT_TO_INT(ctx->Light.Light[l].Ambient[0]);
327 params[1] = FLOAT_TO_INT(ctx->Light.Light[l].Ambient[1]);
328 params[2] = FLOAT_TO_INT(ctx->Light.Light[l].Ambient[2]);
329 params[3] = FLOAT_TO_INT(ctx->Light.Light[l].Ambient[3]);
330 break;
331 case GL_DIFFUSE:
332 params[0] = FLOAT_TO_INT(ctx->Light.Light[l].Diffuse[0]);
333 params[1] = FLOAT_TO_INT(ctx->Light.Light[l].Diffuse[1]);
334 params[2] = FLOAT_TO_INT(ctx->Light.Light[l].Diffuse[2]);
335 params[3] = FLOAT_TO_INT(ctx->Light.Light[l].Diffuse[3]);
336 break;
337 case GL_SPECULAR:
338 params[0] = FLOAT_TO_INT(ctx->Light.Light[l].Specular[0]);
339 params[1] = FLOAT_TO_INT(ctx->Light.Light[l].Specular[1]);
340 params[2] = FLOAT_TO_INT(ctx->Light.Light[l].Specular[2]);
341 params[3] = FLOAT_TO_INT(ctx->Light.Light[l].Specular[3]);
342 break;
343 case GL_POSITION:
344 params[0] = (GLint) ctx->Light.Light[l].EyePosition[0];
345 params[1] = (GLint) ctx->Light.Light[l].EyePosition[1];
346 params[2] = (GLint) ctx->Light.Light[l].EyePosition[2];
347 params[3] = (GLint) ctx->Light.Light[l].EyePosition[3];
348 break;
349 case GL_SPOT_DIRECTION:
350 params[0] = (GLint) ctx->Light.Light[l].EyeDirection[0];
351 params[1] = (GLint) ctx->Light.Light[l].EyeDirection[1];
352 params[2] = (GLint) ctx->Light.Light[l].EyeDirection[2];
353 break;
354 case GL_SPOT_EXPONENT:
355 params[0] = (GLint) ctx->Light.Light[l].SpotExponent;
356 break;
357 case GL_SPOT_CUTOFF:
358 params[0] = (GLint) ctx->Light.Light[l].SpotCutoff;
359 break;
360 case GL_CONSTANT_ATTENUATION:
361 params[0] = (GLint) ctx->Light.Light[l].ConstantAttenuation;
362 break;
363 case GL_LINEAR_ATTENUATION:
364 params[0] = (GLint) ctx->Light.Light[l].LinearAttenuation;
365 break;
366 case GL_QUADRATIC_ATTENUATION:
367 params[0] = (GLint) ctx->Light.Light[l].QuadraticAttenuation;
368 break;
369 default:
Brian Paul08836342001-03-03 20:33:27 +0000370 _mesa_error( ctx, GL_INVALID_ENUM, "glGetLightiv" );
jtgafb833d1999-08-19 00:55:39 +0000371 break;
372 }
373}
374
375
376
377/**********************************************************************/
378/*** Light Model ***/
379/**********************************************************************/
380
381
Brian Paulfbd8f211999-11-11 01:22:25 +0000382void
383_mesa_LightModelfv( GLenum pname, const GLfloat *params )
jtgafb833d1999-08-19 00:55:39 +0000384{
Keith Whitwellcab974c2000-12-26 05:09:27 +0000385 GLenum newenum;
386 GLboolean newbool;
Brian Paulfbd8f211999-11-11 01:22:25 +0000387 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +0000388 ASSERT_OUTSIDE_BEGIN_END(ctx);
jtgafb833d1999-08-19 00:55:39 +0000389
390 switch (pname) {
391 case GL_LIGHT_MODEL_AMBIENT:
Keith Whitwellcab974c2000-12-26 05:09:27 +0000392 if (TEST_EQ_4V( ctx->Light.Model.Ambient, params ))
393 return;
394 FLUSH_VERTICES(ctx, _NEW_LIGHT);
jtgafb833d1999-08-19 00:55:39 +0000395 COPY_4V( ctx->Light.Model.Ambient, params );
396 break;
397 case GL_LIGHT_MODEL_LOCAL_VIEWER:
Keith Whitwellcab974c2000-12-26 05:09:27 +0000398 newbool = (params[0]!=0.0);
399 if (ctx->Light.Model.LocalViewer == newbool)
400 return;
401 FLUSH_VERTICES(ctx, _NEW_LIGHT);
402 ctx->Light.Model.LocalViewer = newbool;
jtgafb833d1999-08-19 00:55:39 +0000403 break;
404 case GL_LIGHT_MODEL_TWO_SIDE:
Keith Whitwellcab974c2000-12-26 05:09:27 +0000405 newbool = (params[0]!=0.0);
406 if (ctx->Light.Model.TwoSide == newbool)
407 return;
408 FLUSH_VERTICES(ctx, _NEW_LIGHT);
409 ctx->Light.Model.TwoSide = newbool;
jtgafb833d1999-08-19 00:55:39 +0000410 break;
411 case GL_LIGHT_MODEL_COLOR_CONTROL:
Keith Whitwellcab974c2000-12-26 05:09:27 +0000412 if (params[0] == (GLfloat) GL_SINGLE_COLOR)
413 newenum = GL_SINGLE_COLOR;
414 else if (params[0] == (GLfloat) GL_SEPARATE_SPECULAR_COLOR)
415 newenum = GL_SEPARATE_SPECULAR_COLOR;
416 else {
Brian Paul08836342001-03-03 20:33:27 +0000417 _mesa_error( ctx, GL_INVALID_ENUM, "glLightModel(param)" );
Keith Whitwellcab974c2000-12-26 05:09:27 +0000418 return;
Brian Paulf3f9b771999-10-19 20:32:40 +0000419 }
Keith Whitwellcab974c2000-12-26 05:09:27 +0000420 if (ctx->Light.Model.ColorControl == newenum)
421 return;
422 FLUSH_VERTICES(ctx, _NEW_LIGHT);
423 ctx->Light.Model.ColorControl = newenum;
Keith Whitwell46b09882001-02-16 18:14:41 +0000424
425 if ((ctx->Light.Enabled &&
426 ctx->Light.Model.ColorControl==GL_SEPARATE_SPECULAR_COLOR)
427 || ctx->Fog.ColorSumEnabled)
428 ctx->_TriangleCaps |= DD_SEPERATE_SPECULAR;
429 else
430 ctx->_TriangleCaps &= ~DD_SEPERATE_SPECULAR;
431
jtgafb833d1999-08-19 00:55:39 +0000432 break;
433 default:
Brian Paul08836342001-03-03 20:33:27 +0000434 _mesa_error( ctx, GL_INVALID_ENUM, "glLightModel" );
jtgafb833d1999-08-19 00:55:39 +0000435 break;
436 }
Keith Whitwell69cfdb21999-09-30 11:18:21 +0000437
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +0000438 if (ctx->Driver.LightModelfv)
Keith Whitwell69cfdb21999-09-30 11:18:21 +0000439 ctx->Driver.LightModelfv( ctx, pname, params );
jtgafb833d1999-08-19 00:55:39 +0000440}
441
442
Brian Paulfbd8f211999-11-11 01:22:25 +0000443void
444_mesa_LightModeliv( GLenum pname, const GLint *params )
445{
446 GLfloat fparam[4];
Brian Paulfbd8f211999-11-11 01:22:25 +0000447
448 switch (pname) {
449 case GL_LIGHT_MODEL_AMBIENT:
450 fparam[0] = INT_TO_FLOAT( params[0] );
451 fparam[1] = INT_TO_FLOAT( params[1] );
452 fparam[2] = INT_TO_FLOAT( params[2] );
453 fparam[3] = INT_TO_FLOAT( params[3] );
454 break;
455 case GL_LIGHT_MODEL_LOCAL_VIEWER:
456 case GL_LIGHT_MODEL_TWO_SIDE:
457 case GL_LIGHT_MODEL_COLOR_CONTROL:
458 fparam[0] = (GLfloat) params[0];
459 break;
460 default:
461 /* Error will be caught later in gl_LightModelfv */
462 ;
463 }
464 _mesa_LightModelfv( pname, fparam );
465}
466
467
468void
469_mesa_LightModeli( GLenum pname, GLint param )
470{
471 _mesa_LightModeliv( pname, &param );
472}
473
474
475void
476_mesa_LightModelf( GLenum pname, GLfloat param )
477{
478 _mesa_LightModelfv( pname, &param );
479}
480
jtgafb833d1999-08-19 00:55:39 +0000481
482
483/********** MATERIAL **********/
484
485
486/*
487 * Given a face and pname value (ala glColorMaterial), compute a bitmask
488 * of the targeted material values.
489 */
Brian Paul08836342001-03-03 20:33:27 +0000490GLuint
491_mesa_material_bitmask( GLcontext *ctx, GLenum face, GLenum pname,
492 GLuint legal, const char *where )
jtgafb833d1999-08-19 00:55:39 +0000493{
494 GLuint bitmask = 0;
495
496 /* Make a bitmask indicating what material attribute(s) we're updating */
497 switch (pname) {
498 case GL_EMISSION:
499 bitmask |= FRONT_EMISSION_BIT | BACK_EMISSION_BIT;
500 break;
501 case GL_AMBIENT:
502 bitmask |= FRONT_AMBIENT_BIT | BACK_AMBIENT_BIT;
503 break;
504 case GL_DIFFUSE:
505 bitmask |= FRONT_DIFFUSE_BIT | BACK_DIFFUSE_BIT;
506 break;
507 case GL_SPECULAR:
508 bitmask |= FRONT_SPECULAR_BIT | BACK_SPECULAR_BIT;
509 break;
510 case GL_SHININESS:
511 bitmask |= FRONT_SHININESS_BIT | BACK_SHININESS_BIT;
512 break;
513 case GL_AMBIENT_AND_DIFFUSE:
514 bitmask |= FRONT_AMBIENT_BIT | BACK_AMBIENT_BIT;
515 bitmask |= FRONT_DIFFUSE_BIT | BACK_DIFFUSE_BIT;
516 break;
517 case GL_COLOR_INDEXES:
518 bitmask |= FRONT_INDEXES_BIT | BACK_INDEXES_BIT;
519 break;
520 default:
Brian Paul08836342001-03-03 20:33:27 +0000521 _mesa_error( ctx, GL_INVALID_ENUM, where );
jtgafb833d1999-08-19 00:55:39 +0000522 return 0;
523 }
524
525 if (face==GL_FRONT) {
526 bitmask &= FRONT_MATERIAL_BITS;
527 }
528 else if (face==GL_BACK) {
529 bitmask &= BACK_MATERIAL_BITS;
530 }
531 else if (face != GL_FRONT_AND_BACK) {
Brian Paul08836342001-03-03 20:33:27 +0000532 _mesa_error( ctx, GL_INVALID_ENUM, where );
jtgafb833d1999-08-19 00:55:39 +0000533 return 0;
534 }
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +0000535
jtgafb833d1999-08-19 00:55:39 +0000536 if (bitmask & ~legal) {
Brian Paul08836342001-03-03 20:33:27 +0000537 _mesa_error( ctx, GL_INVALID_ENUM, where );
jtgafb833d1999-08-19 00:55:39 +0000538 return 0;
539 }
540
541 return bitmask;
542}
543
544
Keith Whitwellc6b2a922001-02-15 01:33:52 +0000545/* Perform a straight copy between pairs of materials.
546 */
Brian Paul08836342001-03-03 20:33:27 +0000547void _mesa_copy_material_pairs( struct gl_material dst[2],
Keith Whitwellc6b2a922001-02-15 01:33:52 +0000548 const struct gl_material src[2],
549 GLuint bitmask )
550{
551 if (bitmask & FRONT_EMISSION_BIT) {
552 COPY_4FV( dst[0].Emission, src[0].Emission );
553 }
554 if (bitmask & BACK_EMISSION_BIT) {
555 COPY_4FV( dst[1].Emission, src[1].Emission );
556 }
557 if (bitmask & FRONT_AMBIENT_BIT) {
558 COPY_4FV( dst[0].Ambient, src[0].Ambient );
559 }
560 if (bitmask & BACK_AMBIENT_BIT) {
561 COPY_4FV( dst[1].Ambient, src[1].Ambient );
562 }
563 if (bitmask & FRONT_DIFFUSE_BIT) {
564 COPY_4FV( dst[0].Diffuse, src[0].Diffuse );
565 }
566 if (bitmask & BACK_DIFFUSE_BIT) {
567 COPY_4FV( dst[1].Diffuse, src[1].Diffuse );
568 }
569 if (bitmask & FRONT_SPECULAR_BIT) {
570 COPY_4FV( dst[0].Specular, src[0].Specular );
571 }
572 if (bitmask & BACK_SPECULAR_BIT) {
573 COPY_4FV( dst[1].Specular, src[1].Specular );
574 }
575 if (bitmask & FRONT_SHININESS_BIT) {
576 dst[0].Shininess = src[0].Shininess;
577 }
578 if (bitmask & BACK_SHININESS_BIT) {
579 dst[1].Shininess = src[1].Shininess;
580 }
581 if (bitmask & FRONT_INDEXES_BIT) {
582 dst[0].AmbientIndex = src[0].AmbientIndex;
583 dst[0].DiffuseIndex = src[0].DiffuseIndex;
584 dst[0].SpecularIndex = src[0].SpecularIndex;
585 }
586 if (bitmask & BACK_INDEXES_BIT) {
587 dst[1].AmbientIndex = src[1].AmbientIndex;
588 dst[1].DiffuseIndex = src[1].DiffuseIndex;
589 dst[1].SpecularIndex = src[1].SpecularIndex;
590 }
591}
592
jtgafb833d1999-08-19 00:55:39 +0000593
jtgafb833d1999-08-19 00:55:39 +0000594/*
595 * Check if the global material has to be updated with info that was
596 * associated with a vertex via glMaterial.
597 * This function is used when any material values get changed between
598 * glBegin/glEnd either by calling glMaterial() or by calling glColor()
599 * when GL_COLOR_MATERIAL is enabled.
600 *
Brian Paulde82d062000-06-26 23:37:46 +0000601 * src[0] is front material, src[1] is back material
602 *
Keith Whitwellc6b2a922001-02-15 01:33:52 +0000603 * Additionally keeps the precomputed lighting state uptodate.
jtgafb833d1999-08-19 00:55:39 +0000604 */
Brian Paul08836342001-03-03 20:33:27 +0000605void _mesa_update_material( GLcontext *ctx,
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +0000606 const struct gl_material src[2],
jtgafb833d1999-08-19 00:55:39 +0000607 GLuint bitmask )
608{
609 struct gl_light *light, *list = &ctx->Light.EnabledList;
jtgafb833d1999-08-19 00:55:39 +0000610
611 if (ctx->Light.ColorMaterialEnabled)
612 bitmask &= ~ctx->Light.ColorMaterialBitmask;
613
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +0000614 if (MESA_VERBOSE&VERBOSE_IMMEDIATE)
Brian Paul08836342001-03-03 20:33:27 +0000615 fprintf(stderr, "_mesa_update_material, mask 0x%x\n", bitmask);
Keith Whitwell06ac5921999-11-10 06:29:44 +0000616
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +0000617 if (!bitmask)
jtgafb833d1999-08-19 00:55:39 +0000618 return;
619
Brian Paulde82d062000-06-26 23:37:46 +0000620 /* update material emission */
621 if (bitmask & FRONT_EMISSION_BIT) {
622 struct gl_material *mat = &ctx->Light.Material[0];
623 COPY_4FV( mat->Emission, src[0].Emission );
624 }
625 if (bitmask & BACK_EMISSION_BIT) {
626 struct gl_material *mat = &ctx->Light.Material[1];
627 COPY_4FV( mat->Emission, src[1].Emission );
628 }
629
630 /* update material ambience */
jtgafb833d1999-08-19 00:55:39 +0000631 if (bitmask & FRONT_AMBIENT_BIT) {
632 struct gl_material *mat = &ctx->Light.Material[0];
jtgafb833d1999-08-19 00:55:39 +0000633 COPY_4FV( mat->Ambient, src[0].Ambient );
Brian Paulde82d062000-06-26 23:37:46 +0000634 foreach (light, list) {
Keith Whitwell14940c42000-11-05 18:40:57 +0000635 SCALE_3V( light->_MatAmbient[0], light->Ambient, src[0].Ambient);
Brian Paulde82d062000-06-26 23:37:46 +0000636 }
jtgafb833d1999-08-19 00:55:39 +0000637 }
638 if (bitmask & BACK_AMBIENT_BIT) {
639 struct gl_material *mat = &ctx->Light.Material[1];
jtgafb833d1999-08-19 00:55:39 +0000640 COPY_4FV( mat->Ambient, src[1].Ambient );
Brian Paulde82d062000-06-26 23:37:46 +0000641 foreach (light, list) {
Keith Whitwell14940c42000-11-05 18:40:57 +0000642 SCALE_3V( light->_MatAmbient[1], light->Ambient, src[1].Ambient);
Brian Paulde82d062000-06-26 23:37:46 +0000643 }
jtgafb833d1999-08-19 00:55:39 +0000644 }
Brian Paulde82d062000-06-26 23:37:46 +0000645
646 /* update BaseColor = emission + scene's ambience * material's ambience */
647 if (bitmask & (FRONT_EMISSION_BIT | FRONT_AMBIENT_BIT)) {
648 struct gl_material *mat = &ctx->Light.Material[0];
Keith Whitwell14940c42000-11-05 18:40:57 +0000649 COPY_3V( ctx->Light._BaseColor[0], mat->Emission );
Keith Whitwellad2ac212000-11-24 10:25:05 +0000650 ACC_SCALE_3V( ctx->Light._BaseColor[0], mat->Ambient,
651 ctx->Light.Model.Ambient );
Brian Paulde82d062000-06-26 23:37:46 +0000652 }
653 if (bitmask & (BACK_EMISSION_BIT | BACK_AMBIENT_BIT)) {
654 struct gl_material *mat = &ctx->Light.Material[1];
Keith Whitwell14940c42000-11-05 18:40:57 +0000655 COPY_3V( ctx->Light._BaseColor[1], mat->Emission );
Keith Whitwellad2ac212000-11-24 10:25:05 +0000656 ACC_SCALE_3V( ctx->Light._BaseColor[1], mat->Ambient,
657 ctx->Light.Model.Ambient );
Brian Paulde82d062000-06-26 23:37:46 +0000658 }
659
660 /* update material diffuse values */
jtgafb833d1999-08-19 00:55:39 +0000661 if (bitmask & FRONT_DIFFUSE_BIT) {
662 struct gl_material *mat = &ctx->Light.Material[0];
jtgafb833d1999-08-19 00:55:39 +0000663 COPY_4FV( mat->Diffuse, src[0].Diffuse );
Keith Whitwellad2ac212000-11-24 10:25:05 +0000664 foreach (light, list) {
665 SCALE_3V( light->_MatDiffuse[0], light->Diffuse, mat->Diffuse );
666 }
Brian Paul3041d052001-01-02 22:02:51 +0000667 UNCLAMPED_FLOAT_TO_CHAN(ctx->Light._BaseAlpha[0], mat->Diffuse[3]);
jtgafb833d1999-08-19 00:55:39 +0000668 }
669 if (bitmask & BACK_DIFFUSE_BIT) {
670 struct gl_material *mat = &ctx->Light.Material[1];
jtgafb833d1999-08-19 00:55:39 +0000671 COPY_4FV( mat->Diffuse, src[1].Diffuse );
Keith Whitwellad2ac212000-11-24 10:25:05 +0000672 foreach (light, list) {
673 SCALE_3V( light->_MatDiffuse[1], light->Diffuse, mat->Diffuse );
674 }
Brian Paul3041d052001-01-02 22:02:51 +0000675 UNCLAMPED_FLOAT_TO_CHAN(ctx->Light._BaseAlpha[1], mat->Diffuse[3]);
jtgafb833d1999-08-19 00:55:39 +0000676 }
Brian Paulde82d062000-06-26 23:37:46 +0000677
678 /* update material specular values */
jtgafb833d1999-08-19 00:55:39 +0000679 if (bitmask & FRONT_SPECULAR_BIT) {
680 struct gl_material *mat = &ctx->Light.Material[0];
jtgafb833d1999-08-19 00:55:39 +0000681 COPY_4FV( mat->Specular, src[0].Specular );
Keith Whitwellad2ac212000-11-24 10:25:05 +0000682 foreach (light, list) {
Keith Whitwell9aff6cf2000-11-24 15:21:59 +0000683 SCALE_3V( light->_MatSpecular[0], light->Specular, mat->Specular);
Keith Whitwellad2ac212000-11-24 10:25:05 +0000684 }
jtgafb833d1999-08-19 00:55:39 +0000685 }
686 if (bitmask & BACK_SPECULAR_BIT) {
687 struct gl_material *mat = &ctx->Light.Material[1];
jtgafb833d1999-08-19 00:55:39 +0000688 COPY_4FV( mat->Specular, src[1].Specular );
Keith Whitwellad2ac212000-11-24 10:25:05 +0000689 foreach (light, list) {
Keith Whitwell9aff6cf2000-11-24 15:21:59 +0000690 SCALE_3V( light->_MatSpecular[1], light->Specular, mat->Specular);
Keith Whitwellad2ac212000-11-24 10:25:05 +0000691 }
jtgafb833d1999-08-19 00:55:39 +0000692 }
Brian Paulde82d062000-06-26 23:37:46 +0000693
jtgafb833d1999-08-19 00:55:39 +0000694 if (bitmask & FRONT_SHININESS_BIT) {
Keith Whitwellcab974c2000-12-26 05:09:27 +0000695 ctx->Light.Material[0].Shininess = src[0].Shininess;
Brian Paul08836342001-03-03 20:33:27 +0000696 _mesa_invalidate_shine_table( ctx, 0 );
jtgafb833d1999-08-19 00:55:39 +0000697 }
698 if (bitmask & BACK_SHININESS_BIT) {
Keith Whitwellcab974c2000-12-26 05:09:27 +0000699 ctx->Light.Material[1].Shininess = src[1].Shininess;
Brian Paul08836342001-03-03 20:33:27 +0000700 _mesa_invalidate_shine_table( ctx, 1 );
jtgafb833d1999-08-19 00:55:39 +0000701 }
Brian Paulde82d062000-06-26 23:37:46 +0000702
jtgafb833d1999-08-19 00:55:39 +0000703 if (bitmask & FRONT_INDEXES_BIT) {
704 ctx->Light.Material[0].AmbientIndex = src[0].AmbientIndex;
705 ctx->Light.Material[0].DiffuseIndex = src[0].DiffuseIndex;
706 ctx->Light.Material[0].SpecularIndex = src[0].SpecularIndex;
707 }
708 if (bitmask & BACK_INDEXES_BIT) {
709 ctx->Light.Material[1].AmbientIndex = src[1].AmbientIndex;
710 ctx->Light.Material[1].DiffuseIndex = src[1].DiffuseIndex;
711 ctx->Light.Material[1].SpecularIndex = src[1].SpecularIndex;
712 }
713
Keith Whitwell06ac5921999-11-10 06:29:44 +0000714 if (0)
715 {
716 struct gl_material *mat = &ctx->Light.Material[0];
717 fprintf(stderr, "update_mat emission : %f %f %f\n",
718 mat->Emission[0],
719 mat->Emission[1],
720 mat->Emission[2]);
721 fprintf(stderr, "update_mat specular : %f %f %f\n",
722 mat->Specular[0],
723 mat->Specular[1],
724 mat->Specular[2]);
725 fprintf(stderr, "update_mat diffuse : %f %f %f\n",
726 mat->Diffuse[0],
727 mat->Diffuse[1],
728 mat->Diffuse[2]);
729 fprintf(stderr, "update_mat ambient : %f %f %f\n",
730 mat->Ambient[0],
731 mat->Ambient[1],
732 mat->Ambient[2]);
733 }
jtgafb833d1999-08-19 00:55:39 +0000734}
735
736
737
738
Keith Whitwellc6b2a922001-02-15 01:33:52 +0000739
740
741
Brian Paulde82d062000-06-26 23:37:46 +0000742/*
743 * Update the current materials from the given rgba color
744 * according to the bitmask in ColorMaterialBitmask, which is
745 * set by glColorMaterial().
746 */
Brian Paul08836342001-03-03 20:33:27 +0000747void _mesa_update_color_material( GLcontext *ctx,
Brian Paulba643a22000-10-28 18:34:48 +0000748 const GLchan rgba[4] )
jtgafb833d1999-08-19 00:55:39 +0000749{
750 struct gl_light *light, *list = &ctx->Light.EnabledList;
751 GLuint bitmask = ctx->Light.ColorMaterialBitmask;
Brian Paulde82d062000-06-26 23:37:46 +0000752 GLfloat color[4];
jtgafb833d1999-08-19 00:55:39 +0000753
Brian Paulc893a012000-10-28 20:41:13 +0000754 color[0] = CHAN_TO_FLOAT(rgba[0]);
755 color[1] = CHAN_TO_FLOAT(rgba[1]);
756 color[2] = CHAN_TO_FLOAT(rgba[2]);
757 color[3] = CHAN_TO_FLOAT(rgba[3]);
758
Keith Whitwell06ac5921999-11-10 06:29:44 +0000759 if (MESA_VERBOSE&VERBOSE_IMMEDIATE)
Brian Paul08836342001-03-03 20:33:27 +0000760 fprintf(stderr, "_mesa_update_color_material, mask 0x%x\n", bitmask);
Keith Whitwell06ac5921999-11-10 06:29:44 +0000761
Brian Paulde82d062000-06-26 23:37:46 +0000762 /* update emissive colors */
763 if (bitmask & FRONT_EMISSION_BIT) {
764 struct gl_material *mat = &ctx->Light.Material[0];
765 COPY_4FV( mat->Emission, color );
766 }
Brian Paulfbd8f211999-11-11 01:22:25 +0000767
Brian Paulde82d062000-06-26 23:37:46 +0000768 if (bitmask & BACK_EMISSION_BIT) {
769 struct gl_material *mat = &ctx->Light.Material[1];
770 COPY_4FV( mat->Emission, color );
771 }
772
Keith Whitwell14940c42000-11-05 18:40:57 +0000773 /* update light->_MatAmbient = light's ambient * material's ambient */
jtgafb833d1999-08-19 00:55:39 +0000774 if (bitmask & FRONT_AMBIENT_BIT) {
775 struct gl_material *mat = &ctx->Light.Material[0];
jtgafb833d1999-08-19 00:55:39 +0000776 foreach (light, list) {
Keith Whitwell14940c42000-11-05 18:40:57 +0000777 SCALE_3V( light->_MatAmbient[0], light->Ambient, color);
jtgafb833d1999-08-19 00:55:39 +0000778 }
779 COPY_4FV( mat->Ambient, color );
780 }
781
782 if (bitmask & BACK_AMBIENT_BIT) {
783 struct gl_material *mat = &ctx->Light.Material[1];
jtgafb833d1999-08-19 00:55:39 +0000784 foreach (light, list) {
Keith Whitwell14940c42000-11-05 18:40:57 +0000785 SCALE_3V( light->_MatAmbient[1], light->Ambient, color);
jtgafb833d1999-08-19 00:55:39 +0000786 }
787 COPY_4FV( mat->Ambient, color );
788 }
789
Brian Paulde82d062000-06-26 23:37:46 +0000790 /* update BaseColor = emission + scene's ambience * material's ambience */
791 if (bitmask & (FRONT_EMISSION_BIT | FRONT_AMBIENT_BIT)) {
792 struct gl_material *mat = &ctx->Light.Material[0];
Keith Whitwell14940c42000-11-05 18:40:57 +0000793 COPY_3V( ctx->Light._BaseColor[0], mat->Emission );
794 ACC_SCALE_3V( ctx->Light._BaseColor[0], mat->Ambient, ctx->Light.Model.Ambient );
Brian Paulde82d062000-06-26 23:37:46 +0000795 }
796
797 if (bitmask & (BACK_EMISSION_BIT | BACK_AMBIENT_BIT)) {
798 struct gl_material *mat = &ctx->Light.Material[1];
Keith Whitwell14940c42000-11-05 18:40:57 +0000799 COPY_3V( ctx->Light._BaseColor[1], mat->Emission );
800 ACC_SCALE_3V( ctx->Light._BaseColor[1], mat->Ambient, ctx->Light.Model.Ambient );
Brian Paulde82d062000-06-26 23:37:46 +0000801 }
802
Keith Whitwell14940c42000-11-05 18:40:57 +0000803 /* update light->_MatDiffuse = light's diffuse * material's diffuse */
jtgafb833d1999-08-19 00:55:39 +0000804 if (bitmask & FRONT_DIFFUSE_BIT) {
805 struct gl_material *mat = &ctx->Light.Material[0];
jtgafb833d1999-08-19 00:55:39 +0000806 COPY_4FV( mat->Diffuse, color );
Keith Whitwellad2ac212000-11-24 10:25:05 +0000807 foreach (light, list) {
808 SCALE_3V( light->_MatDiffuse[0], light->Diffuse, mat->Diffuse );
809 }
Brian Paul3041d052001-01-02 22:02:51 +0000810 UNCLAMPED_FLOAT_TO_CHAN(ctx->Light._BaseAlpha[0], mat->Diffuse[3]);
jtgafb833d1999-08-19 00:55:39 +0000811 }
812
813 if (bitmask & BACK_DIFFUSE_BIT) {
814 struct gl_material *mat = &ctx->Light.Material[1];
jtgafb833d1999-08-19 00:55:39 +0000815 COPY_4FV( mat->Diffuse, color );
Keith Whitwellad2ac212000-11-24 10:25:05 +0000816 foreach (light, list) {
817 SCALE_3V( light->_MatDiffuse[1], light->Diffuse, mat->Diffuse );
818 }
Brian Paul3041d052001-01-02 22:02:51 +0000819 UNCLAMPED_FLOAT_TO_CHAN(ctx->Light._BaseAlpha[1], mat->Diffuse[3]);
jtgafb833d1999-08-19 00:55:39 +0000820 }
821
Keith Whitwell14940c42000-11-05 18:40:57 +0000822 /* update light->_MatSpecular = light's specular * material's specular */
jtgafb833d1999-08-19 00:55:39 +0000823 if (bitmask & FRONT_SPECULAR_BIT) {
824 struct gl_material *mat = &ctx->Light.Material[0];
jtgafb833d1999-08-19 00:55:39 +0000825 COPY_4FV( mat->Specular, color );
Keith Whitwellad2ac212000-11-24 10:25:05 +0000826 foreach (light, list) {
827 ACC_SCALE_3V( light->_MatSpecular[0], light->Specular, mat->Specular);
828 }
jtgafb833d1999-08-19 00:55:39 +0000829 }
Brian Paulde82d062000-06-26 23:37:46 +0000830
jtgafb833d1999-08-19 00:55:39 +0000831 if (bitmask & BACK_SPECULAR_BIT) {
832 struct gl_material *mat = &ctx->Light.Material[1];
jtgafb833d1999-08-19 00:55:39 +0000833 COPY_4FV( mat->Specular, color );
Keith Whitwellad2ac212000-11-24 10:25:05 +0000834 foreach (light, list) {
835 ACC_SCALE_3V( light->_MatSpecular[1], light->Specular, mat->Specular);
836 }
jtgafb833d1999-08-19 00:55:39 +0000837 }
Keith Whitwell06ac5921999-11-10 06:29:44 +0000838
839 if (0)
840 {
841 struct gl_material *mat = &ctx->Light.Material[0];
842 fprintf(stderr, "update_color_mat emission : %f %f %f\n",
843 mat->Emission[0],
844 mat->Emission[1],
845 mat->Emission[2]);
846 fprintf(stderr, "update_color_mat specular : %f %f %f\n",
847 mat->Specular[0],
848 mat->Specular[1],
849 mat->Specular[2]);
850 fprintf(stderr, "update_color_mat diffuse : %f %f %f\n",
851 mat->Diffuse[0],
852 mat->Diffuse[1],
853 mat->Diffuse[2]);
854 fprintf(stderr, "update_color_mat ambient : %f %f %f\n",
855 mat->Ambient[0],
856 mat->Ambient[1],
857 mat->Ambient[2]);
858 }
jtgafb833d1999-08-19 00:55:39 +0000859}
860
861
862
863
Brian Paulfbd8f211999-11-11 01:22:25 +0000864void
865_mesa_ColorMaterial( GLenum face, GLenum mode )
jtgafb833d1999-08-19 00:55:39 +0000866{
Brian Paulfbd8f211999-11-11 01:22:25 +0000867 GET_CURRENT_CONTEXT(ctx);
jtgafb833d1999-08-19 00:55:39 +0000868 GLuint bitmask;
869 GLuint legal = (FRONT_EMISSION_BIT | BACK_EMISSION_BIT |
870 FRONT_SPECULAR_BIT | BACK_SPECULAR_BIT |
871 FRONT_DIFFUSE_BIT | BACK_DIFFUSE_BIT |
872 FRONT_AMBIENT_BIT | BACK_AMBIENT_BIT);
Keith Whitwellcab974c2000-12-26 05:09:27 +0000873 ASSERT_OUTSIDE_BEGIN_END(ctx);
jtgafb833d1999-08-19 00:55:39 +0000874
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +0000875 if (MESA_VERBOSE&VERBOSE_API)
876 fprintf(stderr, "glColorMaterial %s %s\n",
Brian Paul08836342001-03-03 20:33:27 +0000877 _mesa_lookup_enum_by_nr(face),
878 _mesa_lookup_enum_by_nr(mode));
Keith Whitwell06ac5921999-11-10 06:29:44 +0000879
Brian Paul08836342001-03-03 20:33:27 +0000880 bitmask = _mesa_material_bitmask(ctx, face, mode, legal, "glColorMaterial");
jtgafb833d1999-08-19 00:55:39 +0000881
Keith Whitwellcab974c2000-12-26 05:09:27 +0000882 if (ctx->Light.ColorMaterialBitmask == bitmask &&
883 ctx->Light.ColorMaterialFace == face &&
884 ctx->Light.ColorMaterialMode == mode)
885 return;
886
887 FLUSH_VERTICES(ctx, _NEW_LIGHT);
888 ctx->Light.ColorMaterialBitmask = bitmask;
889 ctx->Light.ColorMaterialFace = face;
890 ctx->Light.ColorMaterialMode = mode;
Keith Whitwell06ac5921999-11-10 06:29:44 +0000891
Keith Whitwell23caf202000-11-16 21:05:34 +0000892 if (ctx->Light.ColorMaterialEnabled) {
Keith Whitwellcab974c2000-12-26 05:09:27 +0000893 FLUSH_CURRENT( ctx, 0 );
Brian Paul08836342001-03-03 20:33:27 +0000894 _mesa_update_color_material( ctx, ctx->Current.Color );
Keith Whitwell23caf202000-11-16 21:05:34 +0000895 }
jtgafb833d1999-08-19 00:55:39 +0000896}
897
898
899
Brian Paulfbd8f211999-11-11 01:22:25 +0000900
Brian Paulfbd8f211999-11-11 01:22:25 +0000901
902void
903_mesa_GetMaterialfv( GLenum face, GLenum pname, GLfloat *params )
904{
905 GET_CURRENT_CONTEXT(ctx);
jtgafb833d1999-08-19 00:55:39 +0000906 GLuint f;
Keith Whitwellcab974c2000-12-26 05:09:27 +0000907 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); /* update materials */
jtgafb833d1999-08-19 00:55:39 +0000908
909 if (face==GL_FRONT) {
910 f = 0;
911 }
912 else if (face==GL_BACK) {
913 f = 1;
914 }
915 else {
Brian Paul08836342001-03-03 20:33:27 +0000916 _mesa_error( ctx, GL_INVALID_ENUM, "glGetMaterialfv(face)" );
jtgafb833d1999-08-19 00:55:39 +0000917 return;
918 }
919 switch (pname) {
920 case GL_AMBIENT:
921 COPY_4FV( params, ctx->Light.Material[f].Ambient );
922 break;
923 case GL_DIFFUSE:
924 COPY_4FV( params, ctx->Light.Material[f].Diffuse );
925 break;
926 case GL_SPECULAR:
927 COPY_4FV( params, ctx->Light.Material[f].Specular );
928 break;
929 case GL_EMISSION:
930 COPY_4FV( params, ctx->Light.Material[f].Emission );
931 break;
932 case GL_SHININESS:
933 *params = ctx->Light.Material[f].Shininess;
934 break;
935 case GL_COLOR_INDEXES:
936 params[0] = ctx->Light.Material[f].AmbientIndex;
937 params[1] = ctx->Light.Material[f].DiffuseIndex;
938 params[2] = ctx->Light.Material[f].SpecularIndex;
939 break;
940 default:
Brian Paul08836342001-03-03 20:33:27 +0000941 _mesa_error( ctx, GL_INVALID_ENUM, "glGetMaterialfv(pname)" );
jtgafb833d1999-08-19 00:55:39 +0000942 }
943}
944
945
946
Brian Paulfbd8f211999-11-11 01:22:25 +0000947void
948_mesa_GetMaterialiv( GLenum face, GLenum pname, GLint *params )
jtgafb833d1999-08-19 00:55:39 +0000949{
Brian Paulfbd8f211999-11-11 01:22:25 +0000950 GET_CURRENT_CONTEXT(ctx);
jtgafb833d1999-08-19 00:55:39 +0000951 GLuint f;
Keith Whitwellcab974c2000-12-26 05:09:27 +0000952 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); /* update materials */
jtgafb833d1999-08-19 00:55:39 +0000953
954 if (face==GL_FRONT) {
955 f = 0;
956 }
957 else if (face==GL_BACK) {
958 f = 1;
959 }
960 else {
Brian Paul08836342001-03-03 20:33:27 +0000961 _mesa_error( ctx, GL_INVALID_ENUM, "glGetMaterialiv(face)" );
jtgafb833d1999-08-19 00:55:39 +0000962 return;
963 }
964 switch (pname) {
965 case GL_AMBIENT:
966 params[0] = FLOAT_TO_INT( ctx->Light.Material[f].Ambient[0] );
967 params[1] = FLOAT_TO_INT( ctx->Light.Material[f].Ambient[1] );
968 params[2] = FLOAT_TO_INT( ctx->Light.Material[f].Ambient[2] );
969 params[3] = FLOAT_TO_INT( ctx->Light.Material[f].Ambient[3] );
970 break;
971 case GL_DIFFUSE:
972 params[0] = FLOAT_TO_INT( ctx->Light.Material[f].Diffuse[0] );
973 params[1] = FLOAT_TO_INT( ctx->Light.Material[f].Diffuse[1] );
974 params[2] = FLOAT_TO_INT( ctx->Light.Material[f].Diffuse[2] );
975 params[3] = FLOAT_TO_INT( ctx->Light.Material[f].Diffuse[3] );
976 break;
977 case GL_SPECULAR:
978 params[0] = FLOAT_TO_INT( ctx->Light.Material[f].Specular[0] );
979 params[1] = FLOAT_TO_INT( ctx->Light.Material[f].Specular[1] );
980 params[2] = FLOAT_TO_INT( ctx->Light.Material[f].Specular[2] );
981 params[3] = FLOAT_TO_INT( ctx->Light.Material[f].Specular[3] );
982 break;
983 case GL_EMISSION:
984 params[0] = FLOAT_TO_INT( ctx->Light.Material[f].Emission[0] );
985 params[1] = FLOAT_TO_INT( ctx->Light.Material[f].Emission[1] );
986 params[2] = FLOAT_TO_INT( ctx->Light.Material[f].Emission[2] );
987 params[3] = FLOAT_TO_INT( ctx->Light.Material[f].Emission[3] );
988 break;
989 case GL_SHININESS:
990 *params = ROUNDF( ctx->Light.Material[f].Shininess );
991 break;
992 case GL_COLOR_INDEXES:
993 params[0] = ROUNDF( ctx->Light.Material[f].AmbientIndex );
994 params[1] = ROUNDF( ctx->Light.Material[f].DiffuseIndex );
995 params[2] = ROUNDF( ctx->Light.Material[f].SpecularIndex );
996 break;
997 default:
Brian Paul08836342001-03-03 20:33:27 +0000998 _mesa_error( ctx, GL_INVALID_ENUM, "glGetMaterialfv(pname)" );
jtgafb833d1999-08-19 00:55:39 +0000999 }
1000}
1001
1002
1003
1004
1005/**********************************************************************/
1006/***** Lighting computation *****/
1007/**********************************************************************/
1008
1009
1010/*
1011 * Notes:
1012 * When two-sided lighting is enabled we compute the color (or index)
1013 * for both the front and back side of the primitive. Then, when the
1014 * orientation of the facet is later learned, we can determine which
1015 * color (or index) to use for rendering.
1016 *
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001017 * KW: We now know orientation in advance and only shade for
jtgafb833d1999-08-19 00:55:39 +00001018 * the side or sides which are actually required.
1019 *
1020 * Variables:
1021 * n = normal vector
1022 * V = vertex position
1023 * P = light source position
1024 * Pe = (0,0,0,1)
1025 *
1026 * Precomputed:
1027 * IF P[3]==0 THEN
1028 * // light at infinity
1029 * IF local_viewer THEN
Keith Whitwell14940c42000-11-05 18:40:57 +00001030 * _VP_inf_norm = unit vector from V to P // Precompute
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001031 * ELSE
jtgafb833d1999-08-19 00:55:39 +00001032 * // eye at infinity
Keith Whitwell14940c42000-11-05 18:40:57 +00001033 * _h_inf_norm = Normalize( VP + <0,0,1> ) // Precompute
jtgafb833d1999-08-19 00:55:39 +00001034 * ENDIF
1035 * ENDIF
1036 *
1037 * Functions:
1038 * Normalize( v ) = normalized vector v
1039 * Magnitude( v ) = length of vector v
1040 */
1041
1042
1043
1044/*
1045 * Whenever the spotlight exponent for a light changes we must call
1046 * this function to recompute the exponent lookup table.
1047 */
Brian Paula0faa7f2000-07-18 16:55:56 +00001048void
Brian Paul08836342001-03-03 20:33:27 +00001049_mesa_invalidate_spot_exp_table( struct gl_light *l )
Keith Whitwellcab974c2000-12-26 05:09:27 +00001050{
1051 l->_SpotExpTable[0][0] = -1;
1052}
1053
1054static void validate_spot_exp_table( struct gl_light *l )
jtgafb833d1999-08-19 00:55:39 +00001055{
Brian Paula0faa7f2000-07-18 16:55:56 +00001056 GLint i;
1057 GLdouble exponent = l->SpotExponent;
1058 GLdouble tmp = 0;
1059 GLint clamp = 0;
jtgafb833d1999-08-19 00:55:39 +00001060
Keith Whitwell14940c42000-11-05 18:40:57 +00001061 l->_SpotExpTable[0][0] = 0.0;
jtgafb833d1999-08-19 00:55:39 +00001062
Brian Paula0faa7f2000-07-18 16:55:56 +00001063 for (i = EXP_TABLE_SIZE - 1; i > 0 ;i--) {
jtgafb833d1999-08-19 00:55:39 +00001064 if (clamp == 0) {
Keith Whitwellcab974c2000-12-26 05:09:27 +00001065 tmp = pow(i / (GLdouble) (EXP_TABLE_SIZE - 1), exponent);
1066 if (tmp < FLT_MIN * 100.0) {
1067 tmp = 0.0;
1068 clamp = 1;
1069 }
jtgafb833d1999-08-19 00:55:39 +00001070 }
Keith Whitwell14940c42000-11-05 18:40:57 +00001071 l->_SpotExpTable[i][0] = tmp;
jtgafb833d1999-08-19 00:55:39 +00001072 }
Brian Paula0faa7f2000-07-18 16:55:56 +00001073 for (i = 0; i < EXP_TABLE_SIZE - 1; i++) {
Keith Whitwellcab974c2000-12-26 05:09:27 +00001074 l->_SpotExpTable[i][1] = (l->_SpotExpTable[i+1][0] -
1075 l->_SpotExpTable[i][0]);
jtgafb833d1999-08-19 00:55:39 +00001076 }
Keith Whitwell14940c42000-11-05 18:40:57 +00001077 l->_SpotExpTable[EXP_TABLE_SIZE-1][1] = 0.0;
jtgafb833d1999-08-19 00:55:39 +00001078}
1079
1080
1081
1082
1083/* Calculate a new shine table. Doing this here saves a branch in
1084 * lighting, and the cost of doing it early may be partially offset
1085 * by keeping a MRU cache of shine tables for various shine values.
1086 */
Keith Whitwellcab974c2000-12-26 05:09:27 +00001087void
Brian Paul08836342001-03-03 20:33:27 +00001088_mesa_invalidate_shine_table( GLcontext *ctx, GLuint i )
jtgafb833d1999-08-19 00:55:39 +00001089{
Keith Whitwellcab974c2000-12-26 05:09:27 +00001090 if (ctx->_ShineTable[i])
1091 ctx->_ShineTable[i]->refcount--;
1092 ctx->_ShineTable[i] = 0;
jtgafb833d1999-08-19 00:55:39 +00001093}
1094
Keith Whitwellcab974c2000-12-26 05:09:27 +00001095static void validate_shine_table( GLcontext *ctx, GLuint i, GLfloat shininess )
jtgafb833d1999-08-19 00:55:39 +00001096{
Keith Whitwell14940c42000-11-05 18:40:57 +00001097 struct gl_shine_tab *list = ctx->_ShineTabList;
jtgafb833d1999-08-19 00:55:39 +00001098 struct gl_shine_tab *s;
1099
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001100 foreach(s, list)
Keith Whitwellcab974c2000-12-26 05:09:27 +00001101 if ( s->shininess == shininess )
jtgafb833d1999-08-19 00:55:39 +00001102 break;
1103
Brian Paula0faa7f2000-07-18 16:55:56 +00001104 if (s == list) {
Keith Whitwell321f67c2001-01-13 05:48:25 +00001105 GLint j;
Keith Whitwellcab974c2000-12-26 05:09:27 +00001106 GLfloat *m;
1107
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001108 foreach(s, list)
Brian Paula0faa7f2000-07-18 16:55:56 +00001109 if (s->refcount == 0)
Keith Whitwellcab974c2000-12-26 05:09:27 +00001110 break;
jtgafb833d1999-08-19 00:55:39 +00001111
Keith Whitwellcab974c2000-12-26 05:09:27 +00001112 m = s->tab;
1113 m[0] = 0.0;
1114 if (shininess == 0.0) {
Keith Whitwell321f67c2001-01-13 05:48:25 +00001115 for (j = 1 ; j <= SHINE_TABLE_SIZE ; j++)
1116 m[j] = 1.0;
Keith Whitwellcab974c2000-12-26 05:09:27 +00001117 }
1118 else {
Keith Whitwell321f67c2001-01-13 05:48:25 +00001119 for (j = 1 ; j < SHINE_TABLE_SIZE ; j++) {
1120 GLdouble t, x = j / (GLfloat) (SHINE_TABLE_SIZE - 1);
Brian Paulf22c04c2001-01-04 16:22:18 +00001121 if (x < 0.005) /* underflow check */
1122 x = 0.005;
1123 t = pow(x, shininess);
Keith Whitwellcab974c2000-12-26 05:09:27 +00001124 if (t > 1e-20)
Keith Whitwell321f67c2001-01-13 05:48:25 +00001125 m[j] = t;
Keith Whitwellcab974c2000-12-26 05:09:27 +00001126 else
Keith Whitwell321f67c2001-01-13 05:48:25 +00001127 m[j] = 0.0;
Keith Whitwellcab974c2000-12-26 05:09:27 +00001128 }
1129 m[SHINE_TABLE_SIZE] = 1.0;
1130 }
1131
1132 s->shininess = shininess;
jtgafb833d1999-08-19 00:55:39 +00001133 }
1134
Keith Whitwellcab974c2000-12-26 05:09:27 +00001135 if (ctx->_ShineTable[i])
1136 ctx->_ShineTable[i]->refcount--;
1137
Keith Whitwell14940c42000-11-05 18:40:57 +00001138 ctx->_ShineTable[i] = s;
jtgafb833d1999-08-19 00:55:39 +00001139 move_to_tail( list, s );
1140 s->refcount++;
Keith Whitwellcab974c2000-12-26 05:09:27 +00001141}
1142
1143void
Brian Paul08836342001-03-03 20:33:27 +00001144_mesa_validate_all_lighting_tables( GLcontext *ctx )
Keith Whitwellcab974c2000-12-26 05:09:27 +00001145{
1146 GLint i;
1147 GLfloat shininess;
1148
1149 shininess = ctx->Light.Material[0].Shininess;
Brian Paul45323642001-02-13 23:55:30 +00001150 if (!ctx->_ShineTable[0] || ctx->_ShineTable[0]->shininess != shininess)
1151 validate_shine_table( ctx, 0, shininess );
Keith Whitwellcab974c2000-12-26 05:09:27 +00001152
1153 shininess = ctx->Light.Material[1].Shininess;
Brian Paul45323642001-02-13 23:55:30 +00001154 if (!ctx->_ShineTable[1] || ctx->_ShineTable[1]->shininess != shininess)
1155 validate_shine_table( ctx, 1, shininess );
Keith Whitwellcab974c2000-12-26 05:09:27 +00001156
1157 for (i = 0 ; i < MAX_LIGHTS ; i++)
1158 if (ctx->Light.Light[i]._SpotExpTable[0][0] == -1)
1159 validate_spot_exp_table( &ctx->Light.Light[i] );
jtgafb833d1999-08-19 00:55:39 +00001160}
1161
1162
1163
jtgafb833d1999-08-19 00:55:39 +00001164
1165/*
1166 * Examine current lighting parameters to determine if the optimized lighting
1167 * function can be used.
1168 * Also, precompute some lighting values such as the products of light
1169 * source and material ambient, diffuse and specular coefficients.
1170 */
Brian Paula0faa7f2000-07-18 16:55:56 +00001171void
Brian Paul08836342001-03-03 20:33:27 +00001172_mesa_update_lighting( GLcontext *ctx )
jtgafb833d1999-08-19 00:55:39 +00001173{
1174 struct gl_light *light;
Keith Whitwellcab974c2000-12-26 05:09:27 +00001175 ctx->_TriangleCaps &= ~DD_TRI_LIGHT_TWOSIDE;
Keith Whitwell1e1aac02000-11-13 20:02:56 +00001176 ctx->_NeedEyeCoords &= ~NEED_EYE_LIGHT;
1177 ctx->_NeedNormals &= ~NEED_NORMALS_LIGHT;
Keith Whitwell14940c42000-11-05 18:40:57 +00001178 ctx->Light._Flags = 0;
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001179
1180 if (!ctx->Light.Enabled)
Keith Whitwell1e1aac02000-11-13 20:02:56 +00001181 return;
1182
1183 ctx->_NeedNormals |= NEED_NORMALS_LIGHT;
1184
1185 if (ctx->Light.Model.TwoSide)
Keith Whitwellcab974c2000-12-26 05:09:27 +00001186 ctx->_TriangleCaps |= DD_TRI_LIGHT_TWOSIDE;
jtgafb833d1999-08-19 00:55:39 +00001187
1188 foreach(light, &ctx->Light.EnabledList) {
Keith Whitwell14940c42000-11-05 18:40:57 +00001189 ctx->Light._Flags |= light->_Flags;
jtgafb833d1999-08-19 00:55:39 +00001190 }
1191
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001192 ctx->Light._NeedVertices =
Keith Whitwell14940c42000-11-05 18:40:57 +00001193 ((ctx->Light._Flags & (LIGHT_POSITIONAL|LIGHT_SPOT)) ||
Keith Whitwell1e1aac02000-11-13 20:02:56 +00001194 ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR ||
1195 ctx->Light.Model.LocalViewer);
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001196
1197 if ((ctx->Light._Flags & LIGHT_POSITIONAL) ||
1198 ctx->Light.Model.LocalViewer)
Keith Whitwell1e1aac02000-11-13 20:02:56 +00001199 ctx->_NeedEyeCoords |= NEED_EYE_LIGHT;
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001200
1201
Keith Whitwell1e1aac02000-11-13 20:02:56 +00001202 /* XXX: This test is overkill & needs to be fixed both for software and
1203 * hardware t&l drivers. The above should be sufficient & should
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001204 * be tested to verify this.
Keith Whitwell1e1aac02000-11-13 20:02:56 +00001205 */
1206 if (ctx->Light._NeedVertices)
1207 ctx->_NeedEyeCoords |= NEED_EYE_LIGHT;
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001208
jtgafb833d1999-08-19 00:55:39 +00001209
Keith Whitwellcab974c2000-12-26 05:09:27 +00001210 /* Precompute some shading values. Although we reference
1211 * Light.Material here, we can get away without flushing
1212 * FLUSH_UPDATE_CURRENT, as when any outstanding material changes
1213 * are flushed, they will update the derived state at that time.
jtgafb833d1999-08-19 00:55:39 +00001214 */
Brian Paulb6bcae52001-01-23 23:39:36 +00001215 if (ctx->Visual.rgbMode) {
Keith Whitwell1e1aac02000-11-13 20:02:56 +00001216 GLuint sides = ctx->Light.Model.TwoSide ? 2 : 1;
jtgafb833d1999-08-19 00:55:39 +00001217 GLuint side;
1218 for (side=0; side < sides; side++) {
1219 struct gl_material *mat = &ctx->Light.Material[side];
Brian Paulc535ba52000-06-29 04:56:30 +00001220
Keith Whitwell14940c42000-11-05 18:40:57 +00001221 COPY_3V(ctx->Light._BaseColor[side], mat->Emission);
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001222 ACC_SCALE_3V(ctx->Light._BaseColor[side],
jtgafb833d1999-08-19 00:55:39 +00001223 ctx->Light.Model.Ambient,
1224 mat->Ambient);
1225
Brian Paul3041d052001-01-02 22:02:51 +00001226 UNCLAMPED_FLOAT_TO_CHAN(ctx->Light._BaseAlpha[side],
1227 ctx->Light.Material[side].Diffuse[3] );
jtgafb833d1999-08-19 00:55:39 +00001228 }
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001229
1230 foreach (light, &ctx->Light.EnabledList) {
jtgafb833d1999-08-19 00:55:39 +00001231 for (side=0; side< sides; side++) {
Brian Paulc535ba52000-06-29 04:56:30 +00001232 const struct gl_material *mat = &ctx->Light.Material[side];
Keith Whitwell14940c42000-11-05 18:40:57 +00001233 SCALE_3V( light->_MatDiffuse[side], light->Diffuse, mat->Diffuse );
1234 SCALE_3V( light->_MatAmbient[side], light->Ambient, mat->Ambient );
Keith Whitwell1e1aac02000-11-13 20:02:56 +00001235 SCALE_3V( light->_MatSpecular[side], light->Specular,
1236 mat->Specular);
jtgafb833d1999-08-19 00:55:39 +00001237 }
1238 }
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001239 }
Brian Paulc535ba52000-06-29 04:56:30 +00001240 else {
1241 static const GLfloat ci[3] = { .30, .59, .11 };
jtgafb833d1999-08-19 00:55:39 +00001242 foreach(light, &ctx->Light.EnabledList) {
Keith Whitwell14940c42000-11-05 18:40:57 +00001243 light->_dli = DOT3(ci, light->Diffuse);
1244 light->_sli = DOT3(ci, light->Specular);
jtgafb833d1999-08-19 00:55:39 +00001245 }
1246 }
1247}
1248
Brian Paulc535ba52000-06-29 04:56:30 +00001249
Keith Whitwell1e1aac02000-11-13 20:02:56 +00001250/* _NEW_MODELVIEW
1251 * _NEW_LIGHT
1252 * _TNL_NEW_NEED_EYE_COORDS
1253 *
1254 * Update on (_NEW_MODELVIEW | _NEW_LIGHT) when lighting is enabled.
1255 * Also update on lighting space changes.
jtgafb833d1999-08-19 00:55:39 +00001256 */
Brian Paula0faa7f2000-07-18 16:55:56 +00001257void
Brian Paul08836342001-03-03 20:33:27 +00001258_mesa_compute_light_positions( GLcontext *ctx )
jtgafb833d1999-08-19 00:55:39 +00001259{
1260 struct gl_light *light;
Keith Whitwell1e1aac02000-11-13 20:02:56 +00001261 static const GLfloat eye_z[3] = { 0, 0, 1 };
jtgafb833d1999-08-19 00:55:39 +00001262
Keith Whitwell1e1aac02000-11-13 20:02:56 +00001263 if (!ctx->Light.Enabled)
1264 return;
1265
1266 if (ctx->_NeedEyeCoords) {
1267 COPY_3V( ctx->_EyeZDir, eye_z );
1268 }
1269 else {
1270 TRANSFORM_NORMAL( ctx->_EyeZDir, eye_z, ctx->ModelView.m );
1271 }
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001272
jtgafb833d1999-08-19 00:55:39 +00001273 foreach (light, &ctx->Light.EnabledList) {
1274
Keith Whitwell14940c42000-11-05 18:40:57 +00001275 if (ctx->_NeedEyeCoords) {
1276 COPY_4FV( light->_Position, light->EyePosition );
jtgafb833d1999-08-19 00:55:39 +00001277 }
Brian Paulde82d062000-06-26 23:37:46 +00001278 else {
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001279 TRANSFORM_POINT( light->_Position, ctx->ModelView.inv,
Brian Paulde82d062000-06-26 23:37:46 +00001280 light->EyePosition );
1281 }
jtgafb833d1999-08-19 00:55:39 +00001282
Keith Whitwell14940c42000-11-05 18:40:57 +00001283 if (!(light->_Flags & LIGHT_POSITIONAL)) {
jtgafb833d1999-08-19 00:55:39 +00001284 /* VP (VP) = Normalize( Position ) */
Keith Whitwell14940c42000-11-05 18:40:57 +00001285 COPY_3V( light->_VP_inf_norm, light->_Position );
1286 NORMALIZE_3FV( light->_VP_inf_norm );
jtgafb833d1999-08-19 00:55:39 +00001287
Brian Paulde82d062000-06-26 23:37:46 +00001288 if (!ctx->Light.Model.LocalViewer) {
Keith Whitwell14940c42000-11-05 18:40:57 +00001289 /* _h_inf_norm = Normalize( V_to_P + <0,0,1> ) */
1290 ADD_3V( light->_h_inf_norm, light->_VP_inf_norm, ctx->_EyeZDir);
1291 NORMALIZE_3FV( light->_h_inf_norm );
jtgafb833d1999-08-19 00:55:39 +00001292 }
Keith Whitwell14940c42000-11-05 18:40:57 +00001293 light->_VP_inf_spot_attenuation = 1.0;
jtgafb833d1999-08-19 00:55:39 +00001294 }
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001295
Keith Whitwell14940c42000-11-05 18:40:57 +00001296 if (light->_Flags & LIGHT_SPOT) {
Keith Whitwell1e1aac02000-11-13 20:02:56 +00001297 if (ctx->_NeedEyeCoords) {
Keith Whitwell14940c42000-11-05 18:40:57 +00001298 COPY_3V( light->_NormDirection, light->EyeDirection );
Brian Paulde82d062000-06-26 23:37:46 +00001299 }
1300 else {
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001301 TRANSFORM_NORMAL( light->_NormDirection,
jtgafb833d1999-08-19 00:55:39 +00001302 light->EyeDirection,
1303 ctx->ModelView.m);
1304 }
1305
Keith Whitwell14940c42000-11-05 18:40:57 +00001306 NORMALIZE_3FV( light->_NormDirection );
jtgafb833d1999-08-19 00:55:39 +00001307
Keith Whitwell14940c42000-11-05 18:40:57 +00001308 if (!(light->_Flags & LIGHT_POSITIONAL)) {
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001309 GLfloat PV_dot_dir = - DOT3(light->_VP_inf_norm,
Keith Whitwell14940c42000-11-05 18:40:57 +00001310 light->_NormDirection);
jtgafb833d1999-08-19 00:55:39 +00001311
Keith Whitwell14940c42000-11-05 18:40:57 +00001312 if (PV_dot_dir > light->_CosCutoff) {
jtgafb833d1999-08-19 00:55:39 +00001313 double x = PV_dot_dir * (EXP_TABLE_SIZE-1);
1314 int k = (int) x;
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001315 light->_VP_inf_spot_attenuation =
1316 (light->_SpotExpTable[k][0] +
Keith Whitwell14940c42000-11-05 18:40:57 +00001317 (x-k)*light->_SpotExpTable[k][1]);
jtgafb833d1999-08-19 00:55:39 +00001318 }
Brian Paulde82d062000-06-26 23:37:46 +00001319 else {
Keith Whitwell14940c42000-11-05 18:40:57 +00001320 light->_VP_inf_spot_attenuation = 0;
Brian Paulde82d062000-06-26 23:37:46 +00001321 }
jtgafb833d1999-08-19 00:55:39 +00001322 }
1323 }
1324 }
1325}
1326
1327