2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
26 * Brian Paul Keith Whitwell <keithw@vmware.com>
30 #if IDX & LIGHT_TWOSIDE
37 /* define TRACE to trace lighting code */
41 * ctx is the current context
42 * VB is the vertex buffer
43 * stage is the lighting stage-private data
44 * input is the vector of eye or object-space vertex coordinates
46 static void TAG(light_rgba_spec
)( struct gl_context
*ctx
,
47 struct vertex_buffer
*VB
,
48 struct tnl_pipeline_stage
*stage
,
51 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
52 GLfloat (*base
)[3] = ctx
->Light
._BaseColor
;
56 const GLuint vstride
= input
->stride
;
57 const GLfloat
*vertex
= (GLfloat
*)input
->data
;
58 const GLuint nstride
= VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
]->stride
;
59 const GLfloat
*normal
= (GLfloat
*)VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
]->data
;
61 GLfloat (*Fcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[0].data
;
62 GLfloat (*Fspec
)[4] = (GLfloat (*)[4]) store
->LitSecondary
[0].data
;
63 #if IDX & LIGHT_TWOSIDE
64 GLfloat (*Bcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[1].data
;
65 GLfloat (*Bspec
)[4] = (GLfloat (*)[4]) store
->LitSecondary
[1].data
;
68 const GLuint nr
= VB
->Count
;
71 fprintf(stderr
, "%s\n", __FUNCTION__
);
74 VB
->AttribPtr
[_TNL_ATTRIB_COLOR0
] = &store
->LitColor
[0];
75 VB
->AttribPtr
[_TNL_ATTRIB_COLOR1
] = &store
->LitSecondary
[0];
76 sumA
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
78 #if IDX & LIGHT_TWOSIDE
79 VB
->BackfaceColorPtr
= &store
->LitColor
[1];
80 VB
->BackfaceSecondaryColorPtr
= &store
->LitSecondary
[1];
81 sumA
[1] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
85 store
->LitColor
[0].stride
= 16;
86 store
->LitColor
[1].stride
= 16;
88 for (j
= 0; j
< nr
; j
++,STRIDE_F(vertex
,vstride
),STRIDE_F(normal
,nstride
)) {
89 GLfloat sum
[2][3], spec
[2][3];
90 struct gl_light
*light
;
92 #if IDX & LIGHT_MATERIAL
93 update_materials( ctx
, store
);
94 sumA
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
95 #if IDX & LIGHT_TWOSIDE
96 sumA
[1] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
100 COPY_3V(sum
[0], base
[0]);
103 #if IDX & LIGHT_TWOSIDE
104 COPY_3V(sum
[1], base
[1]);
108 /* Add contribution from each enabled light source */
109 foreach (light
, &ctx
->Light
.EnabledList
) {
115 GLfloat VP
[3]; /* unit vector from vertex to light */
116 GLfloat n_dot_VP
; /* n dot VP */
119 /* compute VP and attenuation */
120 if (!(light
->_Flags
& LIGHT_POSITIONAL
)) {
121 /* directional light */
122 COPY_3V(VP
, light
->_VP_inf_norm
);
123 attenuation
= light
->_VP_inf_spot_attenuation
;
126 GLfloat d
; /* distance from vertex to light */
128 SUB_3V(VP
, light
->_Position
, vertex
);
130 d
= (GLfloat
) LEN_3FV( VP
);
133 GLfloat invd
= 1.0F
/ d
;
134 SELF_SCALE_SCALAR_3V(VP
, invd
);
137 attenuation
= 1.0F
/ (light
->ConstantAttenuation
+ d
*
138 (light
->LinearAttenuation
+ d
*
139 light
->QuadraticAttenuation
));
141 /* spotlight attenuation */
142 if (light
->_Flags
& LIGHT_SPOT
) {
143 GLfloat PV_dot_dir
= - DOT3(VP
, light
->_NormSpotDirection
);
145 if (PV_dot_dir
<light
->_CosCutoff
) {
146 continue; /* this light makes no contribution */
149 GLfloat spot
= powf(PV_dot_dir
, light
->SpotExponent
);
155 if (attenuation
< 1e-3)
156 continue; /* this light makes no contribution */
158 /* Compute dot product or normal and vector from V to light pos */
159 n_dot_VP
= DOT3( normal
, VP
);
161 /* Which side gets the diffuse & specular terms? */
162 if (n_dot_VP
< 0.0F
) {
163 ACC_SCALE_SCALAR_3V(sum
[0], attenuation
, light
->_MatAmbient
[0]);
164 #if IDX & LIGHT_TWOSIDE
167 n_dot_VP
= -n_dot_VP
;
173 #if IDX & LIGHT_TWOSIDE
174 ACC_SCALE_SCALAR_3V( sum
[1], attenuation
, light
->_MatAmbient
[1]);
181 COPY_3V(contrib
, light
->_MatAmbient
[side
]);
182 ACC_SCALE_SCALAR_3V(contrib
, n_dot_VP
, light
->_MatDiffuse
[side
]);
183 ACC_SCALE_SCALAR_3V(sum
[side
], attenuation
, contrib
);
185 /* specular term - cannibalize VP... */
186 if (ctx
->Light
.Model
.LocalViewer
) {
190 SUB_3V(VP
, VP
, v
); /* h = VP + VPe */
194 else if (light
->_Flags
& LIGHT_POSITIONAL
) {
196 ACC_3V(h
, ctx
->_EyeZDir
);
200 h
= light
->_h_inf_norm
;
203 n_dot_h
= correction
* DOT3(normal
, h
);
205 if (n_dot_h
> 0.0F
) {
206 GLfloat spec_coef
= lookup_shininess(ctx
, side
, n_dot_h
);
207 if (spec_coef
> 1.0e-10) {
208 spec_coef
*= attenuation
;
209 ACC_SCALE_SCALAR_3V( spec
[side
], spec_coef
,
210 light
->_MatSpecular
[side
]);
213 } /*loop over lights*/
215 COPY_3V( Fcolor
[j
], sum
[0] );
216 COPY_3V( Fspec
[j
], spec
[0] );
217 Fcolor
[j
][3] = sumA
[0];
219 #if IDX & LIGHT_TWOSIDE
220 COPY_3V( Bcolor
[j
], sum
[1] );
221 COPY_3V( Bspec
[j
], spec
[1] );
222 Bcolor
[j
][3] = sumA
[1];
228 static void TAG(light_rgba
)( struct gl_context
*ctx
,
229 struct vertex_buffer
*VB
,
230 struct tnl_pipeline_stage
*stage
,
233 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
236 GLfloat (*base
)[3] = ctx
->Light
._BaseColor
;
239 const GLuint vstride
= input
->stride
;
240 const GLfloat
*vertex
= (GLfloat
*) input
->data
;
241 const GLuint nstride
= VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
]->stride
;
242 const GLfloat
*normal
= (GLfloat
*)VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
]->data
;
244 GLfloat (*Fcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[0].data
;
245 #if IDX & LIGHT_TWOSIDE
246 GLfloat (*Bcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[1].data
;
249 const GLuint nr
= VB
->Count
;
252 fprintf(stderr
, "%s\n", __FUNCTION__
);
255 VB
->AttribPtr
[_TNL_ATTRIB_COLOR0
] = &store
->LitColor
[0];
256 sumA
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
258 #if IDX & LIGHT_TWOSIDE
259 VB
->BackfaceColorPtr
= &store
->LitColor
[1];
260 sumA
[1] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
263 store
->LitColor
[0].stride
= 16;
264 store
->LitColor
[1].stride
= 16;
266 for (j
= 0; j
< nr
; j
++,STRIDE_F(vertex
,vstride
),STRIDE_F(normal
,nstride
)) {
268 struct gl_light
*light
;
270 #if IDX & LIGHT_MATERIAL
271 update_materials( ctx
, store
);
272 sumA
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
273 #if IDX & LIGHT_TWOSIDE
274 sumA
[1] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
278 COPY_3V(sum
[0], base
[0]);
280 #if IDX & LIGHT_TWOSIDE
281 COPY_3V(sum
[1], base
[1]);
284 /* Add contribution from each enabled light source */
285 foreach (light
, &ctx
->Light
.EnabledList
) {
291 GLfloat attenuation
= 1.0;
292 GLfloat VP
[3]; /* unit vector from vertex to light */
293 GLfloat n_dot_VP
; /* n dot VP */
296 /* compute VP and attenuation */
297 if (!(light
->_Flags
& LIGHT_POSITIONAL
)) {
298 /* directional light */
299 COPY_3V(VP
, light
->_VP_inf_norm
);
300 attenuation
= light
->_VP_inf_spot_attenuation
;
303 GLfloat d
; /* distance from vertex to light */
306 SUB_3V(VP
, light
->_Position
, vertex
);
308 d
= (GLfloat
) LEN_3FV( VP
);
311 GLfloat invd
= 1.0F
/ d
;
312 SELF_SCALE_SCALAR_3V(VP
, invd
);
315 attenuation
= 1.0F
/ (light
->ConstantAttenuation
+ d
*
316 (light
->LinearAttenuation
+ d
*
317 light
->QuadraticAttenuation
));
319 /* spotlight attenuation */
320 if (light
->_Flags
& LIGHT_SPOT
) {
321 GLfloat PV_dot_dir
= - DOT3(VP
, light
->_NormSpotDirection
);
323 if (PV_dot_dir
<light
->_CosCutoff
) {
324 continue; /* this light makes no contribution */
327 GLfloat spot
= powf(PV_dot_dir
, light
->SpotExponent
);
333 if (attenuation
< 1e-3)
334 continue; /* this light makes no contribution */
336 /* Compute dot product or normal and vector from V to light pos */
337 n_dot_VP
= DOT3( normal
, VP
);
339 /* which side are we lighting? */
340 if (n_dot_VP
< 0.0F
) {
341 ACC_SCALE_SCALAR_3V(sum
[0], attenuation
, light
->_MatAmbient
[0]);
342 #if IDX & LIGHT_TWOSIDE
345 n_dot_VP
= -n_dot_VP
;
351 #if IDX & LIGHT_TWOSIDE
352 ACC_SCALE_SCALAR_3V( sum
[1], attenuation
, light
->_MatAmbient
[1]);
358 COPY_3V(contrib
, light
->_MatAmbient
[side
]);
361 ACC_SCALE_SCALAR_3V(contrib
, n_dot_VP
, light
->_MatDiffuse
[side
]);
363 /* specular term - cannibalize VP... */
365 if (ctx
->Light
.Model
.LocalViewer
) {
369 SUB_3V(VP
, VP
, v
); /* h = VP + VPe */
373 else if (light
->_Flags
& LIGHT_POSITIONAL
) {
375 ACC_3V(h
, ctx
->_EyeZDir
);
379 h
= light
->_h_inf_norm
;
382 n_dot_h
= correction
* DOT3(normal
, h
);
384 if (n_dot_h
> 0.0F
) {
385 GLfloat spec_coef
= lookup_shininess(ctx
, side
, n_dot_h
);
386 ACC_SCALE_SCALAR_3V( contrib
, spec_coef
,
387 light
->_MatSpecular
[side
]);
391 ACC_SCALE_SCALAR_3V( sum
[side
], attenuation
, contrib
);
394 COPY_3V( Fcolor
[j
], sum
[0] );
395 Fcolor
[j
][3] = sumA
[0];
397 #if IDX & LIGHT_TWOSIDE
398 COPY_3V( Bcolor
[j
], sum
[1] );
399 Bcolor
[j
][3] = sumA
[1];
407 /* As below, but with just a single light.
409 static void TAG(light_fast_rgba_single
)( struct gl_context
*ctx
,
410 struct vertex_buffer
*VB
,
411 struct tnl_pipeline_stage
*stage
,
415 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
416 const GLuint nstride
= VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
]->stride
;
417 const GLfloat
*normal
= (GLfloat
*)VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
]->data
;
418 GLfloat (*Fcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[0].data
;
419 #if IDX & LIGHT_TWOSIDE
420 GLfloat (*Bcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[1].data
;
422 const struct gl_light
*light
= ctx
->Light
.EnabledList
.next
;
425 #if IDX & LIGHT_MATERIAL
426 const GLuint nr
= VB
->Count
;
428 const GLuint nr
= VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
]->count
;
432 fprintf(stderr
, "%s\n", __FUNCTION__
);
435 (void) input
; /* doesn't refer to Eye or Obj */
437 VB
->AttribPtr
[_TNL_ATTRIB_COLOR0
] = &store
->LitColor
[0];
438 #if IDX & LIGHT_TWOSIDE
439 VB
->BackfaceColorPtr
= &store
->LitColor
[1];
443 store
->LitColor
[0].stride
= 16;
444 store
->LitColor
[1].stride
= 16;
447 store
->LitColor
[0].stride
= 0;
448 store
->LitColor
[1].stride
= 0;
451 for (j
= 0; j
< nr
; j
++, STRIDE_F(normal
,nstride
)) {
455 #if IDX & LIGHT_MATERIAL
456 update_materials( ctx
, store
);
459 /* No attenuation, so incoporate _MatAmbient into base color.
461 #if !(IDX & LIGHT_MATERIAL)
465 COPY_3V(base
[0], light
->_MatAmbient
[0]);
466 ACC_3V(base
[0], ctx
->Light
._BaseColor
[0] );
467 base
[0][3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
469 #if IDX & LIGHT_TWOSIDE
470 COPY_3V(base
[1], light
->_MatAmbient
[1]);
471 ACC_3V(base
[1], ctx
->Light
._BaseColor
[1]);
472 base
[1][3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
476 n_dot_VP
= DOT3(normal
, light
->_VP_inf_norm
);
478 if (n_dot_VP
< 0.0F
) {
479 #if IDX & LIGHT_TWOSIDE
480 GLfloat n_dot_h
= -DOT3(normal
, light
->_h_inf_norm
);
482 COPY_3V(sum
, base
[1]);
483 ACC_SCALE_SCALAR_3V(sum
, -n_dot_VP
, light
->_MatDiffuse
[1]);
484 if (n_dot_h
> 0.0F
) {
485 GLfloat spec
= lookup_shininess(ctx
, 1, n_dot_h
);
486 ACC_SCALE_SCALAR_3V(sum
, spec
, light
->_MatSpecular
[1]);
488 COPY_3V(Bcolor
[j
], sum
);
489 Bcolor
[j
][3] = base
[1][3];
491 COPY_4FV(Fcolor
[j
], base
[0]);
494 GLfloat n_dot_h
= DOT3(normal
, light
->_h_inf_norm
);
496 COPY_3V(sum
, base
[0]);
497 ACC_SCALE_SCALAR_3V(sum
, n_dot_VP
, light
->_MatDiffuse
[0]);
498 if (n_dot_h
> 0.0F
) {
499 GLfloat spec
= lookup_shininess(ctx
, 0, n_dot_h
);
500 ACC_SCALE_SCALAR_3V(sum
, spec
, light
->_MatSpecular
[0]);
502 COPY_3V(Fcolor
[j
], sum
);
503 Fcolor
[j
][3] = base
[0][3];
504 #if IDX & LIGHT_TWOSIDE
505 COPY_4FV(Bcolor
[j
], base
[1]);
512 /* Light infinite lights
514 static void TAG(light_fast_rgba
)( struct gl_context
*ctx
,
515 struct vertex_buffer
*VB
,
516 struct tnl_pipeline_stage
*stage
,
519 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
521 const GLuint nstride
= VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
]->stride
;
522 const GLfloat
*normal
= (GLfloat
*)VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
]->data
;
523 GLfloat (*Fcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[0].data
;
524 #if IDX & LIGHT_TWOSIDE
525 GLfloat (*Bcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[1].data
;
528 #if IDX & LIGHT_MATERIAL
529 const GLuint nr
= VB
->Count
;
531 const GLuint nr
= VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
]->count
;
533 const struct gl_light
*light
;
536 fprintf(stderr
, "%s %d\n", __FUNCTION__
, nr
);
541 sumA
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
542 sumA
[1] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
544 VB
->AttribPtr
[_TNL_ATTRIB_COLOR0
] = &store
->LitColor
[0];
545 #if IDX & LIGHT_TWOSIDE
546 VB
->BackfaceColorPtr
= &store
->LitColor
[1];
550 store
->LitColor
[0].stride
= 16;
551 store
->LitColor
[1].stride
= 16;
554 store
->LitColor
[0].stride
= 0;
555 store
->LitColor
[1].stride
= 0;
558 for (j
= 0; j
< nr
; j
++, STRIDE_F(normal
,nstride
)) {
562 #if IDX & LIGHT_MATERIAL
563 update_materials( ctx
, store
);
565 sumA
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
566 #if IDX & LIGHT_TWOSIDE
567 sumA
[1] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
572 COPY_3V(sum
[0], ctx
->Light
._BaseColor
[0]);
573 #if IDX & LIGHT_TWOSIDE
574 COPY_3V(sum
[1], ctx
->Light
._BaseColor
[1]);
577 foreach (light
, &ctx
->Light
.EnabledList
) {
578 GLfloat n_dot_h
, n_dot_VP
, spec
;
580 ACC_3V(sum
[0], light
->_MatAmbient
[0]);
581 #if IDX & LIGHT_TWOSIDE
582 ACC_3V(sum
[1], light
->_MatAmbient
[1]);
585 n_dot_VP
= DOT3(normal
, light
->_VP_inf_norm
);
587 if (n_dot_VP
> 0.0F
) {
588 ACC_SCALE_SCALAR_3V(sum
[0], n_dot_VP
, light
->_MatDiffuse
[0]);
589 n_dot_h
= DOT3(normal
, light
->_h_inf_norm
);
590 if (n_dot_h
> 0.0F
) {
591 spec
= lookup_shininess(ctx
, 0, n_dot_h
);
592 ACC_SCALE_SCALAR_3V( sum
[0], spec
, light
->_MatSpecular
[0]);
595 #if IDX & LIGHT_TWOSIDE
597 ACC_SCALE_SCALAR_3V(sum
[1], -n_dot_VP
, light
->_MatDiffuse
[1]);
598 n_dot_h
= -DOT3(normal
, light
->_h_inf_norm
);
599 if (n_dot_h
> 0.0F
) {
600 spec
= lookup_shininess(ctx
, 1, n_dot_h
);
601 ACC_SCALE_SCALAR_3V( sum
[1], spec
, light
->_MatSpecular
[1]);
607 COPY_3V( Fcolor
[j
], sum
[0] );
608 Fcolor
[j
][3] = sumA
[0];
610 #if IDX & LIGHT_TWOSIDE
611 COPY_3V( Bcolor
[j
], sum
[1] );
612 Bcolor
[j
][3] = sumA
[1];
620 static void TAG(init_light_tab
)( void )
622 _tnl_light_tab
[IDX
] = TAG(light_rgba
);
623 _tnl_light_fast_tab
[IDX
] = TAG(light_fast_rgba
);
624 _tnl_light_fast_single_tab
[IDX
] = TAG(light_fast_rgba_single
);
625 _tnl_light_spec_tab
[IDX
] = TAG(light_rgba_spec
);