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
;
72 fprintf(stderr
, "%s\n", __FUNCTION__
);
75 VB
->ColorPtr
[0] = &store
->LitColor
[0];
76 VB
->SecondaryColorPtr
[0] = &store
->LitSecondary
[0];
77 sumA
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
79 #if IDX & LIGHT_TWOSIDE
80 VB
->ColorPtr
[1] = &store
->LitColor
[1];
81 VB
->SecondaryColorPtr
[1] = &store
->LitSecondary
[1];
82 sumA
[1] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
86 store
->LitColor
[0].stride
= 16;
87 store
->LitColor
[1].stride
= 16;
89 for (j
= 0; j
< nr
; j
++,STRIDE_F(vertex
,vstride
),STRIDE_F(normal
,nstride
)) {
90 GLfloat sum
[2][3], spec
[2][3];
91 struct gl_light
*light
;
93 #if IDX & LIGHT_MATERIAL
94 update_materials( ctx
, store
);
95 sumA
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
96 #if IDX & LIGHT_TWOSIDE
97 sumA
[1] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
101 COPY_3V(sum
[0], base
[0]);
104 #if IDX & LIGHT_TWOSIDE
105 COPY_3V(sum
[1], base
[1]);
109 /* Add contribution from each enabled light source */
110 foreach (light
, &ctx
->Light
.EnabledList
) {
116 GLfloat VP
[3]; /* unit vector from vertex to light */
117 GLfloat n_dot_VP
; /* n dot VP */
120 /* compute VP and attenuation */
121 if (!(light
->_Flags
& LIGHT_POSITIONAL
)) {
122 /* directional light */
123 COPY_3V(VP
, light
->_VP_inf_norm
);
124 attenuation
= light
->_VP_inf_spot_attenuation
;
127 GLfloat d
; /* distance from vertex to light */
129 SUB_3V(VP
, light
->_Position
, vertex
);
131 d
= (GLfloat
) LEN_3FV( VP
);
134 GLfloat invd
= 1.0F
/ d
;
135 SELF_SCALE_SCALAR_3V(VP
, invd
);
138 attenuation
= 1.0F
/ (light
->ConstantAttenuation
+ d
*
139 (light
->LinearAttenuation
+ d
*
140 light
->QuadraticAttenuation
));
142 /* spotlight attenuation */
143 if (light
->_Flags
& LIGHT_SPOT
) {
144 GLfloat PV_dot_dir
= - DOT3(VP
, light
->_NormDirection
);
146 if (PV_dot_dir
<light
->_CosCutoff
) {
147 continue; /* this light makes no contribution */
150 GLdouble x
= PV_dot_dir
* (EXP_TABLE_SIZE
-1);
152 GLfloat spot
= (GLfloat
) (light
->_SpotExpTable
[k
][0]
153 + (x
-k
)*light
->_SpotExpTable
[k
][1]);
159 if (attenuation
< 1e-3)
160 continue; /* this light makes no contribution */
162 /* Compute dot product or normal and vector from V to light pos */
163 n_dot_VP
= DOT3( normal
, VP
);
165 /* Which side gets the diffuse & specular terms? */
166 if (n_dot_VP
< 0.0F
) {
167 ACC_SCALE_SCALAR_3V(sum
[0], attenuation
, light
->_MatAmbient
[0]);
168 #if IDX & LIGHT_TWOSIDE
171 n_dot_VP
= -n_dot_VP
;
177 #if IDX & LIGHT_TWOSIDE
178 ACC_SCALE_SCALAR_3V( sum
[1], attenuation
, light
->_MatAmbient
[1]);
185 COPY_3V(contrib
, light
->_MatAmbient
[side
]);
186 ACC_SCALE_SCALAR_3V(contrib
, n_dot_VP
, light
->_MatDiffuse
[side
]);
187 ACC_SCALE_SCALAR_3V(sum
[side
], attenuation
, contrib
);
189 /* specular term - cannibalize VP... */
190 if (ctx
->Light
.Model
.LocalViewer
) {
194 SUB_3V(VP
, VP
, v
); /* h = VP + VPe */
198 else if (light
->_Flags
& LIGHT_POSITIONAL
) {
200 ACC_3V(h
, ctx
->_EyeZDir
);
204 h
= light
->_h_inf_norm
;
207 n_dot_h
= correction
* DOT3(normal
, h
);
209 if (n_dot_h
> 0.0F
) {
211 struct gl_shine_tab
*tab
= ctx
->_ShineTable
[side
];
212 GET_SHINE_TAB_ENTRY( tab
, n_dot_h
, spec_coef
);
214 if (spec_coef
> 1.0e-10) {
215 spec_coef
*= attenuation
;
216 ACC_SCALE_SCALAR_3V( spec
[side
], spec_coef
,
217 light
->_MatSpecular
[side
]);
220 } /*loop over lights*/
222 COPY_3V( Fcolor
[j
], sum
[0] );
223 COPY_3V( Fspec
[j
], spec
[0] );
224 Fcolor
[j
][3] = sumA
[0];
226 #if IDX & LIGHT_TWOSIDE
227 COPY_3V( Bcolor
[j
], sum
[1] );
228 COPY_3V( Bspec
[j
], spec
[1] );
229 Bcolor
[j
][3] = sumA
[1];
235 static void TAG(light_rgba
)( GLcontext
*ctx
,
236 struct vertex_buffer
*VB
,
237 struct tnl_pipeline_stage
*stage
,
240 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
243 GLfloat (*base
)[3] = ctx
->Light
._BaseColor
;
246 const GLuint vstride
= input
->stride
;
247 const GLfloat
*vertex
= (GLfloat
*) input
->data
;
248 const GLuint nstride
= VB
->NormalPtr
->stride
;
249 const GLfloat
*normal
= (GLfloat
*)VB
->NormalPtr
->data
;
251 GLfloat (*Fcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[0].data
;
252 #if IDX & LIGHT_TWOSIDE
253 GLfloat (*Bcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[1].data
;
256 const GLuint nr
= VB
->Count
;
259 fprintf(stderr
, "%s\n", __FUNCTION__
);
262 VB
->ColorPtr
[0] = &store
->LitColor
[0];
263 sumA
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
265 #if IDX & LIGHT_TWOSIDE
266 VB
->ColorPtr
[1] = &store
->LitColor
[1];
267 sumA
[1] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
270 store
->LitColor
[0].stride
= 16;
271 store
->LitColor
[1].stride
= 16;
273 for (j
= 0; j
< nr
; j
++,STRIDE_F(vertex
,vstride
),STRIDE_F(normal
,nstride
)) {
275 struct gl_light
*light
;
277 #if IDX & LIGHT_MATERIAL
278 update_materials( ctx
, store
);
279 sumA
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
280 #if IDX & LIGHT_TWOSIDE
281 sumA
[1] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
285 COPY_3V(sum
[0], base
[0]);
287 #if IDX & LIGHT_TWOSIDE
288 COPY_3V(sum
[1], base
[1]);
291 /* Add contribution from each enabled light source */
292 foreach (light
, &ctx
->Light
.EnabledList
) {
298 GLfloat attenuation
= 1.0;
299 GLfloat VP
[3]; /* unit vector from vertex to light */
300 GLfloat n_dot_VP
; /* n dot VP */
303 /* compute VP and attenuation */
304 if (!(light
->_Flags
& LIGHT_POSITIONAL
)) {
305 /* directional light */
306 COPY_3V(VP
, light
->_VP_inf_norm
);
307 attenuation
= light
->_VP_inf_spot_attenuation
;
310 GLfloat d
; /* distance from vertex to light */
313 SUB_3V(VP
, light
->_Position
, vertex
);
315 d
= (GLfloat
) LEN_3FV( VP
);
318 GLfloat invd
= 1.0F
/ d
;
319 SELF_SCALE_SCALAR_3V(VP
, invd
);
322 attenuation
= 1.0F
/ (light
->ConstantAttenuation
+ d
*
323 (light
->LinearAttenuation
+ d
*
324 light
->QuadraticAttenuation
));
326 /* spotlight attenuation */
327 if (light
->_Flags
& LIGHT_SPOT
) {
328 GLfloat PV_dot_dir
= - DOT3(VP
, light
->_NormDirection
);
330 if (PV_dot_dir
<light
->_CosCutoff
) {
331 continue; /* this light makes no contribution */
334 GLdouble x
= PV_dot_dir
* (EXP_TABLE_SIZE
-1);
336 GLfloat spot
= (GLfloat
) (light
->_SpotExpTable
[k
][0]
337 + (x
-k
)*light
->_SpotExpTable
[k
][1]);
343 if (attenuation
< 1e-3)
344 continue; /* this light makes no contribution */
346 /* Compute dot product or normal and vector from V to light pos */
347 n_dot_VP
= DOT3( normal
, VP
);
349 /* which side are we lighting? */
350 if (n_dot_VP
< 0.0F
) {
351 ACC_SCALE_SCALAR_3V(sum
[0], attenuation
, light
->_MatAmbient
[0]);
352 #if IDX & LIGHT_TWOSIDE
355 n_dot_VP
= -n_dot_VP
;
361 #if IDX & LIGHT_TWOSIDE
362 ACC_SCALE_SCALAR_3V( sum
[1], attenuation
, light
->_MatAmbient
[1]);
368 COPY_3V(contrib
, light
->_MatAmbient
[side
]);
371 ACC_SCALE_SCALAR_3V(contrib
, n_dot_VP
, light
->_MatDiffuse
[side
]);
373 /* specular term - cannibalize VP... */
375 if (ctx
->Light
.Model
.LocalViewer
) {
379 SUB_3V(VP
, VP
, v
); /* h = VP + VPe */
383 else if (light
->_Flags
& LIGHT_POSITIONAL
) {
385 ACC_3V(h
, ctx
->_EyeZDir
);
389 h
= light
->_h_inf_norm
;
392 n_dot_h
= correction
* DOT3(normal
, h
);
397 struct gl_shine_tab
*tab
= ctx
->_ShineTable
[side
];
399 GET_SHINE_TAB_ENTRY( tab
, n_dot_h
, spec_coef
);
401 ACC_SCALE_SCALAR_3V( contrib
, spec_coef
,
402 light
->_MatSpecular
[side
]);
406 ACC_SCALE_SCALAR_3V( sum
[side
], attenuation
, contrib
);
409 COPY_3V( Fcolor
[j
], sum
[0] );
410 Fcolor
[j
][3] = sumA
[0];
412 #if IDX & LIGHT_TWOSIDE
413 COPY_3V( Bcolor
[j
], sum
[1] );
414 Bcolor
[j
][3] = sumA
[1];
422 /* As below, but with just a single light.
424 static void TAG(light_fast_rgba_single
)( GLcontext
*ctx
,
425 struct vertex_buffer
*VB
,
426 struct tnl_pipeline_stage
*stage
,
430 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
431 const GLuint nstride
= VB
->NormalPtr
->stride
;
432 const GLfloat
*normal
= (GLfloat
*)VB
->NormalPtr
->data
;
433 GLfloat (*Fcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[0].data
;
434 #if IDX & LIGHT_TWOSIDE
435 GLfloat (*Bcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[1].data
;
437 const struct gl_light
*light
= ctx
->Light
.EnabledList
.next
;
440 #if IDX & LIGHT_MATERIAL
441 const GLuint nr
= VB
->Count
;
443 const GLuint nr
= VB
->NormalPtr
->count
;
447 fprintf(stderr
, "%s\n", __FUNCTION__
);
450 (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];
458 store
->LitColor
[0].stride
= 16;
459 store
->LitColor
[1].stride
= 16;
462 store
->LitColor
[0].stride
= 0;
463 store
->LitColor
[1].stride
= 0;
466 for (j
= 0; j
< nr
; j
++, STRIDE_F(normal
,nstride
)) {
470 #if IDX & LIGHT_MATERIAL
471 update_materials( ctx
, store
);
474 /* No attenuation, so incoporate _MatAmbient into base color.
476 #if !(IDX & LIGHT_MATERIAL)
480 COPY_3V(base
[0], light
->_MatAmbient
[0]);
481 ACC_3V(base
[0], ctx
->Light
._BaseColor
[0] );
482 base
[0][3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
484 #if IDX & LIGHT_TWOSIDE
485 COPY_3V(base
[1], light
->_MatAmbient
[1]);
486 ACC_3V(base
[1], ctx
->Light
._BaseColor
[1]);
487 base
[1][3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
491 n_dot_VP
= DOT3(normal
, light
->_VP_inf_norm
);
493 if (n_dot_VP
< 0.0F
) {
494 #if IDX & LIGHT_TWOSIDE
495 GLfloat n_dot_h
= -DOT3(normal
, light
->_h_inf_norm
);
497 COPY_3V(sum
, base
[1]);
498 ACC_SCALE_SCALAR_3V(sum
, -n_dot_VP
, light
->_MatDiffuse
[1]);
499 if (n_dot_h
> 0.0F
) {
501 GET_SHINE_TAB_ENTRY( ctx
->_ShineTable
[1], n_dot_h
, spec
);
502 ACC_SCALE_SCALAR_3V(sum
, spec
, light
->_MatSpecular
[1]);
504 COPY_3V(Bcolor
[j
], sum
);
505 Bcolor
[j
][3] = base
[1][3];
507 COPY_4FV(Fcolor
[j
], base
[0]);
510 GLfloat n_dot_h
= DOT3(normal
, light
->_h_inf_norm
);
512 COPY_3V(sum
, base
[0]);
513 ACC_SCALE_SCALAR_3V(sum
, n_dot_VP
, light
->_MatDiffuse
[0]);
514 if (n_dot_h
> 0.0F
) {
516 GET_SHINE_TAB_ENTRY( ctx
->_ShineTable
[0], n_dot_h
, spec
);
517 ACC_SCALE_SCALAR_3V(sum
, spec
, light
->_MatSpecular
[0]);
520 COPY_3V(Fcolor
[j
], sum
);
521 Fcolor
[j
][3] = base
[0][3];
522 #if IDX & LIGHT_TWOSIDE
523 COPY_4FV(Bcolor
[j
], base
[1]);
530 /* Light infinite lights
532 static void TAG(light_fast_rgba
)( GLcontext
*ctx
,
533 struct vertex_buffer
*VB
,
534 struct tnl_pipeline_stage
*stage
,
537 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
539 const GLuint nstride
= VB
->NormalPtr
->stride
;
540 const GLfloat
*normal
= (GLfloat
*)VB
->NormalPtr
->data
;
541 GLfloat (*Fcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[0].data
;
542 #if IDX & LIGHT_TWOSIDE
543 GLfloat (*Bcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[1].data
;
546 #if IDX & LIGHT_MATERIAL
547 const GLuint nr
= VB
->Count
;
549 const GLuint nr
= VB
->NormalPtr
->count
;
551 const struct gl_light
*light
;
554 fprintf(stderr
, "%s %d\n", __FUNCTION__
, nr
);
559 sumA
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
560 sumA
[1] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
562 VB
->ColorPtr
[0] = &store
->LitColor
[0];
563 #if IDX & LIGHT_TWOSIDE
564 VB
->ColorPtr
[1] = &store
->LitColor
[1];
568 store
->LitColor
[0].stride
= 16;
569 store
->LitColor
[1].stride
= 16;
572 store
->LitColor
[0].stride
= 0;
573 store
->LitColor
[1].stride
= 0;
576 for (j
= 0; j
< nr
; j
++, STRIDE_F(normal
,nstride
)) {
580 #if IDX & LIGHT_MATERIAL
581 update_materials( ctx
, store
);
583 sumA
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
584 #if IDX & LIGHT_TWOSIDE
585 sumA
[1] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
590 COPY_3V(sum
[0], ctx
->Light
._BaseColor
[0]);
591 #if IDX & LIGHT_TWOSIDE
592 COPY_3V(sum
[1], ctx
->Light
._BaseColor
[1]);
595 foreach (light
, &ctx
->Light
.EnabledList
) {
596 GLfloat n_dot_h
, n_dot_VP
, spec
;
598 ACC_3V(sum
[0], light
->_MatAmbient
[0]);
599 #if IDX & LIGHT_TWOSIDE
600 ACC_3V(sum
[1], light
->_MatAmbient
[1]);
603 n_dot_VP
= DOT3(normal
, light
->_VP_inf_norm
);
605 if (n_dot_VP
> 0.0F
) {
606 ACC_SCALE_SCALAR_3V(sum
[0], n_dot_VP
, light
->_MatDiffuse
[0]);
607 n_dot_h
= DOT3(normal
, light
->_h_inf_norm
);
608 if (n_dot_h
> 0.0F
) {
609 struct gl_shine_tab
*tab
= ctx
->_ShineTable
[0];
610 GET_SHINE_TAB_ENTRY( tab
, n_dot_h
, spec
);
611 ACC_SCALE_SCALAR_3V( sum
[0], spec
, light
->_MatSpecular
[0]);
614 #if IDX & LIGHT_TWOSIDE
616 ACC_SCALE_SCALAR_3V(sum
[1], -n_dot_VP
, light
->_MatDiffuse
[1]);
617 n_dot_h
= -DOT3(normal
, light
->_h_inf_norm
);
618 if (n_dot_h
> 0.0F
) {
619 struct gl_shine_tab
*tab
= ctx
->_ShineTable
[1];
620 GET_SHINE_TAB_ENTRY( tab
, n_dot_h
, spec
);
621 ACC_SCALE_SCALAR_3V( sum
[1], spec
, light
->_MatSpecular
[1]);
627 COPY_3V( Fcolor
[j
], sum
[0] );
628 Fcolor
[j
][3] = sumA
[0];
630 #if IDX & LIGHT_TWOSIDE
631 COPY_3V( Bcolor
[j
], sum
[1] );
632 Bcolor
[j
][3] = sumA
[1];
642 * Use current lighting/material settings to compute the color indexes
643 * for an array of vertices.
644 * Input: n - number of vertices to light
645 * side - 0=use front material, 1=use back material
646 * vertex - array of [n] vertex position in eye coordinates
647 * normal - array of [n] surface normal vector
648 * Output: indexResult - resulting array of [n] color indexes
650 static void TAG(light_ci
)( GLcontext
*ctx
,
651 struct vertex_buffer
*VB
,
652 struct tnl_pipeline_stage
*stage
,
655 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
657 const GLuint vstride
= input
->stride
;
658 const GLfloat
*vertex
= (GLfloat
*) input
->data
;
659 const GLuint nstride
= VB
->NormalPtr
->stride
;
660 const GLfloat
*normal
= (GLfloat
*)VB
->NormalPtr
->data
;
661 GLfloat
*indexResult
[2];
662 const GLuint nr
= VB
->Count
;
665 fprintf(stderr
, "%s\n", __FUNCTION__
);
668 VB
->IndexPtr
[0] = &store
->LitIndex
[0];
669 #if IDX & LIGHT_TWOSIDE
670 VB
->IndexPtr
[1] = &store
->LitIndex
[1];
673 indexResult
[0] = (GLfloat
*)VB
->IndexPtr
[0]->data
;
674 #if IDX & LIGHT_TWOSIDE
675 indexResult
[1] = (GLfloat
*)VB
->IndexPtr
[1]->data
;
678 /* loop over vertices */
679 for (j
=0; j
<nr
; j
++,STRIDE_F(vertex
,vstride
),STRIDE_F(normal
, nstride
)) {
680 GLfloat diffuse
[2], specular
[2];
682 struct gl_light
*light
;
684 #if IDX & LIGHT_MATERIAL
685 update_materials( ctx
, store
);
688 diffuse
[0] = specular
[0] = 0.0F
;
690 #if IDX & LIGHT_TWOSIDE
691 diffuse
[1] = specular
[1] = 0.0F
;
694 /* Accumulate diffuse and specular from each light source */
695 foreach (light
, &ctx
->Light
.EnabledList
) {
697 GLfloat attenuation
= 1.0F
;
698 GLfloat VP
[3]; /* unit vector from vertex to light */
699 GLfloat n_dot_VP
; /* dot product of l and n */
700 GLfloat
*h
, n_dot_h
, correction
= 1.0;
702 /* compute l and attenuation */
703 if (!(light
->_Flags
& LIGHT_POSITIONAL
)) {
704 /* directional light */
705 COPY_3V(VP
, light
->_VP_inf_norm
);
708 GLfloat d
; /* distance from vertex to light */
710 SUB_3V(VP
, light
->_Position
, vertex
);
712 d
= (GLfloat
) LEN_3FV( VP
);
714 GLfloat invd
= 1.0F
/ d
;
715 SELF_SCALE_SCALAR_3V(VP
, invd
);
718 attenuation
= 1.0F
/ (light
->ConstantAttenuation
+ d
*
719 (light
->LinearAttenuation
+ d
*
720 light
->QuadraticAttenuation
));
722 /* spotlight attenuation */
723 if (light
->_Flags
& LIGHT_SPOT
) {
724 GLfloat PV_dot_dir
= - DOT3(VP
, light
->_NormDirection
);
725 if (PV_dot_dir
< light
->_CosCutoff
) {
726 continue; /* this light makes no contribution */
729 GLdouble x
= PV_dot_dir
* (EXP_TABLE_SIZE
-1);
731 GLfloat spot
= (GLfloat
) (light
->_SpotExpTable
[k
][0]
732 + (x
-k
)*light
->_SpotExpTable
[k
][1]);
738 if (attenuation
< 1e-3)
739 continue; /* this light makes no contribution */
741 n_dot_VP
= DOT3( normal
, VP
);
743 /* which side are we lighting? */
744 if (n_dot_VP
< 0.0F
) {
745 #if IDX & LIGHT_TWOSIDE
748 n_dot_VP
= -n_dot_VP
;
754 /* accumulate diffuse term */
755 diffuse
[side
] += n_dot_VP
* light
->_dli
* attenuation
;
758 if (ctx
->Light
.Model
.LocalViewer
) {
762 SUB_3V(VP
, VP
, v
); /* h = VP + VPe */
766 else if (light
->_Flags
& LIGHT_POSITIONAL
) {
768 /* Strangely, disabling this addition fixes a conformance
769 * problem. If this code is enabled, l_sed.c fails.
771 /*ACC_3V(h, ctx->_EyeZDir);*/
775 h
= light
->_h_inf_norm
;
778 n_dot_h
= correction
* DOT3(normal
, h
);
779 if (n_dot_h
> 0.0F
) {
781 struct gl_shine_tab
*tab
= ctx
->_ShineTable
[side
];
782 GET_SHINE_TAB_ENTRY( tab
, n_dot_h
, spec_coef
);
783 specular
[side
] += spec_coef
* light
->_sli
* attenuation
;
785 } /*loop over lights*/
787 /* Now compute final color index */
788 for (side
= 0 ; side
< NR_SIDES
; side
++) {
789 const GLfloat
*ind
= ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_INDEXES
+ side
];
792 if (specular
[side
] > 1.0F
) {
793 index
= ind
[MAT_INDEX_SPECULAR
];
796 GLfloat d_a
= ind
[MAT_INDEX_DIFFUSE
] - ind
[MAT_INDEX_AMBIENT
];
797 GLfloat s_a
= ind
[MAT_INDEX_SPECULAR
] - ind
[MAT_INDEX_AMBIENT
];
798 index
= (ind
[MAT_INDEX_AMBIENT
]
799 + diffuse
[side
] * (1.0F
-specular
[side
]) * d_a
800 + specular
[side
] * s_a
);
801 if (index
> ind
[MAT_INDEX_SPECULAR
]) {
802 index
= ind
[MAT_INDEX_SPECULAR
];
805 indexResult
[side
][j
] = index
;
812 static void TAG(init_light_tab
)( void )
814 _tnl_light_tab
[IDX
] = TAG(light_rgba
);
815 _tnl_light_fast_tab
[IDX
] = TAG(light_fast_rgba
);
816 _tnl_light_fast_single_tab
[IDX
] = TAG(light_fast_rgba_single
);
817 _tnl_light_spec_tab
[IDX
] = TAG(light_rgba_spec
);
818 _tnl_light_ci_tab
[IDX
] = TAG(light_ci
);