1 /* $Id: t_vb_lighttmp.h,v 1.20 2001/12/14 02:51:45 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 * Brian Paul <brianp@valinux.com>
29 * Keith Whitwell <keithw@valinux.com>
33 #if (IDX & LIGHT_FLAGS)
34 # define VSTRIDE (4 * sizeof(GLfloat))
35 # define NSTRIDE (3 * sizeof(GLfloat))
36 # define CHECK_MATERIAL(x) (flags[x] & VERT_MATERIAL)
37 # define CHECK_END_VB(x) (flags[x] & VERT_END_VB)
38 # if (IDX & LIGHT_COLORMATERIAL)
39 # define CMSTRIDE STRIDE_F(CMcolor, CMstride)
40 # define CHECK_COLOR_MATERIAL(x) (flags[x] & VERT_COLOR0_BIT)
41 # define CHECK_VALIDATE(x) (flags[x] & (VERT_COLOR0_BIT|VERT_MATERIAL))
42 # define DO_ANOTHER_NORMAL(x) \
43 ((flags[x] & (VERT_COLOR0_BIT|VERT_NORMAL_BIT|VERT_END_VB|VERT_MATERIAL)) == VERT_NORMAL_BIT)
44 # define REUSE_LIGHT_RESULTS(x) \
45 ((flags[x] & (VERT_COLOR0_BIT|VERT_NORMAL_BIT|VERT_END_VB|VERT_MATERIAL)) == 0)
47 # define CMSTRIDE (void)0
48 # define CHECK_COLOR_MATERIAL(x) 0
49 # define CHECK_VALIDATE(x) (flags[x] & (VERT_MATERIAL))
50 # define DO_ANOTHER_NORMAL(x) \
51 ((flags[x] & (VERT_NORMAL_BIT|VERT_END_VB|VERT_MATERIAL)) == VERT_NORMAL_BIT)
52 # define REUSE_LIGHT_RESULTS(x) \
53 ((flags[x] & (VERT_NORMAL_BIT|VERT_END_VB|VERT_MATERIAL)) == 0)
56 # define VSTRIDE vstride
57 # define NSTRIDE nstride
58 # define CHECK_MATERIAL(x) 0 /* no materials on array paths */
59 # define CHECK_END_VB(XX) (XX >= nr)
60 # if (IDX & LIGHT_COLORMATERIAL)
61 # define CMSTRIDE STRIDE_F(CMcolor, CMstride)
62 # define CHECK_COLOR_MATERIAL(x) (x < nr) /* always have colormaterial */
63 # define CHECK_VALIDATE(x) (x < nr)
64 # define DO_ANOTHER_NORMAL(x) 0 /* always stop to recalc colormat */
66 # define CMSTRIDE (void)0
67 # define CHECK_COLOR_MATERIAL(x) 0 /* no colormaterial */
68 # define CHECK_VALIDATE(x) (0)
69 # define DO_ANOTHER_NORMAL(XX) (XX < nr) /* keep going to end of vb */
71 # define REUSE_LIGHT_RESULTS(x) 0 /* always have a new normal */
76 #if (IDX & LIGHT_TWOSIDE)
84 static void TAG(light_rgba_spec
)( GLcontext
*ctx
,
85 struct vertex_buffer
*VB
,
86 struct gl_pipeline_stage
*stage
,
89 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
90 GLfloat (*base
)[3] = ctx
->Light
._BaseColor
;
95 GLuint vstride
= input
->stride
;
96 const GLfloat
*vertex
= (GLfloat
*)input
->data
;
97 GLuint nstride
= VB
->NormalPtr
->stride
;
98 const GLfloat
*normal
= (GLfloat
*)VB
->NormalPtr
->data
;
103 GLchan (*Fcolor
)[4] = (GLchan (*)[4]) store
->LitColor
[0].Ptr
;
104 GLchan (*Bcolor
)[4] = (GLchan (*)[4]) store
->LitColor
[1].Ptr
;
105 GLchan (*Fspec
)[4] = (GLchan (*)[4]) store
->LitSecondary
[0].Ptr
;
106 GLchan (*Bspec
)[4] = (GLchan (*)[4]) store
->LitSecondary
[1].Ptr
;
107 GLchan (*spec
[2])[4];
109 GLuint nr
= VB
->Count
;
110 GLuint
*flags
= VB
->Flag
;
111 struct gl_material (*new_material
)[2] = VB
->Material
;
112 GLuint
*new_material_mask
= VB
->MaterialMask
;
120 /* fprintf(stderr, "%s\n", __FUNCTION__ ); */
125 if (IDX
& LIGHT_COLORMATERIAL
) {
126 if (VB
->ColorPtr
[0]->Type
!= GL_FLOAT
||
127 VB
->ColorPtr
[0]->Size
!= 4)
128 import_color_material( ctx
, stage
);
130 CMcolor
= (GLfloat
*) VB
->ColorPtr
[0]->Ptr
;
131 CMstride
= VB
->ColorPtr
[0]->StrideB
;
134 VB
->ColorPtr
[0] = &store
->LitColor
[0];
135 VB
->SecondaryColorPtr
[0] = &store
->LitSecondary
[0];
136 UNCLAMPED_FLOAT_TO_CHAN(sumA
[0], ctx
->Light
.Material
[0].Diffuse
[3]);
138 if (IDX
& LIGHT_TWOSIDE
) {
139 VB
->ColorPtr
[1] = &store
->LitColor
[1];
140 VB
->SecondaryColorPtr
[1] = &store
->LitSecondary
[1];
141 UNCLAMPED_FLOAT_TO_CHAN(sumA
[1], ctx
->Light
.Material
[1].Diffuse
[3]);
144 /* Side-effects done, can we finish now?
146 if (stage
->changed_inputs
== 0)
151 j
++,STRIDE_F(vertex
,VSTRIDE
),STRIDE_F(normal
,NSTRIDE
),CMSTRIDE
)
153 GLfloat sum
[2][3], spec
[2][3];
154 struct gl_light
*light
;
156 if ( CHECK_COLOR_MATERIAL(j
) )
157 _mesa_update_color_material( ctx
, CMcolor
);
159 if ( CHECK_MATERIAL(j
) )
160 _mesa_update_material( ctx
, new_material
[j
], new_material_mask
[j
] );
162 if ( CHECK_VALIDATE(j
) ) {
163 _mesa_validate_all_lighting_tables( ctx
);
164 UNCLAMPED_FLOAT_TO_CHAN(sumA
[0], ctx
->Light
.Material
[0].Diffuse
[3]);
165 if (IDX
& LIGHT_TWOSIDE
)
166 UNCLAMPED_FLOAT_TO_CHAN(sumA
[1], ctx
->Light
.Material
[1].Diffuse
[3]);
169 COPY_3V(sum
[0], base
[0]);
172 if (IDX
& LIGHT_TWOSIDE
) {
173 COPY_3V(sum
[1], base
[1]);
177 /* Add contribution from each enabled light source */
178 foreach (light
, &ctx
->Light
.EnabledList
) {
184 GLfloat VP
[3]; /* unit vector from vertex to light */
185 GLfloat n_dot_VP
; /* n dot VP */
188 /* compute VP and attenuation */
189 if (!(light
->_Flags
& LIGHT_POSITIONAL
)) {
190 /* directional light */
191 COPY_3V(VP
, light
->_VP_inf_norm
);
192 attenuation
= light
->_VP_inf_spot_attenuation
;
195 GLfloat d
; /* distance from vertex to light */
197 SUB_3V(VP
, light
->_Position
, vertex
);
199 d
= (GLfloat
) LEN_3FV( VP
);
202 GLfloat invd
= 1.0F
/ d
;
203 SELF_SCALE_SCALAR_3V(VP
, invd
);
206 attenuation
= 1.0F
/ (light
->ConstantAttenuation
+ d
*
207 (light
->LinearAttenuation
+ d
*
208 light
->QuadraticAttenuation
));
210 /* spotlight attenuation */
211 if (light
->_Flags
& LIGHT_SPOT
) {
212 GLfloat PV_dot_dir
= - DOT3(VP
, light
->_NormDirection
);
214 if (PV_dot_dir
<light
->_CosCutoff
) {
215 continue; /* this light makes no contribution */
218 GLdouble x
= PV_dot_dir
* (EXP_TABLE_SIZE
-1);
220 GLfloat spot
= (GLfloat
) (light
->_SpotExpTable
[k
][0]
221 + (x
-k
)*light
->_SpotExpTable
[k
][1]);
227 if (attenuation
< 1e-3)
228 continue; /* this light makes no contribution */
230 /* Compute dot product or normal and vector from V to light pos */
231 n_dot_VP
= DOT3( normal
, VP
);
233 /* Which side gets the diffuse & specular terms? */
234 if (n_dot_VP
< 0.0F
) {
235 ACC_SCALE_SCALAR_3V(sum
[0], attenuation
, light
->_MatAmbient
[0]);
236 if (!(IDX
& LIGHT_TWOSIDE
)) {
241 n_dot_VP
= -n_dot_VP
;
244 if (IDX
& LIGHT_TWOSIDE
) {
245 ACC_SCALE_SCALAR_3V( sum
[1], attenuation
, light
->_MatAmbient
[1]);
252 COPY_3V(contrib
, light
->_MatAmbient
[side
]);
253 ACC_SCALE_SCALAR_3V(contrib
, n_dot_VP
, light
->_MatDiffuse
[side
]);
254 ACC_SCALE_SCALAR_3V(sum
[side
], attenuation
, contrib
);
256 /* specular term - cannibalize VP... */
257 if (ctx
->Light
.Model
.LocalViewer
) {
261 SUB_3V(VP
, VP
, v
); /* h = VP + VPe */
265 else if (light
->_Flags
& LIGHT_POSITIONAL
) {
267 ACC_3V(h
, ctx
->_EyeZDir
);
271 h
= light
->_h_inf_norm
;
274 n_dot_h
= correction
* DOT3(normal
, h
);
276 if (n_dot_h
> 0.0F
) {
278 struct gl_shine_tab
*tab
= ctx
->_ShineTable
[side
];
279 GET_SHINE_TAB_ENTRY( tab
, n_dot_h
, spec_coef
);
281 if (spec_coef
> 1.0e-10) {
282 spec_coef
*= attenuation
;
283 ACC_SCALE_SCALAR_3V( spec
[side
], spec_coef
,
284 light
->_MatSpecular
[side
]);
287 } /*loop over lights*/
289 UNCLAMPED_FLOAT_TO_RGB_CHAN( Fcolor
[j
], sum
[0] );
290 UNCLAMPED_FLOAT_TO_RGB_CHAN( Fspec
[j
], spec
[0] );
291 Fcolor
[j
][3] = sumA
[0];
293 if (IDX
& LIGHT_TWOSIDE
) {
294 UNCLAMPED_FLOAT_TO_RGB_CHAN( Bcolor
[j
], sum
[1] );
295 UNCLAMPED_FLOAT_TO_RGB_CHAN( Bspec
[j
], spec
[1] );
296 Bcolor
[j
][3] = sumA
[1];
302 static void TAG(light_rgba
)( GLcontext
*ctx
,
303 struct vertex_buffer
*VB
,
304 struct gl_pipeline_stage
*stage
,
307 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
310 GLfloat (*base
)[3] = ctx
->Light
._BaseColor
;
313 GLuint vstride
= input
->stride
;
314 const GLfloat
*vertex
= (GLfloat
*) input
->data
;
315 GLuint nstride
= VB
->NormalPtr
->stride
;
316 const GLfloat
*normal
= (GLfloat
*)VB
->NormalPtr
->data
;
321 GLchan (*Fcolor
)[4] = (GLchan (*)[4]) store
->LitColor
[0].Ptr
;
322 GLchan (*Bcolor
)[4] = (GLchan (*)[4]) store
->LitColor
[1].Ptr
;
323 GLchan (*color
[2])[4];
324 GLuint
*flags
= VB
->Flag
;
326 struct gl_material (*new_material
)[2] = VB
->Material
;
327 GLuint
*new_material_mask
= VB
->MaterialMask
;
328 GLuint nr
= VB
->Count
;
330 /* fprintf(stderr, "%s\n", __FUNCTION__ ); */
338 if (IDX
& LIGHT_COLORMATERIAL
) {
339 if (VB
->ColorPtr
[0]->Type
!= GL_FLOAT
||
340 VB
->ColorPtr
[0]->Size
!= 4)
341 import_color_material( ctx
, stage
);
343 CMcolor
= (GLfloat
*)VB
->ColorPtr
[0]->Ptr
;
344 CMstride
= VB
->ColorPtr
[0]->StrideB
;
347 VB
->ColorPtr
[0] = &store
->LitColor
[0];
348 UNCLAMPED_FLOAT_TO_CHAN(sumA
[0], ctx
->Light
.Material
[0].Diffuse
[3]);
350 if (IDX
& LIGHT_TWOSIDE
) {
351 VB
->ColorPtr
[1] = &store
->LitColor
[1];
352 UNCLAMPED_FLOAT_TO_CHAN(sumA
[1], ctx
->Light
.Material
[1].Diffuse
[3]);
355 if (stage
->changed_inputs
== 0)
360 j
++,STRIDE_F(vertex
,VSTRIDE
), STRIDE_F(normal
,NSTRIDE
),CMSTRIDE
)
363 struct gl_light
*light
;
365 if ( CHECK_COLOR_MATERIAL(j
) )
366 _mesa_update_color_material( ctx
, CMcolor
);
368 if ( CHECK_MATERIAL(j
) )
369 _mesa_update_material( ctx
, new_material
[j
], new_material_mask
[j
] );
371 if ( CHECK_VALIDATE(j
) ) {
372 _mesa_validate_all_lighting_tables( ctx
);
373 UNCLAMPED_FLOAT_TO_CHAN(sumA
[0], ctx
->Light
.Material
[0].Diffuse
[3]);
374 if (IDX
& LIGHT_TWOSIDE
)
375 UNCLAMPED_FLOAT_TO_CHAN(sumA
[1], ctx
->Light
.Material
[1].Diffuse
[3]);
378 COPY_3V(sum
[0], base
[0]);
380 if ( IDX
& LIGHT_TWOSIDE
)
381 COPY_3V(sum
[1], base
[1]);
383 /* Add contribution from each enabled light source */
384 foreach (light
, &ctx
->Light
.EnabledList
) {
390 GLfloat attenuation
= 1.0;
391 GLfloat VP
[3]; /* unit vector from vertex to light */
392 GLfloat n_dot_VP
; /* n dot VP */
395 /* compute VP and attenuation */
396 if (!(light
->_Flags
& LIGHT_POSITIONAL
)) {
397 /* directional light */
398 COPY_3V(VP
, light
->_VP_inf_norm
);
399 attenuation
= light
->_VP_inf_spot_attenuation
;
402 GLfloat d
; /* distance from vertex to light */
405 SUB_3V(VP
, light
->_Position
, vertex
);
407 d
= (GLfloat
) LEN_3FV( VP
);
410 GLfloat invd
= 1.0F
/ d
;
411 SELF_SCALE_SCALAR_3V(VP
, invd
);
414 attenuation
= 1.0F
/ (light
->ConstantAttenuation
+ d
*
415 (light
->LinearAttenuation
+ d
*
416 light
->QuadraticAttenuation
));
418 /* spotlight attenuation */
419 if (light
->_Flags
& LIGHT_SPOT
) {
420 GLfloat PV_dot_dir
= - DOT3(VP
, light
->_NormDirection
);
422 if (PV_dot_dir
<light
->_CosCutoff
) {
423 continue; /* this light makes no contribution */
426 GLdouble x
= PV_dot_dir
* (EXP_TABLE_SIZE
-1);
428 GLfloat spot
= (GLfloat
) (light
->_SpotExpTable
[k
][0]
429 + (x
-k
)*light
->_SpotExpTable
[k
][1]);
435 if (attenuation
< 1e-3)
436 continue; /* this light makes no contribution */
438 /* Compute dot product or normal and vector from V to light pos */
439 n_dot_VP
= DOT3( normal
, VP
);
441 /* which side are we lighting? */
442 if (n_dot_VP
< 0.0F
) {
443 ACC_SCALE_SCALAR_3V(sum
[0], attenuation
, light
->_MatAmbient
[0]);
445 if (!(IDX
& LIGHT_TWOSIDE
))
450 n_dot_VP
= -n_dot_VP
;
453 if (IDX
& LIGHT_TWOSIDE
) {
454 ACC_SCALE_SCALAR_3V( sum
[1], attenuation
, light
->_MatAmbient
[1]);
460 COPY_3V(contrib
, light
->_MatAmbient
[side
]);
463 ACC_SCALE_SCALAR_3V(contrib
, n_dot_VP
, light
->_MatDiffuse
[side
]);
465 /* specular term - cannibalize VP... */
467 if (ctx
->Light
.Model
.LocalViewer
) {
471 SUB_3V(VP
, VP
, v
); /* h = VP + VPe */
475 else if (light
->_Flags
& LIGHT_POSITIONAL
) {
477 ACC_3V(h
, ctx
->_EyeZDir
);
481 h
= light
->_h_inf_norm
;
484 n_dot_h
= correction
* DOT3(normal
, h
);
489 struct gl_shine_tab
*tab
= ctx
->_ShineTable
[side
];
491 GET_SHINE_TAB_ENTRY( tab
, n_dot_h
, spec_coef
);
493 ACC_SCALE_SCALAR_3V( contrib
, spec_coef
,
494 light
->_MatSpecular
[side
]);
498 ACC_SCALE_SCALAR_3V( sum
[side
], attenuation
, contrib
);
501 UNCLAMPED_FLOAT_TO_RGB_CHAN( Fcolor
[j
], sum
[0] );
502 Fcolor
[j
][3] = sumA
[0];
504 if (IDX
& LIGHT_TWOSIDE
) {
505 UNCLAMPED_FLOAT_TO_RGB_CHAN( Bcolor
[j
], sum
[1] );
506 Bcolor
[j
][3] = sumA
[1];
514 /* As below, but with just a single light.
516 static void TAG(light_fast_rgba_single
)( GLcontext
*ctx
,
517 struct vertex_buffer
*VB
,
518 struct gl_pipeline_stage
*stage
,
522 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
523 GLuint nstride
= VB
->NormalPtr
->stride
;
524 const GLfloat
*normal
= (GLfloat
*)VB
->NormalPtr
->data
;
527 GLchan (*Fcolor
)[4] = (GLchan (*)[4]) store
->LitColor
[0].Ptr
;
528 GLchan (*Bcolor
)[4] = (GLchan (*)[4]) store
->LitColor
[1].Ptr
;
529 struct gl_light
*light
= ctx
->Light
.EnabledList
.next
;
530 GLuint
*flags
= VB
->Flag
;
531 GLchan basechan
[2][4];
533 struct gl_material (*new_material
)[2] = VB
->Material
;
534 GLuint
*new_material_mask
= VB
->MaterialMask
;
536 GLuint nr
= VB
->Count
;
538 /* fprintf(stderr, "%s\n", __FUNCTION__ ); */
539 (void) input
; /* doesn't refer to Eye or Obj */
544 if (IDX
& LIGHT_COLORMATERIAL
) {
545 if (VB
->ColorPtr
[0]->Type
!= GL_FLOAT
||
546 VB
->ColorPtr
[0]->Size
!= 4)
547 import_color_material( ctx
, stage
);
549 CMcolor
= (GLfloat
*)VB
->ColorPtr
[0]->Ptr
;
550 CMstride
= VB
->ColorPtr
[0]->StrideB
;
553 VB
->ColorPtr
[0] = &store
->LitColor
[0];
554 if (IDX
& LIGHT_TWOSIDE
)
555 VB
->ColorPtr
[1] = &store
->LitColor
[1];
557 if (stage
->changed_inputs
== 0)
562 if ( CHECK_COLOR_MATERIAL(j
) ) {
563 /* fprintf(stderr, "colormaterial at %d (%p)\n", j, CMcolor); */
564 _mesa_update_color_material( ctx
, CMcolor
);
567 if ( CHECK_MATERIAL(j
) )
568 _mesa_update_material( ctx
, new_material
[j
], new_material_mask
[j
] );
570 if ( CHECK_VALIDATE(j
) )
571 _mesa_validate_all_lighting_tables( ctx
);
574 /* No attenuation, so incoporate _MatAmbient into base color.
576 COPY_3V(base
[0], light
->_MatAmbient
[0]);
577 ACC_3V(base
[0], ctx
->Light
._BaseColor
[0] );
578 UNCLAMPED_FLOAT_TO_RGB_CHAN( basechan
[0], base
[0] );
579 UNCLAMPED_FLOAT_TO_CHAN(basechan
[0][3],
580 ctx
->Light
.Material
[0].Diffuse
[3]);
582 if (IDX
& LIGHT_TWOSIDE
) {
583 COPY_3V(base
[1], light
->_MatAmbient
[1]);
584 ACC_3V(base
[1], ctx
->Light
._BaseColor
[1]);
585 UNCLAMPED_FLOAT_TO_RGB_CHAN( basechan
[1], base
[1]);
586 UNCLAMPED_FLOAT_TO_CHAN(basechan
[1][3],
587 ctx
->Light
.Material
[1].Diffuse
[3]);
591 GLfloat n_dot_VP
= DOT3(normal
, light
->_VP_inf_norm
);
594 /* fprintf(stderr, "light normal %d: %f %f %f\n", */
600 if (n_dot_VP
< 0.0F
) {
601 if (IDX
& LIGHT_TWOSIDE
) {
602 GLfloat n_dot_h
= -DOT3(normal
, light
->_h_inf_norm
);
604 COPY_3V(sum
, base
[1]);
605 ACC_SCALE_SCALAR_3V(sum
, -n_dot_VP
, light
->_MatDiffuse
[1]);
606 if (n_dot_h
> 0.0F
) {
608 GET_SHINE_TAB_ENTRY( ctx
->_ShineTable
[1], n_dot_h
, spec
);
609 ACC_SCALE_SCALAR_3V(sum
, spec
, light
->_MatSpecular
[1]);
611 UNCLAMPED_FLOAT_TO_RGB_CHAN(Bcolor
[j
], sum
);
612 Bcolor
[j
][3] = basechan
[1][3];
614 COPY_CHAN4(Fcolor
[j
], basechan
[0]);
616 GLfloat n_dot_h
= DOT3(normal
, light
->_h_inf_norm
);
618 COPY_3V(sum
, base
[0]);
619 ACC_SCALE_SCALAR_3V(sum
, n_dot_VP
, light
->_MatDiffuse
[0]);
620 if (n_dot_h
> 0.0F
) {
622 GET_SHINE_TAB_ENTRY( ctx
->_ShineTable
[0], n_dot_h
, spec
);
623 ACC_SCALE_SCALAR_3V(sum
, spec
, light
->_MatSpecular
[0]);
626 UNCLAMPED_FLOAT_TO_RGB_CHAN(Fcolor
[j
], sum
);
627 Fcolor
[j
][3] = basechan
[0][3];
628 if (IDX
& LIGHT_TWOSIDE
) COPY_CHAN4(Bcolor
[j
], basechan
[1]);
633 STRIDE_F(normal
, NSTRIDE
);
634 } while (DO_ANOTHER_NORMAL(j
));
637 for ( ; REUSE_LIGHT_RESULTS(j
) ; j
++, CMSTRIDE
, STRIDE_F(normal
,NSTRIDE
))
639 COPY_CHAN4(Fcolor
[j
], Fcolor
[j
-1]);
640 if (IDX
& LIGHT_TWOSIDE
)
641 COPY_CHAN4(Bcolor
[j
], Bcolor
[j
-1]);
644 /* fprintf(stderr, "skip normal %d: %f %f %f\n", */
652 } while (!CHECK_END_VB(j
));
656 /* Light infinite lights
658 static void TAG(light_fast_rgba
)( GLcontext
*ctx
,
659 struct vertex_buffer
*VB
,
660 struct gl_pipeline_stage
*stage
,
663 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
665 GLuint nstride
= VB
->NormalPtr
->stride
;
666 const GLfloat
*normal
= (GLfloat
*)VB
->NormalPtr
->data
;
669 GLchan (*Fcolor
)[4] = (GLchan (*)[4]) store
->LitColor
[0].Ptr
;
670 GLchan (*Bcolor
)[4] = (GLchan (*)[4]) store
->LitColor
[1].Ptr
;
671 GLuint
*flags
= VB
->Flag
;
673 struct gl_material (*new_material
)[2] = VB
->Material
;
674 GLuint
*new_material_mask
= VB
->MaterialMask
;
675 GLuint nr
= VB
->Count
;
676 struct gl_light
*light
;
678 /* fprintf(stderr, "%s\n", __FUNCTION__ ); */
684 UNCLAMPED_FLOAT_TO_CHAN(sumA
[0], ctx
->Light
.Material
[0].Diffuse
[3]);
685 UNCLAMPED_FLOAT_TO_CHAN(sumA
[1], ctx
->Light
.Material
[1].Diffuse
[3]);
687 if (IDX
& LIGHT_COLORMATERIAL
) {
688 if (VB
->ColorPtr
[0]->Type
!= GL_FLOAT
||
689 VB
->ColorPtr
[0]->Size
!= 4)
690 import_color_material( ctx
, stage
);
692 CMcolor
= (GLfloat
*)VB
->ColorPtr
[0]->Ptr
;
693 CMstride
= VB
->ColorPtr
[0]->StrideB
;
696 VB
->ColorPtr
[0] = &store
->LitColor
[0];
697 if (IDX
& LIGHT_TWOSIDE
)
698 VB
->ColorPtr
[1] = &store
->LitColor
[1];
700 if (stage
->changed_inputs
== 0)
707 if ( CHECK_COLOR_MATERIAL(j
) )
708 _mesa_update_color_material( ctx
, CMcolor
);
710 if ( CHECK_MATERIAL(j
) )
711 _mesa_update_material( ctx
, new_material
[j
], new_material_mask
[j
] );
713 if ( CHECK_VALIDATE(j
) ) {
714 _mesa_validate_all_lighting_tables( ctx
);
715 UNCLAMPED_FLOAT_TO_CHAN(sumA
[0], ctx
->Light
.Material
[0].Diffuse
[3]);
716 if (IDX
& LIGHT_TWOSIDE
)
717 UNCLAMPED_FLOAT_TO_CHAN(sumA
[1],
718 ctx
->Light
.Material
[1].Diffuse
[3]);
722 COPY_3V(sum
[0], ctx
->Light
._BaseColor
[0]);
723 if (IDX
& LIGHT_TWOSIDE
)
724 COPY_3V(sum
[1], ctx
->Light
._BaseColor
[1]);
726 foreach (light
, &ctx
->Light
.EnabledList
) {
727 GLfloat n_dot_h
, n_dot_VP
, spec
;
729 ACC_3V(sum
[0], light
->_MatAmbient
[0]);
730 if (IDX
& LIGHT_TWOSIDE
)
731 ACC_3V(sum
[1], light
->_MatAmbient
[1]);
733 n_dot_VP
= DOT3(normal
, light
->_VP_inf_norm
);
735 if (n_dot_VP
> 0.0F
) {
736 ACC_SCALE_SCALAR_3V(sum
[0], n_dot_VP
, light
->_MatDiffuse
[0]);
737 n_dot_h
= DOT3(normal
, light
->_h_inf_norm
);
738 if (n_dot_h
> 0.0F
) {
739 struct gl_shine_tab
*tab
= ctx
->_ShineTable
[0];
740 GET_SHINE_TAB_ENTRY( tab
, n_dot_h
, spec
);
741 ACC_SCALE_SCALAR_3V( sum
[0], spec
,
742 light
->_MatSpecular
[0]);
745 else if (IDX
& LIGHT_TWOSIDE
) {
746 ACC_SCALE_SCALAR_3V(sum
[1], -n_dot_VP
, light
->_MatDiffuse
[1]);
747 n_dot_h
= -DOT3(normal
, light
->_h_inf_norm
);
748 if (n_dot_h
> 0.0F
) {
749 struct gl_shine_tab
*tab
= ctx
->_ShineTable
[1];
750 GET_SHINE_TAB_ENTRY( tab
, n_dot_h
, spec
);
751 ACC_SCALE_SCALAR_3V( sum
[1], spec
,
752 light
->_MatSpecular
[1]);
757 UNCLAMPED_FLOAT_TO_RGB_CHAN( Fcolor
[j
], sum
[0] );
758 Fcolor
[j
][3] = sumA
[0];
760 if (IDX
& LIGHT_TWOSIDE
) {
761 UNCLAMPED_FLOAT_TO_RGB_CHAN( Bcolor
[j
], sum
[1] );
762 Bcolor
[j
][3] = sumA
[1];
767 STRIDE_F(normal
, NSTRIDE
);
768 } while (DO_ANOTHER_NORMAL(j
));
770 /* Reuse the shading results while there is no change to
771 * normal or material values.
773 for ( ; REUSE_LIGHT_RESULTS(j
) ; j
++, CMSTRIDE
, STRIDE_F(normal
, NSTRIDE
))
775 COPY_CHAN4(Fcolor
[j
], Fcolor
[j
-1]);
776 if (IDX
& LIGHT_TWOSIDE
)
777 COPY_CHAN4(Bcolor
[j
], Bcolor
[j
-1]);
780 } while (!CHECK_END_VB(j
));
788 * Use current lighting/material settings to compute the color indexes
789 * for an array of vertices.
790 * Input: n - number of vertices to light
791 * side - 0=use front material, 1=use back material
792 * vertex - array of [n] vertex position in eye coordinates
793 * normal - array of [n] surface normal vector
794 * Output: indexResult - resulting array of [n] color indexes
796 static void TAG(light_ci
)( GLcontext
*ctx
,
797 struct vertex_buffer
*VB
,
798 struct gl_pipeline_stage
*stage
,
801 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
803 GLuint vstride
= input
->stride
;
804 const GLfloat
*vertex
= (GLfloat
*) input
->data
;
805 GLuint nstride
= VB
->NormalPtr
->stride
;
806 const GLfloat
*normal
= (GLfloat
*)VB
->NormalPtr
->data
;
809 GLuint
*flags
= VB
->Flag
;
810 GLuint
*indexResult
[2];
811 struct gl_material (*new_material
)[2] = VB
->Material
;
812 GLuint
*new_material_mask
= VB
->MaterialMask
;
813 GLuint nr
= VB
->Count
;
815 /* fprintf(stderr, "%s\n", __FUNCTION__ ); */
820 VB
->IndexPtr
[0] = &store
->LitIndex
[0];
821 if (IDX
& LIGHT_TWOSIDE
)
822 VB
->IndexPtr
[1] = &store
->LitIndex
[1];
824 if (stage
->changed_inputs
== 0)
827 indexResult
[0] = VB
->IndexPtr
[0]->data
;
828 if (IDX
& LIGHT_TWOSIDE
)
829 indexResult
[1] = VB
->IndexPtr
[1]->data
;
831 if (IDX
& LIGHT_COLORMATERIAL
) {
832 if (VB
->ColorPtr
[0]->Type
!= GL_FLOAT
||
833 VB
->ColorPtr
[0]->Size
!= 4)
834 import_color_material( ctx
, stage
);
836 CMcolor
= (GLfloat
*)VB
->ColorPtr
[0]->Ptr
;
837 CMstride
= VB
->ColorPtr
[0]->StrideB
;
840 /* loop over vertices */
843 j
++,STRIDE_F(vertex
,VSTRIDE
),STRIDE_F(normal
, NSTRIDE
), CMSTRIDE
)
845 GLfloat diffuse
[2], specular
[2];
847 struct gl_light
*light
;
849 if ( CHECK_COLOR_MATERIAL(j
) )
850 _mesa_update_color_material( ctx
, CMcolor
);
852 if ( CHECK_MATERIAL(j
) )
853 _mesa_update_material( ctx
, new_material
[j
], new_material_mask
[j
] );
855 if ( CHECK_VALIDATE(j
) )
856 _mesa_validate_all_lighting_tables( ctx
);
858 diffuse
[0] = specular
[0] = 0.0F
;
860 if ( IDX
& LIGHT_TWOSIDE
) {
861 diffuse
[1] = specular
[1] = 0.0F
;
864 /* Accumulate diffuse and specular from each light source */
865 foreach (light
, &ctx
->Light
.EnabledList
) {
867 GLfloat attenuation
= 1.0F
;
868 GLfloat VP
[3]; /* unit vector from vertex to light */
869 GLfloat n_dot_VP
; /* dot product of l and n */
870 GLfloat
*h
, n_dot_h
, correction
= 1.0;
872 /* compute l and attenuation */
873 if (!(light
->_Flags
& LIGHT_POSITIONAL
)) {
874 /* directional light */
875 COPY_3V(VP
, light
->_VP_inf_norm
);
878 GLfloat d
; /* distance from vertex to light */
880 SUB_3V(VP
, light
->_Position
, vertex
);
882 d
= (GLfloat
) LEN_3FV( VP
);
884 GLfloat invd
= 1.0F
/ d
;
885 SELF_SCALE_SCALAR_3V(VP
, invd
);
888 attenuation
= 1.0F
/ (light
->ConstantAttenuation
+ d
*
889 (light
->LinearAttenuation
+ d
*
890 light
->QuadraticAttenuation
));
892 /* spotlight attenuation */
893 if (light
->_Flags
& LIGHT_SPOT
) {
894 GLfloat PV_dot_dir
= - DOT3(VP
, light
->_NormDirection
);
895 if (PV_dot_dir
< light
->_CosCutoff
) {
896 continue; /* this light makes no contribution */
899 GLdouble x
= PV_dot_dir
* (EXP_TABLE_SIZE
-1);
901 GLfloat spot
= (GLfloat
) (light
->_SpotExpTable
[k
][0]
902 + (x
-k
)*light
->_SpotExpTable
[k
][1]);
908 if (attenuation
< 1e-3)
909 continue; /* this light makes no contribution */
911 n_dot_VP
= DOT3( normal
, VP
);
913 /* which side are we lighting? */
914 if (n_dot_VP
< 0.0F
) {
915 if (!(IDX
& LIGHT_TWOSIDE
))
919 n_dot_VP
= -n_dot_VP
;
922 /* accumulate diffuse term */
923 diffuse
[side
] += n_dot_VP
* light
->_dli
* attenuation
;
926 if (ctx
->Light
.Model
.LocalViewer
) {
930 SUB_3V(VP
, VP
, v
); /* h = VP + VPe */
934 else if (light
->_Flags
& LIGHT_POSITIONAL
) {
936 /* Strangely, disabling this addition fixes a conformance
937 * problem. If this code is enabled, l_sed.c fails.
939 /*ACC_3V(h, ctx->_EyeZDir);*/
943 h
= light
->_h_inf_norm
;
946 n_dot_h
= correction
* DOT3(normal
, h
);
947 if (n_dot_h
> 0.0F
) {
949 struct gl_shine_tab
*tab
= ctx
->_ShineTable
[side
];
950 GET_SHINE_TAB_ENTRY( tab
, n_dot_h
, spec_coef
);
951 specular
[side
] += spec_coef
* light
->_sli
* attenuation
;
953 } /*loop over lights*/
955 /* Now compute final color index */
956 for (side
= 0 ; side
< NR_SIDES
; side
++) {
957 struct gl_material
*mat
= &ctx
->Light
.Material
[side
];
960 if (specular
[side
] > 1.0F
) {
961 index
= mat
->SpecularIndex
;
964 GLfloat d_a
= mat
->DiffuseIndex
- mat
->AmbientIndex
;
965 GLfloat s_a
= mat
->SpecularIndex
- mat
->AmbientIndex
;
967 index
= mat
->AmbientIndex
968 + diffuse
[side
] * (1.0F
-specular
[side
]) * d_a
969 + specular
[side
] * s_a
;
971 if (index
> mat
->SpecularIndex
) {
972 index
= mat
->SpecularIndex
;
975 indexResult
[side
][j
] = (GLuint
) (GLint
) index
;
982 static void TAG(init_light_tab
)( void )
984 _tnl_light_tab
[IDX
] = TAG(light_rgba
);
985 _tnl_light_fast_tab
[IDX
] = TAG(light_fast_rgba
);
986 _tnl_light_fast_single_tab
[IDX
] = TAG(light_fast_rgba_single
);
987 _tnl_light_spec_tab
[IDX
] = TAG(light_rgba_spec
);
988 _tnl_light_ci_tab
[IDX
] = TAG(light_ci
);
997 #undef CHECK_MATERIAL
999 #undef DO_ANOTHER_NORMAL
1000 #undef REUSE_LIGHT_RESULTS
1002 #undef CHECK_COLOR_MATERIAL
1003 #undef CHECK_VALIDATE