1 /* $Id: t_vb_lighttmp.h,v 1.18 2001/09/14 21:30:31 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_RGBA)
41 # define CHECK_VALIDATE(x) (flags[x] & (VERT_RGBA|VERT_MATERIAL))
42 # define DO_ANOTHER_NORMAL(x) \
43 ((flags[x] & (VERT_RGBA|VERT_NORM|VERT_END_VB|VERT_MATERIAL)) == VERT_NORM)
44 # define REUSE_LIGHT_RESULTS(x) \
45 ((flags[x] & (VERT_RGBA|VERT_NORM|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_NORM|VERT_END_VB|VERT_MATERIAL)) == VERT_NORM)
52 # define REUSE_LIGHT_RESULTS(x) \
53 ((flags[x] & (VERT_NORM|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
);
613 COPY_CHAN4(Fcolor
[j
], basechan
[0]);
615 GLfloat n_dot_h
= DOT3(normal
, light
->_h_inf_norm
);
617 COPY_3V(sum
, base
[0]);
618 ACC_SCALE_SCALAR_3V(sum
, n_dot_VP
, light
->_MatDiffuse
[0]);
619 if (n_dot_h
> 0.0F
) {
621 GET_SHINE_TAB_ENTRY( ctx
->_ShineTable
[0], n_dot_h
, spec
);
622 ACC_SCALE_SCALAR_3V(sum
, spec
, light
->_MatSpecular
[0]);
625 UNCLAMPED_FLOAT_TO_RGB_CHAN(Fcolor
[j
], sum
);
626 if (IDX
& LIGHT_TWOSIDE
) COPY_CHAN4(Bcolor
[j
], basechan
[1]);
631 STRIDE_F(normal
, NSTRIDE
);
632 } while (DO_ANOTHER_NORMAL(j
));
635 for ( ; REUSE_LIGHT_RESULTS(j
) ; j
++, CMSTRIDE
, STRIDE_F(normal
,NSTRIDE
))
637 COPY_CHAN4(Fcolor
[j
], Fcolor
[j
-1]);
638 if (IDX
& LIGHT_TWOSIDE
)
639 COPY_CHAN4(Bcolor
[j
], Bcolor
[j
-1]);
642 /* fprintf(stderr, "skip normal %d: %f %f %f\n", */
650 } while (!CHECK_END_VB(j
));
654 /* Light infinite lights
656 static void TAG(light_fast_rgba
)( GLcontext
*ctx
,
657 struct vertex_buffer
*VB
,
658 struct gl_pipeline_stage
*stage
,
661 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
663 GLuint nstride
= VB
->NormalPtr
->stride
;
664 const GLfloat
*normal
= (GLfloat
*)VB
->NormalPtr
->data
;
667 GLchan (*Fcolor
)[4] = (GLchan (*)[4]) store
->LitColor
[0].Ptr
;
668 GLchan (*Bcolor
)[4] = (GLchan (*)[4]) store
->LitColor
[1].Ptr
;
669 GLuint
*flags
= VB
->Flag
;
671 struct gl_material (*new_material
)[2] = VB
->Material
;
672 GLuint
*new_material_mask
= VB
->MaterialMask
;
673 GLuint nr
= VB
->Count
;
674 struct gl_light
*light
;
676 /* fprintf(stderr, "%s\n", __FUNCTION__ ); */
682 UNCLAMPED_FLOAT_TO_CHAN(sumA
[0], ctx
->Light
.Material
[0].Diffuse
[3]);
683 UNCLAMPED_FLOAT_TO_CHAN(sumA
[1], ctx
->Light
.Material
[1].Diffuse
[3]);
685 if (IDX
& LIGHT_COLORMATERIAL
) {
686 if (VB
->ColorPtr
[0]->Type
!= GL_FLOAT
||
687 VB
->ColorPtr
[0]->Size
!= 4)
688 import_color_material( ctx
, stage
);
690 CMcolor
= (GLfloat
*)VB
->ColorPtr
[0]->Ptr
;
691 CMstride
= VB
->ColorPtr
[0]->StrideB
;
694 VB
->ColorPtr
[0] = &store
->LitColor
[0];
695 if (IDX
& LIGHT_TWOSIDE
)
696 VB
->ColorPtr
[1] = &store
->LitColor
[1];
698 if (stage
->changed_inputs
== 0)
705 if ( CHECK_COLOR_MATERIAL(j
) )
706 _mesa_update_color_material( ctx
, CMcolor
);
708 if ( CHECK_MATERIAL(j
) )
709 _mesa_update_material( ctx
, new_material
[j
], new_material_mask
[j
] );
711 if ( CHECK_VALIDATE(j
) ) {
712 _mesa_validate_all_lighting_tables( ctx
);
713 UNCLAMPED_FLOAT_TO_CHAN(sumA
[0], ctx
->Light
.Material
[0].Diffuse
[3]);
714 if (IDX
& LIGHT_TWOSIDE
)
715 UNCLAMPED_FLOAT_TO_CHAN(sumA
[1],
716 ctx
->Light
.Material
[1].Diffuse
[3]);
720 COPY_3V(sum
[0], ctx
->Light
._BaseColor
[0]);
721 if (IDX
& LIGHT_TWOSIDE
)
722 COPY_3V(sum
[1], ctx
->Light
._BaseColor
[1]);
724 foreach (light
, &ctx
->Light
.EnabledList
) {
725 GLfloat n_dot_h
, n_dot_VP
, spec
;
727 ACC_3V(sum
[0], light
->_MatAmbient
[0]);
728 if (IDX
& LIGHT_TWOSIDE
)
729 ACC_3V(sum
[1], light
->_MatAmbient
[1]);
731 n_dot_VP
= DOT3(normal
, light
->_VP_inf_norm
);
733 if (n_dot_VP
> 0.0F
) {
734 ACC_SCALE_SCALAR_3V(sum
[0], n_dot_VP
, light
->_MatDiffuse
[0]);
735 n_dot_h
= DOT3(normal
, light
->_h_inf_norm
);
736 if (n_dot_h
> 0.0F
) {
737 struct gl_shine_tab
*tab
= ctx
->_ShineTable
[0];
738 GET_SHINE_TAB_ENTRY( tab
, n_dot_h
, spec
);
739 ACC_SCALE_SCALAR_3V( sum
[0], spec
,
740 light
->_MatSpecular
[0]);
743 else if (IDX
& LIGHT_TWOSIDE
) {
744 ACC_SCALE_SCALAR_3V(sum
[1], -n_dot_VP
, light
->_MatDiffuse
[1]);
745 n_dot_h
= -DOT3(normal
, light
->_h_inf_norm
);
746 if (n_dot_h
> 0.0F
) {
747 struct gl_shine_tab
*tab
= ctx
->_ShineTable
[1];
748 GET_SHINE_TAB_ENTRY( tab
, n_dot_h
, spec
);
749 ACC_SCALE_SCALAR_3V( sum
[1], spec
,
750 light
->_MatSpecular
[1]);
755 UNCLAMPED_FLOAT_TO_RGB_CHAN( Fcolor
[j
], sum
[0] );
756 Fcolor
[j
][3] = sumA
[0];
758 if (IDX
& LIGHT_TWOSIDE
) {
759 UNCLAMPED_FLOAT_TO_RGB_CHAN( Bcolor
[j
], sum
[1] );
760 Bcolor
[j
][3] = sumA
[1];
765 STRIDE_F(normal
, NSTRIDE
);
766 } while (DO_ANOTHER_NORMAL(j
));
768 /* Reuse the shading results while there is no change to
769 * normal or material values.
771 for ( ; REUSE_LIGHT_RESULTS(j
) ; j
++, CMSTRIDE
, STRIDE_F(normal
, NSTRIDE
))
773 COPY_CHAN4(Fcolor
[j
], Fcolor
[j
-1]);
774 if (IDX
& LIGHT_TWOSIDE
)
775 COPY_CHAN4(Bcolor
[j
], Bcolor
[j
-1]);
778 } while (!CHECK_END_VB(j
));
786 * Use current lighting/material settings to compute the color indexes
787 * for an array of vertices.
788 * Input: n - number of vertices to light
789 * side - 0=use front material, 1=use back material
790 * vertex - array of [n] vertex position in eye coordinates
791 * normal - array of [n] surface normal vector
792 * Output: indexResult - resulting array of [n] color indexes
794 static void TAG(light_ci
)( GLcontext
*ctx
,
795 struct vertex_buffer
*VB
,
796 struct gl_pipeline_stage
*stage
,
799 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
801 GLuint vstride
= input
->stride
;
802 const GLfloat
*vertex
= (GLfloat
*) input
->data
;
803 GLuint nstride
= VB
->NormalPtr
->stride
;
804 const GLfloat
*normal
= (GLfloat
*)VB
->NormalPtr
->data
;
807 GLuint
*flags
= VB
->Flag
;
808 GLuint
*indexResult
[2];
809 struct gl_material (*new_material
)[2] = VB
->Material
;
810 GLuint
*new_material_mask
= VB
->MaterialMask
;
811 GLuint nr
= VB
->Count
;
813 /* fprintf(stderr, "%s\n", __FUNCTION__ ); */
818 VB
->IndexPtr
[0] = &store
->LitIndex
[0];
819 if (IDX
& LIGHT_TWOSIDE
)
820 VB
->IndexPtr
[1] = &store
->LitIndex
[1];
822 if (stage
->changed_inputs
== 0)
825 indexResult
[0] = VB
->IndexPtr
[0]->data
;
826 if (IDX
& LIGHT_TWOSIDE
)
827 indexResult
[1] = VB
->IndexPtr
[1]->data
;
829 if (IDX
& LIGHT_COLORMATERIAL
) {
830 if (VB
->ColorPtr
[0]->Type
!= GL_FLOAT
||
831 VB
->ColorPtr
[0]->Size
!= 4)
832 import_color_material( ctx
, stage
);
834 CMcolor
= (GLfloat
*)VB
->ColorPtr
[0]->Ptr
;
835 CMstride
= VB
->ColorPtr
[0]->StrideB
;
838 /* loop over vertices */
841 j
++,STRIDE_F(vertex
,VSTRIDE
),STRIDE_F(normal
, NSTRIDE
), CMSTRIDE
)
843 GLfloat diffuse
[2], specular
[2];
845 struct gl_light
*light
;
847 if ( CHECK_COLOR_MATERIAL(j
) )
848 _mesa_update_color_material( ctx
, CMcolor
);
850 if ( CHECK_MATERIAL(j
) )
851 _mesa_update_material( ctx
, new_material
[j
], new_material_mask
[j
] );
853 if ( CHECK_VALIDATE(j
) )
854 _mesa_validate_all_lighting_tables( ctx
);
856 diffuse
[0] = specular
[0] = 0.0F
;
858 if ( IDX
& LIGHT_TWOSIDE
) {
859 diffuse
[1] = specular
[1] = 0.0F
;
862 /* Accumulate diffuse and specular from each light source */
863 foreach (light
, &ctx
->Light
.EnabledList
) {
865 GLfloat attenuation
= 1.0F
;
866 GLfloat VP
[3]; /* unit vector from vertex to light */
867 GLfloat n_dot_VP
; /* dot product of l and n */
868 GLfloat
*h
, n_dot_h
, correction
= 1.0;
870 /* compute l and attenuation */
871 if (!(light
->_Flags
& LIGHT_POSITIONAL
)) {
872 /* directional light */
873 COPY_3V(VP
, light
->_VP_inf_norm
);
876 GLfloat d
; /* distance from vertex to light */
878 SUB_3V(VP
, light
->_Position
, vertex
);
880 d
= (GLfloat
) LEN_3FV( VP
);
882 GLfloat invd
= 1.0F
/ d
;
883 SELF_SCALE_SCALAR_3V(VP
, invd
);
886 attenuation
= 1.0F
/ (light
->ConstantAttenuation
+ d
*
887 (light
->LinearAttenuation
+ d
*
888 light
->QuadraticAttenuation
));
890 /* spotlight attenuation */
891 if (light
->_Flags
& LIGHT_SPOT
) {
892 GLfloat PV_dot_dir
= - DOT3(VP
, light
->_NormDirection
);
893 if (PV_dot_dir
< light
->_CosCutoff
) {
894 continue; /* this light makes no contribution */
897 GLdouble x
= PV_dot_dir
* (EXP_TABLE_SIZE
-1);
899 GLfloat spot
= (GLfloat
) (light
->_SpotExpTable
[k
][0]
900 + (x
-k
)*light
->_SpotExpTable
[k
][1]);
906 if (attenuation
< 1e-3)
907 continue; /* this light makes no contribution */
909 n_dot_VP
= DOT3( normal
, VP
);
911 /* which side are we lighting? */
912 if (n_dot_VP
< 0.0F
) {
913 if (!(IDX
& LIGHT_TWOSIDE
))
917 n_dot_VP
= -n_dot_VP
;
920 /* accumulate diffuse term */
921 diffuse
[side
] += n_dot_VP
* light
->_dli
* attenuation
;
924 if (ctx
->Light
.Model
.LocalViewer
) {
928 SUB_3V(VP
, VP
, v
); /* h = VP + VPe */
932 else if (light
->_Flags
& LIGHT_POSITIONAL
) {
934 /* Strangely, disabling this addition fixes a conformance
935 * problem. If this code is enabled, l_sed.c fails.
937 /*ACC_3V(h, ctx->_EyeZDir);*/
941 h
= light
->_h_inf_norm
;
944 n_dot_h
= correction
* DOT3(normal
, h
);
945 if (n_dot_h
> 0.0F
) {
947 struct gl_shine_tab
*tab
= ctx
->_ShineTable
[side
];
948 GET_SHINE_TAB_ENTRY( tab
, n_dot_h
, spec_coef
);
949 specular
[side
] += spec_coef
* light
->_sli
* attenuation
;
951 } /*loop over lights*/
953 /* Now compute final color index */
954 for (side
= 0 ; side
< NR_SIDES
; side
++) {
955 struct gl_material
*mat
= &ctx
->Light
.Material
[side
];
958 if (specular
[side
] > 1.0F
) {
959 index
= mat
->SpecularIndex
;
962 GLfloat d_a
= mat
->DiffuseIndex
- mat
->AmbientIndex
;
963 GLfloat s_a
= mat
->SpecularIndex
- mat
->AmbientIndex
;
965 index
= mat
->AmbientIndex
966 + diffuse
[side
] * (1.0F
-specular
[side
]) * d_a
967 + specular
[side
] * s_a
;
969 if (index
> mat
->SpecularIndex
) {
970 index
= mat
->SpecularIndex
;
973 indexResult
[side
][j
] = (GLuint
) (GLint
) index
;
980 static void TAG(init_light_tab
)( void )
982 _tnl_light_tab
[IDX
] = TAG(light_rgba
);
983 _tnl_light_fast_tab
[IDX
] = TAG(light_fast_rgba
);
984 _tnl_light_fast_single_tab
[IDX
] = TAG(light_fast_rgba_single
);
985 _tnl_light_spec_tab
[IDX
] = TAG(light_rgba_spec
);
986 _tnl_light_ci_tab
[IDX
] = TAG(light_ci
);
995 #undef CHECK_MATERIAL
997 #undef DO_ANOTHER_NORMAL
998 #undef REUSE_LIGHT_RESULTS
1000 #undef CHECK_COLOR_MATERIAL
1001 #undef CHECK_VALIDATE