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];
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 mask
= ctx
->Light
._EnabledLights
;
111 const int l
= u_bit_scan(&mask
);
112 struct gl_light
*light
= &ctx
->Light
.Light
[l
];
118 GLfloat VP
[3]; /* unit vector from vertex to light */
119 GLfloat n_dot_VP
; /* n dot VP */
122 /* compute VP and attenuation */
123 if (!(light
->_Flags
& LIGHT_POSITIONAL
)) {
124 /* directional light */
125 COPY_3V(VP
, light
->_VP_inf_norm
);
126 attenuation
= light
->_VP_inf_spot_attenuation
;
129 GLfloat d
; /* distance from vertex to light */
131 SUB_3V(VP
, light
->_Position
, vertex
);
133 d
= (GLfloat
) LEN_3FV( VP
);
136 GLfloat invd
= 1.0F
/ d
;
137 SELF_SCALE_SCALAR_3V(VP
, invd
);
140 attenuation
= 1.0F
/ (light
->ConstantAttenuation
+ d
*
141 (light
->LinearAttenuation
+ d
*
142 light
->QuadraticAttenuation
));
144 /* spotlight attenuation */
145 if (light
->_Flags
& LIGHT_SPOT
) {
146 GLfloat PV_dot_dir
= - DOT3(VP
, light
->_NormSpotDirection
);
148 if (PV_dot_dir
<light
->_CosCutoff
) {
149 continue; /* this light makes no contribution */
152 GLfloat spot
= powf(PV_dot_dir
, light
->SpotExponent
);
158 if (attenuation
< 1e-3F
)
159 continue; /* this light makes no contribution */
161 /* Compute dot product or normal and vector from V to light pos */
162 n_dot_VP
= DOT3( normal
, VP
);
164 /* Which side gets the diffuse & specular terms? */
165 if (n_dot_VP
< 0.0F
) {
166 ACC_SCALE_SCALAR_3V(sum
[0], attenuation
, light
->_MatAmbient
[0]);
167 #if IDX & LIGHT_TWOSIDE
170 n_dot_VP
= -n_dot_VP
;
176 #if IDX & LIGHT_TWOSIDE
177 ACC_SCALE_SCALAR_3V( sum
[1], attenuation
, light
->_MatAmbient
[1]);
184 COPY_3V(contrib
, light
->_MatAmbient
[side
]);
185 ACC_SCALE_SCALAR_3V(contrib
, n_dot_VP
, light
->_MatDiffuse
[side
]);
186 ACC_SCALE_SCALAR_3V(sum
[side
], attenuation
, contrib
);
188 /* specular term - cannibalize VP... */
189 if (ctx
->Light
.Model
.LocalViewer
) {
193 SUB_3V(VP
, VP
, v
); /* h = VP + VPe */
197 else if (light
->_Flags
& LIGHT_POSITIONAL
) {
199 ACC_3V(h
, ctx
->_EyeZDir
);
203 h
= light
->_h_inf_norm
;
206 n_dot_h
= correction
* DOT3(normal
, h
);
208 if (n_dot_h
> 0.0F
) {
209 GLfloat spec_coef
= lookup_shininess(ctx
, side
, n_dot_h
);
210 if (spec_coef
> 1.0e-10F
) {
211 spec_coef
*= attenuation
;
212 ACC_SCALE_SCALAR_3V( spec
[side
], spec_coef
,
213 light
->_MatSpecular
[side
]);
216 } /*loop over lights*/
218 COPY_3V( Fcolor
[j
], sum
[0] );
219 COPY_3V( Fspec
[j
], spec
[0] );
220 Fcolor
[j
][3] = sumA
[0];
222 #if IDX & LIGHT_TWOSIDE
223 COPY_3V( Bcolor
[j
], sum
[1] );
224 COPY_3V( Bspec
[j
], spec
[1] );
225 Bcolor
[j
][3] = sumA
[1];
231 static void TAG(light_rgba
)( struct gl_context
*ctx
,
232 struct vertex_buffer
*VB
,
233 struct tnl_pipeline_stage
*stage
,
236 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
239 GLfloat (*base
)[3] = ctx
->Light
._BaseColor
;
242 const GLuint vstride
= input
->stride
;
243 const GLfloat
*vertex
= (GLfloat
*) input
->data
;
244 const GLuint nstride
= VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
]->stride
;
245 const GLfloat
*normal
= (GLfloat
*)VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
]->data
;
247 GLfloat (*Fcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[0].data
;
248 #if IDX & LIGHT_TWOSIDE
249 GLfloat (*Bcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[1].data
;
252 const GLuint nr
= VB
->Count
;
255 fprintf(stderr
, "%s\n", __func__
);
258 VB
->AttribPtr
[_TNL_ATTRIB_COLOR0
] = &store
->LitColor
[0];
259 sumA
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
261 #if IDX & LIGHT_TWOSIDE
262 VB
->BackfaceColorPtr
= &store
->LitColor
[1];
263 sumA
[1] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
266 store
->LitColor
[0].stride
= 16;
267 store
->LitColor
[1].stride
= 16;
269 for (j
= 0; j
< nr
; j
++,STRIDE_F(vertex
,vstride
),STRIDE_F(normal
,nstride
)) {
273 #if IDX & LIGHT_MATERIAL
274 update_materials( ctx
, store
);
275 sumA
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
276 #if IDX & LIGHT_TWOSIDE
277 sumA
[1] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
281 COPY_3V(sum
[0], base
[0]);
283 #if IDX & LIGHT_TWOSIDE
284 COPY_3V(sum
[1], base
[1]);
287 /* Add contribution from each enabled light source */
288 mask
= ctx
->Light
._EnabledLights
;
290 const int l
= u_bit_scan(&mask
);
291 struct gl_light
*light
= &ctx
->Light
.Light
[l
];
297 GLfloat VP
[3]; /* unit vector from vertex to light */
298 GLfloat n_dot_VP
; /* n dot VP */
301 /* compute VP and attenuation */
302 if (!(light
->_Flags
& LIGHT_POSITIONAL
)) {
303 /* directional light */
304 COPY_3V(VP
, light
->_VP_inf_norm
);
305 attenuation
= light
->_VP_inf_spot_attenuation
;
308 GLfloat d
; /* distance from vertex to light */
310 SUB_3V(VP
, light
->_Position
, vertex
);
312 d
= (GLfloat
) LEN_3FV( VP
);
315 GLfloat invd
= 1.0F
/ d
;
316 SELF_SCALE_SCALAR_3V(VP
, invd
);
319 attenuation
= 1.0F
/ (light
->ConstantAttenuation
+ d
*
320 (light
->LinearAttenuation
+ d
*
321 light
->QuadraticAttenuation
));
323 /* spotlight attenuation */
324 if (light
->_Flags
& LIGHT_SPOT
) {
325 GLfloat PV_dot_dir
= - DOT3(VP
, light
->_NormSpotDirection
);
327 if (PV_dot_dir
<light
->_CosCutoff
) {
328 continue; /* this light makes no contribution */
331 GLfloat spot
= powf(PV_dot_dir
, light
->SpotExponent
);
337 if (attenuation
< 1e-3F
)
338 continue; /* this light makes no contribution */
340 /* Compute dot product or normal and vector from V to light pos */
341 n_dot_VP
= DOT3( normal
, VP
);
343 /* which side are we lighting? */
344 if (n_dot_VP
< 0.0F
) {
345 ACC_SCALE_SCALAR_3V(sum
[0], attenuation
, light
->_MatAmbient
[0]);
346 #if IDX & LIGHT_TWOSIDE
349 n_dot_VP
= -n_dot_VP
;
355 #if IDX & LIGHT_TWOSIDE
356 ACC_SCALE_SCALAR_3V( sum
[1], attenuation
, light
->_MatAmbient
[1]);
362 COPY_3V(contrib
, light
->_MatAmbient
[side
]);
365 ACC_SCALE_SCALAR_3V(contrib
, n_dot_VP
, light
->_MatDiffuse
[side
]);
367 /* specular term - cannibalize VP... */
369 if (ctx
->Light
.Model
.LocalViewer
) {
373 SUB_3V(VP
, VP
, v
); /* h = VP + VPe */
377 else if (light
->_Flags
& LIGHT_POSITIONAL
) {
379 ACC_3V(h
, ctx
->_EyeZDir
);
383 h
= light
->_h_inf_norm
;
386 n_dot_h
= correction
* DOT3(normal
, h
);
388 if (n_dot_h
> 0.0F
) {
389 GLfloat spec_coef
= lookup_shininess(ctx
, side
, n_dot_h
);
390 ACC_SCALE_SCALAR_3V( contrib
, spec_coef
,
391 light
->_MatSpecular
[side
]);
395 ACC_SCALE_SCALAR_3V( sum
[side
], attenuation
, contrib
);
398 COPY_3V( Fcolor
[j
], sum
[0] );
399 Fcolor
[j
][3] = sumA
[0];
401 #if IDX & LIGHT_TWOSIDE
402 COPY_3V( Bcolor
[j
], sum
[1] );
403 Bcolor
[j
][3] = sumA
[1];
411 /* As below, but with just a single light.
413 static void TAG(light_fast_rgba_single
)( struct gl_context
*ctx
,
414 struct vertex_buffer
*VB
,
415 struct tnl_pipeline_stage
*stage
,
419 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
420 const GLuint nstride
= VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
]->stride
;
421 const GLfloat
*normal
= (GLfloat
*)VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
]->data
;
422 GLfloat (*Fcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[0].data
;
423 #if IDX & LIGHT_TWOSIDE
424 GLfloat (*Bcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[1].data
;
426 const struct gl_light
*light
=
427 &ctx
->Light
.Light
[ffs(ctx
->Light
._EnabledLights
) - 1];
430 #if IDX & LIGHT_MATERIAL
431 const GLuint nr
= VB
->Count
;
433 const GLuint nr
= VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
]->count
;
437 fprintf(stderr
, "%s\n", __func__
);
440 (void) input
; /* doesn't refer to Eye or Obj */
442 VB
->AttribPtr
[_TNL_ATTRIB_COLOR0
] = &store
->LitColor
[0];
443 #if IDX & LIGHT_TWOSIDE
444 VB
->BackfaceColorPtr
= &store
->LitColor
[1];
448 store
->LitColor
[0].stride
= 16;
449 store
->LitColor
[1].stride
= 16;
452 store
->LitColor
[0].stride
= 0;
453 store
->LitColor
[1].stride
= 0;
456 for (j
= 0; j
< nr
; j
++, STRIDE_F(normal
,nstride
)) {
460 #if IDX & LIGHT_MATERIAL
461 update_materials( ctx
, store
);
464 /* No attenuation, so incoporate _MatAmbient into base color.
466 #if !(IDX & LIGHT_MATERIAL)
470 COPY_3V(base
[0], light
->_MatAmbient
[0]);
471 ACC_3V(base
[0], ctx
->Light
._BaseColor
[0] );
472 base
[0][3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
474 #if IDX & LIGHT_TWOSIDE
475 COPY_3V(base
[1], light
->_MatAmbient
[1]);
476 ACC_3V(base
[1], ctx
->Light
._BaseColor
[1]);
477 base
[1][3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
481 n_dot_VP
= DOT3(normal
, light
->_VP_inf_norm
);
483 if (n_dot_VP
< 0.0F
) {
484 #if IDX & LIGHT_TWOSIDE
485 GLfloat n_dot_h
= -DOT3(normal
, light
->_h_inf_norm
);
487 COPY_3V(sum
, base
[1]);
488 ACC_SCALE_SCALAR_3V(sum
, -n_dot_VP
, light
->_MatDiffuse
[1]);
489 if (n_dot_h
> 0.0F
) {
490 GLfloat spec
= lookup_shininess(ctx
, 1, n_dot_h
);
491 ACC_SCALE_SCALAR_3V(sum
, spec
, light
->_MatSpecular
[1]);
493 COPY_3V(Bcolor
[j
], sum
);
494 Bcolor
[j
][3] = base
[1][3];
496 COPY_4FV(Fcolor
[j
], base
[0]);
499 GLfloat n_dot_h
= DOT3(normal
, light
->_h_inf_norm
);
501 COPY_3V(sum
, base
[0]);
502 ACC_SCALE_SCALAR_3V(sum
, n_dot_VP
, light
->_MatDiffuse
[0]);
503 if (n_dot_h
> 0.0F
) {
504 GLfloat spec
= lookup_shininess(ctx
, 0, n_dot_h
);
505 ACC_SCALE_SCALAR_3V(sum
, spec
, light
->_MatSpecular
[0]);
507 COPY_3V(Fcolor
[j
], sum
);
508 Fcolor
[j
][3] = base
[0][3];
509 #if IDX & LIGHT_TWOSIDE
510 COPY_4FV(Bcolor
[j
], base
[1]);
517 /* Light infinite lights
519 static void TAG(light_fast_rgba
)( struct gl_context
*ctx
,
520 struct vertex_buffer
*VB
,
521 struct tnl_pipeline_stage
*stage
,
524 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
526 const GLuint nstride
= VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
]->stride
;
527 const GLfloat
*normal
= (GLfloat
*)VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
]->data
;
528 GLfloat (*Fcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[0].data
;
529 #if IDX & LIGHT_TWOSIDE
530 GLfloat (*Bcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[1].data
;
533 #if IDX & LIGHT_MATERIAL
534 const GLuint nr
= VB
->Count
;
536 const GLuint nr
= VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
]->count
;
540 fprintf(stderr
, "%s %d\n", __func__
, nr
);
545 sumA
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
546 sumA
[1] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
548 VB
->AttribPtr
[_TNL_ATTRIB_COLOR0
] = &store
->LitColor
[0];
549 #if IDX & LIGHT_TWOSIDE
550 VB
->BackfaceColorPtr
= &store
->LitColor
[1];
554 store
->LitColor
[0].stride
= 16;
555 store
->LitColor
[1].stride
= 16;
558 store
->LitColor
[0].stride
= 0;
559 store
->LitColor
[1].stride
= 0;
562 for (j
= 0; j
< nr
; j
++, STRIDE_F(normal
,nstride
)) {
567 #if IDX & LIGHT_MATERIAL
568 update_materials( ctx
, store
);
570 sumA
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
571 #if IDX & LIGHT_TWOSIDE
572 sumA
[1] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
577 COPY_3V(sum
[0], ctx
->Light
._BaseColor
[0]);
578 #if IDX & LIGHT_TWOSIDE
579 COPY_3V(sum
[1], ctx
->Light
._BaseColor
[1]);
582 mask
= ctx
->Light
._EnabledLights
;
584 const int l
= u_bit_scan(&mask
);
585 const struct gl_light
*light
= &ctx
->Light
.Light
[l
];
586 GLfloat n_dot_h
, n_dot_VP
, spec
;
588 ACC_3V(sum
[0], light
->_MatAmbient
[0]);
589 #if IDX & LIGHT_TWOSIDE
590 ACC_3V(sum
[1], light
->_MatAmbient
[1]);
593 n_dot_VP
= DOT3(normal
, light
->_VP_inf_norm
);
595 if (n_dot_VP
> 0.0F
) {
596 ACC_SCALE_SCALAR_3V(sum
[0], n_dot_VP
, light
->_MatDiffuse
[0]);
597 n_dot_h
= DOT3(normal
, light
->_h_inf_norm
);
598 if (n_dot_h
> 0.0F
) {
599 spec
= lookup_shininess(ctx
, 0, n_dot_h
);
600 ACC_SCALE_SCALAR_3V( sum
[0], spec
, light
->_MatSpecular
[0]);
603 #if IDX & LIGHT_TWOSIDE
605 ACC_SCALE_SCALAR_3V(sum
[1], -n_dot_VP
, light
->_MatDiffuse
[1]);
606 n_dot_h
= -DOT3(normal
, light
->_h_inf_norm
);
607 if (n_dot_h
> 0.0F
) {
608 spec
= lookup_shininess(ctx
, 1, n_dot_h
);
609 ACC_SCALE_SCALAR_3V( sum
[1], spec
, light
->_MatSpecular
[1]);
615 COPY_3V( Fcolor
[j
], sum
[0] );
616 Fcolor
[j
][3] = sumA
[0];
618 #if IDX & LIGHT_TWOSIDE
619 COPY_3V( Bcolor
[j
], sum
[1] );
620 Bcolor
[j
][3] = sumA
[1];
628 static void TAG(init_light_tab
)( void )
630 _tnl_light_tab
[IDX
] = TAG(light_rgba
);
631 _tnl_light_fast_tab
[IDX
] = TAG(light_fast_rgba
);
632 _tnl_light_fast_single_tab
[IDX
] = TAG(light_fast_rgba_single
);
633 _tnl_light_spec_tab
[IDX
] = TAG(light_rgba_spec
);