1 /* $Id: t_vb_lighttmp.h,v 1.14 2001/07/28 19:28:49 keithw 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, (4 * sizeof(GLfloat)))
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 import_color_material( ctx
, stage
);
129 CMcolor
= (GLfloat
*) VB
->ColorPtr
[0]->Ptr
;
130 CMstride
= VB
->ColorPtr
[0]->StrideB
;
133 VB
->ColorPtr
[0] = &store
->LitColor
[0];
134 VB
->SecondaryColorPtr
[0] = &store
->LitSecondary
[0];
135 UNCLAMPED_FLOAT_TO_CHAN(sumA
[0], ctx
->Light
.Material
[0].Diffuse
[3]);
137 if (IDX
& LIGHT_TWOSIDE
) {
138 VB
->ColorPtr
[1] = &store
->LitColor
[1];
139 VB
->SecondaryColorPtr
[1] = &store
->LitSecondary
[1];
140 UNCLAMPED_FLOAT_TO_CHAN(sumA
[1], ctx
->Light
.Material
[1].Diffuse
[3]);
143 /* Side-effects done, can we finish now?
145 if (stage
->changed_inputs
== 0)
150 j
++,STRIDE_F(vertex
,VSTRIDE
),STRIDE_F(normal
,NSTRIDE
),CMSTRIDE
)
152 GLfloat sum
[2][3], spec
[2][3];
153 struct gl_light
*light
;
155 if ( CHECK_COLOR_MATERIAL(j
) )
156 _mesa_update_color_material( ctx
, CMcolor
);
158 if ( CHECK_MATERIAL(j
) )
159 _mesa_update_material( ctx
, new_material
[j
], new_material_mask
[j
] );
161 if ( CHECK_VALIDATE(j
) ) {
162 _mesa_validate_all_lighting_tables( ctx
);
163 UNCLAMPED_FLOAT_TO_CHAN(sumA
[0], ctx
->Light
.Material
[0].Diffuse
[3]);
164 if (IDX
& LIGHT_TWOSIDE
)
165 UNCLAMPED_FLOAT_TO_CHAN(sumA
[1], ctx
->Light
.Material
[1].Diffuse
[3]);
168 COPY_3V(sum
[0], base
[0]);
171 if (IDX
& LIGHT_TWOSIDE
) {
172 COPY_3V(sum
[1], base
[1]);
176 /* Add contribution from each enabled light source */
177 foreach (light
, &ctx
->Light
.EnabledList
) {
183 GLfloat VP
[3]; /* unit vector from vertex to light */
184 GLfloat n_dot_VP
; /* n dot VP */
187 /* compute VP and attenuation */
188 if (!(light
->_Flags
& LIGHT_POSITIONAL
)) {
189 /* directional light */
190 COPY_3V(VP
, light
->_VP_inf_norm
);
191 attenuation
= light
->_VP_inf_spot_attenuation
;
194 GLfloat d
; /* distance from vertex to light */
196 SUB_3V(VP
, light
->_Position
, vertex
);
198 d
= (GLfloat
) LEN_3FV( VP
);
201 GLfloat invd
= 1.0F
/ d
;
202 SELF_SCALE_SCALAR_3V(VP
, invd
);
205 attenuation
= 1.0F
/ (light
->ConstantAttenuation
+ d
*
206 (light
->LinearAttenuation
+ d
*
207 light
->QuadraticAttenuation
));
209 /* spotlight attenuation */
210 if (light
->_Flags
& LIGHT_SPOT
) {
211 GLfloat PV_dot_dir
= - DOT3(VP
, light
->_NormDirection
);
213 if (PV_dot_dir
<light
->_CosCutoff
) {
214 continue; /* this light makes no contribution */
217 double x
= PV_dot_dir
* (EXP_TABLE_SIZE
-1);
219 GLfloat spot
= (GLfloat
) (light
->_SpotExpTable
[k
][0]
220 + (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 import_color_material( ctx
, stage
);
342 CMcolor
= (GLfloat
*)VB
->ColorPtr
[0]->Ptr
;
343 CMstride
= VB
->ColorPtr
[0]->StrideB
;
346 VB
->ColorPtr
[0] = &store
->LitColor
[0];
347 UNCLAMPED_FLOAT_TO_CHAN(sumA
[0], ctx
->Light
.Material
[0].Diffuse
[3]);
349 if (IDX
& LIGHT_TWOSIDE
) {
350 VB
->ColorPtr
[1] = &store
->LitColor
[1];
351 UNCLAMPED_FLOAT_TO_CHAN(sumA
[1], ctx
->Light
.Material
[1].Diffuse
[3]);
354 if (stage
->changed_inputs
== 0)
359 j
++,STRIDE_F(vertex
,VSTRIDE
), STRIDE_F(normal
,NSTRIDE
),CMSTRIDE
)
362 struct gl_light
*light
;
364 if ( CHECK_COLOR_MATERIAL(j
) )
365 _mesa_update_color_material( ctx
, CMcolor
);
367 if ( CHECK_MATERIAL(j
) )
368 _mesa_update_material( ctx
, new_material
[j
], new_material_mask
[j
] );
370 if ( CHECK_VALIDATE(j
) ) {
371 _mesa_validate_all_lighting_tables( ctx
);
372 UNCLAMPED_FLOAT_TO_CHAN(sumA
[0], ctx
->Light
.Material
[0].Diffuse
[3]);
373 if (IDX
& LIGHT_TWOSIDE
)
374 UNCLAMPED_FLOAT_TO_CHAN(sumA
[1], ctx
->Light
.Material
[1].Diffuse
[3]);
377 COPY_3V(sum
[0], base
[0]);
379 if ( IDX
& LIGHT_TWOSIDE
)
380 COPY_3V(sum
[1], base
[1]);
382 /* Add contribution from each enabled light source */
383 foreach (light
, &ctx
->Light
.EnabledList
) {
389 GLfloat attenuation
= 1.0;
390 GLfloat VP
[3]; /* unit vector from vertex to light */
391 GLfloat n_dot_VP
; /* n dot VP */
394 /* compute VP and attenuation */
395 if (!(light
->_Flags
& LIGHT_POSITIONAL
)) {
396 /* directional light */
397 COPY_3V(VP
, light
->_VP_inf_norm
);
398 attenuation
= light
->_VP_inf_spot_attenuation
;
401 GLfloat d
; /* distance from vertex to light */
404 SUB_3V(VP
, light
->_Position
, vertex
);
409 GLfloat invd
= 1.0F
/ d
;
410 SELF_SCALE_SCALAR_3V(VP
, invd
);
413 attenuation
= 1.0F
/ (light
->ConstantAttenuation
+ d
*
414 (light
->LinearAttenuation
+ d
*
415 light
->QuadraticAttenuation
));
417 /* spotlight attenuation */
418 if (light
->_Flags
& LIGHT_SPOT
) {
419 GLfloat PV_dot_dir
= - DOT3(VP
, light
->_NormDirection
);
421 if (PV_dot_dir
<light
->_CosCutoff
) {
422 continue; /* this light makes no contribution */
425 double x
= PV_dot_dir
* (EXP_TABLE_SIZE
-1);
427 GLfloat spot
= (light
->_SpotExpTable
[k
][0]
428 + (x
-k
)*light
->_SpotExpTable
[k
][1]);
435 if (attenuation
< 1e-3)
436 continue; /* this light makes no contribution */
439 /* Compute dot product or normal and vector from V to light pos */
440 n_dot_VP
= DOT3( normal
, VP
);
442 /* which side are we lighting? */
443 if (n_dot_VP
< 0.0F
) {
444 ACC_SCALE_SCALAR_3V(sum
[0], attenuation
, light
->_MatAmbient
[0]);
446 if (!(IDX
& LIGHT_TWOSIDE
))
451 n_dot_VP
= -n_dot_VP
;
454 if (IDX
& LIGHT_TWOSIDE
) {
455 ACC_SCALE_SCALAR_3V( sum
[1], attenuation
, light
->_MatAmbient
[1]);
461 COPY_3V(contrib
, light
->_MatAmbient
[side
]);
464 ACC_SCALE_SCALAR_3V(contrib
, n_dot_VP
, light
->_MatDiffuse
[side
]);
466 /* specular term - cannibalize VP... */
468 if (ctx
->Light
.Model
.LocalViewer
) {
472 SUB_3V(VP
, VP
, v
); /* h = VP + VPe */
476 else if (light
->_Flags
& LIGHT_POSITIONAL
) {
478 ACC_3V(h
, ctx
->_EyeZDir
);
482 h
= light
->_h_inf_norm
;
485 n_dot_h
= correction
* DOT3(normal
, h
);
490 struct gl_shine_tab
*tab
= ctx
->_ShineTable
[side
];
492 GET_SHINE_TAB_ENTRY( tab
, n_dot_h
, spec_coef
);
494 ACC_SCALE_SCALAR_3V( contrib
, spec_coef
,
495 light
->_MatSpecular
[side
]);
499 ACC_SCALE_SCALAR_3V( sum
[side
], attenuation
, contrib
);
502 UNCLAMPED_FLOAT_TO_RGB_CHAN( Fcolor
[j
], sum
[0] );
503 Fcolor
[j
][3] = sumA
[0];
505 if (IDX
& LIGHT_TWOSIDE
) {
506 UNCLAMPED_FLOAT_TO_RGB_CHAN( Bcolor
[j
], sum
[1] );
507 Bcolor
[j
][3] = sumA
[1];
515 /* As below, but with just a single light.
517 static void TAG(light_fast_rgba_single
)( GLcontext
*ctx
,
518 struct vertex_buffer
*VB
,
519 struct gl_pipeline_stage
*stage
,
523 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
524 GLuint nstride
= VB
->NormalPtr
->stride
;
525 const GLfloat
*normal
= (GLfloat
*)VB
->NormalPtr
->data
;
528 GLchan (*Fcolor
)[4] = (GLchan (*)[4]) store
->LitColor
[0].Ptr
;
529 GLchan (*Bcolor
)[4] = (GLchan (*)[4]) store
->LitColor
[1].Ptr
;
530 struct gl_light
*light
= ctx
->Light
.EnabledList
.next
;
531 GLuint
*flags
= VB
->Flag
;
532 GLchan basechan
[2][4];
534 struct gl_material (*new_material
)[2] = VB
->Material
;
535 GLuint
*new_material_mask
= VB
->MaterialMask
;
537 GLuint nr
= VB
->Count
;
539 /* fprintf(stderr, "%s\n", __FUNCTION__ ); */
540 (void) input
; /* doesn't refer to Eye or Obj */
545 if (IDX
& LIGHT_COLORMATERIAL
) {
546 if (VB
->ColorPtr
[0]->Type
!= GL_FLOAT
)
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 _mesa_update_color_material( ctx
, CMcolor
);
565 if ( CHECK_MATERIAL(j
) )
566 _mesa_update_material( ctx
, new_material
[j
], new_material_mask
[j
] );
568 if ( CHECK_VALIDATE(j
) )
569 _mesa_validate_all_lighting_tables( ctx
);
572 /* No attenuation, so incoporate _MatAmbient into base color.
574 COPY_3V(base
[0], light
->_MatAmbient
[0]);
575 ACC_3V(base
[0], ctx
->Light
._BaseColor
[0] );
576 UNCLAMPED_FLOAT_TO_RGB_CHAN( basechan
[0], base
[0] );
577 UNCLAMPED_FLOAT_TO_CHAN(basechan
[0][3],
578 ctx
->Light
.Material
[0].Diffuse
[3]);
580 if (IDX
& LIGHT_TWOSIDE
) {
581 COPY_3V(base
[1], light
->_MatAmbient
[1]);
582 ACC_3V(base
[1], ctx
->Light
._BaseColor
[1]);
583 UNCLAMPED_FLOAT_TO_RGB_CHAN( basechan
[1], base
[1]);
584 UNCLAMPED_FLOAT_TO_CHAN(basechan
[1][3],
585 ctx
->Light
.Material
[1].Diffuse
[3]);
589 GLfloat n_dot_VP
= DOT3(normal
, light
->_VP_inf_norm
);
592 if (n_dot_VP
< 0.0F
) {
593 if (IDX
& LIGHT_TWOSIDE
) {
594 GLfloat n_dot_h
= -DOT3(normal
, light
->_h_inf_norm
);
596 COPY_3V(sum
, base
[1]);
597 ACC_SCALE_SCALAR_3V(sum
, -n_dot_VP
, light
->_MatDiffuse
[1]);
598 if (n_dot_h
> 0.0F
) {
600 GET_SHINE_TAB_ENTRY( ctx
->_ShineTable
[1], n_dot_h
, spec
);
601 ACC_SCALE_SCALAR_3V(sum
, spec
, light
->_MatSpecular
[1]);
603 UNCLAMPED_FLOAT_TO_RGB_CHAN(Bcolor
[j
], sum
);
605 COPY_CHAN4(Fcolor
[j
], basechan
[0]);
607 GLfloat n_dot_h
= DOT3(normal
, light
->_h_inf_norm
);
609 COPY_3V(sum
, base
[0]);
610 ACC_SCALE_SCALAR_3V(sum
, n_dot_VP
, light
->_MatDiffuse
[0]);
611 if (n_dot_h
> 0.0F
) {
613 GET_SHINE_TAB_ENTRY( ctx
->_ShineTable
[0], n_dot_h
, spec
);
614 ACC_SCALE_SCALAR_3V(sum
, spec
, light
->_MatSpecular
[0]);
617 UNCLAMPED_FLOAT_TO_RGB_CHAN(Fcolor
[j
], sum
);
618 if (IDX
& LIGHT_TWOSIDE
) COPY_CHAN4(Bcolor
[j
], basechan
[1]);
623 STRIDE_F(normal
, NSTRIDE
);
624 } while (DO_ANOTHER_NORMAL(j
));
627 for ( ; REUSE_LIGHT_RESULTS(j
) ; j
++, CMSTRIDE
, STRIDE_F(normal
,NSTRIDE
))
629 COPY_CHAN4(Fcolor
[j
], Fcolor
[j
-1]);
630 if (IDX
& LIGHT_TWOSIDE
)
631 COPY_CHAN4(Bcolor
[j
], Bcolor
[j
-1]);
634 } while (!CHECK_END_VB(j
));
638 /* Light infinite lights
640 static void TAG(light_fast_rgba
)( GLcontext
*ctx
,
641 struct vertex_buffer
*VB
,
642 struct gl_pipeline_stage
*stage
,
645 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
647 GLuint nstride
= VB
->NormalPtr
->stride
;
648 const GLfloat
*normal
= (GLfloat
*)VB
->NormalPtr
->data
;
651 GLchan (*Fcolor
)[4] = (GLchan (*)[4]) store
->LitColor
[0].Ptr
;
652 GLchan (*Bcolor
)[4] = (GLchan (*)[4]) store
->LitColor
[1].Ptr
;
653 GLuint
*flags
= VB
->Flag
;
655 struct gl_material (*new_material
)[2] = VB
->Material
;
656 GLuint
*new_material_mask
= VB
->MaterialMask
;
657 GLuint nr
= VB
->Count
;
658 struct gl_light
*light
;
660 /* fprintf(stderr, "%s\n", __FUNCTION__ ); */
666 UNCLAMPED_FLOAT_TO_CHAN(sumA
[0], ctx
->Light
.Material
[0].Diffuse
[3]);
667 UNCLAMPED_FLOAT_TO_CHAN(sumA
[1], ctx
->Light
.Material
[1].Diffuse
[3]);
669 if (IDX
& LIGHT_COLORMATERIAL
) {
670 if (VB
->ColorPtr
[0]->Type
!= GL_FLOAT
)
671 import_color_material( ctx
, stage
);
673 CMcolor
= (GLfloat
*)VB
->ColorPtr
[0]->Ptr
;
674 CMstride
= VB
->ColorPtr
[0]->StrideB
;
677 VB
->ColorPtr
[0] = &store
->LitColor
[0];
678 if (IDX
& LIGHT_TWOSIDE
)
679 VB
->ColorPtr
[1] = &store
->LitColor
[1];
681 if (stage
->changed_inputs
== 0)
688 if ( CHECK_COLOR_MATERIAL(j
) )
689 _mesa_update_color_material( ctx
, CMcolor
);
691 if ( CHECK_MATERIAL(j
) )
692 _mesa_update_material( ctx
, new_material
[j
], new_material_mask
[j
] );
694 if ( CHECK_VALIDATE(j
) ) {
695 _mesa_validate_all_lighting_tables( ctx
);
696 UNCLAMPED_FLOAT_TO_CHAN(sumA
[0], ctx
->Light
.Material
[0].Diffuse
[3]);
697 if (IDX
& LIGHT_TWOSIDE
)
698 UNCLAMPED_FLOAT_TO_CHAN(sumA
[1],
699 ctx
->Light
.Material
[1].Diffuse
[3]);
702 COPY_3V(sum
[0], ctx
->Light
._BaseColor
[0]);
703 if (IDX
& LIGHT_TWOSIDE
)
704 COPY_3V(sum
[1], ctx
->Light
._BaseColor
[1]);
706 foreach (light
, &ctx
->Light
.EnabledList
) {
707 GLfloat n_dot_h
, n_dot_VP
, spec
;
709 ACC_3V(sum
[0], light
->_MatAmbient
[0]);
710 if (IDX
& LIGHT_TWOSIDE
)
711 ACC_3V(sum
[1], light
->_MatAmbient
[1]);
713 n_dot_VP
= DOT3(normal
, light
->_VP_inf_norm
);
715 if (n_dot_VP
> 0.0F
) {
716 ACC_SCALE_SCALAR_3V(sum
[0], n_dot_VP
, light
->_MatDiffuse
[0]);
717 n_dot_h
= DOT3(normal
, light
->_h_inf_norm
);
718 if (n_dot_h
> 0.0F
) {
719 struct gl_shine_tab
*tab
= ctx
->_ShineTable
[0];
720 GET_SHINE_TAB_ENTRY( tab
, n_dot_h
, spec
);
721 ACC_SCALE_SCALAR_3V( sum
[0], spec
,
722 light
->_MatSpecular
[0]);
725 else if (IDX
& LIGHT_TWOSIDE
) {
726 ACC_SCALE_SCALAR_3V(sum
[1], -n_dot_VP
, light
->_MatDiffuse
[1]);
727 n_dot_h
= -DOT3(normal
, light
->_h_inf_norm
);
728 if (n_dot_h
> 0.0F
) {
729 struct gl_shine_tab
*tab
= ctx
->_ShineTable
[1];
730 GET_SHINE_TAB_ENTRY( tab
, n_dot_h
, spec
);
731 ACC_SCALE_SCALAR_3V( sum
[1], spec
,
732 light
->_MatSpecular
[1]);
737 UNCLAMPED_FLOAT_TO_RGB_CHAN( Fcolor
[j
], sum
[0] );
738 Fcolor
[j
][3] = sumA
[0];
740 if (IDX
& LIGHT_TWOSIDE
) {
741 UNCLAMPED_FLOAT_TO_RGB_CHAN( Bcolor
[j
], sum
[1] );
742 Bcolor
[j
][3] = sumA
[1];
747 STRIDE_F(normal
, NSTRIDE
);
748 } while (DO_ANOTHER_NORMAL(j
));
750 /* Reuse the shading results while there is no change to
751 * normal or material values.
753 for ( ; REUSE_LIGHT_RESULTS(j
) ; j
++, CMSTRIDE
, STRIDE_F(normal
, NSTRIDE
))
755 COPY_CHAN4(Fcolor
[j
], Fcolor
[j
-1]);
756 if (IDX
& LIGHT_TWOSIDE
)
757 COPY_CHAN4(Bcolor
[j
], Bcolor
[j
-1]);
760 } while (!CHECK_END_VB(j
));
768 * Use current lighting/material settings to compute the color indexes
769 * for an array of vertices.
770 * Input: n - number of vertices to light
771 * side - 0=use front material, 1=use back material
772 * vertex - array of [n] vertex position in eye coordinates
773 * normal - array of [n] surface normal vector
774 * Output: indexResult - resulting array of [n] color indexes
776 static void TAG(light_ci
)( GLcontext
*ctx
,
777 struct vertex_buffer
*VB
,
778 struct gl_pipeline_stage
*stage
,
781 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
783 GLuint vstride
= input
->stride
;
784 const GLfloat
*vertex
= (GLfloat
*) input
->data
;
785 GLuint nstride
= VB
->NormalPtr
->stride
;
786 const GLfloat
*normal
= (GLfloat
*)VB
->NormalPtr
->data
;
789 GLuint
*flags
= VB
->Flag
;
790 GLuint
*indexResult
[2];
791 struct gl_material (*new_material
)[2] = VB
->Material
;
792 GLuint
*new_material_mask
= VB
->MaterialMask
;
793 GLuint nr
= VB
->Count
;
795 /* fprintf(stderr, "%s\n", __FUNCTION__ ); */
800 VB
->IndexPtr
[0] = &store
->LitIndex
[0];
801 if (IDX
& LIGHT_TWOSIDE
)
802 VB
->IndexPtr
[1] = &store
->LitIndex
[1];
804 if (stage
->changed_inputs
== 0)
807 indexResult
[0] = VB
->IndexPtr
[0]->data
;
808 if (IDX
& LIGHT_TWOSIDE
)
809 indexResult
[1] = VB
->IndexPtr
[1]->data
;
811 if (IDX
& LIGHT_COLORMATERIAL
) {
812 if (VB
->ColorPtr
[0]->Type
!= GL_FLOAT
)
813 import_color_material( ctx
, stage
);
815 CMcolor
= (GLfloat
*)VB
->ColorPtr
[0]->Ptr
;
816 CMstride
= VB
->ColorPtr
[0]->StrideB
;
819 /* loop over vertices */
822 j
++,STRIDE_F(vertex
,VSTRIDE
),STRIDE_F(normal
, NSTRIDE
), CMSTRIDE
)
824 GLfloat diffuse
[2], specular
[2];
826 struct gl_light
*light
;
828 if ( CHECK_COLOR_MATERIAL(j
) )
829 _mesa_update_color_material( ctx
, CMcolor
);
831 if ( CHECK_MATERIAL(j
) )
832 _mesa_update_material( ctx
, new_material
[j
], new_material_mask
[j
] );
834 if ( CHECK_VALIDATE(j
) )
835 _mesa_validate_all_lighting_tables( ctx
);
837 diffuse
[0] = specular
[0] = 0.0F
;
839 if ( IDX
& LIGHT_TWOSIDE
) {
840 diffuse
[1] = specular
[1] = 0.0F
;
843 /* Accumulate diffuse and specular from each light source */
844 foreach (light
, &ctx
->Light
.EnabledList
) {
846 GLfloat attenuation
= 1.0F
;
847 GLfloat VP
[3]; /* unit vector from vertex to light */
848 GLfloat n_dot_VP
; /* dot product of l and n */
849 GLfloat
*h
, n_dot_h
, correction
= 1.0;
851 /* compute l and attenuation */
852 if (!(light
->_Flags
& LIGHT_POSITIONAL
)) {
853 /* directional light */
854 COPY_3V(VP
, light
->_VP_inf_norm
);
857 GLfloat d
; /* distance from vertex to light */
859 SUB_3V(VP
, light
->_Position
, vertex
);
863 GLfloat invd
= 1.0F
/ d
;
864 SELF_SCALE_SCALAR_3V(VP
, invd
);
867 attenuation
= 1.0F
/ (light
->ConstantAttenuation
+ d
*
868 (light
->LinearAttenuation
+ d
*
869 light
->QuadraticAttenuation
));
871 /* spotlight attenuation */
872 if (light
->_Flags
& LIGHT_SPOT
) {
873 GLfloat PV_dot_dir
= - DOT3(VP
, light
->_NormDirection
);
874 if (PV_dot_dir
< light
->_CosCutoff
) {
875 continue; /* this light makes no contribution */
878 double x
= PV_dot_dir
* (EXP_TABLE_SIZE
-1);
880 GLfloat spot
= (light
->_SpotExpTable
[k
][0]
881 + (x
-k
)*light
->_SpotExpTable
[k
][1]);
887 if (attenuation
< 1e-3)
888 continue; /* this light makes no contribution */
890 n_dot_VP
= DOT3( normal
, VP
);
892 /* which side are we lighting? */
893 if (n_dot_VP
< 0.0F
) {
894 if (!(IDX
& LIGHT_TWOSIDE
))
898 n_dot_VP
= -n_dot_VP
;
901 /* accumulate diffuse term */
902 diffuse
[side
] += n_dot_VP
* light
->_dli
* attenuation
;
905 if (ctx
->Light
.Model
.LocalViewer
) {
909 SUB_3V(VP
, VP
, v
); /* h = VP + VPe */
913 else if (light
->_Flags
& LIGHT_POSITIONAL
) {
915 /* Strangely, disabling this addition fixes a conformance
916 * problem. If this code is enabled, l_sed.c fails.
918 /*ACC_3V(h, ctx->_EyeZDir);*/
922 h
= light
->_h_inf_norm
;
925 n_dot_h
= correction
* DOT3(normal
, h
);
926 if (n_dot_h
> 0.0F
) {
928 struct gl_shine_tab
*tab
= ctx
->_ShineTable
[side
];
929 GET_SHINE_TAB_ENTRY( tab
, n_dot_h
, spec_coef
);
930 specular
[side
] += spec_coef
* light
->_sli
* attenuation
;
932 } /*loop over lights*/
934 /* Now compute final color index */
935 for (side
= 0 ; side
< NR_SIDES
; side
++) {
936 struct gl_material
*mat
= &ctx
->Light
.Material
[side
];
939 if (specular
[side
] > 1.0F
) {
940 index
= mat
->SpecularIndex
;
943 GLfloat d_a
= mat
->DiffuseIndex
- mat
->AmbientIndex
;
944 GLfloat s_a
= mat
->SpecularIndex
- mat
->AmbientIndex
;
946 index
= mat
->AmbientIndex
947 + diffuse
[side
] * (1.0F
-specular
[side
]) * d_a
948 + specular
[side
] * s_a
;
950 if (index
> mat
->SpecularIndex
) {
951 index
= mat
->SpecularIndex
;
954 indexResult
[side
][j
] = (GLuint
) (GLint
) index
;
961 static void TAG(init_light_tab
)( void )
963 _tnl_light_tab
[IDX
] = TAG(light_rgba
);
964 _tnl_light_fast_tab
[IDX
] = TAG(light_fast_rgba
);
965 _tnl_light_fast_single_tab
[IDX
] = TAG(light_fast_rgba_single
);
966 _tnl_light_spec_tab
[IDX
] = TAG(light_rgba_spec
);
967 _tnl_light_ci_tab
[IDX
] = TAG(light_ci
);
976 #undef CHECK_MATERIAL
978 #undef DO_ANOTHER_NORMAL
979 #undef REUSE_LIGHT_RESULTS
981 #undef CHECK_COLOR_MATERIAL
982 #undef CHECK_VALIDATE