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 * BRIAN PAUL 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 /* define TRACE to trace lighting code */
35 * ctx is the current context
36 * VB is the vertex buffer
37 * stage is the lighting stage-private data
38 * input is the vector of eye or object-space vertex coordinates
40 static void TAG(light_rgba_spec
)( struct gl_context
*ctx
,
41 struct vertex_buffer
*VB
,
42 struct tnl_pipeline_stage
*stage
,
45 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
46 GLfloat (*base
)[3] = ctx
->Light
._BaseColor
;
50 const GLuint vstride
= input
->stride
;
51 const GLfloat
*vertex
= (GLfloat
*)input
->data
;
52 const GLuint nstride
= VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
]->stride
;
53 const GLfloat
*normal
= (GLfloat
*)VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
]->data
;
55 GLfloat (*Fcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[0].data
;
56 #if IDX & LIGHT_TWOSIDE
57 GLfloat (*Bcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[1].data
;
60 const GLuint nr
= VB
->Count
;
63 fprintf(stderr
, "%s\n", __FUNCTION__
);
66 VB
->AttribPtr
[_TNL_ATTRIB_COLOR
] = &store
->LitColor
[0];
67 sumA
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
69 #if IDX & LIGHT_TWOSIDE
70 VB
->BackfaceColorPtr
= &store
->LitColor
[1];
71 sumA
[1] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
75 store
->LitColor
[0].stride
= 16;
76 store
->LitColor
[1].stride
= 16;
78 for (j
= 0; j
< nr
; j
++,STRIDE_F(vertex
,vstride
),STRIDE_F(normal
,nstride
)) {
79 GLfloat sum
[2][3], spec
[2][3];
80 struct gl_light
*light
;
82 #if IDX & LIGHT_MATERIAL
83 update_materials( ctx
, store
);
84 sumA
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
85 #if IDX & LIGHT_TWOSIDE
86 sumA
[1] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
90 COPY_3V(sum
[0], base
[0]);
93 #if IDX & LIGHT_TWOSIDE
94 COPY_3V(sum
[1], base
[1]);
98 /* Add contribution from each enabled light source */
99 foreach (light
, &ctx
->Light
.EnabledList
) {
105 GLfloat VP
[3]; /* unit vector from vertex to light */
106 GLfloat n_dot_VP
; /* n dot VP */
109 /* compute VP and attenuation */
110 if (!(light
->_Flags
& LIGHT_POSITIONAL
)) {
111 /* directional light */
112 COPY_3V(VP
, light
->_VP_inf_norm
);
113 attenuation
= light
->_VP_inf_spot_attenuation
;
116 GLfloat d
; /* distance from vertex to light */
118 SUB_3V(VP
, light
->_Position
, vertex
);
120 d
= (GLfloat
) LEN_3FV( VP
);
123 GLfloat invd
= 1.0F
/ d
;
124 SELF_SCALE_SCALAR_3V(VP
, invd
);
127 attenuation
= 1.0F
/ (light
->ConstantAttenuation
+ d
*
128 (light
->LinearAttenuation
+ d
*
129 light
->QuadraticAttenuation
));
131 /* spotlight attenuation */
132 if (light
->_Flags
& LIGHT_SPOT
) {
133 GLfloat PV_dot_dir
= - DOT3(VP
, light
->_NormSpotDirection
);
135 if (PV_dot_dir
<light
->_CosCutoff
) {
136 continue; /* this light makes no contribution */
139 GLdouble x
= PV_dot_dir
* (EXP_TABLE_SIZE
-1);
141 GLfloat spot
= (GLfloat
) (light
->_SpotExpTable
[k
][0]
142 + (x
-k
)*light
->_SpotExpTable
[k
][1]);
148 if (attenuation
< 1e-3)
149 continue; /* this light makes no contribution */
151 /* Compute dot product or normal and vector from V to light pos */
152 n_dot_VP
= DOT3( normal
, VP
);
154 /* Which side gets the diffuse & specular terms? */
155 if (n_dot_VP
< 0.0F
) {
156 ACC_SCALE_SCALAR_3V(sum
[0], attenuation
, light
->_MatAmbient
[0]);
157 #if IDX & LIGHT_TWOSIDE
160 n_dot_VP
= -n_dot_VP
;
166 #if IDX & LIGHT_TWOSIDE
167 ACC_SCALE_SCALAR_3V( sum
[1], attenuation
, light
->_MatAmbient
[1]);
174 COPY_3V(contrib
, light
->_MatAmbient
[side
]);
175 ACC_SCALE_SCALAR_3V(contrib
, n_dot_VP
, light
->_MatDiffuse
[side
]);
176 ACC_SCALE_SCALAR_3V(sum
[side
], attenuation
, contrib
);
178 /* specular term - cannibalize VP... */
179 if (ctx
->Light
.Model
.LocalViewer
) {
183 SUB_3V(VP
, VP
, v
); /* h = VP + VPe */
187 else if (light
->_Flags
& LIGHT_POSITIONAL
) {
189 ACC_3V(h
, ctx
->_EyeZDir
);
193 h
= light
->_h_inf_norm
;
196 n_dot_h
= correction
* DOT3(normal
, h
);
198 if (n_dot_h
> 0.0F
) {
200 struct gl_shine_tab
*tab
= ctx
->_ShineTable
[side
];
201 GET_SHINE_TAB_ENTRY( tab
, n_dot_h
, spec_coef
);
203 if (spec_coef
> 1.0e-10) {
204 spec_coef
*= attenuation
;
205 ACC_SCALE_SCALAR_3V( spec
[side
], spec_coef
,
206 light
->_MatSpecular
[side
]);
209 } /*loop over lights*/
211 COPY_3V( Fcolor
[j
], sum
[0] );
212 Fcolor
[j
][3] = sumA
[0];
214 #if IDX & LIGHT_TWOSIDE
215 COPY_3V( Bcolor
[j
], sum
[1] );
216 Bcolor
[j
][3] = sumA
[1];
222 static void TAG(light_rgba
)( struct gl_context
*ctx
,
223 struct vertex_buffer
*VB
,
224 struct tnl_pipeline_stage
*stage
,
227 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
230 GLfloat (*base
)[3] = ctx
->Light
._BaseColor
;
233 const GLuint vstride
= input
->stride
;
234 const GLfloat
*vertex
= (GLfloat
*) input
->data
;
235 const GLuint nstride
= VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
]->stride
;
236 const GLfloat
*normal
= (GLfloat
*)VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
]->data
;
238 GLfloat (*Fcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[0].data
;
239 #if IDX & LIGHT_TWOSIDE
240 GLfloat (*Bcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[1].data
;
243 const GLuint nr
= VB
->Count
;
246 fprintf(stderr
, "%s\n", __FUNCTION__
);
249 VB
->AttribPtr
[_TNL_ATTRIB_COLOR
] = &store
->LitColor
[0];
250 sumA
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
252 #if IDX & LIGHT_TWOSIDE
253 VB
->BackfaceColorPtr
= &store
->LitColor
[1];
254 sumA
[1] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
257 store
->LitColor
[0].stride
= 16;
258 store
->LitColor
[1].stride
= 16;
260 for (j
= 0; j
< nr
; j
++,STRIDE_F(vertex
,vstride
),STRIDE_F(normal
,nstride
)) {
262 struct gl_light
*light
;
264 #if IDX & LIGHT_MATERIAL
265 update_materials( ctx
, store
);
266 sumA
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
267 #if IDX & LIGHT_TWOSIDE
268 sumA
[1] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
272 COPY_3V(sum
[0], base
[0]);
274 #if IDX & LIGHT_TWOSIDE
275 COPY_3V(sum
[1], base
[1]);
278 /* Add contribution from each enabled light source */
279 foreach (light
, &ctx
->Light
.EnabledList
) {
285 GLfloat attenuation
= 1.0;
286 GLfloat VP
[3]; /* unit vector from vertex to light */
287 GLfloat n_dot_VP
; /* n dot VP */
290 /* compute VP and attenuation */
291 if (!(light
->_Flags
& LIGHT_POSITIONAL
)) {
292 /* directional light */
293 COPY_3V(VP
, light
->_VP_inf_norm
);
294 attenuation
= light
->_VP_inf_spot_attenuation
;
297 GLfloat d
; /* distance from vertex to light */
300 SUB_3V(VP
, light
->_Position
, vertex
);
302 d
= (GLfloat
) LEN_3FV( VP
);
305 GLfloat invd
= 1.0F
/ d
;
306 SELF_SCALE_SCALAR_3V(VP
, invd
);
309 attenuation
= 1.0F
/ (light
->ConstantAttenuation
+ d
*
310 (light
->LinearAttenuation
+ d
*
311 light
->QuadraticAttenuation
));
313 /* spotlight attenuation */
314 if (light
->_Flags
& LIGHT_SPOT
) {
315 GLfloat PV_dot_dir
= - DOT3(VP
, light
->_NormSpotDirection
);
317 if (PV_dot_dir
<light
->_CosCutoff
) {
318 continue; /* this light makes no contribution */
321 GLdouble x
= PV_dot_dir
* (EXP_TABLE_SIZE
-1);
323 GLfloat spot
= (GLfloat
) (light
->_SpotExpTable
[k
][0]
324 + (x
-k
)*light
->_SpotExpTable
[k
][1]);
330 if (attenuation
< 1e-3)
331 continue; /* this light makes no contribution */
333 /* Compute dot product or normal and vector from V to light pos */
334 n_dot_VP
= DOT3( normal
, VP
);
336 /* which side are we lighting? */
337 if (n_dot_VP
< 0.0F
) {
338 ACC_SCALE_SCALAR_3V(sum
[0], attenuation
, light
->_MatAmbient
[0]);
339 #if IDX & LIGHT_TWOSIDE
342 n_dot_VP
= -n_dot_VP
;
348 #if IDX & LIGHT_TWOSIDE
349 ACC_SCALE_SCALAR_3V( sum
[1], attenuation
, light
->_MatAmbient
[1]);
355 COPY_3V(contrib
, light
->_MatAmbient
[side
]);
358 ACC_SCALE_SCALAR_3V(contrib
, n_dot_VP
, light
->_MatDiffuse
[side
]);
360 /* specular term - cannibalize VP... */
362 if (ctx
->Light
.Model
.LocalViewer
) {
366 SUB_3V(VP
, VP
, v
); /* h = VP + VPe */
370 else if (light
->_Flags
& LIGHT_POSITIONAL
) {
372 ACC_3V(h
, ctx
->_EyeZDir
);
376 h
= light
->_h_inf_norm
;
379 n_dot_h
= correction
* DOT3(normal
, h
);
384 struct gl_shine_tab
*tab
= ctx
->_ShineTable
[side
];
386 GET_SHINE_TAB_ENTRY( tab
, n_dot_h
, spec_coef
);
388 ACC_SCALE_SCALAR_3V( contrib
, spec_coef
,
389 light
->_MatSpecular
[side
]);
393 ACC_SCALE_SCALAR_3V( sum
[side
], attenuation
, contrib
);
396 COPY_3V( Fcolor
[j
], sum
[0] );
397 Fcolor
[j
][3] = sumA
[0];
399 #if IDX & LIGHT_TWOSIDE
400 COPY_3V( Bcolor
[j
], sum
[1] );
401 Bcolor
[j
][3] = sumA
[1];
409 /* As below, but with just a single light.
411 static void TAG(light_fast_rgba_single
)( struct gl_context
*ctx
,
412 struct vertex_buffer
*VB
,
413 struct tnl_pipeline_stage
*stage
,
417 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
418 const GLuint nstride
= VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
]->stride
;
419 const GLfloat
*normal
= (GLfloat
*)VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
]->data
;
420 GLfloat (*Fcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[0].data
;
421 #if IDX & LIGHT_TWOSIDE
422 GLfloat (*Bcolor
)[4] = (GLfloat (*)[4]) store
->LitColor
[1].data
;
424 const struct gl_light
*light
= ctx
->Light
.EnabledList
.next
;
427 #if IDX & LIGHT_MATERIAL
428 const GLuint nr
= VB
->Count
;
430 const GLuint nr
= VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
]->count
;
434 fprintf(stderr
, "%s\n", __FUNCTION__
);
437 (void) input
; /* doesn't refer to Eye or Obj */
439 VB
->AttribPtr
[_TNL_ATTRIB_COLOR
] = &store
->LitColor
[0];
440 #if IDX & LIGHT_TWOSIDE
441 VB
->BackfaceColorPtr
= &store
->LitColor
[1];
445 store
->LitColor
[0].stride
= 16;
446 store
->LitColor
[1].stride
= 16;
449 store
->LitColor
[0].stride
= 0;
450 store
->LitColor
[1].stride
= 0;
453 for (j
= 0; j
< nr
; j
++, STRIDE_F(normal
,nstride
)) {
457 #if IDX & LIGHT_MATERIAL
458 update_materials( ctx
, store
);
461 /* No attenuation, so incoporate _MatAmbient into base color.
463 #if !(IDX & LIGHT_MATERIAL)
467 COPY_3V(base
[0], light
->_MatAmbient
[0]);
468 ACC_3V(base
[0], ctx
->Light
._BaseColor
[0] );
469 base
[0][3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
471 #if IDX & LIGHT_TWOSIDE
472 COPY_3V(base
[1], light
->_MatAmbient
[1]);
473 ACC_3V(base
[1], ctx
->Light
._BaseColor
[1]);
474 base
[1][3] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
478 n_dot_VP
= DOT3(normal
, light
->_VP_inf_norm
);
480 if (n_dot_VP
< 0.0F
) {
481 #if IDX & LIGHT_TWOSIDE
482 GLfloat n_dot_h
= -DOT3(normal
, light
->_h_inf_norm
);
484 COPY_3V(sum
, base
[1]);
485 ACC_SCALE_SCALAR_3V(sum
, -n_dot_VP
, light
->_MatDiffuse
[1]);
486 if (n_dot_h
> 0.0F
) {
488 GET_SHINE_TAB_ENTRY( ctx
->_ShineTable
[1], n_dot_h
, spec
);
489 ACC_SCALE_SCALAR_3V(sum
, spec
, light
->_MatSpecular
[1]);
491 COPY_3V(Bcolor
[j
], sum
);
492 Bcolor
[j
][3] = base
[1][3];
494 COPY_4FV(Fcolor
[j
], base
[0]);
497 GLfloat n_dot_h
= DOT3(normal
, light
->_h_inf_norm
);
499 COPY_3V(sum
, base
[0]);
500 ACC_SCALE_SCALAR_3V(sum
, n_dot_VP
, light
->_MatDiffuse
[0]);
501 if (n_dot_h
> 0.0F
) {
503 GET_SHINE_TAB_ENTRY( ctx
->_ShineTable
[0], n_dot_h
, spec
);
504 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
;
538 const struct gl_light
*light
;
541 fprintf(stderr
, "%s %d\n", __FUNCTION__
, nr
);
546 sumA
[0] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_DIFFUSE
][3];
547 sumA
[1] = ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_BACK_DIFFUSE
][3];
549 VB
->AttribPtr
[_TNL_ATTRIB_COLOR
] = &store
->LitColor
[0];
550 #if IDX & LIGHT_TWOSIDE
551 VB
->BackfaceColorPtr
= &store
->LitColor
[1];
555 store
->LitColor
[0].stride
= 16;
556 store
->LitColor
[1].stride
= 16;
559 store
->LitColor
[0].stride
= 0;
560 store
->LitColor
[1].stride
= 0;
563 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 foreach (light
, &ctx
->Light
.EnabledList
) {
583 GLfloat n_dot_h
, n_dot_VP
, spec
;
585 ACC_3V(sum
[0], light
->_MatAmbient
[0]);
586 #if IDX & LIGHT_TWOSIDE
587 ACC_3V(sum
[1], light
->_MatAmbient
[1]);
590 n_dot_VP
= DOT3(normal
, light
->_VP_inf_norm
);
592 if (n_dot_VP
> 0.0F
) {
593 ACC_SCALE_SCALAR_3V(sum
[0], n_dot_VP
, light
->_MatDiffuse
[0]);
594 n_dot_h
= DOT3(normal
, light
->_h_inf_norm
);
595 if (n_dot_h
> 0.0F
) {
596 struct gl_shine_tab
*tab
= ctx
->_ShineTable
[0];
597 GET_SHINE_TAB_ENTRY( tab
, n_dot_h
, spec
);
598 ACC_SCALE_SCALAR_3V( sum
[0], spec
, light
->_MatSpecular
[0]);
601 #if IDX & LIGHT_TWOSIDE
603 ACC_SCALE_SCALAR_3V(sum
[1], -n_dot_VP
, light
->_MatDiffuse
[1]);
604 n_dot_h
= -DOT3(normal
, light
->_h_inf_norm
);
605 if (n_dot_h
> 0.0F
) {
606 struct gl_shine_tab
*tab
= ctx
->_ShineTable
[1];
607 GET_SHINE_TAB_ENTRY( tab
, n_dot_h
, spec
);
608 ACC_SCALE_SCALAR_3V( sum
[1], spec
, light
->_MatSpecular
[1]);
614 COPY_3V( Fcolor
[j
], sum
[0] );
615 Fcolor
[j
][3] = sumA
[0];
617 #if IDX & LIGHT_TWOSIDE
618 COPY_3V( Bcolor
[j
], sum
[1] );
619 Bcolor
[j
][3] = sumA
[1];
627 static void TAG(init_light_tab
)( void )
629 _tnl_light_tab
[IDX
] = TAG(light_rgba
);
630 _tnl_light_fast_tab
[IDX
] = TAG(light_fast_rgba
);
631 _tnl_light_fast_single_tab
[IDX
] = TAG(light_fast_rgba_single
);
632 _tnl_light_spec_tab
[IDX
] = TAG(light_rgba_spec
);