167325eae83d6782289c3942555a82912cea1b9f
[mesa.git] / src / mesa / tnl / t_vb_lighttmp.h
1
2 /*
3 * Mesa 3-D graphics library
4 * Version: 4.1
5 *
6 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 *
26 * Authors:
27 * Brian Paul
28 * Keith Whitwell <keith@tungstengraphics.com>
29 */
30
31
32 #if (IDX & LIGHT_TWOSIDE)
33 # define NR_SIDES 2
34 #else
35 # define NR_SIDES 1
36 #endif
37
38
39 /* define TRACE to trace lighting code */
40 /* #define TRACE 1 */
41
42 /*
43 * ctx is the current context
44 * VB is the vertex buffer
45 * stage is the lighting stage-private data
46 * input is the vector of eye or object-space vertex coordinates
47 */
48 static void TAG(light_rgba_spec)( GLcontext *ctx,
49 struct vertex_buffer *VB,
50 struct tnl_pipeline_stage *stage,
51 GLvector4f *input )
52 {
53 struct light_stage_data *store = LIGHT_STAGE_DATA(stage);
54 GLfloat (*base)[3] = ctx->Light._BaseColor;
55 GLfloat sumA[2];
56 GLuint j;
57
58 const GLuint vstride = input->stride;
59 const GLfloat *vertex = (GLfloat *)input->data;
60 const GLuint nstride = VB->NormalPtr->stride;
61 const GLfloat *normal = (GLfloat *)VB->NormalPtr->data;
62
63 GLfloat (*Fcolor)[4] = (GLfloat (*)[4]) store->LitColor[0].data;
64 GLfloat (*Bcolor)[4] = (GLfloat (*)[4]) store->LitColor[1].data;
65 GLfloat (*Fspec)[4] = (GLfloat (*)[4]) store->LitSecondary[0].data;
66 GLfloat (*Bspec)[4] = (GLfloat (*)[4]) store->LitSecondary[1].data;
67
68 const GLuint nr = VB->Count;
69
70 (void) nstride;
71 (void) vstride;
72
73 #ifdef TRACE
74 fprintf(stderr, "%s\n", __FUNCTION__ );
75 #endif
76
77 VB->ColorPtr[0] = &store->LitColor[0];
78 VB->SecondaryColorPtr[0] = &store->LitSecondary[0];
79 sumA[0] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE][3];
80
81 if (IDX & LIGHT_TWOSIDE) {
82 VB->ColorPtr[1] = &store->LitColor[1];
83 VB->SecondaryColorPtr[1] = &store->LitSecondary[1];
84 sumA[1] = ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_DIFFUSE][3];
85 }
86
87 /* Side-effects done, can we finish now?
88 */
89 if (stage->changed_inputs == 0)
90 return;
91
92 for (j = 0; j < nr; j++,STRIDE_F(vertex,vstride),STRIDE_F(normal,nstride)) {
93 GLfloat sum[2][3], spec[2][3];
94 struct gl_light *light;
95
96 if ( IDX & LIGHT_MATERIAL ) {
97 update_materials( ctx, store );
98 sumA[0] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE][3];
99 if (IDX & LIGHT_TWOSIDE)
100 sumA[1] = ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_DIFFUSE][3];
101 }
102
103 COPY_3V(sum[0], base[0]);
104 ZERO_3V(spec[0]);
105
106 if (IDX & LIGHT_TWOSIDE) {
107 COPY_3V(sum[1], base[1]);
108 ZERO_3V(spec[1]);
109 }
110
111 /* Add contribution from each enabled light source */
112 foreach (light, &ctx->Light.EnabledList) {
113 GLfloat n_dot_h;
114 GLfloat correction;
115 GLint side;
116 GLfloat contrib[3];
117 GLfloat attenuation;
118 GLfloat VP[3]; /* unit vector from vertex to light */
119 GLfloat n_dot_VP; /* n dot VP */
120 GLfloat *h;
121
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;
127 }
128 else {
129 GLfloat d; /* distance from vertex to light */
130
131 SUB_3V(VP, light->_Position, vertex);
132
133 d = (GLfloat) LEN_3FV( VP );
134
135 if (d > 1e-6) {
136 GLfloat invd = 1.0F / d;
137 SELF_SCALE_SCALAR_3V(VP, invd);
138 }
139
140 attenuation = 1.0F / (light->ConstantAttenuation + d *
141 (light->LinearAttenuation + d *
142 light->QuadraticAttenuation));
143
144 /* spotlight attenuation */
145 if (light->_Flags & LIGHT_SPOT) {
146 GLfloat PV_dot_dir = - DOT3(VP, light->_NormDirection);
147
148 if (PV_dot_dir<light->_CosCutoff) {
149 continue; /* this light makes no contribution */
150 }
151 else {
152 GLdouble x = PV_dot_dir * (EXP_TABLE_SIZE-1);
153 GLint k = (GLint) x;
154 GLfloat spot = (GLfloat) (light->_SpotExpTable[k][0]
155 + (x-k)*light->_SpotExpTable[k][1]);
156 attenuation *= spot;
157 }
158 }
159 }
160
161 if (attenuation < 1e-3)
162 continue; /* this light makes no contribution */
163
164 /* Compute dot product or normal and vector from V to light pos */
165 n_dot_VP = DOT3( normal, VP );
166
167 /* Which side gets the diffuse & specular terms? */
168 if (n_dot_VP < 0.0F) {
169 ACC_SCALE_SCALAR_3V(sum[0], attenuation, light->_MatAmbient[0]);
170 if (!(IDX & LIGHT_TWOSIDE)) {
171 continue;
172 }
173 side = 1;
174 correction = -1;
175 n_dot_VP = -n_dot_VP;
176 }
177 else {
178 if (IDX & LIGHT_TWOSIDE) {
179 ACC_SCALE_SCALAR_3V( sum[1], attenuation, light->_MatAmbient[1]);
180 }
181 side = 0;
182 correction = 1;
183 }
184
185 /* diffuse term */
186 COPY_3V(contrib, light->_MatAmbient[side]);
187 ACC_SCALE_SCALAR_3V(contrib, n_dot_VP, light->_MatDiffuse[side]);
188 ACC_SCALE_SCALAR_3V(sum[side], attenuation, contrib );
189
190 /* specular term - cannibalize VP... */
191 if (ctx->Light.Model.LocalViewer) {
192 GLfloat v[3];
193 COPY_3V(v, vertex);
194 NORMALIZE_3FV(v);
195 SUB_3V(VP, VP, v); /* h = VP + VPe */
196 h = VP;
197 NORMALIZE_3FV(h);
198 }
199 else if (light->_Flags & LIGHT_POSITIONAL) {
200 h = VP;
201 ACC_3V(h, ctx->_EyeZDir);
202 NORMALIZE_3FV(h);
203 }
204 else {
205 h = light->_h_inf_norm;
206 }
207
208 n_dot_h = correction * DOT3(normal, h);
209
210 if (n_dot_h > 0.0F) {
211 GLfloat spec_coef;
212 struct gl_shine_tab *tab = ctx->_ShineTable[side];
213 GET_SHINE_TAB_ENTRY( tab, n_dot_h, spec_coef );
214
215 if (spec_coef > 1.0e-10) {
216 spec_coef *= attenuation;
217 ACC_SCALE_SCALAR_3V( spec[side], spec_coef,
218 light->_MatSpecular[side]);
219 }
220 }
221 } /*loop over lights*/
222
223 COPY_3V( Fcolor[j], sum[0] );
224 COPY_3V( Fspec[j], spec[0] );
225 Fcolor[j][3] = sumA[0];
226
227 if (IDX & LIGHT_TWOSIDE) {
228 COPY_3V( Bcolor[j], sum[1] );
229 COPY_3V( Bspec[j], spec[1] );
230 Bcolor[j][3] = sumA[1];
231 }
232 }
233 }
234
235
236 static void TAG(light_rgba)( GLcontext *ctx,
237 struct vertex_buffer *VB,
238 struct tnl_pipeline_stage *stage,
239 GLvector4f *input )
240 {
241 struct light_stage_data *store = LIGHT_STAGE_DATA(stage);
242 GLuint j;
243
244 GLfloat (*base)[3] = ctx->Light._BaseColor;
245 GLfloat sumA[2];
246
247 const GLuint vstride = input->stride;
248 const GLfloat *vertex = (GLfloat *) input->data;
249 const GLuint nstride = VB->NormalPtr->stride;
250 const GLfloat *normal = (GLfloat *)VB->NormalPtr->data;
251
252 GLfloat (*Fcolor)[4] = (GLfloat (*)[4]) store->LitColor[0].data;
253 GLfloat (*Bcolor)[4] = (GLfloat (*)[4]) store->LitColor[1].data;
254 GLfloat (*color[2])[4];
255
256 const GLuint nr = VB->Count;
257
258 #ifdef TRACE
259 fprintf(stderr, "%s\n", __FUNCTION__ );
260 #endif
261
262 (void) nstride;
263 (void) vstride;
264
265 color[0] = Fcolor;
266 color[1] = Bcolor;
267
268 VB->ColorPtr[0] = &store->LitColor[0];
269 sumA[0] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE][3];
270
271 if (IDX & LIGHT_TWOSIDE) {
272 VB->ColorPtr[1] = &store->LitColor[1];
273 sumA[1] = ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_DIFFUSE][3];
274 }
275
276 if (stage->changed_inputs == 0)
277 return;
278
279 for (j = 0; j < nr; j++,STRIDE_F(vertex,vstride),STRIDE_F(normal,nstride)) {
280 GLfloat sum[2][3];
281 struct gl_light *light;
282
283 if ( IDX & LIGHT_MATERIAL ) {
284 update_materials( ctx, store );
285 sumA[0] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE][3];
286 if (IDX & LIGHT_TWOSIDE)
287 sumA[1] = ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_DIFFUSE][3];
288 }
289
290 COPY_3V(sum[0], base[0]);
291
292 if ( IDX & LIGHT_TWOSIDE )
293 COPY_3V(sum[1], base[1]);
294
295 /* Add contribution from each enabled light source */
296 foreach (light, &ctx->Light.EnabledList) {
297
298 GLfloat n_dot_h;
299 GLfloat correction;
300 GLint side;
301 GLfloat contrib[3];
302 GLfloat attenuation = 1.0;
303 GLfloat VP[3]; /* unit vector from vertex to light */
304 GLfloat n_dot_VP; /* n dot VP */
305 GLfloat *h;
306
307 /* compute VP and attenuation */
308 if (!(light->_Flags & LIGHT_POSITIONAL)) {
309 /* directional light */
310 COPY_3V(VP, light->_VP_inf_norm);
311 attenuation = light->_VP_inf_spot_attenuation;
312 }
313 else {
314 GLfloat d; /* distance from vertex to light */
315
316
317 SUB_3V(VP, light->_Position, vertex);
318
319 d = (GLfloat) LEN_3FV( VP );
320
321 if ( d > 1e-6) {
322 GLfloat invd = 1.0F / d;
323 SELF_SCALE_SCALAR_3V(VP, invd);
324 }
325
326 attenuation = 1.0F / (light->ConstantAttenuation + d *
327 (light->LinearAttenuation + d *
328 light->QuadraticAttenuation));
329
330 /* spotlight attenuation */
331 if (light->_Flags & LIGHT_SPOT) {
332 GLfloat PV_dot_dir = - DOT3(VP, light->_NormDirection);
333
334 if (PV_dot_dir<light->_CosCutoff) {
335 continue; /* this light makes no contribution */
336 }
337 else {
338 GLdouble x = PV_dot_dir * (EXP_TABLE_SIZE-1);
339 GLint k = (GLint) x;
340 GLfloat spot = (GLfloat) (light->_SpotExpTable[k][0]
341 + (x-k)*light->_SpotExpTable[k][1]);
342 attenuation *= spot;
343 }
344 }
345 }
346
347 if (attenuation < 1e-3)
348 continue; /* this light makes no contribution */
349
350 /* Compute dot product or normal and vector from V to light pos */
351 n_dot_VP = DOT3( normal, VP );
352
353 /* which side are we lighting? */
354 if (n_dot_VP < 0.0F) {
355 ACC_SCALE_SCALAR_3V(sum[0], attenuation, light->_MatAmbient[0]);
356
357 if (!(IDX & LIGHT_TWOSIDE))
358 continue;
359
360 side = 1;
361 correction = -1;
362 n_dot_VP = -n_dot_VP;
363 }
364 else {
365 if (IDX & LIGHT_TWOSIDE) {
366 ACC_SCALE_SCALAR_3V( sum[1], attenuation, light->_MatAmbient[1]);
367 }
368 side = 0;
369 correction = 1;
370 }
371
372 COPY_3V(contrib, light->_MatAmbient[side]);
373
374 /* diffuse term */
375 ACC_SCALE_SCALAR_3V(contrib, n_dot_VP, light->_MatDiffuse[side]);
376
377 /* specular term - cannibalize VP... */
378 {
379 if (ctx->Light.Model.LocalViewer) {
380 GLfloat v[3];
381 COPY_3V(v, vertex);
382 NORMALIZE_3FV(v);
383 SUB_3V(VP, VP, v); /* h = VP + VPe */
384 h = VP;
385 NORMALIZE_3FV(h);
386 }
387 else if (light->_Flags & LIGHT_POSITIONAL) {
388 h = VP;
389 ACC_3V(h, ctx->_EyeZDir);
390 NORMALIZE_3FV(h);
391 }
392 else {
393 h = light->_h_inf_norm;
394 }
395
396 n_dot_h = correction * DOT3(normal, h);
397
398 if (n_dot_h > 0.0F)
399 {
400 GLfloat spec_coef;
401 struct gl_shine_tab *tab = ctx->_ShineTable[side];
402
403 GET_SHINE_TAB_ENTRY( tab, n_dot_h, spec_coef );
404
405 ACC_SCALE_SCALAR_3V( contrib, spec_coef,
406 light->_MatSpecular[side]);
407 }
408 }
409
410 ACC_SCALE_SCALAR_3V( sum[side], attenuation, contrib );
411 }
412
413 COPY_3V( Fcolor[j], sum[0] );
414 Fcolor[j][3] = sumA[0];
415
416 if (IDX & LIGHT_TWOSIDE) {
417 COPY_3V( Bcolor[j], sum[1] );
418 Bcolor[j][3] = sumA[1];
419 }
420 }
421 }
422
423
424
425
426 /* As below, but with just a single light.
427 */
428 static void TAG(light_fast_rgba_single)( GLcontext *ctx,
429 struct vertex_buffer *VB,
430 struct tnl_pipeline_stage *stage,
431 GLvector4f *input )
432
433 {
434 struct light_stage_data *store = LIGHT_STAGE_DATA(stage);
435 const GLuint nstride = VB->NormalPtr->stride;
436 const GLfloat *normal = (GLfloat *)VB->NormalPtr->data;
437 GLfloat (*Fcolor)[4] = (GLfloat (*)[4]) store->LitColor[0].data;
438 GLfloat (*Bcolor)[4] = (GLfloat (*)[4]) store->LitColor[1].data;
439 const struct gl_light *light = ctx->Light.EnabledList.next;
440 GLuint j = 0;
441 GLfloat base[2][3];
442 const GLuint nr = VB->Count;
443
444 #ifdef TRACE
445 fprintf(stderr, "%s\n", __FUNCTION__ );
446 #endif
447
448 (void) input; /* doesn't refer to Eye or Obj */
449 (void) nr;
450 (void) nstride;
451
452 VB->ColorPtr[0] = &store->LitColor[0];
453 if (IDX & LIGHT_TWOSIDE)
454 VB->ColorPtr[1] = &store->LitColor[1];
455
456 if (stage->changed_inputs == 0)
457 return;
458
459 for (j = 0; j < nr; j++, STRIDE_F(normal,nstride)) {
460
461 GLfloat n_dot_VP;
462
463 if ( IDX & LIGHT_MATERIAL )
464 update_materials( ctx, store );
465
466 /* No attenuation, so incoporate _MatAmbient into base color.
467 */
468 if ( j == 0 || (IDX & LIGHT_MATERIAL) ) {
469 COPY_3V(base[0], light->_MatAmbient[0]);
470 ACC_3V(base[0], ctx->Light._BaseColor[0] );
471 base[0][3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE][3];
472
473 if (IDX & LIGHT_TWOSIDE) {
474 COPY_3V(base[1], light->_MatAmbient[1]);
475 ACC_3V(base[1], ctx->Light._BaseColor[1]);
476 base[1][3] = ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_DIFFUSE][3];
477 }
478 }
479
480 n_dot_VP = DOT3(normal, light->_VP_inf_norm);
481
482 if (n_dot_VP < 0.0F) {
483 if (IDX & LIGHT_TWOSIDE) {
484 GLfloat n_dot_h = -DOT3(normal, light->_h_inf_norm);
485 GLfloat sum[3];
486 COPY_3V(sum, base[1]);
487 ACC_SCALE_SCALAR_3V(sum, -n_dot_VP, light->_MatDiffuse[1]);
488 if (n_dot_h > 0.0F) {
489 GLfloat spec;
490 GET_SHINE_TAB_ENTRY( ctx->_ShineTable[1], n_dot_h, spec );
491 ACC_SCALE_SCALAR_3V(sum, spec, light->_MatSpecular[1]);
492 }
493 COPY_3V(Bcolor[j], sum );
494 Bcolor[j][3] = base[1][3];
495 }
496 COPY_4FV(Fcolor[j], base[0]);
497 }
498 else {
499 GLfloat n_dot_h = DOT3(normal, light->_h_inf_norm);
500 GLfloat sum[3];
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;
505 GET_SHINE_TAB_ENTRY( ctx->_ShineTable[0], n_dot_h, spec );
506 ACC_SCALE_SCALAR_3V(sum, spec, light->_MatSpecular[0]);
507
508 }
509 COPY_3V(Fcolor[j], sum );
510 Fcolor[j][3] = base[0][3];
511 if (IDX & LIGHT_TWOSIDE) COPY_4FV(Bcolor[j], base[1]);
512 }
513 }
514 }
515
516
517 /* Light infinite lights
518 */
519 static void TAG(light_fast_rgba)( GLcontext *ctx,
520 struct vertex_buffer *VB,
521 struct tnl_pipeline_stage *stage,
522 GLvector4f *input )
523 {
524 struct light_stage_data *store = LIGHT_STAGE_DATA(stage);
525 GLfloat sumA[2];
526 const GLuint nstride = VB->NormalPtr->stride;
527 const GLfloat *normal = (GLfloat *)VB->NormalPtr->data;
528 GLfloat (*Fcolor)[4] = (GLfloat (*)[4]) store->LitColor[0].data;
529 GLfloat (*Bcolor)[4] = (GLfloat (*)[4]) store->LitColor[1].data;
530 GLuint j = 0;
531 const GLuint nr = VB->Count;
532 const struct gl_light *light;
533
534 #ifdef TRACE
535 fprintf(stderr, "%s %d\n", __FUNCTION__, nr );
536 #endif
537
538 (void) input;
539 (void) nr;
540 (void) nstride;
541
542 sumA[0] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE][3];
543 sumA[1] = ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_DIFFUSE][3];
544
545 VB->ColorPtr[0] = &store->LitColor[0];
546 if (IDX & LIGHT_TWOSIDE)
547 VB->ColorPtr[1] = &store->LitColor[1];
548
549 if (stage->changed_inputs == 0)
550 return;
551
552 for (j = 0; j < nr; j++, STRIDE_F(normal,nstride)) {
553
554 GLfloat sum[2][3];
555
556 if ( IDX & LIGHT_MATERIAL ) {
557 update_materials( ctx, store );
558
559 sumA[0] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE][3];
560 if (IDX & LIGHT_TWOSIDE)
561 sumA[1] = ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_DIFFUSE][3];
562 }
563
564
565 COPY_3V(sum[0], ctx->Light._BaseColor[0]);
566 if (IDX & LIGHT_TWOSIDE)
567 COPY_3V(sum[1], ctx->Light._BaseColor[1]);
568
569 foreach (light, &ctx->Light.EnabledList) {
570 GLfloat n_dot_h, n_dot_VP, spec;
571
572 ACC_3V(sum[0], light->_MatAmbient[0]);
573 if (IDX & LIGHT_TWOSIDE)
574 ACC_3V(sum[1], light->_MatAmbient[1]);
575
576 n_dot_VP = DOT3(normal, light->_VP_inf_norm);
577
578 if (n_dot_VP > 0.0F) {
579 ACC_SCALE_SCALAR_3V(sum[0], n_dot_VP, light->_MatDiffuse[0]);
580 n_dot_h = DOT3(normal, light->_h_inf_norm);
581 if (n_dot_h > 0.0F) {
582 struct gl_shine_tab *tab = ctx->_ShineTable[0];
583 GET_SHINE_TAB_ENTRY( tab, n_dot_h, spec );
584 ACC_SCALE_SCALAR_3V( sum[0], spec, light->_MatSpecular[0]);
585 }
586 }
587 else if (IDX & LIGHT_TWOSIDE) {
588 ACC_SCALE_SCALAR_3V(sum[1], -n_dot_VP, light->_MatDiffuse[1]);
589 n_dot_h = -DOT3(normal, light->_h_inf_norm);
590 if (n_dot_h > 0.0F) {
591 struct gl_shine_tab *tab = ctx->_ShineTable[1];
592 GET_SHINE_TAB_ENTRY( tab, n_dot_h, spec );
593 ACC_SCALE_SCALAR_3V( sum[1], spec, light->_MatSpecular[1]);
594 }
595 }
596 }
597
598 COPY_3V( Fcolor[j], sum[0] );
599 Fcolor[j][3] = sumA[0];
600
601 if (IDX & LIGHT_TWOSIDE) {
602 COPY_3V( Bcolor[j], sum[1] );
603 Bcolor[j][3] = sumA[1];
604 }
605 }
606 }
607
608
609
610
611
612 /*
613 * Use current lighting/material settings to compute the color indexes
614 * for an array of vertices.
615 * Input: n - number of vertices to light
616 * side - 0=use front material, 1=use back material
617 * vertex - array of [n] vertex position in eye coordinates
618 * normal - array of [n] surface normal vector
619 * Output: indexResult - resulting array of [n] color indexes
620 */
621 static void TAG(light_ci)( GLcontext *ctx,
622 struct vertex_buffer *VB,
623 struct tnl_pipeline_stage *stage,
624 GLvector4f *input )
625 {
626 struct light_stage_data *store = LIGHT_STAGE_DATA(stage);
627 GLuint j;
628 const GLuint vstride = input->stride;
629 const GLfloat *vertex = (GLfloat *) input->data;
630 const GLuint nstride = VB->NormalPtr->stride;
631 const GLfloat *normal = (GLfloat *)VB->NormalPtr->data;
632 GLfloat *indexResult[2];
633 const GLuint nr = VB->Count;
634
635 #ifdef TRACE
636 fprintf(stderr, "%s\n", __FUNCTION__ );
637 #endif
638
639 (void) nstride;
640 (void) vstride;
641
642 VB->IndexPtr[0] = &store->LitIndex[0];
643 if (IDX & LIGHT_TWOSIDE)
644 VB->IndexPtr[1] = &store->LitIndex[1];
645
646 if (stage->changed_inputs == 0)
647 return;
648
649 indexResult[0] = (GLfloat *)VB->IndexPtr[0]->data;
650 if (IDX & LIGHT_TWOSIDE)
651 indexResult[1] = (GLfloat *)VB->IndexPtr[1]->data;
652
653 /* loop over vertices */
654 for (j=0; j<nr; j++,STRIDE_F(vertex,vstride),STRIDE_F(normal, nstride)) {
655 GLfloat diffuse[2], specular[2];
656 GLuint side = 0;
657 struct gl_light *light;
658
659 if ( IDX & LIGHT_MATERIAL )
660 update_materials( ctx, store );
661
662 diffuse[0] = specular[0] = 0.0F;
663
664 if ( IDX & LIGHT_TWOSIDE ) {
665 diffuse[1] = specular[1] = 0.0F;
666 }
667
668 /* Accumulate diffuse and specular from each light source */
669 foreach (light, &ctx->Light.EnabledList) {
670
671 GLfloat attenuation = 1.0F;
672 GLfloat VP[3]; /* unit vector from vertex to light */
673 GLfloat n_dot_VP; /* dot product of l and n */
674 GLfloat *h, n_dot_h, correction = 1.0;
675
676 /* compute l and attenuation */
677 if (!(light->_Flags & LIGHT_POSITIONAL)) {
678 /* directional light */
679 COPY_3V(VP, light->_VP_inf_norm);
680 }
681 else {
682 GLfloat d; /* distance from vertex to light */
683
684 SUB_3V(VP, light->_Position, vertex);
685
686 d = (GLfloat) LEN_3FV( VP );
687 if ( d > 1e-6) {
688 GLfloat invd = 1.0F / d;
689 SELF_SCALE_SCALAR_3V(VP, invd);
690 }
691
692 attenuation = 1.0F / (light->ConstantAttenuation + d *
693 (light->LinearAttenuation + d *
694 light->QuadraticAttenuation));
695
696 /* spotlight attenuation */
697 if (light->_Flags & LIGHT_SPOT) {
698 GLfloat PV_dot_dir = - DOT3(VP, light->_NormDirection);
699 if (PV_dot_dir < light->_CosCutoff) {
700 continue; /* this light makes no contribution */
701 }
702 else {
703 GLdouble x = PV_dot_dir * (EXP_TABLE_SIZE-1);
704 GLint k = (GLint) x;
705 GLfloat spot = (GLfloat) (light->_SpotExpTable[k][0]
706 + (x-k)*light->_SpotExpTable[k][1]);
707 attenuation *= spot;
708 }
709 }
710 }
711
712 if (attenuation < 1e-3)
713 continue; /* this light makes no contribution */
714
715 n_dot_VP = DOT3( normal, VP );
716
717 /* which side are we lighting? */
718 if (n_dot_VP < 0.0F) {
719 if (!(IDX & LIGHT_TWOSIDE))
720 continue;
721 side = 1;
722 correction = -1;
723 n_dot_VP = -n_dot_VP;
724 }
725
726 /* accumulate diffuse term */
727 diffuse[side] += n_dot_VP * light->_dli * attenuation;
728
729 /* specular term */
730 if (ctx->Light.Model.LocalViewer) {
731 GLfloat v[3];
732 COPY_3V(v, vertex);
733 NORMALIZE_3FV(v);
734 SUB_3V(VP, VP, v); /* h = VP + VPe */
735 h = VP;
736 NORMALIZE_3FV(h);
737 }
738 else if (light->_Flags & LIGHT_POSITIONAL) {
739 h = VP;
740 /* Strangely, disabling this addition fixes a conformance
741 * problem. If this code is enabled, l_sed.c fails.
742 */
743 /*ACC_3V(h, ctx->_EyeZDir);*/
744 NORMALIZE_3FV(h);
745 }
746 else {
747 h = light->_h_inf_norm;
748 }
749
750 n_dot_h = correction * DOT3(normal, h);
751 if (n_dot_h > 0.0F) {
752 GLfloat spec_coef;
753 struct gl_shine_tab *tab = ctx->_ShineTable[side];
754 GET_SHINE_TAB_ENTRY( tab, n_dot_h, spec_coef);
755 specular[side] += spec_coef * light->_sli * attenuation;
756 }
757 } /*loop over lights*/
758
759 /* Now compute final color index */
760 for (side = 0 ; side < NR_SIDES ; side++) {
761 const GLfloat *ind = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_INDEXES + side];
762 GLfloat index;
763
764 if (specular[side] > 1.0F) {
765 index = ind[MAT_INDEX_SPECULAR];
766 }
767 else {
768 GLfloat d_a = ind[MAT_INDEX_DIFFUSE] - ind[MAT_INDEX_AMBIENT];
769 GLfloat s_a = ind[MAT_INDEX_SPECULAR] - ind[MAT_INDEX_AMBIENT];
770 index = (ind[MAT_INDEX_AMBIENT]
771 + diffuse[side] * (1.0F-specular[side]) * d_a
772 + specular[side] * s_a);
773 if (index > ind[MAT_INDEX_SPECULAR]) {
774 index = ind[MAT_INDEX_SPECULAR];
775 }
776 }
777 indexResult[side][j] = index;
778 }
779 } /*for vertex*/
780 }
781
782
783
784 static void TAG(init_light_tab)( void )
785 {
786 _tnl_light_tab[IDX] = TAG(light_rgba);
787 _tnl_light_fast_tab[IDX] = TAG(light_fast_rgba);
788 _tnl_light_fast_single_tab[IDX] = TAG(light_fast_rgba_single);
789 _tnl_light_spec_tab[IDX] = TAG(light_rgba_spec);
790 _tnl_light_ci_tab[IDX] = TAG(light_ci);
791 }
792
793
794 #undef TAG
795 #undef IDX
796 #undef NR_SIDES