Debian Codegraph @

debian/external_deps/fetk/sg/src/gl/light.c

raw
1 /* $Id: light.c,v 1.2 2000/10/27 15:21:40 mholst Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 2.2
6 * Copyright (C) 1995-1997 Brian Paul
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the Free
20 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include <assert.h>
24 #include <float.h>
25 #include <math.h>
26 #include <stdlib.h>
27 #include "context.h"
28 #include "light.h"
29 #include "dlist.h"
30 #include "macros.h"
31 #include "matrix.h"
32 #include "types.h"
33 #include "vb.h"
34 #include "xform.h"
35
36
37 #ifdef DEBUG
38 # define ASSERT(X) assert(X)
39 #else
40 # define ASSERT(X)
41 #endif
42
43
44 #define DEG2RAD (M_PI/180.0)
45
46
47
48 void gl_ShadeModel( GLcontext *ctx, GLenum mode )
49 {
50 if (INSIDE_BEGIN_END(ctx)) {
51 gl_error( ctx, GL_INVALID_OPERATION, "glShadeModel" );
52 return;
53 }
54
55 switch (mode) {
56 case GL_FLAT:
57 case GL_SMOOTH:
58 if (ctx->Light.ShadeModel!=mode) {
59 ctx->Light.ShadeModel = mode;
60 ctx->NewState |= NEW_RASTER_OPS;
61 }
62 break;
63 default:
64 gl_error( ctx, GL_INVALID_ENUM, "glShadeModel" );
65 }
66 }
67
68
69
70 void gl_Lightfv( GLcontext *ctx,
71 GLenum light, GLenum pname, const GLfloat *params,
72 GLint nparams )
73 {
74 GLint l;
75
76 if (INSIDE_BEGIN_END(ctx)) {
77 gl_error( ctx, GL_INVALID_OPERATION, "glShadeModel" );
78 return;
79 }
80
81 l = (GLint) (light - GL_LIGHT0);
82
83 if (l<0 || l>=MAX_LIGHTS) {
84 gl_error( ctx, GL_INVALID_ENUM, "glLight" );
85 return;
86 }
87
88 switch (pname) {
89 case GL_AMBIENT:
90 COPY_4V( ctx->Light.Light[l].Ambient, params );
91 break;
92 case GL_DIFFUSE:
93 COPY_4V( ctx->Light.Light[l].Diffuse, params );
94 break;
95 case GL_SPECULAR:
96 COPY_4V( ctx->Light.Light[l].Specular, params );
97 break;
98 case GL_POSITION:
99 /* transform position by ModelView matrix */
100 TRANSFORM_POINT( ctx->Light.Light[l].Position, ctx->ModelViewMatrix,
101 params );
102 break;
103 case GL_SPOT_DIRECTION:
104 /* transform direction by inverse modelview */
105 {
106 GLfloat direction[4];
107 direction[0] = params[0];
108 direction[1] = params[1];
109 direction[2] = params[2];
110 direction[3] = 0.0;
111 if (!ctx->ModelViewInvValid) {
112 gl_compute_modelview_inverse( ctx );
113 }
114 gl_transform_vector( ctx->Light.Light[l].Direction,
115 direction, ctx->ModelViewInv);
116 }
117 break;
118 case GL_SPOT_EXPONENT:
119 if (params[0]<0.0 || params[0]>128.0) {
120 gl_error( ctx, GL_INVALID_VALUE, "glLight" );
121 return;
122 }
123 ctx->Light.Light[l].SpotExponent = params[0];
124 gl_compute_spot_exp_table( &ctx->Light.Light[l] );
125 break;
126 case GL_SPOT_CUTOFF:
127 if ((params[0]<0.0 || params[0]>90.0) && params[0]!=180.0) {
128 gl_error( ctx, GL_INVALID_VALUE, "glLight" );
129 return;
130 }
131 ctx->Light.Light[l].SpotCutoff = params[0];
132 ctx->Light.Light[l].CosCutoff = cos(params[0]*DEG2RAD);
133 break;
134 case GL_CONSTANT_ATTENUATION:
135 if (params[0]<0.0) {
136 gl_error( ctx, GL_INVALID_VALUE, "glLight" );
137 return;
138 }
139 ctx->Light.Light[l].ConstantAttenuation = params[0];
140 break;
141 case GL_LINEAR_ATTENUATION:
142 if (params[0]<0.0) {
143 gl_error( ctx, GL_INVALID_VALUE, "glLight" );
144 return;
145 }
146 ctx->Light.Light[l].LinearAttenuation = params[0];
147 break;
148 case GL_QUADRATIC_ATTENUATION:
149 if (params[0]<0.0) {
150 gl_error( ctx, GL_INVALID_VALUE, "glLight" );
151 return;
152 }
153 ctx->Light.Light[l].QuadraticAttenuation = params[0];
154 break;
155 default:
156 gl_error( ctx, GL_INVALID_ENUM, "glLight" );
157 break;
158 }
159
160 ctx->NewState |= NEW_LIGHTING;
161 }
162
163
164
165 void gl_GetLightfv( GLcontext *ctx,
166 GLenum light, GLenum pname, GLfloat *params )
167 {
168 GLint l = (GLint) (light - GL_LIGHT0);
169
170 if (l<0 || l>=MAX_LIGHTS) {
171 gl_error( ctx, GL_INVALID_ENUM, "glGetLightfv" );
172 return;
173 }
174
175 switch (pname) {
176 case GL_AMBIENT:
177 COPY_4V( params, ctx->Light.Light[l].Ambient );
178 break;
179 case GL_DIFFUSE:
180 COPY_4V( params, ctx->Light.Light[l].Diffuse );
181 break;
182 case GL_SPECULAR:
183 COPY_4V( params, ctx->Light.Light[l].Specular );
184 break;
185 case GL_POSITION:
186 COPY_4V( params, ctx->Light.Light[l].Position );
187 break;
188 case GL_SPOT_DIRECTION:
189 COPY_3V( params, ctx->Light.Light[l].Direction );
190 break;
191 case GL_SPOT_EXPONENT:
192 params[0] = ctx->Light.Light[l].SpotExponent;
193 break;
194 case GL_SPOT_CUTOFF:
195 params[0] = ctx->Light.Light[l].SpotCutoff;
196 break;
197 case GL_CONSTANT_ATTENUATION:
198 params[0] = ctx->Light.Light[l].ConstantAttenuation;
199 break;
200 case GL_LINEAR_ATTENUATION:
201 params[0] = ctx->Light.Light[l].LinearAttenuation;
202 break;
203 case GL_QUADRATIC_ATTENUATION:
204 params[0] = ctx->Light.Light[l].QuadraticAttenuation;
205 break;
206 default:
207 gl_error( ctx, GL_INVALID_ENUM, "glGetLightfv" );
208 break;
209 }
210 }
211
212
213
214 void gl_GetLightiv( GLcontext *ctx, GLenum light, GLenum pname, GLint *params )
215 {
216 GLint l = (GLint) (light - GL_LIGHT0);
217
218 if (l<0 || l>=MAX_LIGHTS) {
219 gl_error( ctx, GL_INVALID_ENUM, "glGetLightiv" );
220 return;
221 }
222
223 switch (pname) {
224 case GL_AMBIENT:
225 params[0] = FLOAT_TO_INT(ctx->Light.Light[l].Ambient[0]);
226 params[1] = FLOAT_TO_INT(ctx->Light.Light[l].Ambient[1]);
227 params[2] = FLOAT_TO_INT(ctx->Light.Light[l].Ambient[2]);
228 params[3] = FLOAT_TO_INT(ctx->Light.Light[l].Ambient[3]);
229 break;
230 case GL_DIFFUSE:
231 params[0] = FLOAT_TO_INT(ctx->Light.Light[l].Diffuse[0]);
232 params[1] = FLOAT_TO_INT(ctx->Light.Light[l].Diffuse[1]);
233 params[2] = FLOAT_TO_INT(ctx->Light.Light[l].Diffuse[2]);
234 params[3] = FLOAT_TO_INT(ctx->Light.Light[l].Diffuse[3]);
235 break;
236 case GL_SPECULAR:
237 params[0] = FLOAT_TO_INT(ctx->Light.Light[l].Specular[0]);
238 params[1] = FLOAT_TO_INT(ctx->Light.Light[l].Specular[1]);
239 params[2] = FLOAT_TO_INT(ctx->Light.Light[l].Specular[2]);
240 params[3] = FLOAT_TO_INT(ctx->Light.Light[l].Specular[3]);
241 break;
242 case GL_POSITION:
243 params[0] = ctx->Light.Light[l].Position[0];
244 params[1] = ctx->Light.Light[l].Position[1];
245 params[2] = ctx->Light.Light[l].Position[2];
246 params[3] = ctx->Light.Light[l].Position[3];
247 break;
248 case GL_SPOT_DIRECTION:
249 params[0] = ctx->Light.Light[l].Direction[0];
250 params[1] = ctx->Light.Light[l].Direction[1];
251 params[2] = ctx->Light.Light[l].Direction[2];
252 break;
253 case GL_SPOT_EXPONENT:
254 params[0] = ctx->Light.Light[l].SpotExponent;
255 break;
256 case GL_SPOT_CUTOFF:
257 params[0] = ctx->Light.Light[l].SpotCutoff;
258 break;
259 case GL_CONSTANT_ATTENUATION:
260 params[0] = ctx->Light.Light[l].ConstantAttenuation;
261 break;
262 case GL_LINEAR_ATTENUATION:
263 params[0] = ctx->Light.Light[l].LinearAttenuation;
264 break;
265 case GL_QUADRATIC_ATTENUATION:
266 params[0] = ctx->Light.Light[l].QuadraticAttenuation;
267 break;
268 default:
269 gl_error( ctx, GL_INVALID_ENUM, "glGetLightiv" );
270 break;
271 }
272 }
273
274
275
276 /**********************************************************************/
277 /*** Light Model ***/
278 /**********************************************************************/
279
280
281 void gl_LightModelfv( GLcontext *ctx, GLenum pname, const GLfloat *params )
282 {
283 switch (pname) {
284 case GL_LIGHT_MODEL_AMBIENT:
285 COPY_4V( ctx->Light.Model.Ambient, params );
286 break;
287 case GL_LIGHT_MODEL_LOCAL_VIEWER:
288 if (params[0]==0.0)
289 ctx->Light.Model.LocalViewer = GL_FALSE;
290 else
291 ctx->Light.Model.LocalViewer = GL_TRUE;
292 break;
293 case GL_LIGHT_MODEL_TWO_SIDE:
294 if (params[0]==0.0)
295 ctx->Light.Model.TwoSide = GL_FALSE;
296 else
297 ctx->Light.Model.TwoSide = GL_TRUE;
298 break;
299 default:
300 gl_error( ctx, GL_INVALID_ENUM, "glLightModel" );
301 break;
302 }
303 ctx->NewState |= NEW_LIGHTING;
304 }
305
306
307
308
309 /********** MATERIAL **********/
310
311
312 /*
313 * Given a face and pname value (ala glColorMaterial), compute a bitmask
314 * of the targeted material values.
315 */
316 GLuint gl_material_bitmask( GLenum face, GLenum pname )
317 {
318 GLuint bitmask = 0;
319
320 /* Make a bitmask indicating what material attribute(s) we're updating */
321 switch (pname) {
322 case GL_EMISSION:
323 bitmask |= FRONT_EMISSION_BIT | BACK_EMISSION_BIT;
324 break;
325 case GL_AMBIENT:
326 bitmask |= FRONT_AMBIENT_BIT | BACK_AMBIENT_BIT;
327 break;
328 case GL_DIFFUSE:
329 bitmask |= FRONT_DIFFUSE_BIT | BACK_DIFFUSE_BIT;
330 break;
331 case GL_SPECULAR:
332 bitmask |= FRONT_SPECULAR_BIT | BACK_SPECULAR_BIT;
333 break;
334 case GL_SHININESS:
335 bitmask |= FRONT_SHININESS_BIT | BACK_SHININESS_BIT;
336 break;
337 case GL_AMBIENT_AND_DIFFUSE:
338 bitmask |= FRONT_AMBIENT_BIT | BACK_AMBIENT_BIT;
339 bitmask |= FRONT_DIFFUSE_BIT | BACK_DIFFUSE_BIT;
340 break;
341 case GL_COLOR_INDEXES:
342 bitmask |= FRONT_INDEXES_BIT | BACK_INDEXES_BIT;
343 break;
344 default:
345 abort();
346 }
347
348 ASSERT( face==GL_FRONT || face==GL_BACK || face==GL_FRONT_AND_BACK );
349
350 if (face==GL_FRONT) {
351 bitmask &= FRONT_MATERIAL_BITS;
352 }
353 else if (face==GL_BACK) {
354 bitmask &= BACK_MATERIAL_BITS;
355 }
356
357 return bitmask;
358 }
359
360
361
362 /*
363 * This is called by glColor() when GL_COLOR_MATERIAL is enabled and
364 * called by glMaterial() when GL_COLOR_MATERIAL is disabled.
365 */
366 void gl_set_material( GLcontext *ctx, GLuint bitmask, const GLfloat *params )
367 {
368 struct gl_material *mat;
369
370 if (INSIDE_BEGIN_END(ctx)) {
371 struct vertex_buffer *VB = ctx->VB;
372 /* Save per-vertex material changes in the Vertex Buffer.
373 * The update_material function will eventually update the global
374 * ctx->Light.Material values.
375 */
376 mat = VB->Material[VB->Count];
377 VB->MaterialMask[VB->Count] |= bitmask;
378 VB->MaterialChanges = GL_TRUE;
379 }
380 else {
381 /* just update the global material property */
382 mat = ctx->Light.Material;
383 ctx->NewState |= NEW_LIGHTING;
384 }
385
386 if (bitmask & FRONT_AMBIENT_BIT) {
387 COPY_4V( mat[0].Ambient, params );
388 }
389 if (bitmask & BACK_AMBIENT_BIT) {
390 COPY_4V( mat[1].Ambient, params );
391 }
392 if (bitmask & FRONT_DIFFUSE_BIT) {
393 COPY_4V( mat[0].Diffuse, params );
394 }
395 if (bitmask & BACK_DIFFUSE_BIT) {
396 COPY_4V( mat[1].Diffuse, params );
397 }
398 if (bitmask & FRONT_SPECULAR_BIT) {
399 COPY_4V( mat[0].Specular, params );
400 }
401 if (bitmask & BACK_SPECULAR_BIT) {
402 COPY_4V( mat[1].Specular, params );
403 }
404 if (bitmask & FRONT_EMISSION_BIT) {
405 COPY_4V( mat[0].Emission, params );
406 }
407 if (bitmask & BACK_EMISSION_BIT) {
408 COPY_4V( mat[1].Emission, params );
409 }
410 if (bitmask & FRONT_SHININESS_BIT) {
411 mat[0].Shininess = CLAMP( params[0], 0.0, 128.0 );
412 gl_compute_material_shine_table( &mat[0] );
413 }
414 if (bitmask & BACK_SHININESS_BIT) {
415 mat[1].Shininess = CLAMP( params[0], 0.0, 128.0 );
416 gl_compute_material_shine_table( &mat[1] );
417 }
418 if (bitmask & FRONT_INDEXES_BIT) {
419 mat[0].AmbientIndex = params[0];
420 mat[0].DiffuseIndex = params[1];
421 mat[0].SpecularIndex = params[2];
422 }
423 if (bitmask & BACK_INDEXES_BIT) {
424 mat[1].AmbientIndex = params[0];
425 mat[1].DiffuseIndex = params[1];
426 mat[1].SpecularIndex = params[2];
427 }
428 }
429
430
431
432 void gl_ColorMaterial( GLcontext *ctx, GLenum face, GLenum mode )
433 {
434 if (INSIDE_BEGIN_END(ctx)) {
435 gl_error( ctx, GL_INVALID_OPERATION, "glColorMaterial" );
436 return;
437 }
438 switch (face) {
439 case GL_FRONT:
440 case GL_BACK:
441 case GL_FRONT_AND_BACK:
442 ctx->Light.ColorMaterialFace = face;
443 break;
444 default:
445 gl_error( ctx, GL_INVALID_ENUM, "glColorMaterial(face)" );
446 return;
447 }
448 switch (mode) {
449 case GL_EMISSION:
450 case GL_AMBIENT:
451 case GL_DIFFUSE:
452 case GL_SPECULAR:
453 case GL_AMBIENT_AND_DIFFUSE:
454 ctx->Light.ColorMaterialMode = mode;
455 break;
456 default:
457 gl_error( ctx, GL_INVALID_ENUM, "glColorMaterial(mode)" );
458 return;
459 }
460
461 ctx->Light.ColorMaterialBitmask = gl_material_bitmask( face, mode );
462 }
463
464
465
466 /*
467 * This is only called via the api_function_table struct or by the
468 * display list executor.
469 */
470 void gl_Materialfv( GLcontext *ctx,
471 GLenum face, GLenum pname, const GLfloat *params )
472 {
473 GLuint bitmask;
474
475 /* error checking */
476 if (face!=GL_FRONT && face!=GL_BACK && face!=GL_FRONT_AND_BACK) {
477 gl_error( ctx, GL_INVALID_ENUM, "glMaterial(face)" );
478 return;
479 }
480 switch (pname) {
481 case GL_EMISSION:
482 case GL_AMBIENT:
483 case GL_DIFFUSE:
484 case GL_SPECULAR:
485 case GL_SHININESS:
486 case GL_AMBIENT_AND_DIFFUSE:
487 case GL_COLOR_INDEXES:
488 /* OK */
489 break;
490 default:
491 gl_error( ctx, GL_INVALID_ENUM, "glMaterial(pname)" );
492 return;
493 }
494
495 /* convert face and pname to a bitmask */
496 bitmask = gl_material_bitmask( face, pname );
497
498 if (ctx->Light.ColorMaterialEnabled) {
499 /* The material values specified by glColorMaterial() can't be */
500 /* updated by glMaterial() while GL_COLOR_MATERIAL is enabled! */
501 bitmask &= ~ctx->Light.ColorMaterialBitmask;
502 }
503
504 gl_set_material( ctx, bitmask, params );
505 }
506
507
508
509
510 void gl_GetMaterialfv( GLcontext *ctx,
511 GLenum face, GLenum pname, GLfloat *params )
512 {
513 GLuint f;
514
515 if (INSIDE_BEGIN_END(ctx)) {
516 gl_error( ctx, GL_INVALID_OPERATION, "glGetMaterialfv" );
517 return;
518 }
519 if (face==GL_FRONT) {
520 f = 0;
521 }
522 else if (face==GL_BACK) {
523 f = 1;
524 }
525 else {
526 gl_error( ctx, GL_INVALID_ENUM, "glGetMaterialfv(face)" );
527 return;
528 }
529 switch (pname) {
530 case GL_AMBIENT:
531 COPY_4V( params, ctx->Light.Material[f].Ambient );
532 break;
533 case GL_DIFFUSE:
534 COPY_4V( params, ctx->Light.Material[f].Diffuse );
535 break;
536 case GL_SPECULAR:
537 COPY_4V( params, ctx->Light.Material[f].Specular );
538 break;
539 case GL_EMISSION:
540 COPY_4V( params, ctx->Light.Material[f].Emission );
541 break;
542 case GL_SHININESS:
543 *params = ctx->Light.Material[f].Shininess;
544 break;
545 case GL_COLOR_INDEXES:
546 params[0] = ctx->Light.Material[f].AmbientIndex;
547 params[1] = ctx->Light.Material[f].DiffuseIndex;
548 params[2] = ctx->Light.Material[f].SpecularIndex;
549 break;
550 default:
551 gl_error( ctx, GL_INVALID_ENUM, "glGetMaterialfv(pname)" );
552 }
553 }
554
555
556
557 void gl_GetMaterialiv( GLcontext *ctx,
558 GLenum face, GLenum pname, GLint *params )
559 {
560 GLuint f;
561
562 if (INSIDE_BEGIN_END(ctx)) {
563 gl_error( ctx, GL_INVALID_OPERATION, "glGetMaterialiv" );
564 return;
565 }
566 if (face==GL_FRONT) {
567 f = 0;
568 }
569 else if (face==GL_BACK) {
570 f = 1;
571 }
572 else {
573 gl_error( ctx, GL_INVALID_ENUM, "glGetMaterialiv(face)" );
574 return;
575 }
576 switch (pname) {
577 case GL_AMBIENT:
578 params[0] = FLOAT_TO_INT( ctx->Light.Material[f].Ambient[0] );
579 params[1] = FLOAT_TO_INT( ctx->Light.Material[f].Ambient[1] );
580 params[2] = FLOAT_TO_INT( ctx->Light.Material[f].Ambient[2] );
581 params[3] = FLOAT_TO_INT( ctx->Light.Material[f].Ambient[3] );
582 break;
583 case GL_DIFFUSE:
584 params[0] = FLOAT_TO_INT( ctx->Light.Material[f].Diffuse[0] );
585 params[1] = FLOAT_TO_INT( ctx->Light.Material[f].Diffuse[1] );
586 params[2] = FLOAT_TO_INT( ctx->Light.Material[f].Diffuse[2] );
587 params[3] = FLOAT_TO_INT( ctx->Light.Material[f].Diffuse[3] );
588 break;
589 case GL_SPECULAR:
590 params[0] = FLOAT_TO_INT( ctx->Light.Material[f].Specular[0] );
591 params[1] = FLOAT_TO_INT( ctx->Light.Material[f].Specular[1] );
592 params[2] = FLOAT_TO_INT( ctx->Light.Material[f].Specular[2] );
593 params[3] = FLOAT_TO_INT( ctx->Light.Material[f].Specular[3] );
594 break;
595 case GL_EMISSION:
596 params[0] = FLOAT_TO_INT( ctx->Light.Material[f].Emission[0] );
597 params[1] = FLOAT_TO_INT( ctx->Light.Material[f].Emission[1] );
598 params[2] = FLOAT_TO_INT( ctx->Light.Material[f].Emission[2] );
599 params[3] = FLOAT_TO_INT( ctx->Light.Material[f].Emission[3] );
600 break;
601 case GL_SHININESS:
602 *params = ROUNDF( ctx->Light.Material[f].Shininess );
603 break;
604 case GL_COLOR_INDEXES:
605 params[0] = ROUNDF( ctx->Light.Material[f].AmbientIndex );
606 params[1] = ROUNDF( ctx->Light.Material[f].DiffuseIndex );
607 params[2] = ROUNDF( ctx->Light.Material[f].SpecularIndex );
608 break;
609 default:
610 gl_error( ctx, GL_INVALID_ENUM, "glGetMaterialfv(pname)" );
611 }
612 }
613
614
615
616
617 /**********************************************************************/
618 /***** Lighting computation *****/
619 /**********************************************************************/
620
621
622 /*
623 * Notes:
624 * When two-sided lighting is enabled we compute the color (or index)
625 * for both the front and back side of the primitive. Then, when the
626 * orientation of the facet is later learned, we can determine which
627 * color (or index) to use for rendering.
628 *
629 * Variables:
630 * n = normal vector
631 * V = vertex position
632 * P = light source position
633 * Pe = (0,0,0,1)
634 *
635 * Precomputed:
636 * IF P[3]==0 THEN
637 * // light at infinity
638 * IF local_viewer THEN
639 * VP_inf_norm = unit vector from V to P // Precompute
640 * ELSE
641 * // eye at infinity
642 * h_inf_norm = Normalize( VP + <0,0,1> ) // Precompute
643 * ENDIF
644 * ENDIF
645 *
646 * Functions:
647 * Normalize( v ) = normalized vector v
648 * Magnitude( v ) = length of vector v
649 */
650
651
652
653 /*
654 * Whenever the spotlight exponent for a light changes we must call
655 * this function to recompute the exponent lookup table.
656 */
657 void gl_compute_spot_exp_table( struct gl_light *l )
658 {
659 int i;
660 double exponent = l->SpotExponent;
661 double tmp;
662 int clamp = 0;
663
664 l->SpotExpTable[0][0] = 0.0;
665
666 for (i=EXP_TABLE_SIZE-1;i>0;i--) {
667 if (clamp == 0) {
668 tmp = pow(i/(double)(EXP_TABLE_SIZE-1), exponent);
669 if (tmp < FLT_MIN*100.0) {
670 tmp = 0.0;
671 clamp = 1;
672 }
673 }
674 l->SpotExpTable[i][0] = tmp;
675 }
676 for (i=0;i<EXP_TABLE_SIZE-1;i++) {
677 l->SpotExpTable[i][1] = l->SpotExpTable[i+1][0] - l->SpotExpTable[i][0];
678 }
679 l->SpotExpTable[EXP_TABLE_SIZE-1][1] = 0.0;
680 }
681
682
683
684 /*
685 * Whenever the shininess of a material changes we must call this
686 * function to recompute the exponential lookup table.
687 */
688 void gl_compute_material_shine_table( struct gl_material *m )
689 {
690 int i;
691 double exponent = m->Shininess;
692
693 m->ShineTable[0] = 0.0;
694 for (i=1;i<SHINE_TABLE_SIZE;i++) {
695 double x = pow( i/(double)(SHINE_TABLE_SIZE-1), exponent );
696 if (x<1.0e-10) {
697 m->ShineTable[i] = 0.0;
698 }
699 else {
700 m->ShineTable[i] = x;
701 }
702 }
703 }
704
705
706
707 /*
708 * Examine current lighting parameters to determine if the optimized lighting
709 * function can be used. Also, precompute some lighting values which are
710 * used by gl_color_shade_vertices_fast().
711 */
712 void gl_update_lighting( GLcontext *ctx )
713 {
714 GLint i;
715 struct gl_light *prev_enabled, *light;
716
717 if (!ctx->Light.Enabled) {
718 /* If lighting is not enabled, we can skip all this. */
719 return;
720 }
721
722 /* base color = material_emission + global_ambient */
723 ctx->Light.BaseColor[0] = ctx->Light.Material[0].Emission[0]
724 + ctx->Light.Model.Ambient[0] * ctx->Light.Material[0].Ambient[0];
725 ctx->Light.BaseColor[1] = ctx->Light.Material[0].Emission[1]
726 + ctx->Light.Model.Ambient[1] * ctx->Light.Material[0].Ambient[1];
727 ctx->Light.BaseColor[2] = ctx->Light.Material[0].Emission[2]
728 + ctx->Light.Model.Ambient[2] * ctx->Light.Material[0].Ambient[2];
729 ctx->Light.BaseColor[3] = MIN2( ctx->Light.Material[0].Diffuse[3], 1.0F );
730
731 /* Setup linked list of enabled light sources */
732 prev_enabled = NULL;
733 ctx->Light.FirstEnabled = NULL;
734 for (i=0;i<MAX_LIGHTS;i++) {
735 ctx->Light.Light[i].NextEnabled = NULL;
736 if (ctx->Light.Light[i].Enabled) {
737 if (prev_enabled) {
738 prev_enabled->NextEnabled = &ctx->Light.Light[i];
739 }
740 else {
741 ctx->Light.FirstEnabled = &ctx->Light.Light[i];
742 }
743 prev_enabled = &ctx->Light.Light[i];
744 }
745 }
746
747 /* Precompute some lighting stuff */
748 for (light = ctx->Light.FirstEnabled; light; light = light->NextEnabled) {
749 struct gl_material *mat = &ctx->Light.Material[0];
750 /* Add each light's ambient component to base color */
751 ctx->Light.BaseColor[0] += light->Ambient[0] * mat->Ambient[0];
752 ctx->Light.BaseColor[1] += light->Ambient[1] * mat->Ambient[1];
753 ctx->Light.BaseColor[2] += light->Ambient[2] * mat->Ambient[2];
754 /* compute product of light's ambient with front material ambient */
755 light->MatAmbient[0] = light->Ambient[0] * mat->Ambient[0];
756 light->MatAmbient[1] = light->Ambient[1] * mat->Ambient[1];
757 light->MatAmbient[2] = light->Ambient[2] * mat->Ambient[2];
758 /* compute product of light's diffuse with front material diffuse */
759 light->MatDiffuse[0] = light->Diffuse[0] * mat->Diffuse[0];
760 light->MatDiffuse[1] = light->Diffuse[1] * mat->Diffuse[1];
761 light->MatDiffuse[2] = light->Diffuse[2] * mat->Diffuse[2];
762 /* compute product of light's specular with front material specular */
763 light->MatSpecular[0] = light->Specular[0] * mat->Specular[0];
764 light->MatSpecular[1] = light->Specular[1] * mat->Specular[1];
765 light->MatSpecular[2] = light->Specular[2] * mat->Specular[2];
766
767 /* VP (VP) = Normalize( Position ) */
768 COPY_3V( light->VP_inf_norm, light->Position );
769 NORMALIZE_3V( light->VP_inf_norm );
770
771 /* h_inf_norm = Normalize( V_to_P + <0,0,1> ) */
772 COPY_3V( light->h_inf_norm, light->VP_inf_norm );
773 light->h_inf_norm[2] += 1.0F;
774 NORMALIZE_3V( light->h_inf_norm );
775
776 COPY_3V( light->NormDirection, light->Direction );
777 NORMALIZE_3V( light->NormDirection );
778
779 /* Compute color index diffuse and specular light intensities */
780 light->dli = 0.30F * light->Diffuse[0]
781 + 0.59F * light->Diffuse[1]
782 + 0.11F * light->Diffuse[2];
783 light->sli = 0.30F * light->Specular[0]
784 + 0.59F * light->Specular[1]
785 + 0.11F * light->Specular[2];
786
787 }
788
789 /* Determine if the fast lighting function can be used */
790 ctx->Light.Fast = GL_TRUE;
791 if ( ctx->Light.BaseColor[0]<0.0F
792 || ctx->Light.BaseColor[1]<0.0F
793 || ctx->Light.BaseColor[2]<0.0F
794 || ctx->Light.BaseColor[3]<0.0F
795 || ctx->Light.Model.TwoSide
796 || ctx->Light.Model.LocalViewer
797 || ctx->Light.ColorMaterialEnabled) {
798 ctx->Light.Fast = GL_FALSE;
799 }
800 else {
801 for (light=ctx->Light.FirstEnabled; light; light=light->NextEnabled) {
802 if ( light->Position[3]!=0.0F
803 || light->SpotCutoff!=180.0F
804 || light->MatDiffuse[0]<0.0F
805 || light->MatDiffuse[1]<0.0F
806 || light->MatDiffuse[2]<0.0F
807 || light->MatSpecular[0]<0.0F
808 || light->MatSpecular[1]<0.0F
809 || light->MatSpecular[2]<0.0F) {
810 ctx->Light.Fast = GL_FALSE;
811 break;
812 }
813 }
814 }
815 }
816
817
818
819
820
821 /*
822 * Use current lighting/material settings to compute the RGBA colors of
823 * an array of vertexes.
824 * Input: n - number of vertexes to process
825 * vertex - array of vertex positions in eye coordinates
826 * normal - array of surface normal vectors
827 * twoside - 0 = front face shading only, 1 = two-sided lighting
828 * Output: frontcolor - array of resulting front-face colors
829 * backcolor - array of resulting back-face colors
830 */
831 void gl_color_shade_vertices( GLcontext *ctx,
832 GLuint n,
833 GLfloat vertex[][4],
834 GLfloat normal[][3],
835 GLuint twoside,
836 GLfixed frontcolor[][4],
837 GLfixed backcolor[][4] )
838 {
839 GLint side, j;
840 GLfloat rscale, gscale, bscale, ascale;
841
842 /* Compute scale factor to go from floats in [0,1] to integers or fixed
843 * point values:
844 */
845 rscale = (GLfloat) ( (GLint) ctx->Visual->RedScale << ctx->ColorShift );
846 gscale = (GLfloat) ( (GLint) ctx->Visual->GreenScale << ctx->ColorShift );
847 bscale = (GLfloat) ( (GLint) ctx->Visual->BlueScale << ctx->ColorShift );
848 ascale = (GLfloat) ( (GLint) ctx->Visual->AlphaScale << ctx->ColorShift );
849
850
851 for (side=0;side<=twoside;side++) {
852 GLfloat baseR, baseG, baseB, baseA;
853 GLfixed sumA;
854 struct gl_light *light;
855 struct gl_material *mat;
856
857 mat = &ctx->Light.Material[side];
858
859 /*** Compute color contribution from global lighting ***/
860 baseR = mat->Emission[0] + ctx->Light.Model.Ambient[0] * mat->Ambient[0];
861 baseG = mat->Emission[1] + ctx->Light.Model.Ambient[1] * mat->Ambient[1];
862 baseB = mat->Emission[2] + ctx->Light.Model.Ambient[2] * mat->Ambient[2];
863
864 /* Alpha is simple, same for all vertices */
865 baseA = mat->Diffuse[3];
866 sumA = (GLfixed) (CLAMP( baseA, 0.0F, 1.0F ) * ascale);
867
868 for (j=0;j<n;j++) {
869 GLfloat sumR, sumG, sumB;
870 GLfloat nx, ny, nz;
871
872 if (side==0) {
873 /* shade frontside */
874 nx = normal[j][0];
875 ny = normal[j][1];
876 nz = normal[j][2];
877 }
878 else {
879 /* shade backside */
880 nx = -normal[j][0];
881 ny = -normal[j][1];
882 nz = -normal[j][2];
883 }
884
885 sumR = baseR;
886 sumG = baseG;
887 sumB = baseB;
888
889 /* Add contribution from each enabled light source */
890 for (light=ctx->Light.FirstEnabled; light; light=light->NextEnabled) {
891 GLfloat ambientR, ambientG, ambientB;
892 GLfloat attenuation, spot;
893 GLfloat VPx, VPy, VPz; /* unit vector from vertex to light */
894 GLfloat n_dot_VP; /* n dot VP */
895
896 /* compute VP and attenuation */
897 if (light->Position[3]==0.0) {
898 /* directional light */
899 VPx = light->VP_inf_norm[0];
900 VPy = light->VP_inf_norm[1];
901 VPz = light->VP_inf_norm[2];
902 attenuation = 1.0F;
903 }
904 else {
905 /* positional light */
906 GLfloat d; /* distance from vertex to light */
907 VPx = light->Position[0] - vertex[j][0];
908 VPy = light->Position[1] - vertex[j][1];
909 VPz = light->Position[2] - vertex[j][2];
910 d = (GLfloat) sqrt( VPx*VPx + VPy*VPy + VPz*VPz );
911 if (d>0.001F) {
912 GLfloat invd = 1.0F / d;
913 VPx *= invd;
914 VPy *= invd;
915 VPz *= invd;
916 }
917 attenuation = 1.0F / (light->ConstantAttenuation
918 + d * (light->LinearAttenuation
919 + d * light->QuadraticAttenuation));
920 }
921
922 /* spotlight factor */
923 if (light->SpotCutoff==180.0F) {
924 /* not a spot light */
925 spot = 1.0F;
926 }
927 else {
928 GLfloat PVx, PVy, PVz, PV_dot_dir;
929 PVx = -VPx;
930 PVy = -VPy;
931 PVz = -VPz;
932 PV_dot_dir = PVx*light->NormDirection[0]
933 + PVy*light->NormDirection[1]
934 + PVz*light->NormDirection[2];
935 if (PV_dot_dir<=0.0F || PV_dot_dir<light->CosCutoff) {
936 /* outside of cone */
937 spot = 0.0F;
938 }
939 else {
940 double x = PV_dot_dir * (EXP_TABLE_SIZE-1);
941 int k = (int) x;
942 spot = light->SpotExpTable[k][0]
943 + (x-k)*light->SpotExpTable[k][1];
944 }
945 }
946
947 ambientR = light->Ambient[0] * mat->Ambient[0];
948 ambientG = light->Ambient[1] * mat->Ambient[1];
949 ambientB = light->Ambient[2] * mat->Ambient[2];
950
951 /* Compute dot product or normal and vector from V to light pos */
952 n_dot_VP = nx * VPx + ny * VPy + nz * VPz;
953
954 /* diffuse and specular terms */
955 if (n_dot_VP<=0.0F) {
956 /* surface face away from light, no diffuse or specular */
957 GLfloat t = attenuation * spot;
958 sumR += t * ambientR;
959 sumG += t * ambientG;
960 sumB += t * ambientB;
961 /* done with this light */
962 }
963 else {
964 GLfloat diffuseR, diffuseG, diffuseB;
965 GLfloat specularR, specularG, specularB;
966 GLfloat hx, hy, hz, n_dot_h, t;
967
968 /* diffuse term */
969 diffuseR = n_dot_VP * light->Diffuse[0] * mat->Diffuse[0];
970 diffuseG = n_dot_VP * light->Diffuse[1] * mat->Diffuse[1];
971 diffuseB = n_dot_VP * light->Diffuse[2] * mat->Diffuse[2];
972
973 /* specular term */
974 if (ctx->Light.Model.LocalViewer) {
975 GLfloat vx, vy, vz, vlen;
976 vx = vertex[j][0];
977 vy = vertex[j][1];
978 vz = vertex[j][2];
979 vlen = sqrt( vx*vx + vy*vy + vz*vz );
980 if (vlen>0.0001F) {
981 GLfloat invlen = 1.0F / vlen;
982 vx *= invlen;
983 vy *= invlen;
984 vz *= invlen;
985 }
986 /* h = VP + VPe */
987 hx = VPx - vx;
988 hy = VPy - vy;
989 hz = VPz - vz;
990 }
991 else {
992 /* h = VP + <0,0,1> */
993 hx = VPx;
994 hy = VPy;
995 hz = VPz + 1.0F;
996 }
997
998 /* attention: h is not normalized, done later if needed */
999 n_dot_h = nx*hx + ny*hy + nz*hz;
1000
1001 if (n_dot_h<=0.0F) {
1002 specularR = 0.0F;
1003 specularG = 0.0F;
1004 specularB = 0.0F;
1005 }
1006 else {
1007 GLfloat spec_coef;
1008 /* now `correct' the dot product */
1009 n_dot_h = n_dot_h / sqrt( hx*hx + hy*hy + hz*hz );
1010 if (n_dot_h>1.0F) {
1011 /* only happens if normal vector length > 1.0 */
1012 spec_coef = pow( n_dot_h, mat->Shininess );
1013 }
1014 else {
1015 /* use table lookup approximation */
1016 int k = (int) (n_dot_h * (GLfloat) (SHINE_TABLE_SIZE-1));
1017 spec_coef = mat->ShineTable[k];
1018 }
1019 if (spec_coef<1.0e-10) {
1020 specularR = 0.0F;
1021 specularG = 0.0F;
1022 specularB = 0.0F;
1023 }
1024 else {
1025 specularR = spec_coef * light->Specular[0]
1026 * mat->Specular[0];
1027 specularG = spec_coef * light->Specular[1]
1028 * mat->Specular[1];
1029 specularB = spec_coef * light->Specular[2]
1030 * mat->Specular[2];
1031 }
1032 }
1033
1034 t = attenuation * spot;
1035 sumR += t * (ambientR + diffuseR + specularR);
1036 sumG += t * (ambientG + diffuseG + specularG);
1037 sumB += t * (ambientB + diffuseB + specularB);
1038 }
1039
1040 } /*loop over lights*/
1041
1042 if (side==0) {
1043 /* clamp and convert to integer or fixed point */
1044 frontcolor[j][0] = (GLfixed) (CLAMP( sumR, 0.0F, 1.0F ) * rscale);
1045 frontcolor[j][1] = (GLfixed) (CLAMP( sumG, 0.0F, 1.0F ) * gscale);
1046 frontcolor[j][2] = (GLfixed) (CLAMP( sumB, 0.0F, 1.0F ) * bscale);
1047 frontcolor[j][3] = sumA;
1048 }
1049 else {
1050 /* clamp and convert to integer or fixed point */
1051 backcolor[j][0] = (GLfixed) (CLAMP( sumR, 0.0F, 1.0F ) * rscale);
1052 backcolor[j][1] = (GLfixed) (CLAMP( sumG, 0.0F, 1.0F ) * gscale);
1053 backcolor[j][2] = (GLfixed) (CLAMP( sumB, 0.0F, 1.0F ) * bscale);
1054 backcolor[j][3] = sumA;
1055 }
1056 } /*loop over vertices*/
1057
1058 } /*for side*/
1059 }
1060
1061
1062
1063 /*
1064 * This is an optimized version of the above function.
1065 */
1066 void gl_color_shade_vertices_fast( GLcontext *ctx,
1067 GLuint n,
1068 GLfloat vertex[][4],
1069 GLfloat normal[][3],
1070 GLuint twoside,
1071 GLfixed frontcolor[][4],
1072 GLfixed backcolor[][4] )
1073 {
1074 GLint j;
1075 GLfloat rscale, gscale, bscale, ascale;
1076 GLfixed A;
1077
1078 /* Compute scale factor to go from floats in [0,1] to integers or fixed
1079 * point values:
1080 */
1081 rscale = (GLfloat) ( (GLint) ctx->Visual->RedScale << ctx->ColorShift );
1082 gscale = (GLfloat) ( (GLint) ctx->Visual->GreenScale << ctx->ColorShift );
1083 bscale = (GLfloat) ( (GLint) ctx->Visual->BlueScale << ctx->ColorShift );
1084 ascale = (GLfloat) ( (GLint) ctx->Visual->AlphaScale << ctx->ColorShift );
1085
1086 /* Alpha is easy to compute, same for all vertices */
1087 A = (GLfixed) ( ctx->Light.BaseColor[3] * ascale);
1088
1089 /* Loop over vertices */
1090 for (j=0;j<n;j++) {
1091 GLfloat R, G, B;
1092 GLfloat nx, ny, nz;
1093 struct gl_light *light;
1094
1095 /* the normal vector */
1096 nx = normal[j][0];
1097 ny = normal[j][1];
1098 nz = normal[j][2];
1099
1100 /* base color from global illumination and enabled light's ambient */
1101 R = ctx->Light.BaseColor[0];
1102 G = ctx->Light.BaseColor[1];
1103 B = ctx->Light.BaseColor[2];
1104
1105 /* Add contribution from each light source */
1106 for (light=ctx->Light.FirstEnabled; light; light=light->NextEnabled) {
1107 GLfloat n_dot_VP; /* n dot VP */
1108
1109 n_dot_VP = nx * light->VP_inf_norm[0]
1110 + ny * light->VP_inf_norm[1]
1111 + nz * light->VP_inf_norm[2];
1112
1113 /* diffuse and specular terms */
1114 if (n_dot_VP>0.0F) {
1115 GLfloat n_dot_h;
1116
1117 /** add diffuse term **/
1118 R += n_dot_VP * light->MatDiffuse[0];
1119 G += n_dot_VP * light->MatDiffuse[1];
1120 B += n_dot_VP * light->MatDiffuse[2];
1121
1122 /** specular term **/
1123 /* dot product of n and h_inf_norm */
1124 n_dot_h = nx * light->h_inf_norm[0]
1125 + ny * light->h_inf_norm[1]
1126 + nz * light->h_inf_norm[2];
1127 if (n_dot_h>0.0F) {
1128 if (n_dot_h>1.0F) {
1129 /* only happens if Magnitude(n) > 1.0 */
1130 GLfloat spec_coef = pow( n_dot_h,
1131 ctx->Light.Material[0].Shininess );
1132 if (spec_coef>1.0e-10F) {
1133 R += spec_coef * light->MatSpecular[0];
1134 G += spec_coef * light->MatSpecular[1];
1135 B += spec_coef * light->MatSpecular[2];
1136 }
1137 }
1138 else {
1139 /* use table lookup approximation */
1140 int k = (int) (n_dot_h * (GLfloat) (SHINE_TABLE_SIZE-1));
1141 GLfloat spec_coef = ctx->Light.Material[0].ShineTable[k];
1142 R += spec_coef * light->MatSpecular[0];
1143 G += spec_coef * light->MatSpecular[1];
1144 B += spec_coef * light->MatSpecular[2];
1145 }
1146 }
1147 }
1148
1149 } /*loop over lights*/
1150
1151 /* clamp and convert to integer or fixed point */
1152 frontcolor[j][0] = (GLfixed) (MIN2( R, 1.0F ) * rscale);
1153 frontcolor[j][1] = (GLfixed) (MIN2( G, 1.0F ) * gscale);
1154 frontcolor[j][2] = (GLfixed) (MIN2( B, 1.0F ) * bscale);
1155 frontcolor[j][3] = A;
1156
1157 } /*loop over vertices*/
1158 }
1159
1160
1161
1162 /*
1163 * Use current lighting/material settings to compute the color indexes
1164 * for an array of vertices.
1165 * Input: n - number of vertices to shade
1166 * vertex - array of [n] vertex position in viewing coordinates
1167 * normal - array of [n] surface normal vector
1168 * twoside - 0 = front face shading only, 1 = two-sided lighting
1169 * Output: frontindex - resulting array of [n] front-face color indexes
1170 * backindex - resulting array of [n] back-face color indexes
1171 */
1172 void gl_index_shade_vertices( GLcontext *ctx,
1173 GLuint n,
1174 GLfloat vertex[][4],
1175 GLfloat normal[][3],
1176 GLuint twoside,
1177 GLuint frontindex[],
1178 GLuint backindex[] )
1179 {
1180 GLint side, j;
1181 GLuint *output_index;
1182
1183 for (side=0;side<=twoside;side++) {
1184 struct gl_material *mat = &ctx->Light.Material[side];
1185
1186 if (side==0) {
1187 output_index = frontindex;
1188 }
1189 else {
1190 output_index = backindex;
1191 }
1192
1193 /* loop over vertices */
1194 for (j=0;j<n;j++) {
1195 GLfloat index;
1196 GLfloat diffuse, specular; /* accumulated diffuse and specular */
1197 GLfloat nx, ny, nz; /* normal vector */
1198 struct gl_light *light;
1199
1200 if (side==0) {
1201 /* shade frontside */
1202 nx = normal[j][0];
1203 ny = normal[j][1];
1204 nz = normal[j][2];
1205 }
1206 else {
1207 /* shade backside */
1208 nx = -normal[j][0];
1209 ny = -normal[j][1];
1210 nz = -normal[j][2];
1211 }
1212
1213 diffuse = specular = 0.0F;
1214
1215 /* Accumulate diffuse and specular from each light source */
1216 for (light=ctx->Light.FirstEnabled; light; light=light->NextEnabled) {
1217 GLfloat attenuation;
1218 GLfloat lx, ly, lz; /* unit vector from vertex to light */
1219 GLfloat l_dot_norm; /* dot product of l and n */
1220
1221 /* compute l and attenuation */
1222 if (light->Position[3]==0.0) {
1223 /* directional light */
1224 /* Effectively, l is a vector from the origin to the light. */
1225 lx = light->VP_inf_norm[0];
1226 ly = light->VP_inf_norm[1];
1227 lz = light->VP_inf_norm[2];
1228 attenuation = 1.0F;
1229 }
1230 else {
1231 /* positional light */
1232 GLfloat d; /* distance from vertex to light */
1233 lx = light->Position[0] - vertex[j][0];
1234 ly = light->Position[1] - vertex[j][1];
1235 lz = light->Position[2] - vertex[j][2];
1236 d = (GLfloat) sqrt( lx*lx + ly*ly + lz*lz );
1237 if (d>0.001F) {
1238 GLfloat invd = 1.0F / d;
1239 lx *= invd;
1240 ly *= invd;
1241 lz *= invd;
1242 }
1243 attenuation = 1.0F / (light->ConstantAttenuation
1244 + d * (light->LinearAttenuation
1245 + d * light->QuadraticAttenuation));
1246 }
1247
1248 l_dot_norm = lx*nx + ly*ny + lz*nz;
1249
1250 if (l_dot_norm>0.0F) {
1251 GLfloat spot_times_atten;
1252
1253 /* spotlight factor */
1254 if (light->SpotCutoff==180.0F) {
1255 /* not a spot light */
1256 spot_times_atten = attenuation;
1257 }
1258 else {
1259 GLfloat v[3], dot;
1260 v[0] = -lx; /* v points from light to vertex */
1261 v[1] = -ly;
1262 v[2] = -lz;
1263 dot = DOT3( v, light->NormDirection );
1264 if (dot<=0.0F || dot<light->CosCutoff) {
1265 /* outside of cone */
1266 spot_times_atten = 0.0F;
1267 }
1268 else {
1269 double x = dot * (EXP_TABLE_SIZE-1);
1270 int k = (int) x;
1271 GLfloat spot = light->SpotExpTable[k][0]
1272 + (x-k)*light->SpotExpTable[k][1];
1273 spot_times_atten = spot * attenuation;
1274 }
1275 }
1276
1277 /* accumulate diffuse term */
1278 diffuse += l_dot_norm * light->dli * spot_times_atten;
1279
1280 /* accumulate specular term */
1281 {
1282 GLfloat hx, hy, hz, dot, spec_coef;
1283
1284 /* specular term */
1285 if (ctx->Light.Model.LocalViewer) {
1286 GLfloat vx, vy, vz, vlen;
1287 vx = vertex[j][0];
1288 vy = vertex[j][1];
1289 vz = vertex[j][2];
1290 vlen = sqrt( vx*vx + vy*vy + vz*vz );
1291 if (vlen>0.0001F) {
1292 GLfloat invlen = 1.0F / vlen;
1293 vx *= invlen;
1294 vy *= invlen;
1295 vz *= invlen;
1296 }
1297 hx = lx - vx;
1298 hy = ly - vy;
1299 hz = lz - vz;
1300 }
1301 else {
1302 hx = lx;
1303 hy = ly;
1304 hz = lz + 1.0F;
1305 }
1306 /* attention: s is not normalized, done later if necessary */
1307 dot = hx*nx + hy*ny + hz*nz;
1308
1309 if (dot<=0.0F) {
1310 spec_coef = 0.0F;
1311 }
1312 else {
1313 /* now `correct' the dot product */
1314 dot = dot / sqrt(hx*hx + hy*hy + hz*hz);
1315 if (dot>1.0F) {
1316 spec_coef = pow( dot, mat->Shininess );
1317 }
1318 else {
1319 int k = (int) (dot * (GLfloat)(SHINE_TABLE_SIZE-1));
1320 spec_coef = mat->ShineTable[k];
1321 }
1322 }
1323 specular += spec_coef * light->sli * spot_times_atten;
1324 }
1325 }
1326
1327 } /*loop over lights*/
1328
1329 /* Now compute final color index */
1330 if (specular>1.0F) {
1331 index = mat->SpecularIndex;
1332 }
1333 else {
1334 GLfloat d_a, s_a;
1335 d_a = mat->DiffuseIndex - mat->AmbientIndex;
1336 s_a = mat->SpecularIndex - mat->AmbientIndex;
1337
1338 index = mat->AmbientIndex
1339 + diffuse * (1.0F-specular) * d_a
1340 + specular * s_a;
1341 if (index>mat->SpecularIndex) {
1342 index = mat->SpecularIndex;
1343 }
1344 }
1345 output_index[j] = (GLuint) (GLint) index;
1346
1347 } /*for vertex*/
1348
1349 } /*for side*/
1350 }