2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 * Keith Whitwell <keith@tungstengraphics.com>
31 #if IDX & LIGHT_TWOSIDE
38 /* define TRACE to trace lighting code */
42 * ctx is the current context
43 * VB is the vertex buffer
44 * stage is the lighting stage-private data
45 * input is the vector of eye or object-space vertex coordinates
47 static void TAG(light_rgba_spec
)( struct gl_context
*ctx
,
48 struct vertex_buffer
*VB
,
49 struct tnl_pipeline_stage
*stage
,
52 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
53 GLfloat (*base
)[3] = ctx
->Light
._BaseColor
;
57 const GLuint vstride
= input
->stride
;
58 const GLfloat
*vertex
= (GLfloat
*)input
->data
;
59 const GLuint nstride
= VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
]->stride
;
60 const GLfloat
*normal
= (GLfloat
*)VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
]->data
;
62 GLfloat (*Fcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[0].data
;
63 GLfloat (*Fspec
)[4] = (GLfloat (*)[4]) store
->LitSecondary
[0].data
;
64 #if IDX & LIGHT_TWOSIDE
65 GLfloat (*Bcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[1].data
;
66 GLfloat (*Bspec
)[4] = (GLfloat (*)[4]) store
->LitSecondary
[1].data
;
69 const GLuint nr
= VB
->Count
;
72 fprintf(stderr
, "%s\n", __FUNCTION__
);
75 VB
->AttribPtr
[_TNL_ATTRIB_COLOR0
] = &store
->LitColor
[0];
76 VB
->AttribPtr
[_TNL_ATTRIB_COLOR1
] = &store
->LitSecondary
[0];
77 sumA
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
79 #if IDX & LIGHT_TWOSIDE
80 VB
->BackfaceColorPtr
= &store
->LitColor
[1];
81 VB
->BackfaceSecondaryColorPtr
= &store
->LitSecondary
[1];
82 sumA
[1] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
86 store
->LitColor
[0].stride
= 16;
87 store
->LitColor
[1].stride
= 16;
89 for (j
= 0; j
< nr
; j
++,STRIDE_F(vertex
,vstride
),STRIDE_F(normal
,nstride
)) {
90 GLfloat sum
[2][3], spec
[2][3];
91 struct gl_light
*light
;
93 #if IDX & LIGHT_MATERIAL
94 update_materials( ctx
, store
);
95 sumA
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
96 #if IDX & LIGHT_TWOSIDE
97 sumA
[1] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
101 COPY_3V(sum
[0], base
[0]);
104 #if IDX & LIGHT_TWOSIDE
105 COPY_3V(sum
[1], base
[1]);
109 /* Add contribution from each enabled light source */
110 foreach (light
, &ctx
->Light
.EnabledList
) {
116 GLfloat VP
[3]; /* unit vector from vertex to light */
117 GLfloat n_dot_VP
; /* n dot VP */
120 /* compute VP and attenuation */
121 if (!(light
->_Flags
& LIGHT_POSITIONAL
)) {
122 /* directional light */
123 COPY_3V(VP
, light
->_VP_inf_norm
);
124 attenuation
= light
->_VP_inf_spot_attenuation
;
127 GLfloat d
; /* distance from vertex to light */
129 SUB_3V(VP
, light
->_Position
, vertex
);
131 d
= (GLfloat
) LEN_3FV( VP
);
134 GLfloat invd
= 1.0F
/ d
;
135 SELF_SCALE_SCALAR_3V(VP
, invd
);
138 attenuation
= 1.0F
/ (light
->ConstantAttenuation
+ d
*
139 (light
->LinearAttenuation
+ d
*
140 light
->QuadraticAttenuation
));
142 /* spotlight attenuation */
143 if (light
->_Flags
& LIGHT_SPOT
) {
144 GLfloat PV_dot_dir
= - DOT3(VP
, light
->_NormSpotDirection
);
146 if (PV_dot_dir
<light
->_CosCutoff
) {
147 continue; /* this light makes no contribution */
150 GLfloat spot
= powf(PV_dot_dir
, light
->SpotExponent
);
156 if (attenuation
< 1e-3)
157 continue; /* this light makes no contribution */
159 /* Compute dot product or normal and vector from V to light pos */
160 n_dot_VP
= DOT3( normal
, VP
);
162 /* Which side gets the diffuse & specular terms? */
163 if (n_dot_VP
< 0.0F
) {
164 ACC_SCALE_SCALAR_3V(sum
[0], attenuation
, light
->_MatAmbient
[0]);
165 #if IDX & LIGHT_TWOSIDE
168 n_dot_VP
= -n_dot_VP
;
174 #if IDX & LIGHT_TWOSIDE
175 ACC_SCALE_SCALAR_3V( sum
[1], attenuation
, light
->_MatAmbient
[1]);
182 COPY_3V(contrib
, light
->_MatAmbient
[side
]);
183 ACC_SCALE_SCALAR_3V(contrib
, n_dot_VP
, light
->_MatDiffuse
[side
]);
184 ACC_SCALE_SCALAR_3V(sum
[side
], attenuation
, contrib
);
186 /* specular term - cannibalize VP... */
187 if (ctx
->Light
.Model
.LocalViewer
) {
191 SUB_3V(VP
, VP
, v
); /* h = VP + VPe */
195 else if (light
->_Flags
& LIGHT_POSITIONAL
) {
197 ACC_3V(h
, ctx
->_EyeZDir
);
201 h
= light
->_h_inf_norm
;
204 n_dot_h
= correction
* DOT3(normal
, h
);
206 if (n_dot_h
> 0.0F
) {
207 GLfloat spec_coef
= lookup_shininess(ctx
, side
, n_dot_h
);
208 if (spec_coef
> 1.0e-10) {
209 spec_coef
*= attenuation
;
210 ACC_SCALE_SCALAR_3V( spec
[side
], spec_coef
,
211 light
->_MatSpecular
[side
]);
214 } /*loop over lights*/
216 COPY_3V( Fcolor
[j
], sum
[0] );
217 COPY_3V( Fspec
[j
], spec
[0] );
218 Fcolor
[j
][3] = sumA
[0];
220 #if IDX & LIGHT_TWOSIDE
221 COPY_3V( Bcolor
[j
], sum
[1] );
222 COPY_3V( Bspec
[j
], spec
[1] );
223 Bcolor
[j
][3] = sumA
[1];
229 static void TAG(light_rgba
)( struct gl_context
*ctx
,
230 struct vertex_buffer
*VB
,
231 struct tnl_pipeline_stage
*stage
,
234 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
237 GLfloat (*base
)[3] = ctx
->Light
._BaseColor
;
240 const GLuint vstride
= input
->stride
;
241 const GLfloat
*vertex
= (GLfloat
*) input
->data
;
242 const GLuint nstride
= VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
]->stride
;
243 const GLfloat
*normal
= (GLfloat
*)VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
]->data
;
245 GLfloat (*Fcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[0].data
;
246 #if IDX & LIGHT_TWOSIDE
247 GLfloat (*Bcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[1].data
;
250 const GLuint nr
= VB
->Count
;
253 fprintf(stderr
, "%s\n", __FUNCTION__
);
256 VB
->AttribPtr
[_TNL_ATTRIB_COLOR0
] = &store
->LitColor
[0];
257 sumA
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
259 #if IDX & LIGHT_TWOSIDE
260 VB
->BackfaceColorPtr
= &store
->LitColor
[1];
261 sumA
[1] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
264 store
->LitColor
[0].stride
= 16;
265 store
->LitColor
[1].stride
= 16;
267 for (j
= 0; j
< nr
; j
++,STRIDE_F(vertex
,vstride
),STRIDE_F(normal
,nstride
)) {
269 struct gl_light
*light
;
271 #if IDX & LIGHT_MATERIAL
272 update_materials( ctx
, store
);
273 sumA
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
274 #if IDX & LIGHT_TWOSIDE
275 sumA
[1] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
279 COPY_3V(sum
[0], base
[0]);
281 #if IDX & LIGHT_TWOSIDE
282 COPY_3V(sum
[1], base
[1]);
285 /* Add contribution from each enabled light source */
286 foreach (light
, &ctx
->Light
.EnabledList
) {
292 GLfloat attenuation
= 1.0;
293 GLfloat VP
[3]; /* unit vector from vertex to light */
294 GLfloat n_dot_VP
; /* n dot VP */
297 /* compute VP and attenuation */
298 if (!(light
->_Flags
& LIGHT_POSITIONAL
)) {
299 /* directional light */
300 COPY_3V(VP
, light
->_VP_inf_norm
);
301 attenuation
= light
->_VP_inf_spot_attenuation
;
304 GLfloat d
; /* distance from vertex to light */
307 SUB_3V(VP
, light
->_Position
, vertex
);
309 d
= (GLfloat
) LEN_3FV( VP
);
312 GLfloat invd
= 1.0F
/ d
;
313 SELF_SCALE_SCALAR_3V(VP
, invd
);
316 attenuation
= 1.0F
/ (light
->ConstantAttenuation
+ d
*
317 (light
->LinearAttenuation
+ d
*
318 light
->QuadraticAttenuation
));
320 /* spotlight attenuation */
321 if (light
->_Flags
& LIGHT_SPOT
) {
322 GLfloat PV_dot_dir
= - DOT3(VP
, light
->_NormSpotDirection
);
324 if (PV_dot_dir
<light
->_CosCutoff
) {
325 continue; /* this light makes no contribution */
328 GLfloat spot
= powf(PV_dot_dir
, light
->SpotExponent
);
334 if (attenuation
< 1e-3)
335 continue; /* this light makes no contribution */
337 /* Compute dot product or normal and vector from V to light pos */
338 n_dot_VP
= DOT3( normal
, VP
);
340 /* which side are we lighting? */
341 if (n_dot_VP
< 0.0F
) {
342 ACC_SCALE_SCALAR_3V(sum
[0], attenuation
, light
->_MatAmbient
[0]);
343 #if IDX & LIGHT_TWOSIDE
346 n_dot_VP
= -n_dot_VP
;
352 #if IDX & LIGHT_TWOSIDE
353 ACC_SCALE_SCALAR_3V( sum
[1], attenuation
, light
->_MatAmbient
[1]);
359 COPY_3V(contrib
, light
->_MatAmbient
[side
]);
362 ACC_SCALE_SCALAR_3V(contrib
, n_dot_VP
, light
->_MatDiffuse
[side
]);
364 /* specular term - cannibalize VP... */
366 if (ctx
->Light
.Model
.LocalViewer
) {
370 SUB_3V(VP
, VP
, v
); /* h = VP + VPe */
374 else if (light
->_Flags
& LIGHT_POSITIONAL
) {
376 ACC_3V(h
, ctx
->_EyeZDir
);
380 h
= light
->_h_inf_norm
;
383 n_dot_h
= correction
* DOT3(normal
, h
);
385 if (n_dot_h
> 0.0F
) {
386 GLfloat spec_coef
= lookup_shininess(ctx
, side
, n_dot_h
);
387 ACC_SCALE_SCALAR_3V( contrib
, spec_coef
,
388 light
->_MatSpecular
[side
]);
392 ACC_SCALE_SCALAR_3V( sum
[side
], attenuation
, contrib
);
395 COPY_3V( Fcolor
[j
], sum
[0] );
396 Fcolor
[j
][3] = sumA
[0];
398 #if IDX & LIGHT_TWOSIDE
399 COPY_3V( Bcolor
[j
], sum
[1] );
400 Bcolor
[j
][3] = sumA
[1];
408 /* As below, but with just a single light.
410 static void TAG(light_fast_rgba_single
)( struct gl_context
*ctx
,
411 struct vertex_buffer
*VB
,
412 struct tnl_pipeline_stage
*stage
,
416 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
417 const GLuint nstride
= VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
]->stride
;
418 const GLfloat
*normal
= (GLfloat
*)VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
]->data
;
419 GLfloat (*Fcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[0].data
;
420 #if IDX & LIGHT_TWOSIDE
421 GLfloat (*Bcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[1].data
;
423 const struct gl_light
*light
= ctx
->Light
.EnabledList
.next
;
426 #if IDX & LIGHT_MATERIAL
427 const GLuint nr
= VB
->Count
;
429 const GLuint nr
= VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
]->count
;
433 fprintf(stderr
, "%s\n", __FUNCTION__
);
436 (void) input
; /* doesn't refer to Eye or Obj */
438 VB
->AttribPtr
[_TNL_ATTRIB_COLOR0
] = &store
->LitColor
[0];
439 #if IDX & LIGHT_TWOSIDE
440 VB
->BackfaceColorPtr
= &store
->LitColor
[1];
444 store
->LitColor
[0].stride
= 16;
445 store
->LitColor
[1].stride
= 16;
448 store
->LitColor
[0].stride
= 0;
449 store
->LitColor
[1].stride
= 0;
452 for (j
= 0; j
< nr
; j
++, STRIDE_F(normal
,nstride
)) {
456 #if IDX & LIGHT_MATERIAL
457 update_materials( ctx
, store
);
460 /* No attenuation, so incoporate _MatAmbient into base color.
462 #if !(IDX & LIGHT_MATERIAL)
466 COPY_3V(base
[0], light
->_MatAmbient
[0]);
467 ACC_3V(base
[0], ctx
->Light
._BaseColor
[0] );
468 base
[0][3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
470 #if IDX & LIGHT_TWOSIDE
471 COPY_3V(base
[1], light
->_MatAmbient
[1]);
472 ACC_3V(base
[1], ctx
->Light
._BaseColor
[1]);
473 base
[1][3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
477 n_dot_VP
= DOT3(normal
, light
->_VP_inf_norm
);
479 if (n_dot_VP
< 0.0F
) {
480 #if IDX & LIGHT_TWOSIDE
481 GLfloat n_dot_h
= -DOT3(normal
, light
->_h_inf_norm
);
483 COPY_3V(sum
, base
[1]);
484 ACC_SCALE_SCALAR_3V(sum
, -n_dot_VP
, light
->_MatDiffuse
[1]);
485 if (n_dot_h
> 0.0F
) {
486 GLfloat spec
= lookup_shininess(ctx
, 1, n_dot_h
);
487 ACC_SCALE_SCALAR_3V(sum
, spec
, light
->_MatSpecular
[1]);
489 COPY_3V(Bcolor
[j
], sum
);
490 Bcolor
[j
][3] = base
[1][3];
492 COPY_4FV(Fcolor
[j
], base
[0]);
495 GLfloat n_dot_h
= DOT3(normal
, light
->_h_inf_norm
);
497 COPY_3V(sum
, base
[0]);
498 ACC_SCALE_SCALAR_3V(sum
, n_dot_VP
, light
->_MatDiffuse
[0]);
499 if (n_dot_h
> 0.0F
) {
500 GLfloat spec
= lookup_shininess(ctx
, 0, n_dot_h
);
501 ACC_SCALE_SCALAR_3V(sum
, spec
, light
->_MatSpecular
[0]);
503 COPY_3V(Fcolor
[j
], sum
);
504 Fcolor
[j
][3] = base
[0][3];
505 #if IDX & LIGHT_TWOSIDE
506 COPY_4FV(Bcolor
[j
], base
[1]);
513 /* Light infinite lights
515 static void TAG(light_fast_rgba
)( struct gl_context
*ctx
,
516 struct vertex_buffer
*VB
,
517 struct tnl_pipeline_stage
*stage
,
520 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
522 const GLuint nstride
= VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
]->stride
;
523 const GLfloat
*normal
= (GLfloat
*)VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
]->data
;
524 GLfloat (*Fcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[0].data
;
525 #if IDX & LIGHT_TWOSIDE
526 GLfloat (*Bcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[1].data
;
529 #if IDX & LIGHT_MATERIAL
530 const GLuint nr
= VB
->Count
;
532 const GLuint nr
= VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
]->count
;
534 const struct gl_light
*light
;
537 fprintf(stderr
, "%s %d\n", __FUNCTION__
, nr
);
542 sumA
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
543 sumA
[1] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
545 VB
->AttribPtr
[_TNL_ATTRIB_COLOR0
] = &store
->LitColor
[0];
546 #if IDX & LIGHT_TWOSIDE
547 VB
->BackfaceColorPtr
= &store
->LitColor
[1];
551 store
->LitColor
[0].stride
= 16;
552 store
->LitColor
[1].stride
= 16;
555 store
->LitColor
[0].stride
= 0;
556 store
->LitColor
[1].stride
= 0;
559 for (j
= 0; j
< nr
; j
++, STRIDE_F(normal
,nstride
)) {
563 #if IDX & LIGHT_MATERIAL
564 update_materials( ctx
, store
);
566 sumA
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
567 #if IDX & LIGHT_TWOSIDE
568 sumA
[1] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
573 COPY_3V(sum
[0], ctx
->Light
._BaseColor
[0]);
574 #if IDX & LIGHT_TWOSIDE
575 COPY_3V(sum
[1], ctx
->Light
._BaseColor
[1]);
578 foreach (light
, &ctx
->Light
.EnabledList
) {
579 GLfloat n_dot_h
, n_dot_VP
, spec
;
581 ACC_3V(sum
[0], light
->_MatAmbient
[0]);
582 #if IDX & LIGHT_TWOSIDE
583 ACC_3V(sum
[1], light
->_MatAmbient
[1]);
586 n_dot_VP
= DOT3(normal
, light
->_VP_inf_norm
);
588 if (n_dot_VP
> 0.0F
) {
589 ACC_SCALE_SCALAR_3V(sum
[0], n_dot_VP
, light
->_MatDiffuse
[0]);
590 n_dot_h
= DOT3(normal
, light
->_h_inf_norm
);
591 if (n_dot_h
> 0.0F
) {
592 spec
= lookup_shininess(ctx
, 0, n_dot_h
);
593 ACC_SCALE_SCALAR_3V( sum
[0], spec
, light
->_MatSpecular
[0]);
596 #if IDX & LIGHT_TWOSIDE
598 ACC_SCALE_SCALAR_3V(sum
[1], -n_dot_VP
, light
->_MatDiffuse
[1]);
599 n_dot_h
= -DOT3(normal
, light
->_h_inf_norm
);
600 if (n_dot_h
> 0.0F
) {
601 spec
= lookup_shininess(ctx
, 1, n_dot_h
);
602 ACC_SCALE_SCALAR_3V( sum
[1], spec
, light
->_MatSpecular
[1]);
608 COPY_3V( Fcolor
[j
], sum
[0] );
609 Fcolor
[j
][3] = sumA
[0];
611 #if IDX & LIGHT_TWOSIDE
612 COPY_3V( Bcolor
[j
], sum
[1] );
613 Bcolor
[j
][3] = sumA
[1];
621 static void TAG(init_light_tab
)( void )
623 _tnl_light_tab
[IDX
] = TAG(light_rgba
);
624 _tnl_light_fast_tab
[IDX
] = TAG(light_fast_rgba
);
625 _tnl_light_fast_single_tab
[IDX
] = TAG(light_fast_rgba_single
);
626 _tnl_light_spec_tab
[IDX
] = TAG(light_rgba_spec
);