3 * Mesa 3-D graphics library
6 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 * Keith Whitwell <keith@tungstengraphics.com>
32 #if (IDX & LIGHT_TWOSIDE)
39 /* define TRACE to trace lighting code */
43 * ctx is the current context
44 * VB is the vertex buffer
45 * stage is the lighting stage-private data
46 * input is the vector of eye or object-space vertex coordinates
48 static void TAG(light_rgba_spec
)( GLcontext
*ctx
,
49 struct vertex_buffer
*VB
,
50 struct tnl_pipeline_stage
*stage
,
53 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
54 GLfloat (*base
)[3] = ctx
->Light
._BaseColor
;
58 const GLuint vstride
= input
->stride
;
59 const GLfloat
*vertex
= (GLfloat
*)input
->data
;
60 const GLuint nstride
= VB
->NormalPtr
->stride
;
61 const GLfloat
*normal
= (GLfloat
*)VB
->NormalPtr
->data
;
63 GLfloat (*Fcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[0].data
;
64 GLfloat (*Bcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[1].data
;
65 GLfloat (*Fspec
)[4] = (GLfloat (*)[4]) store
->LitSecondary
[0].data
;
66 GLfloat (*Bspec
)[4] = (GLfloat (*)[4]) store
->LitSecondary
[1].data
;
68 const GLuint nr
= VB
->Count
;
74 fprintf(stderr
, "%s\n", __FUNCTION__
);
77 VB
->ColorPtr
[0] = &store
->LitColor
[0];
78 VB
->SecondaryColorPtr
[0] = &store
->LitSecondary
[0];
79 sumA
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
81 if (IDX
& LIGHT_TWOSIDE
) {
82 VB
->ColorPtr
[1] = &store
->LitColor
[1];
83 VB
->SecondaryColorPtr
[1] = &store
->LitSecondary
[1];
84 sumA
[1] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
87 /* Side-effects done, can we finish now?
89 if (stage
->changed_inputs
== 0)
92 for (j
= 0; j
< nr
; j
++,STRIDE_F(vertex
,vstride
),STRIDE_F(normal
,nstride
)) {
93 GLfloat sum
[2][3], spec
[2][3];
94 struct gl_light
*light
;
96 if ( IDX
& LIGHT_MATERIAL
) {
97 update_materials( ctx
, store
);
98 sumA
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
99 if (IDX
& LIGHT_TWOSIDE
)
100 sumA
[1] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
103 COPY_3V(sum
[0], base
[0]);
106 if (IDX
& LIGHT_TWOSIDE
) {
107 COPY_3V(sum
[1], base
[1]);
111 /* Add contribution from each enabled light source */
112 foreach (light
, &ctx
->Light
.EnabledList
) {
118 GLfloat VP
[3]; /* unit vector from vertex to light */
119 GLfloat n_dot_VP
; /* n dot VP */
122 /* compute VP and attenuation */
123 if (!(light
->_Flags
& LIGHT_POSITIONAL
)) {
124 /* directional light */
125 COPY_3V(VP
, light
->_VP_inf_norm
);
126 attenuation
= light
->_VP_inf_spot_attenuation
;
129 GLfloat d
; /* distance from vertex to light */
131 SUB_3V(VP
, light
->_Position
, vertex
);
133 d
= (GLfloat
) LEN_3FV( VP
);
136 GLfloat invd
= 1.0F
/ d
;
137 SELF_SCALE_SCALAR_3V(VP
, invd
);
140 attenuation
= 1.0F
/ (light
->ConstantAttenuation
+ d
*
141 (light
->LinearAttenuation
+ d
*
142 light
->QuadraticAttenuation
));
144 /* spotlight attenuation */
145 if (light
->_Flags
& LIGHT_SPOT
) {
146 GLfloat PV_dot_dir
= - DOT3(VP
, light
->_NormDirection
);
148 if (PV_dot_dir
<light
->_CosCutoff
) {
149 continue; /* this light makes no contribution */
152 GLdouble x
= PV_dot_dir
* (EXP_TABLE_SIZE
-1);
154 GLfloat spot
= (GLfloat
) (light
->_SpotExpTable
[k
][0]
155 + (x
-k
)*light
->_SpotExpTable
[k
][1]);
161 if (attenuation
< 1e-3)
162 continue; /* this light makes no contribution */
164 /* Compute dot product or normal and vector from V to light pos */
165 n_dot_VP
= DOT3( normal
, VP
);
167 /* Which side gets the diffuse & specular terms? */
168 if (n_dot_VP
< 0.0F
) {
169 ACC_SCALE_SCALAR_3V(sum
[0], attenuation
, light
->_MatAmbient
[0]);
170 if (!(IDX
& LIGHT_TWOSIDE
)) {
175 n_dot_VP
= -n_dot_VP
;
178 if (IDX
& LIGHT_TWOSIDE
) {
179 ACC_SCALE_SCALAR_3V( sum
[1], attenuation
, light
->_MatAmbient
[1]);
186 COPY_3V(contrib
, light
->_MatAmbient
[side
]);
187 ACC_SCALE_SCALAR_3V(contrib
, n_dot_VP
, light
->_MatDiffuse
[side
]);
188 ACC_SCALE_SCALAR_3V(sum
[side
], attenuation
, contrib
);
190 /* specular term - cannibalize VP... */
191 if (ctx
->Light
.Model
.LocalViewer
) {
195 SUB_3V(VP
, VP
, v
); /* h = VP + VPe */
199 else if (light
->_Flags
& LIGHT_POSITIONAL
) {
201 ACC_3V(h
, ctx
->_EyeZDir
);
205 h
= light
->_h_inf_norm
;
208 n_dot_h
= correction
* DOT3(normal
, h
);
210 if (n_dot_h
> 0.0F
) {
212 struct gl_shine_tab
*tab
= ctx
->_ShineTable
[side
];
213 GET_SHINE_TAB_ENTRY( tab
, n_dot_h
, spec_coef
);
215 if (spec_coef
> 1.0e-10) {
216 spec_coef
*= attenuation
;
217 ACC_SCALE_SCALAR_3V( spec
[side
], spec_coef
,
218 light
->_MatSpecular
[side
]);
221 } /*loop over lights*/
223 COPY_3V( Fcolor
[j
], sum
[0] );
224 COPY_3V( Fspec
[j
], spec
[0] );
225 Fcolor
[j
][3] = sumA
[0];
227 if (IDX
& LIGHT_TWOSIDE
) {
228 COPY_3V( Bcolor
[j
], sum
[1] );
229 COPY_3V( Bspec
[j
], spec
[1] );
230 Bcolor
[j
][3] = sumA
[1];
236 static void TAG(light_rgba
)( GLcontext
*ctx
,
237 struct vertex_buffer
*VB
,
238 struct tnl_pipeline_stage
*stage
,
241 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
244 GLfloat (*base
)[3] = ctx
->Light
._BaseColor
;
247 const GLuint vstride
= input
->stride
;
248 const GLfloat
*vertex
= (GLfloat
*) input
->data
;
249 const GLuint nstride
= VB
->NormalPtr
->stride
;
250 const GLfloat
*normal
= (GLfloat
*)VB
->NormalPtr
->data
;
252 GLfloat (*Fcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[0].data
;
253 GLfloat (*Bcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[1].data
;
254 GLfloat (*color
[2])[4];
256 const GLuint nr
= VB
->Count
;
259 fprintf(stderr
, "%s\n", __FUNCTION__
);
268 VB
->ColorPtr
[0] = &store
->LitColor
[0];
269 sumA
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
271 if (IDX
& LIGHT_TWOSIDE
) {
272 VB
->ColorPtr
[1] = &store
->LitColor
[1];
273 sumA
[1] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
276 if (stage
->changed_inputs
== 0)
279 for (j
= 0; j
< nr
; j
++,STRIDE_F(vertex
,vstride
),STRIDE_F(normal
,nstride
)) {
281 struct gl_light
*light
;
283 if ( IDX
& LIGHT_MATERIAL
) {
284 update_materials( ctx
, store
);
285 sumA
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
286 if (IDX
& LIGHT_TWOSIDE
)
287 sumA
[1] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
290 COPY_3V(sum
[0], base
[0]);
292 if ( IDX
& LIGHT_TWOSIDE
)
293 COPY_3V(sum
[1], base
[1]);
295 /* Add contribution from each enabled light source */
296 foreach (light
, &ctx
->Light
.EnabledList
) {
302 GLfloat attenuation
= 1.0;
303 GLfloat VP
[3]; /* unit vector from vertex to light */
304 GLfloat n_dot_VP
; /* n dot VP */
307 /* compute VP and attenuation */
308 if (!(light
->_Flags
& LIGHT_POSITIONAL
)) {
309 /* directional light */
310 COPY_3V(VP
, light
->_VP_inf_norm
);
311 attenuation
= light
->_VP_inf_spot_attenuation
;
314 GLfloat d
; /* distance from vertex to light */
317 SUB_3V(VP
, light
->_Position
, vertex
);
319 d
= (GLfloat
) LEN_3FV( VP
);
322 GLfloat invd
= 1.0F
/ d
;
323 SELF_SCALE_SCALAR_3V(VP
, invd
);
326 attenuation
= 1.0F
/ (light
->ConstantAttenuation
+ d
*
327 (light
->LinearAttenuation
+ d
*
328 light
->QuadraticAttenuation
));
330 /* spotlight attenuation */
331 if (light
->_Flags
& LIGHT_SPOT
) {
332 GLfloat PV_dot_dir
= - DOT3(VP
, light
->_NormDirection
);
334 if (PV_dot_dir
<light
->_CosCutoff
) {
335 continue; /* this light makes no contribution */
338 GLdouble x
= PV_dot_dir
* (EXP_TABLE_SIZE
-1);
340 GLfloat spot
= (GLfloat
) (light
->_SpotExpTable
[k
][0]
341 + (x
-k
)*light
->_SpotExpTable
[k
][1]);
347 if (attenuation
< 1e-3)
348 continue; /* this light makes no contribution */
350 /* Compute dot product or normal and vector from V to light pos */
351 n_dot_VP
= DOT3( normal
, VP
);
353 /* which side are we lighting? */
354 if (n_dot_VP
< 0.0F
) {
355 ACC_SCALE_SCALAR_3V(sum
[0], attenuation
, light
->_MatAmbient
[0]);
357 if (!(IDX
& LIGHT_TWOSIDE
))
362 n_dot_VP
= -n_dot_VP
;
365 if (IDX
& LIGHT_TWOSIDE
) {
366 ACC_SCALE_SCALAR_3V( sum
[1], attenuation
, light
->_MatAmbient
[1]);
372 COPY_3V(contrib
, light
->_MatAmbient
[side
]);
375 ACC_SCALE_SCALAR_3V(contrib
, n_dot_VP
, light
->_MatDiffuse
[side
]);
377 /* specular term - cannibalize VP... */
379 if (ctx
->Light
.Model
.LocalViewer
) {
383 SUB_3V(VP
, VP
, v
); /* h = VP + VPe */
387 else if (light
->_Flags
& LIGHT_POSITIONAL
) {
389 ACC_3V(h
, ctx
->_EyeZDir
);
393 h
= light
->_h_inf_norm
;
396 n_dot_h
= correction
* DOT3(normal
, h
);
401 struct gl_shine_tab
*tab
= ctx
->_ShineTable
[side
];
403 GET_SHINE_TAB_ENTRY( tab
, n_dot_h
, spec_coef
);
405 ACC_SCALE_SCALAR_3V( contrib
, spec_coef
,
406 light
->_MatSpecular
[side
]);
410 ACC_SCALE_SCALAR_3V( sum
[side
], attenuation
, contrib
);
413 COPY_3V( Fcolor
[j
], sum
[0] );
414 Fcolor
[j
][3] = sumA
[0];
416 if (IDX
& LIGHT_TWOSIDE
) {
417 COPY_3V( Bcolor
[j
], sum
[1] );
418 Bcolor
[j
][3] = sumA
[1];
426 /* As below, but with just a single light.
428 static void TAG(light_fast_rgba_single
)( GLcontext
*ctx
,
429 struct vertex_buffer
*VB
,
430 struct tnl_pipeline_stage
*stage
,
434 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
435 const GLuint nstride
= VB
->NormalPtr
->stride
;
436 const GLfloat
*normal
= (GLfloat
*)VB
->NormalPtr
->data
;
437 GLfloat (*Fcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[0].data
;
438 GLfloat (*Bcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[1].data
;
439 const struct gl_light
*light
= ctx
->Light
.EnabledList
.next
;
442 const GLuint nr
= VB
->Count
;
445 fprintf(stderr
, "%s\n", __FUNCTION__
);
448 (void) input
; /* doesn't refer to Eye or Obj */
452 VB
->ColorPtr
[0] = &store
->LitColor
[0];
453 if (IDX
& LIGHT_TWOSIDE
)
454 VB
->ColorPtr
[1] = &store
->LitColor
[1];
456 if (stage
->changed_inputs
== 0)
459 for (j
= 0; j
< nr
; j
++, STRIDE_F(normal
,nstride
)) {
463 if ( IDX
& LIGHT_MATERIAL
)
464 update_materials( ctx
, store
);
466 /* No attenuation, so incoporate _MatAmbient into base color.
468 if ( j
== 0 || (IDX
& LIGHT_MATERIAL
) ) {
469 COPY_3V(base
[0], light
->_MatAmbient
[0]);
470 ACC_3V(base
[0], ctx
->Light
._BaseColor
[0] );
471 base
[0][3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
473 if (IDX
& LIGHT_TWOSIDE
) {
474 COPY_3V(base
[1], light
->_MatAmbient
[1]);
475 ACC_3V(base
[1], ctx
->Light
._BaseColor
[1]);
476 base
[1][3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
480 n_dot_VP
= DOT3(normal
, light
->_VP_inf_norm
);
482 if (n_dot_VP
< 0.0F
) {
483 if (IDX
& LIGHT_TWOSIDE
) {
484 GLfloat n_dot_h
= -DOT3(normal
, light
->_h_inf_norm
);
486 COPY_3V(sum
, base
[1]);
487 ACC_SCALE_SCALAR_3V(sum
, -n_dot_VP
, light
->_MatDiffuse
[1]);
488 if (n_dot_h
> 0.0F
) {
490 GET_SHINE_TAB_ENTRY( ctx
->_ShineTable
[1], n_dot_h
, spec
);
491 ACC_SCALE_SCALAR_3V(sum
, spec
, light
->_MatSpecular
[1]);
493 COPY_3V(Bcolor
[j
], sum
);
494 Bcolor
[j
][3] = base
[1][3];
496 COPY_4FV(Fcolor
[j
], base
[0]);
499 GLfloat n_dot_h
= DOT3(normal
, light
->_h_inf_norm
);
501 COPY_3V(sum
, base
[0]);
502 ACC_SCALE_SCALAR_3V(sum
, n_dot_VP
, light
->_MatDiffuse
[0]);
503 if (n_dot_h
> 0.0F
) {
505 GET_SHINE_TAB_ENTRY( ctx
->_ShineTable
[0], n_dot_h
, spec
);
506 ACC_SCALE_SCALAR_3V(sum
, spec
, light
->_MatSpecular
[0]);
509 COPY_3V(Fcolor
[j
], sum
);
510 Fcolor
[j
][3] = base
[0][3];
511 if (IDX
& LIGHT_TWOSIDE
) COPY_4FV(Bcolor
[j
], base
[1]);
517 /* Light infinite lights
519 static void TAG(light_fast_rgba
)( GLcontext
*ctx
,
520 struct vertex_buffer
*VB
,
521 struct tnl_pipeline_stage
*stage
,
524 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
526 const GLuint nstride
= VB
->NormalPtr
->stride
;
527 const GLfloat
*normal
= (GLfloat
*)VB
->NormalPtr
->data
;
528 GLfloat (*Fcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[0].data
;
529 GLfloat (*Bcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[1].data
;
531 const GLuint nr
= VB
->Count
;
532 const struct gl_light
*light
;
535 fprintf(stderr
, "%s %d\n", __FUNCTION__
, nr
);
542 sumA
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
543 sumA
[1] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
545 VB
->ColorPtr
[0] = &store
->LitColor
[0];
546 if (IDX
& LIGHT_TWOSIDE
)
547 VB
->ColorPtr
[1] = &store
->LitColor
[1];
549 if (stage
->changed_inputs
== 0)
552 for (j
= 0; j
< nr
; j
++, STRIDE_F(normal
,nstride
)) {
556 if ( IDX
& LIGHT_MATERIAL
) {
557 update_materials( ctx
, store
);
559 sumA
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
560 if (IDX
& LIGHT_TWOSIDE
)
561 sumA
[1] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
565 COPY_3V(sum
[0], ctx
->Light
._BaseColor
[0]);
566 if (IDX
& LIGHT_TWOSIDE
)
567 COPY_3V(sum
[1], ctx
->Light
._BaseColor
[1]);
569 foreach (light
, &ctx
->Light
.EnabledList
) {
570 GLfloat n_dot_h
, n_dot_VP
, spec
;
572 ACC_3V(sum
[0], light
->_MatAmbient
[0]);
573 if (IDX
& LIGHT_TWOSIDE
)
574 ACC_3V(sum
[1], light
->_MatAmbient
[1]);
576 n_dot_VP
= DOT3(normal
, light
->_VP_inf_norm
);
578 if (n_dot_VP
> 0.0F
) {
579 ACC_SCALE_SCALAR_3V(sum
[0], n_dot_VP
, light
->_MatDiffuse
[0]);
580 n_dot_h
= DOT3(normal
, light
->_h_inf_norm
);
581 if (n_dot_h
> 0.0F
) {
582 struct gl_shine_tab
*tab
= ctx
->_ShineTable
[0];
583 GET_SHINE_TAB_ENTRY( tab
, n_dot_h
, spec
);
584 ACC_SCALE_SCALAR_3V( sum
[0], spec
, light
->_MatSpecular
[0]);
587 else if (IDX
& LIGHT_TWOSIDE
) {
588 ACC_SCALE_SCALAR_3V(sum
[1], -n_dot_VP
, light
->_MatDiffuse
[1]);
589 n_dot_h
= -DOT3(normal
, light
->_h_inf_norm
);
590 if (n_dot_h
> 0.0F
) {
591 struct gl_shine_tab
*tab
= ctx
->_ShineTable
[1];
592 GET_SHINE_TAB_ENTRY( tab
, n_dot_h
, spec
);
593 ACC_SCALE_SCALAR_3V( sum
[1], spec
, light
->_MatSpecular
[1]);
598 COPY_3V( Fcolor
[j
], sum
[0] );
599 Fcolor
[j
][3] = sumA
[0];
601 if (IDX
& LIGHT_TWOSIDE
) {
602 COPY_3V( Bcolor
[j
], sum
[1] );
603 Bcolor
[j
][3] = sumA
[1];
613 * Use current lighting/material settings to compute the color indexes
614 * for an array of vertices.
615 * Input: n - number of vertices to light
616 * side - 0=use front material, 1=use back material
617 * vertex - array of [n] vertex position in eye coordinates
618 * normal - array of [n] surface normal vector
619 * Output: indexResult - resulting array of [n] color indexes
621 static void TAG(light_ci
)( GLcontext
*ctx
,
622 struct vertex_buffer
*VB
,
623 struct tnl_pipeline_stage
*stage
,
626 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
628 const GLuint vstride
= input
->stride
;
629 const GLfloat
*vertex
= (GLfloat
*) input
->data
;
630 const GLuint nstride
= VB
->NormalPtr
->stride
;
631 const GLfloat
*normal
= (GLfloat
*)VB
->NormalPtr
->data
;
632 GLfloat
*indexResult
[2];
633 const GLuint nr
= VB
->Count
;
636 fprintf(stderr
, "%s\n", __FUNCTION__
);
642 VB
->IndexPtr
[0] = &store
->LitIndex
[0];
643 if (IDX
& LIGHT_TWOSIDE
)
644 VB
->IndexPtr
[1] = &store
->LitIndex
[1];
646 if (stage
->changed_inputs
== 0)
649 indexResult
[0] = (GLfloat
*)VB
->IndexPtr
[0]->data
;
650 if (IDX
& LIGHT_TWOSIDE
)
651 indexResult
[1] = (GLfloat
*)VB
->IndexPtr
[1]->data
;
653 /* loop over vertices */
654 for (j
=0; j
<nr
; j
++,STRIDE_F(vertex
,vstride
),STRIDE_F(normal
, nstride
)) {
655 GLfloat diffuse
[2], specular
[2];
657 struct gl_light
*light
;
659 if ( IDX
& LIGHT_MATERIAL
)
660 update_materials( ctx
, store
);
662 diffuse
[0] = specular
[0] = 0.0F
;
664 if ( IDX
& LIGHT_TWOSIDE
) {
665 diffuse
[1] = specular
[1] = 0.0F
;
668 /* Accumulate diffuse and specular from each light source */
669 foreach (light
, &ctx
->Light
.EnabledList
) {
671 GLfloat attenuation
= 1.0F
;
672 GLfloat VP
[3]; /* unit vector from vertex to light */
673 GLfloat n_dot_VP
; /* dot product of l and n */
674 GLfloat
*h
, n_dot_h
, correction
= 1.0;
676 /* compute l and attenuation */
677 if (!(light
->_Flags
& LIGHT_POSITIONAL
)) {
678 /* directional light */
679 COPY_3V(VP
, light
->_VP_inf_norm
);
682 GLfloat d
; /* distance from vertex to light */
684 SUB_3V(VP
, light
->_Position
, vertex
);
686 d
= (GLfloat
) LEN_3FV( VP
);
688 GLfloat invd
= 1.0F
/ d
;
689 SELF_SCALE_SCALAR_3V(VP
, invd
);
692 attenuation
= 1.0F
/ (light
->ConstantAttenuation
+ d
*
693 (light
->LinearAttenuation
+ d
*
694 light
->QuadraticAttenuation
));
696 /* spotlight attenuation */
697 if (light
->_Flags
& LIGHT_SPOT
) {
698 GLfloat PV_dot_dir
= - DOT3(VP
, light
->_NormDirection
);
699 if (PV_dot_dir
< light
->_CosCutoff
) {
700 continue; /* this light makes no contribution */
703 GLdouble x
= PV_dot_dir
* (EXP_TABLE_SIZE
-1);
705 GLfloat spot
= (GLfloat
) (light
->_SpotExpTable
[k
][0]
706 + (x
-k
)*light
->_SpotExpTable
[k
][1]);
712 if (attenuation
< 1e-3)
713 continue; /* this light makes no contribution */
715 n_dot_VP
= DOT3( normal
, VP
);
717 /* which side are we lighting? */
718 if (n_dot_VP
< 0.0F
) {
719 if (!(IDX
& LIGHT_TWOSIDE
))
723 n_dot_VP
= -n_dot_VP
;
726 /* accumulate diffuse term */
727 diffuse
[side
] += n_dot_VP
* light
->_dli
* attenuation
;
730 if (ctx
->Light
.Model
.LocalViewer
) {
734 SUB_3V(VP
, VP
, v
); /* h = VP + VPe */
738 else if (light
->_Flags
& LIGHT_POSITIONAL
) {
740 /* Strangely, disabling this addition fixes a conformance
741 * problem. If this code is enabled, l_sed.c fails.
743 /*ACC_3V(h, ctx->_EyeZDir);*/
747 h
= light
->_h_inf_norm
;
750 n_dot_h
= correction
* DOT3(normal
, h
);
751 if (n_dot_h
> 0.0F
) {
753 struct gl_shine_tab
*tab
= ctx
->_ShineTable
[side
];
754 GET_SHINE_TAB_ENTRY( tab
, n_dot_h
, spec_coef
);
755 specular
[side
] += spec_coef
* light
->_sli
* attenuation
;
757 } /*loop over lights*/
759 /* Now compute final color index */
760 for (side
= 0 ; side
< NR_SIDES
; side
++) {
761 const GLfloat
*ind
= ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_INDEXES
+ side
];
764 if (specular
[side
] > 1.0F
) {
765 index
= ind
[MAT_INDEX_SPECULAR
];
768 GLfloat d_a
= ind
[MAT_INDEX_DIFFUSE
] - ind
[MAT_INDEX_AMBIENT
];
769 GLfloat s_a
= ind
[MAT_INDEX_SPECULAR
] - ind
[MAT_INDEX_AMBIENT
];
770 index
= (ind
[MAT_INDEX_AMBIENT
]
771 + diffuse
[side
] * (1.0F
-specular
[side
]) * d_a
772 + specular
[side
] * s_a
);
773 if (index
> ind
[MAT_INDEX_SPECULAR
]) {
774 index
= ind
[MAT_INDEX_SPECULAR
];
777 indexResult
[side
][j
] = index
;
784 static void TAG(init_light_tab
)( void )
786 _tnl_light_tab
[IDX
] = TAG(light_rgba
);
787 _tnl_light_fast_tab
[IDX
] = TAG(light_fast_rgba
);
788 _tnl_light_fast_single_tab
[IDX
] = TAG(light_fast_rgba_single
);
789 _tnl_light_spec_tab
[IDX
] = TAG(light_rgba_spec
);
790 _tnl_light_ci_tab
[IDX
] = TAG(light_ci
);