2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 * Keith Whitwell <keith@tungstengraphics.com>
31 #if IDX & LIGHT_TWOSIDE
38 /* define TRACE to trace lighting code */
42 * ctx is the current context
43 * VB is the vertex buffer
44 * stage is the lighting stage-private data
45 * input is the vector of eye or object-space vertex coordinates
47 static void TAG(light_rgba_spec
)( GLcontext
*ctx
,
48 struct vertex_buffer
*VB
,
49 struct tnl_pipeline_stage
*stage
,
52 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
53 GLfloat (*base
)[3] = ctx
->Light
._BaseColor
;
57 const GLuint vstride
= input
->stride
;
58 const GLfloat
*vertex
= (GLfloat
*)input
->data
;
59 const GLuint nstride
= VB
->NormalPtr
->stride
;
60 const GLfloat
*normal
= (GLfloat
*)VB
->NormalPtr
->data
;
62 GLfloat (*Fcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[0].data
;
63 GLfloat (*Fspec
)[4] = (GLfloat (*)[4]) store
->LitSecondary
[0].data
;
64 #if IDX & LIGHT_TWOSIDE
65 GLfloat (*Bcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[1].data
;
66 GLfloat (*Bspec
)[4] = (GLfloat (*)[4]) store
->LitSecondary
[1].data
;
69 const GLuint nr
= VB
->Count
;
75 fprintf(stderr
, "%s\n", __FUNCTION__
);
78 VB
->ColorPtr
[0] = &store
->LitColor
[0];
79 VB
->SecondaryColorPtr
[0] = &store
->LitSecondary
[0];
80 sumA
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
82 #if IDX & LIGHT_TWOSIDE
83 VB
->ColorPtr
[1] = &store
->LitColor
[1];
84 VB
->SecondaryColorPtr
[1] = &store
->LitSecondary
[1];
85 sumA
[1] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
88 /* Side-effects done, can we finish now?
90 if (stage
->changed_inputs
== 0)
93 for (j
= 0; j
< nr
; j
++,STRIDE_F(vertex
,vstride
),STRIDE_F(normal
,nstride
)) {
94 GLfloat sum
[2][3], spec
[2][3];
95 struct gl_light
*light
;
97 #if IDX & LIGHT_MATERIAL
98 update_materials( ctx
, store
);
99 sumA
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
100 #if IDX & LIGHT_TWOSIDE
101 sumA
[1] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
105 COPY_3V(sum
[0], base
[0]);
108 #if IDX & LIGHT_TWOSIDE
109 COPY_3V(sum
[1], base
[1]);
113 /* Add contribution from each enabled light source */
114 foreach (light
, &ctx
->Light
.EnabledList
) {
120 GLfloat VP
[3]; /* unit vector from vertex to light */
121 GLfloat n_dot_VP
; /* n dot VP */
124 /* compute VP and attenuation */
125 if (!(light
->_Flags
& LIGHT_POSITIONAL
)) {
126 /* directional light */
127 COPY_3V(VP
, light
->_VP_inf_norm
);
128 attenuation
= light
->_VP_inf_spot_attenuation
;
131 GLfloat d
; /* distance from vertex to light */
133 SUB_3V(VP
, light
->_Position
, vertex
);
135 d
= (GLfloat
) LEN_3FV( VP
);
138 GLfloat invd
= 1.0F
/ d
;
139 SELF_SCALE_SCALAR_3V(VP
, invd
);
142 attenuation
= 1.0F
/ (light
->ConstantAttenuation
+ d
*
143 (light
->LinearAttenuation
+ d
*
144 light
->QuadraticAttenuation
));
146 /* spotlight attenuation */
147 if (light
->_Flags
& LIGHT_SPOT
) {
148 GLfloat PV_dot_dir
= - DOT3(VP
, light
->_NormDirection
);
150 if (PV_dot_dir
<light
->_CosCutoff
) {
151 continue; /* this light makes no contribution */
154 GLdouble x
= PV_dot_dir
* (EXP_TABLE_SIZE
-1);
156 GLfloat spot
= (GLfloat
) (light
->_SpotExpTable
[k
][0]
157 + (x
-k
)*light
->_SpotExpTable
[k
][1]);
163 if (attenuation
< 1e-3)
164 continue; /* this light makes no contribution */
166 /* Compute dot product or normal and vector from V to light pos */
167 n_dot_VP
= DOT3( normal
, VP
);
169 /* Which side gets the diffuse & specular terms? */
170 if (n_dot_VP
< 0.0F
) {
171 ACC_SCALE_SCALAR_3V(sum
[0], attenuation
, light
->_MatAmbient
[0]);
172 #if IDX & LIGHT_TWOSIDE
175 n_dot_VP
= -n_dot_VP
;
181 #if IDX & LIGHT_TWOSIDE
182 ACC_SCALE_SCALAR_3V( sum
[1], attenuation
, light
->_MatAmbient
[1]);
189 COPY_3V(contrib
, light
->_MatAmbient
[side
]);
190 ACC_SCALE_SCALAR_3V(contrib
, n_dot_VP
, light
->_MatDiffuse
[side
]);
191 ACC_SCALE_SCALAR_3V(sum
[side
], attenuation
, contrib
);
193 /* specular term - cannibalize VP... */
194 if (ctx
->Light
.Model
.LocalViewer
) {
198 SUB_3V(VP
, VP
, v
); /* h = VP + VPe */
202 else if (light
->_Flags
& LIGHT_POSITIONAL
) {
204 ACC_3V(h
, ctx
->_EyeZDir
);
208 h
= light
->_h_inf_norm
;
211 n_dot_h
= correction
* DOT3(normal
, h
);
213 if (n_dot_h
> 0.0F
) {
215 struct gl_shine_tab
*tab
= ctx
->_ShineTable
[side
];
216 GET_SHINE_TAB_ENTRY( tab
, n_dot_h
, spec_coef
);
218 if (spec_coef
> 1.0e-10) {
219 spec_coef
*= attenuation
;
220 ACC_SCALE_SCALAR_3V( spec
[side
], spec_coef
,
221 light
->_MatSpecular
[side
]);
224 } /*loop over lights*/
226 COPY_3V( Fcolor
[j
], sum
[0] );
227 COPY_3V( Fspec
[j
], spec
[0] );
228 Fcolor
[j
][3] = sumA
[0];
230 #if IDX & LIGHT_TWOSIDE
231 COPY_3V( Bcolor
[j
], sum
[1] );
232 COPY_3V( Bspec
[j
], spec
[1] );
233 Bcolor
[j
][3] = sumA
[1];
239 static void TAG(light_rgba
)( GLcontext
*ctx
,
240 struct vertex_buffer
*VB
,
241 struct tnl_pipeline_stage
*stage
,
244 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
247 GLfloat (*base
)[3] = ctx
->Light
._BaseColor
;
250 const GLuint vstride
= input
->stride
;
251 const GLfloat
*vertex
= (GLfloat
*) input
->data
;
252 const GLuint nstride
= VB
->NormalPtr
->stride
;
253 const GLfloat
*normal
= (GLfloat
*)VB
->NormalPtr
->data
;
255 GLfloat (*Fcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[0].data
;
256 #if IDX & LIGHT_TWOSIDE
257 GLfloat (*Bcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[1].data
;
260 const GLuint nr
= VB
->Count
;
263 fprintf(stderr
, "%s\n", __FUNCTION__
);
269 VB
->ColorPtr
[0] = &store
->LitColor
[0];
270 sumA
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
272 #if IDX & LIGHT_TWOSIDE
273 VB
->ColorPtr
[1] = &store
->LitColor
[1];
274 sumA
[1] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
277 if (stage
->changed_inputs
== 0)
280 for (j
= 0; j
< nr
; j
++,STRIDE_F(vertex
,vstride
),STRIDE_F(normal
,nstride
)) {
282 struct gl_light
*light
;
284 #if IDX & LIGHT_MATERIAL
285 update_materials( ctx
, store
);
286 sumA
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
287 #if IDX & LIGHT_TWOSIDE
288 sumA
[1] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
292 COPY_3V(sum
[0], base
[0]);
294 #if IDX & LIGHT_TWOSIDE
295 COPY_3V(sum
[1], base
[1]);
298 /* Add contribution from each enabled light source */
299 foreach (light
, &ctx
->Light
.EnabledList
) {
305 GLfloat attenuation
= 1.0;
306 GLfloat VP
[3]; /* unit vector from vertex to light */
307 GLfloat n_dot_VP
; /* n dot VP */
310 /* compute VP and attenuation */
311 if (!(light
->_Flags
& LIGHT_POSITIONAL
)) {
312 /* directional light */
313 COPY_3V(VP
, light
->_VP_inf_norm
);
314 attenuation
= light
->_VP_inf_spot_attenuation
;
317 GLfloat d
; /* distance from vertex to light */
320 SUB_3V(VP
, light
->_Position
, vertex
);
322 d
= (GLfloat
) LEN_3FV( VP
);
325 GLfloat invd
= 1.0F
/ d
;
326 SELF_SCALE_SCALAR_3V(VP
, invd
);
329 attenuation
= 1.0F
/ (light
->ConstantAttenuation
+ d
*
330 (light
->LinearAttenuation
+ d
*
331 light
->QuadraticAttenuation
));
333 /* spotlight attenuation */
334 if (light
->_Flags
& LIGHT_SPOT
) {
335 GLfloat PV_dot_dir
= - DOT3(VP
, light
->_NormDirection
);
337 if (PV_dot_dir
<light
->_CosCutoff
) {
338 continue; /* this light makes no contribution */
341 GLdouble x
= PV_dot_dir
* (EXP_TABLE_SIZE
-1);
343 GLfloat spot
= (GLfloat
) (light
->_SpotExpTable
[k
][0]
344 + (x
-k
)*light
->_SpotExpTable
[k
][1]);
350 if (attenuation
< 1e-3)
351 continue; /* this light makes no contribution */
353 /* Compute dot product or normal and vector from V to light pos */
354 n_dot_VP
= DOT3( normal
, VP
);
356 /* which side are we lighting? */
357 if (n_dot_VP
< 0.0F
) {
358 ACC_SCALE_SCALAR_3V(sum
[0], attenuation
, light
->_MatAmbient
[0]);
359 #if IDX & LIGHT_TWOSIDE
362 n_dot_VP
= -n_dot_VP
;
368 #if IDX & LIGHT_TWOSIDE
369 ACC_SCALE_SCALAR_3V( sum
[1], attenuation
, light
->_MatAmbient
[1]);
375 COPY_3V(contrib
, light
->_MatAmbient
[side
]);
378 ACC_SCALE_SCALAR_3V(contrib
, n_dot_VP
, light
->_MatDiffuse
[side
]);
380 /* specular term - cannibalize VP... */
382 if (ctx
->Light
.Model
.LocalViewer
) {
386 SUB_3V(VP
, VP
, v
); /* h = VP + VPe */
390 else if (light
->_Flags
& LIGHT_POSITIONAL
) {
392 ACC_3V(h
, ctx
->_EyeZDir
);
396 h
= light
->_h_inf_norm
;
399 n_dot_h
= correction
* DOT3(normal
, h
);
404 struct gl_shine_tab
*tab
= ctx
->_ShineTable
[side
];
406 GET_SHINE_TAB_ENTRY( tab
, n_dot_h
, spec_coef
);
408 ACC_SCALE_SCALAR_3V( contrib
, spec_coef
,
409 light
->_MatSpecular
[side
]);
413 ACC_SCALE_SCALAR_3V( sum
[side
], attenuation
, contrib
);
416 COPY_3V( Fcolor
[j
], sum
[0] );
417 Fcolor
[j
][3] = sumA
[0];
419 #if IDX & LIGHT_TWOSIDE
420 COPY_3V( Bcolor
[j
], sum
[1] );
421 Bcolor
[j
][3] = sumA
[1];
429 /* As below, but with just a single light.
431 static void TAG(light_fast_rgba_single
)( GLcontext
*ctx
,
432 struct vertex_buffer
*VB
,
433 struct tnl_pipeline_stage
*stage
,
437 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
438 const GLuint nstride
= VB
->NormalPtr
->stride
;
439 const GLfloat
*normal
= (GLfloat
*)VB
->NormalPtr
->data
;
440 GLfloat (*Fcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[0].data
;
441 #if IDX & LIGHT_TWOSIDE
442 GLfloat (*Bcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[1].data
;
444 const struct gl_light
*light
= ctx
->Light
.EnabledList
.next
;
447 const GLuint nr
= VB
->Count
;
450 fprintf(stderr
, "%s\n", __FUNCTION__
);
453 (void) input
; /* doesn't refer to Eye or Obj */
457 VB
->ColorPtr
[0] = &store
->LitColor
[0];
458 #if IDX & LIGHT_TWOSIDE
459 VB
->ColorPtr
[1] = &store
->LitColor
[1];
462 if (stage
->changed_inputs
== 0)
465 for (j
= 0; j
< nr
; j
++, STRIDE_F(normal
,nstride
)) {
469 #if IDX & LIGHT_MATERIAL
470 update_materials( ctx
, store
);
473 /* No attenuation, so incoporate _MatAmbient into base color.
475 #if !(IDX & LIGHT_MATERIAL)
479 COPY_3V(base
[0], light
->_MatAmbient
[0]);
480 ACC_3V(base
[0], ctx
->Light
._BaseColor
[0] );
481 base
[0][3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
483 #if IDX & LIGHT_TWOSIDE
484 COPY_3V(base
[1], light
->_MatAmbient
[1]);
485 ACC_3V(base
[1], ctx
->Light
._BaseColor
[1]);
486 base
[1][3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
490 n_dot_VP
= DOT3(normal
, light
->_VP_inf_norm
);
492 if (n_dot_VP
< 0.0F
) {
493 #if IDX & LIGHT_TWOSIDE
494 GLfloat n_dot_h
= -DOT3(normal
, light
->_h_inf_norm
);
496 COPY_3V(sum
, base
[1]);
497 ACC_SCALE_SCALAR_3V(sum
, -n_dot_VP
, light
->_MatDiffuse
[1]);
498 if (n_dot_h
> 0.0F
) {
500 GET_SHINE_TAB_ENTRY( ctx
->_ShineTable
[1], n_dot_h
, spec
);
501 ACC_SCALE_SCALAR_3V(sum
, spec
, light
->_MatSpecular
[1]);
503 COPY_3V(Bcolor
[j
], sum
);
504 Bcolor
[j
][3] = base
[1][3];
506 COPY_4FV(Fcolor
[j
], base
[0]);
509 GLfloat n_dot_h
= DOT3(normal
, light
->_h_inf_norm
);
511 COPY_3V(sum
, base
[0]);
512 ACC_SCALE_SCALAR_3V(sum
, n_dot_VP
, light
->_MatDiffuse
[0]);
513 if (n_dot_h
> 0.0F
) {
515 GET_SHINE_TAB_ENTRY( ctx
->_ShineTable
[0], n_dot_h
, spec
);
516 ACC_SCALE_SCALAR_3V(sum
, spec
, light
->_MatSpecular
[0]);
519 COPY_3V(Fcolor
[j
], sum
);
520 Fcolor
[j
][3] = base
[0][3];
521 #if IDX & LIGHT_TWOSIDE
522 COPY_4FV(Bcolor
[j
], base
[1]);
529 /* Light infinite lights
531 static void TAG(light_fast_rgba
)( GLcontext
*ctx
,
532 struct vertex_buffer
*VB
,
533 struct tnl_pipeline_stage
*stage
,
536 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
538 const GLuint nstride
= VB
->NormalPtr
->stride
;
539 const GLfloat
*normal
= (GLfloat
*)VB
->NormalPtr
->data
;
540 GLfloat (*Fcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[0].data
;
541 #if IDX & LIGHT_TWOSIDE
542 GLfloat (*Bcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[1].data
;
545 const GLuint nr
= VB
->Count
;
546 const struct gl_light
*light
;
549 fprintf(stderr
, "%s %d\n", __FUNCTION__
, nr
);
556 sumA
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
557 sumA
[1] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
559 VB
->ColorPtr
[0] = &store
->LitColor
[0];
560 #if IDX & LIGHT_TWOSIDE
561 VB
->ColorPtr
[1] = &store
->LitColor
[1];
564 if (stage
->changed_inputs
== 0)
567 for (j
= 0; j
< nr
; j
++, STRIDE_F(normal
,nstride
)) {
571 #if IDX & LIGHT_MATERIAL
572 update_materials( ctx
, store
);
574 sumA
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
575 #if IDX & LIGHT_TWOSIDE
576 sumA
[1] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
581 COPY_3V(sum
[0], ctx
->Light
._BaseColor
[0]);
582 #if IDX & LIGHT_TWOSIDE
583 COPY_3V(sum
[1], ctx
->Light
._BaseColor
[1]);
586 foreach (light
, &ctx
->Light
.EnabledList
) {
587 GLfloat n_dot_h
, n_dot_VP
, spec
;
589 ACC_3V(sum
[0], light
->_MatAmbient
[0]);
590 #if IDX & LIGHT_TWOSIDE
591 ACC_3V(sum
[1], light
->_MatAmbient
[1]);
594 n_dot_VP
= DOT3(normal
, light
->_VP_inf_norm
);
596 if (n_dot_VP
> 0.0F
) {
597 ACC_SCALE_SCALAR_3V(sum
[0], n_dot_VP
, light
->_MatDiffuse
[0]);
598 n_dot_h
= DOT3(normal
, light
->_h_inf_norm
);
599 if (n_dot_h
> 0.0F
) {
600 struct gl_shine_tab
*tab
= ctx
->_ShineTable
[0];
601 GET_SHINE_TAB_ENTRY( tab
, n_dot_h
, spec
);
602 ACC_SCALE_SCALAR_3V( sum
[0], spec
, light
->_MatSpecular
[0]);
605 #if IDX & LIGHT_TWOSIDE
607 ACC_SCALE_SCALAR_3V(sum
[1], -n_dot_VP
, light
->_MatDiffuse
[1]);
608 n_dot_h
= -DOT3(normal
, light
->_h_inf_norm
);
609 if (n_dot_h
> 0.0F
) {
610 struct gl_shine_tab
*tab
= ctx
->_ShineTable
[1];
611 GET_SHINE_TAB_ENTRY( tab
, n_dot_h
, spec
);
612 ACC_SCALE_SCALAR_3V( sum
[1], spec
, light
->_MatSpecular
[1]);
618 COPY_3V( Fcolor
[j
], sum
[0] );
619 Fcolor
[j
][3] = sumA
[0];
621 #if IDX & LIGHT_TWOSIDE
622 COPY_3V( Bcolor
[j
], sum
[1] );
623 Bcolor
[j
][3] = sumA
[1];
633 * Use current lighting/material settings to compute the color indexes
634 * for an array of vertices.
635 * Input: n - number of vertices to light
636 * side - 0=use front material, 1=use back material
637 * vertex - array of [n] vertex position in eye coordinates
638 * normal - array of [n] surface normal vector
639 * Output: indexResult - resulting array of [n] color indexes
641 static void TAG(light_ci
)( GLcontext
*ctx
,
642 struct vertex_buffer
*VB
,
643 struct tnl_pipeline_stage
*stage
,
646 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
648 const GLuint vstride
= input
->stride
;
649 const GLfloat
*vertex
= (GLfloat
*) input
->data
;
650 const GLuint nstride
= VB
->NormalPtr
->stride
;
651 const GLfloat
*normal
= (GLfloat
*)VB
->NormalPtr
->data
;
652 GLfloat
*indexResult
[2];
653 const GLuint nr
= VB
->Count
;
656 fprintf(stderr
, "%s\n", __FUNCTION__
);
662 VB
->IndexPtr
[0] = &store
->LitIndex
[0];
663 #if IDX & LIGHT_TWOSIDE
664 VB
->IndexPtr
[1] = &store
->LitIndex
[1];
667 if (stage
->changed_inputs
== 0)
670 indexResult
[0] = (GLfloat
*)VB
->IndexPtr
[0]->data
;
671 #if IDX & LIGHT_TWOSIDE
672 indexResult
[1] = (GLfloat
*)VB
->IndexPtr
[1]->data
;
675 /* loop over vertices */
676 for (j
=0; j
<nr
; j
++,STRIDE_F(vertex
,vstride
),STRIDE_F(normal
, nstride
)) {
677 GLfloat diffuse
[2], specular
[2];
679 struct gl_light
*light
;
681 #if IDX & LIGHT_MATERIAL
682 update_materials( ctx
, store
);
685 diffuse
[0] = specular
[0] = 0.0F
;
687 #if IDX & LIGHT_TWOSIDE
688 diffuse
[1] = specular
[1] = 0.0F
;
691 /* Accumulate diffuse and specular from each light source */
692 foreach (light
, &ctx
->Light
.EnabledList
) {
694 GLfloat attenuation
= 1.0F
;
695 GLfloat VP
[3]; /* unit vector from vertex to light */
696 GLfloat n_dot_VP
; /* dot product of l and n */
697 GLfloat
*h
, n_dot_h
, correction
= 1.0;
699 /* compute l and attenuation */
700 if (!(light
->_Flags
& LIGHT_POSITIONAL
)) {
701 /* directional light */
702 COPY_3V(VP
, light
->_VP_inf_norm
);
705 GLfloat d
; /* distance from vertex to light */
707 SUB_3V(VP
, light
->_Position
, vertex
);
709 d
= (GLfloat
) LEN_3FV( VP
);
711 GLfloat invd
= 1.0F
/ d
;
712 SELF_SCALE_SCALAR_3V(VP
, invd
);
715 attenuation
= 1.0F
/ (light
->ConstantAttenuation
+ d
*
716 (light
->LinearAttenuation
+ d
*
717 light
->QuadraticAttenuation
));
719 /* spotlight attenuation */
720 if (light
->_Flags
& LIGHT_SPOT
) {
721 GLfloat PV_dot_dir
= - DOT3(VP
, light
->_NormDirection
);
722 if (PV_dot_dir
< light
->_CosCutoff
) {
723 continue; /* this light makes no contribution */
726 GLdouble x
= PV_dot_dir
* (EXP_TABLE_SIZE
-1);
728 GLfloat spot
= (GLfloat
) (light
->_SpotExpTable
[k
][0]
729 + (x
-k
)*light
->_SpotExpTable
[k
][1]);
735 if (attenuation
< 1e-3)
736 continue; /* this light makes no contribution */
738 n_dot_VP
= DOT3( normal
, VP
);
740 /* which side are we lighting? */
741 if (n_dot_VP
< 0.0F
) {
742 #if IDX & LIGHT_TWOSIDE
745 n_dot_VP
= -n_dot_VP
;
751 /* accumulate diffuse term */
752 diffuse
[side
] += n_dot_VP
* light
->_dli
* attenuation
;
755 if (ctx
->Light
.Model
.LocalViewer
) {
759 SUB_3V(VP
, VP
, v
); /* h = VP + VPe */
763 else if (light
->_Flags
& LIGHT_POSITIONAL
) {
765 /* Strangely, disabling this addition fixes a conformance
766 * problem. If this code is enabled, l_sed.c fails.
768 /*ACC_3V(h, ctx->_EyeZDir);*/
772 h
= light
->_h_inf_norm
;
775 n_dot_h
= correction
* DOT3(normal
, h
);
776 if (n_dot_h
> 0.0F
) {
778 struct gl_shine_tab
*tab
= ctx
->_ShineTable
[side
];
779 GET_SHINE_TAB_ENTRY( tab
, n_dot_h
, spec_coef
);
780 specular
[side
] += spec_coef
* light
->_sli
* attenuation
;
782 } /*loop over lights*/
784 /* Now compute final color index */
785 for (side
= 0 ; side
< NR_SIDES
; side
++) {
786 const GLfloat
*ind
= ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_INDEXES
+ side
];
789 if (specular
[side
] > 1.0F
) {
790 index
= ind
[MAT_INDEX_SPECULAR
];
793 GLfloat d_a
= ind
[MAT_INDEX_DIFFUSE
] - ind
[MAT_INDEX_AMBIENT
];
794 GLfloat s_a
= ind
[MAT_INDEX_SPECULAR
] - ind
[MAT_INDEX_AMBIENT
];
795 index
= (ind
[MAT_INDEX_AMBIENT
]
796 + diffuse
[side
] * (1.0F
-specular
[side
]) * d_a
797 + specular
[side
] * s_a
);
798 if (index
> ind
[MAT_INDEX_SPECULAR
]) {
799 index
= ind
[MAT_INDEX_SPECULAR
];
802 indexResult
[side
][j
] = index
;
809 static void TAG(init_light_tab
)( void )
811 _tnl_light_tab
[IDX
] = TAG(light_rgba
);
812 _tnl_light_fast_tab
[IDX
] = TAG(light_fast_rgba
);
813 _tnl_light_fast_single_tab
[IDX
] = TAG(light_fast_rgba_single
);
814 _tnl_light_spec_tab
[IDX
] = TAG(light_rgba_spec
);
815 _tnl_light_ci_tab
[IDX
] = TAG(light_ci
);