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", __func__
);
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-3F
)
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-10F
) {
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", __func__
);
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 VP
[3]; /* unit vector from vertex to light */
292 GLfloat n_dot_VP
; /* n dot VP */
295 /* compute VP and attenuation */
296 if (!(light
->_Flags
& LIGHT_POSITIONAL
)) {
297 /* directional light */
298 COPY_3V(VP
, light
->_VP_inf_norm
);
299 attenuation
= light
->_VP_inf_spot_attenuation
;
302 GLfloat d
; /* distance from vertex to light */
304 SUB_3V(VP
, light
->_Position
, vertex
);
306 d
= (GLfloat
) LEN_3FV( VP
);
309 GLfloat invd
= 1.0F
/ d
;
310 SELF_SCALE_SCALAR_3V(VP
, invd
);
313 attenuation
= 1.0F
/ (light
->ConstantAttenuation
+ d
*
314 (light
->LinearAttenuation
+ d
*
315 light
->QuadraticAttenuation
));
317 /* spotlight attenuation */
318 if (light
->_Flags
& LIGHT_SPOT
) {
319 GLfloat PV_dot_dir
= - DOT3(VP
, light
->_NormSpotDirection
);
321 if (PV_dot_dir
<light
->_CosCutoff
) {
322 continue; /* this light makes no contribution */
325 GLfloat spot
= powf(PV_dot_dir
, light
->SpotExponent
);
331 if (attenuation
< 1e-3F
)
332 continue; /* this light makes no contribution */
334 /* Compute dot product or normal and vector from V to light pos */
335 n_dot_VP
= DOT3( normal
, VP
);
337 /* which side are we lighting? */
338 if (n_dot_VP
< 0.0F
) {
339 ACC_SCALE_SCALAR_3V(sum
[0], attenuation
, light
->_MatAmbient
[0]);
340 #if IDX & LIGHT_TWOSIDE
343 n_dot_VP
= -n_dot_VP
;
349 #if IDX & LIGHT_TWOSIDE
350 ACC_SCALE_SCALAR_3V( sum
[1], attenuation
, light
->_MatAmbient
[1]);
356 COPY_3V(contrib
, light
->_MatAmbient
[side
]);
359 ACC_SCALE_SCALAR_3V(contrib
, n_dot_VP
, light
->_MatDiffuse
[side
]);
361 /* specular term - cannibalize VP... */
363 if (ctx
->Light
.Model
.LocalViewer
) {
367 SUB_3V(VP
, VP
, v
); /* h = VP + VPe */
371 else if (light
->_Flags
& LIGHT_POSITIONAL
) {
373 ACC_3V(h
, ctx
->_EyeZDir
);
377 h
= light
->_h_inf_norm
;
380 n_dot_h
= correction
* DOT3(normal
, h
);
382 if (n_dot_h
> 0.0F
) {
383 GLfloat spec_coef
= lookup_shininess(ctx
, side
, n_dot_h
);
384 ACC_SCALE_SCALAR_3V( contrib
, spec_coef
,
385 light
->_MatSpecular
[side
]);
389 ACC_SCALE_SCALAR_3V( sum
[side
], attenuation
, contrib
);
392 COPY_3V( Fcolor
[j
], sum
[0] );
393 Fcolor
[j
][3] = sumA
[0];
395 #if IDX & LIGHT_TWOSIDE
396 COPY_3V( Bcolor
[j
], sum
[1] );
397 Bcolor
[j
][3] = sumA
[1];
405 /* As below, but with just a single light.
407 static void TAG(light_fast_rgba_single
)( struct gl_context
*ctx
,
408 struct vertex_buffer
*VB
,
409 struct tnl_pipeline_stage
*stage
,
413 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
414 const GLuint nstride
= VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
]->stride
;
415 const GLfloat
*normal
= (GLfloat
*)VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
]->data
;
416 GLfloat (*Fcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[0].data
;
417 #if IDX & LIGHT_TWOSIDE
418 GLfloat (*Bcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[1].data
;
420 const struct gl_light
*light
= ctx
->Light
.EnabledList
.next
;
423 #if IDX & LIGHT_MATERIAL
424 const GLuint nr
= VB
->Count
;
426 const GLuint nr
= VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
]->count
;
430 fprintf(stderr
, "%s\n", __func__
);
433 (void) input
; /* doesn't refer to Eye or Obj */
435 VB
->AttribPtr
[_TNL_ATTRIB_COLOR0
] = &store
->LitColor
[0];
436 #if IDX & LIGHT_TWOSIDE
437 VB
->BackfaceColorPtr
= &store
->LitColor
[1];
441 store
->LitColor
[0].stride
= 16;
442 store
->LitColor
[1].stride
= 16;
445 store
->LitColor
[0].stride
= 0;
446 store
->LitColor
[1].stride
= 0;
449 for (j
= 0; j
< nr
; j
++, STRIDE_F(normal
,nstride
)) {
453 #if IDX & LIGHT_MATERIAL
454 update_materials( ctx
, store
);
457 /* No attenuation, so incoporate _MatAmbient into base color.
459 #if !(IDX & LIGHT_MATERIAL)
463 COPY_3V(base
[0], light
->_MatAmbient
[0]);
464 ACC_3V(base
[0], ctx
->Light
._BaseColor
[0] );
465 base
[0][3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
467 #if IDX & LIGHT_TWOSIDE
468 COPY_3V(base
[1], light
->_MatAmbient
[1]);
469 ACC_3V(base
[1], ctx
->Light
._BaseColor
[1]);
470 base
[1][3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
474 n_dot_VP
= DOT3(normal
, light
->_VP_inf_norm
);
476 if (n_dot_VP
< 0.0F
) {
477 #if IDX & LIGHT_TWOSIDE
478 GLfloat n_dot_h
= -DOT3(normal
, light
->_h_inf_norm
);
480 COPY_3V(sum
, base
[1]);
481 ACC_SCALE_SCALAR_3V(sum
, -n_dot_VP
, light
->_MatDiffuse
[1]);
482 if (n_dot_h
> 0.0F
) {
483 GLfloat spec
= lookup_shininess(ctx
, 1, n_dot_h
);
484 ACC_SCALE_SCALAR_3V(sum
, spec
, light
->_MatSpecular
[1]);
486 COPY_3V(Bcolor
[j
], sum
);
487 Bcolor
[j
][3] = base
[1][3];
489 COPY_4FV(Fcolor
[j
], base
[0]);
492 GLfloat n_dot_h
= DOT3(normal
, light
->_h_inf_norm
);
494 COPY_3V(sum
, base
[0]);
495 ACC_SCALE_SCALAR_3V(sum
, n_dot_VP
, light
->_MatDiffuse
[0]);
496 if (n_dot_h
> 0.0F
) {
497 GLfloat spec
= lookup_shininess(ctx
, 0, n_dot_h
);
498 ACC_SCALE_SCALAR_3V(sum
, spec
, light
->_MatSpecular
[0]);
500 COPY_3V(Fcolor
[j
], sum
);
501 Fcolor
[j
][3] = base
[0][3];
502 #if IDX & LIGHT_TWOSIDE
503 COPY_4FV(Bcolor
[j
], base
[1]);
510 /* Light infinite lights
512 static void TAG(light_fast_rgba
)( struct gl_context
*ctx
,
513 struct vertex_buffer
*VB
,
514 struct tnl_pipeline_stage
*stage
,
517 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
519 const GLuint nstride
= VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
]->stride
;
520 const GLfloat
*normal
= (GLfloat
*)VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
]->data
;
521 GLfloat (*Fcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[0].data
;
522 #if IDX & LIGHT_TWOSIDE
523 GLfloat (*Bcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[1].data
;
526 #if IDX & LIGHT_MATERIAL
527 const GLuint nr
= VB
->Count
;
529 const GLuint nr
= VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
]->count
;
531 const struct gl_light
*light
;
534 fprintf(stderr
, "%s %d\n", __func__
, nr
);
539 sumA
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
540 sumA
[1] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
542 VB
->AttribPtr
[_TNL_ATTRIB_COLOR0
] = &store
->LitColor
[0];
543 #if IDX & LIGHT_TWOSIDE
544 VB
->BackfaceColorPtr
= &store
->LitColor
[1];
548 store
->LitColor
[0].stride
= 16;
549 store
->LitColor
[1].stride
= 16;
552 store
->LitColor
[0].stride
= 0;
553 store
->LitColor
[1].stride
= 0;
556 for (j
= 0; j
< nr
; j
++, STRIDE_F(normal
,nstride
)) {
560 #if IDX & LIGHT_MATERIAL
561 update_materials( ctx
, store
);
563 sumA
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
564 #if IDX & LIGHT_TWOSIDE
565 sumA
[1] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
570 COPY_3V(sum
[0], ctx
->Light
._BaseColor
[0]);
571 #if IDX & LIGHT_TWOSIDE
572 COPY_3V(sum
[1], ctx
->Light
._BaseColor
[1]);
575 foreach (light
, &ctx
->Light
.EnabledList
) {
576 GLfloat n_dot_h
, n_dot_VP
, spec
;
578 ACC_3V(sum
[0], light
->_MatAmbient
[0]);
579 #if IDX & LIGHT_TWOSIDE
580 ACC_3V(sum
[1], light
->_MatAmbient
[1]);
583 n_dot_VP
= DOT3(normal
, light
->_VP_inf_norm
);
585 if (n_dot_VP
> 0.0F
) {
586 ACC_SCALE_SCALAR_3V(sum
[0], n_dot_VP
, light
->_MatDiffuse
[0]);
587 n_dot_h
= DOT3(normal
, light
->_h_inf_norm
);
588 if (n_dot_h
> 0.0F
) {
589 spec
= lookup_shininess(ctx
, 0, n_dot_h
);
590 ACC_SCALE_SCALAR_3V( sum
[0], spec
, light
->_MatSpecular
[0]);
593 #if IDX & LIGHT_TWOSIDE
595 ACC_SCALE_SCALAR_3V(sum
[1], -n_dot_VP
, light
->_MatDiffuse
[1]);
596 n_dot_h
= -DOT3(normal
, light
->_h_inf_norm
);
597 if (n_dot_h
> 0.0F
) {
598 spec
= lookup_shininess(ctx
, 1, n_dot_h
);
599 ACC_SCALE_SCALAR_3V( sum
[1], spec
, light
->_MatSpecular
[1]);
605 COPY_3V( Fcolor
[j
], sum
[0] );
606 Fcolor
[j
][3] = sumA
[0];
608 #if IDX & LIGHT_TWOSIDE
609 COPY_3V( Bcolor
[j
], sum
[1] );
610 Bcolor
[j
][3] = sumA
[1];
618 static void TAG(init_light_tab
)( void )
620 _tnl_light_tab
[IDX
] = TAG(light_rgba
);
621 _tnl_light_fast_tab
[IDX
] = TAG(light_fast_rgba
);
622 _tnl_light_fast_single_tab
[IDX
] = TAG(light_fast_rgba_single
);
623 _tnl_light_spec_tab
[IDX
] = TAG(light_rgba_spec
);