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 GLfloat (*Bcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[1].data
;
257 GLfloat (*color
[2])[4];
259 const GLuint nr
= VB
->Count
;
262 fprintf(stderr
, "%s\n", __FUNCTION__
);
271 VB
->ColorPtr
[0] = &store
->LitColor
[0];
272 sumA
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
274 #if IDX & LIGHT_TWOSIDE
275 VB
->ColorPtr
[1] = &store
->LitColor
[1];
276 sumA
[1] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
279 if (stage
->changed_inputs
== 0)
282 for (j
= 0; j
< nr
; j
++,STRIDE_F(vertex
,vstride
),STRIDE_F(normal
,nstride
)) {
284 struct gl_light
*light
;
286 if ( IDX
& LIGHT_MATERIAL
) {
287 update_materials( ctx
, store
);
288 sumA
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
289 #if IDX & LIGHT_TWOSIDE
290 sumA
[1] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
294 COPY_3V(sum
[0], base
[0]);
296 if ( IDX
& LIGHT_TWOSIDE
)
297 COPY_3V(sum
[1], base
[1]);
299 /* Add contribution from each enabled light source */
300 foreach (light
, &ctx
->Light
.EnabledList
) {
306 GLfloat attenuation
= 1.0;
307 GLfloat VP
[3]; /* unit vector from vertex to light */
308 GLfloat n_dot_VP
; /* n dot VP */
311 /* compute VP and attenuation */
312 if (!(light
->_Flags
& LIGHT_POSITIONAL
)) {
313 /* directional light */
314 COPY_3V(VP
, light
->_VP_inf_norm
);
315 attenuation
= light
->_VP_inf_spot_attenuation
;
318 GLfloat d
; /* distance from vertex to light */
321 SUB_3V(VP
, light
->_Position
, vertex
);
323 d
= (GLfloat
) LEN_3FV( VP
);
326 GLfloat invd
= 1.0F
/ d
;
327 SELF_SCALE_SCALAR_3V(VP
, invd
);
330 attenuation
= 1.0F
/ (light
->ConstantAttenuation
+ d
*
331 (light
->LinearAttenuation
+ d
*
332 light
->QuadraticAttenuation
));
334 /* spotlight attenuation */
335 if (light
->_Flags
& LIGHT_SPOT
) {
336 GLfloat PV_dot_dir
= - DOT3(VP
, light
->_NormDirection
);
338 if (PV_dot_dir
<light
->_CosCutoff
) {
339 continue; /* this light makes no contribution */
342 GLdouble x
= PV_dot_dir
* (EXP_TABLE_SIZE
-1);
344 GLfloat spot
= (GLfloat
) (light
->_SpotExpTable
[k
][0]
345 + (x
-k
)*light
->_SpotExpTable
[k
][1]);
351 if (attenuation
< 1e-3)
352 continue; /* this light makes no contribution */
354 /* Compute dot product or normal and vector from V to light pos */
355 n_dot_VP
= DOT3( normal
, VP
);
357 /* which side are we lighting? */
358 if (n_dot_VP
< 0.0F
) {
359 ACC_SCALE_SCALAR_3V(sum
[0], attenuation
, light
->_MatAmbient
[0]);
360 #if IDX & LIGHT_TWOSIDE
363 n_dot_VP
= -n_dot_VP
;
369 #if IDX & LIGHT_TWOSIDE
370 ACC_SCALE_SCALAR_3V( sum
[1], attenuation
, light
->_MatAmbient
[1]);
376 COPY_3V(contrib
, light
->_MatAmbient
[side
]);
379 ACC_SCALE_SCALAR_3V(contrib
, n_dot_VP
, light
->_MatDiffuse
[side
]);
381 /* specular term - cannibalize VP... */
383 if (ctx
->Light
.Model
.LocalViewer
) {
387 SUB_3V(VP
, VP
, v
); /* h = VP + VPe */
391 else if (light
->_Flags
& LIGHT_POSITIONAL
) {
393 ACC_3V(h
, ctx
->_EyeZDir
);
397 h
= light
->_h_inf_norm
;
400 n_dot_h
= correction
* DOT3(normal
, h
);
405 struct gl_shine_tab
*tab
= ctx
->_ShineTable
[side
];
407 GET_SHINE_TAB_ENTRY( tab
, n_dot_h
, spec_coef
);
409 ACC_SCALE_SCALAR_3V( contrib
, spec_coef
,
410 light
->_MatSpecular
[side
]);
414 ACC_SCALE_SCALAR_3V( sum
[side
], attenuation
, contrib
);
417 COPY_3V( Fcolor
[j
], sum
[0] );
418 Fcolor
[j
][3] = sumA
[0];
420 #if IDX & LIGHT_TWOSIDE
421 COPY_3V( Bcolor
[j
], sum
[1] );
422 Bcolor
[j
][3] = sumA
[1];
430 /* As below, but with just a single light.
432 static void TAG(light_fast_rgba_single
)( GLcontext
*ctx
,
433 struct vertex_buffer
*VB
,
434 struct tnl_pipeline_stage
*stage
,
438 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
439 const GLuint nstride
= VB
->NormalPtr
->stride
;
440 const GLfloat
*normal
= (GLfloat
*)VB
->NormalPtr
->data
;
441 GLfloat (*Fcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[0].data
;
442 #if IDX & LIGHT_TWOSIDE
443 GLfloat (*Bcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[1].data
;
445 const struct gl_light
*light
= ctx
->Light
.EnabledList
.next
;
448 const GLuint nr
= VB
->Count
;
451 fprintf(stderr
, "%s\n", __FUNCTION__
);
454 (void) input
; /* doesn't refer to Eye or Obj */
458 VB
->ColorPtr
[0] = &store
->LitColor
[0];
459 #if IDX & LIGHT_TWOSIDE
460 VB
->ColorPtr
[1] = &store
->LitColor
[1];
463 if (stage
->changed_inputs
== 0)
466 for (j
= 0; j
< nr
; j
++, STRIDE_F(normal
,nstride
)) {
470 if ( IDX
& LIGHT_MATERIAL
)
471 update_materials( ctx
, store
);
473 /* No attenuation, so incoporate _MatAmbient into base color.
475 if ( j
== 0 || (IDX
& LIGHT_MATERIAL
) ) {
476 COPY_3V(base
[0], light
->_MatAmbient
[0]);
477 ACC_3V(base
[0], ctx
->Light
._BaseColor
[0] );
478 base
[0][3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
480 #if IDX & LIGHT_TWOSIDE
481 COPY_3V(base
[1], light
->_MatAmbient
[1]);
482 ACC_3V(base
[1], ctx
->Light
._BaseColor
[1]);
483 base
[1][3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
487 n_dot_VP
= DOT3(normal
, light
->_VP_inf_norm
);
489 if (n_dot_VP
< 0.0F
) {
490 #if IDX & LIGHT_TWOSIDE
491 GLfloat n_dot_h
= -DOT3(normal
, light
->_h_inf_norm
);
493 COPY_3V(sum
, base
[1]);
494 ACC_SCALE_SCALAR_3V(sum
, -n_dot_VP
, light
->_MatDiffuse
[1]);
495 if (n_dot_h
> 0.0F
) {
497 GET_SHINE_TAB_ENTRY( ctx
->_ShineTable
[1], n_dot_h
, spec
);
498 ACC_SCALE_SCALAR_3V(sum
, spec
, light
->_MatSpecular
[1]);
500 COPY_3V(Bcolor
[j
], sum
);
501 Bcolor
[j
][3] = base
[1][3];
503 COPY_4FV(Fcolor
[j
], base
[0]);
506 GLfloat n_dot_h
= DOT3(normal
, light
->_h_inf_norm
);
508 COPY_3V(sum
, base
[0]);
509 ACC_SCALE_SCALAR_3V(sum
, n_dot_VP
, light
->_MatDiffuse
[0]);
510 if (n_dot_h
> 0.0F
) {
512 GET_SHINE_TAB_ENTRY( ctx
->_ShineTable
[0], n_dot_h
, spec
);
513 ACC_SCALE_SCALAR_3V(sum
, spec
, light
->_MatSpecular
[0]);
516 COPY_3V(Fcolor
[j
], sum
);
517 Fcolor
[j
][3] = base
[0][3];
518 #if IDX & LIGHT_TWOSIDE
519 COPY_4FV(Bcolor
[j
], base
[1]);
526 /* Light infinite lights
528 static void TAG(light_fast_rgba
)( GLcontext
*ctx
,
529 struct vertex_buffer
*VB
,
530 struct tnl_pipeline_stage
*stage
,
533 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
535 const GLuint nstride
= VB
->NormalPtr
->stride
;
536 const GLfloat
*normal
= (GLfloat
*)VB
->NormalPtr
->data
;
537 GLfloat (*Fcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[0].data
;
538 #if IDX & LIGHT_TWOSIDE
539 GLfloat (*Bcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[1].data
;
542 const GLuint nr
= VB
->Count
;
543 const struct gl_light
*light
;
546 fprintf(stderr
, "%s %d\n", __FUNCTION__
, nr
);
553 sumA
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
554 sumA
[1] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
556 VB
->ColorPtr
[0] = &store
->LitColor
[0];
557 #if IDX & LIGHT_TWOSIDE
558 VB
->ColorPtr
[1] = &store
->LitColor
[1];
561 if (stage
->changed_inputs
== 0)
564 for (j
= 0; j
< nr
; j
++, STRIDE_F(normal
,nstride
)) {
568 if ( IDX
& LIGHT_MATERIAL
) {
569 update_materials( ctx
, store
);
571 sumA
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
572 #if IDX & LIGHT_TWOSIDE
573 sumA
[1] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
578 COPY_3V(sum
[0], ctx
->Light
._BaseColor
[0]);
579 #if IDX & LIGHT_TWOSIDE
580 COPY_3V(sum
[1], ctx
->Light
._BaseColor
[1]);
583 foreach (light
, &ctx
->Light
.EnabledList
) {
584 GLfloat n_dot_h
, n_dot_VP
, spec
;
586 ACC_3V(sum
[0], light
->_MatAmbient
[0]);
587 #if IDX & LIGHT_TWOSIDE
588 ACC_3V(sum
[1], light
->_MatAmbient
[1]);
591 n_dot_VP
= DOT3(normal
, light
->_VP_inf_norm
);
593 if (n_dot_VP
> 0.0F
) {
594 ACC_SCALE_SCALAR_3V(sum
[0], n_dot_VP
, light
->_MatDiffuse
[0]);
595 n_dot_h
= DOT3(normal
, light
->_h_inf_norm
);
596 if (n_dot_h
> 0.0F
) {
597 struct gl_shine_tab
*tab
= ctx
->_ShineTable
[0];
598 GET_SHINE_TAB_ENTRY( tab
, n_dot_h
, spec
);
599 ACC_SCALE_SCALAR_3V( sum
[0], spec
, light
->_MatSpecular
[0]);
602 #if IDX & LIGHT_TWOSIDE
604 ACC_SCALE_SCALAR_3V(sum
[1], -n_dot_VP
, light
->_MatDiffuse
[1]);
605 n_dot_h
= -DOT3(normal
, light
->_h_inf_norm
);
606 if (n_dot_h
> 0.0F
) {
607 struct gl_shine_tab
*tab
= ctx
->_ShineTable
[1];
608 GET_SHINE_TAB_ENTRY( tab
, n_dot_h
, spec
);
609 ACC_SCALE_SCALAR_3V( sum
[1], spec
, light
->_MatSpecular
[1]);
615 COPY_3V( Fcolor
[j
], sum
[0] );
616 Fcolor
[j
][3] = sumA
[0];
618 #if IDX & LIGHT_TWOSIDE
619 COPY_3V( Bcolor
[j
], sum
[1] );
620 Bcolor
[j
][3] = sumA
[1];
630 * Use current lighting/material settings to compute the color indexes
631 * for an array of vertices.
632 * Input: n - number of vertices to light
633 * side - 0=use front material, 1=use back material
634 * vertex - array of [n] vertex position in eye coordinates
635 * normal - array of [n] surface normal vector
636 * Output: indexResult - resulting array of [n] color indexes
638 static void TAG(light_ci
)( GLcontext
*ctx
,
639 struct vertex_buffer
*VB
,
640 struct tnl_pipeline_stage
*stage
,
643 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
645 const GLuint vstride
= input
->stride
;
646 const GLfloat
*vertex
= (GLfloat
*) input
->data
;
647 const GLuint nstride
= VB
->NormalPtr
->stride
;
648 const GLfloat
*normal
= (GLfloat
*)VB
->NormalPtr
->data
;
649 GLfloat
*indexResult
[2];
650 const GLuint nr
= VB
->Count
;
653 fprintf(stderr
, "%s\n", __FUNCTION__
);
659 VB
->IndexPtr
[0] = &store
->LitIndex
[0];
660 #if IDX & LIGHT_TWOSIDE
661 VB
->IndexPtr
[1] = &store
->LitIndex
[1];
664 if (stage
->changed_inputs
== 0)
667 indexResult
[0] = (GLfloat
*)VB
->IndexPtr
[0]->data
;
668 #if IDX & LIGHT_TWOSIDE
669 indexResult
[1] = (GLfloat
*)VB
->IndexPtr
[1]->data
;
672 /* loop over vertices */
673 for (j
=0; j
<nr
; j
++,STRIDE_F(vertex
,vstride
),STRIDE_F(normal
, nstride
)) {
674 GLfloat diffuse
[2], specular
[2];
676 struct gl_light
*light
;
678 if ( IDX
& LIGHT_MATERIAL
)
679 update_materials( ctx
, store
);
681 diffuse
[0] = specular
[0] = 0.0F
;
683 if ( IDX
& LIGHT_TWOSIDE
) {
684 diffuse
[1] = specular
[1] = 0.0F
;
687 /* Accumulate diffuse and specular from each light source */
688 foreach (light
, &ctx
->Light
.EnabledList
) {
690 GLfloat attenuation
= 1.0F
;
691 GLfloat VP
[3]; /* unit vector from vertex to light */
692 GLfloat n_dot_VP
; /* dot product of l and n */
693 GLfloat
*h
, n_dot_h
, correction
= 1.0;
695 /* compute l and attenuation */
696 if (!(light
->_Flags
& LIGHT_POSITIONAL
)) {
697 /* directional light */
698 COPY_3V(VP
, light
->_VP_inf_norm
);
701 GLfloat d
; /* distance from vertex to light */
703 SUB_3V(VP
, light
->_Position
, vertex
);
705 d
= (GLfloat
) LEN_3FV( VP
);
707 GLfloat invd
= 1.0F
/ d
;
708 SELF_SCALE_SCALAR_3V(VP
, invd
);
711 attenuation
= 1.0F
/ (light
->ConstantAttenuation
+ d
*
712 (light
->LinearAttenuation
+ d
*
713 light
->QuadraticAttenuation
));
715 /* spotlight attenuation */
716 if (light
->_Flags
& LIGHT_SPOT
) {
717 GLfloat PV_dot_dir
= - DOT3(VP
, light
->_NormDirection
);
718 if (PV_dot_dir
< light
->_CosCutoff
) {
719 continue; /* this light makes no contribution */
722 GLdouble x
= PV_dot_dir
* (EXP_TABLE_SIZE
-1);
724 GLfloat spot
= (GLfloat
) (light
->_SpotExpTable
[k
][0]
725 + (x
-k
)*light
->_SpotExpTable
[k
][1]);
731 if (attenuation
< 1e-3)
732 continue; /* this light makes no contribution */
734 n_dot_VP
= DOT3( normal
, VP
);
736 /* which side are we lighting? */
737 if (n_dot_VP
< 0.0F
) {
738 #if IDX & LIGHT_TWOSIDE
741 n_dot_VP
= -n_dot_VP
;
747 /* accumulate diffuse term */
748 diffuse
[side
] += n_dot_VP
* light
->_dli
* attenuation
;
751 if (ctx
->Light
.Model
.LocalViewer
) {
755 SUB_3V(VP
, VP
, v
); /* h = VP + VPe */
759 else if (light
->_Flags
& LIGHT_POSITIONAL
) {
761 /* Strangely, disabling this addition fixes a conformance
762 * problem. If this code is enabled, l_sed.c fails.
764 /*ACC_3V(h, ctx->_EyeZDir);*/
768 h
= light
->_h_inf_norm
;
771 n_dot_h
= correction
* DOT3(normal
, h
);
772 if (n_dot_h
> 0.0F
) {
774 struct gl_shine_tab
*tab
= ctx
->_ShineTable
[side
];
775 GET_SHINE_TAB_ENTRY( tab
, n_dot_h
, spec_coef
);
776 specular
[side
] += spec_coef
* light
->_sli
* attenuation
;
778 } /*loop over lights*/
780 /* Now compute final color index */
781 for (side
= 0 ; side
< NR_SIDES
; side
++) {
782 const GLfloat
*ind
= ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_INDEXES
+ side
];
785 if (specular
[side
] > 1.0F
) {
786 index
= ind
[MAT_INDEX_SPECULAR
];
789 GLfloat d_a
= ind
[MAT_INDEX_DIFFUSE
] - ind
[MAT_INDEX_AMBIENT
];
790 GLfloat s_a
= ind
[MAT_INDEX_SPECULAR
] - ind
[MAT_INDEX_AMBIENT
];
791 index
= (ind
[MAT_INDEX_AMBIENT
]
792 + diffuse
[side
] * (1.0F
-specular
[side
]) * d_a
793 + specular
[side
] * s_a
);
794 if (index
> ind
[MAT_INDEX_SPECULAR
]) {
795 index
= ind
[MAT_INDEX_SPECULAR
];
798 indexResult
[side
][j
] = index
;
805 static void TAG(init_light_tab
)( void )
807 _tnl_light_tab
[IDX
] = TAG(light_rgba
);
808 _tnl_light_fast_tab
[IDX
] = TAG(light_fast_rgba
);
809 _tnl_light_fast_single_tab
[IDX
] = TAG(light_fast_rgba_single
);
810 _tnl_light_spec_tab
[IDX
] = TAG(light_rgba_spec
);
811 _tnl_light_ci_tab
[IDX
] = TAG(light_ci
);