17117f1b4Smrg/* 27117f1b4Smrg * Mesa 3-D graphics library 37117f1b4Smrg * 47117f1b4Smrg * Copyright (C) 1999-2003 Brian Paul All Rights Reserved. 57117f1b4Smrg * 67117f1b4Smrg * Permission is hereby granted, free of charge, to any person obtaining a 77117f1b4Smrg * copy of this software and associated documentation files (the "Software"), 87117f1b4Smrg * to deal in the Software without restriction, including without limitation 97117f1b4Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 107117f1b4Smrg * and/or sell copies of the Software, and to permit persons to whom the 117117f1b4Smrg * Software is furnished to do so, subject to the following conditions: 127117f1b4Smrg * 137117f1b4Smrg * The above copyright notice and this permission notice shall be included 147117f1b4Smrg * in all copies or substantial portions of the Software. 157117f1b4Smrg * 167117f1b4Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 177117f1b4Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 187117f1b4Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19af69d88dSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20af69d88dSmrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21af69d88dSmrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22af69d88dSmrg * OTHER DEALINGS IN THE SOFTWARE. 237117f1b4Smrg * 247117f1b4Smrg * 257117f1b4Smrg * Authors: 26af69d88dSmrg * Brian Paul Keith Whitwell <keithw@vmware.com> 277117f1b4Smrg */ 287117f1b4Smrg 297117f1b4Smrg 307117f1b4Smrg#if IDX & LIGHT_TWOSIDE 317117f1b4Smrg# define NR_SIDES 2 327117f1b4Smrg#else 337117f1b4Smrg# define NR_SIDES 1 347117f1b4Smrg#endif 357117f1b4Smrg 367117f1b4Smrg 377117f1b4Smrg/* define TRACE to trace lighting code */ 387117f1b4Smrg/* #define TRACE 1 */ 397117f1b4Smrg 407117f1b4Smrg/* 417117f1b4Smrg * ctx is the current context 427117f1b4Smrg * VB is the vertex buffer 437117f1b4Smrg * stage is the lighting stage-private data 447117f1b4Smrg * input is the vector of eye or object-space vertex coordinates 457117f1b4Smrg */ 463464ebd5Sriastradhstatic void TAG(light_rgba_spec)( struct gl_context *ctx, 477117f1b4Smrg struct vertex_buffer *VB, 487117f1b4Smrg struct tnl_pipeline_stage *stage, 497117f1b4Smrg GLvector4f *input ) 507117f1b4Smrg{ 517117f1b4Smrg struct light_stage_data *store = LIGHT_STAGE_DATA(stage); 527117f1b4Smrg GLfloat (*base)[3] = ctx->Light._BaseColor; 537117f1b4Smrg GLfloat sumA[2]; 547117f1b4Smrg GLuint j; 557117f1b4Smrg 567117f1b4Smrg const GLuint vstride = input->stride; 577117f1b4Smrg const GLfloat *vertex = (GLfloat *)input->data; 587117f1b4Smrg const GLuint nstride = VB->AttribPtr[_TNL_ATTRIB_NORMAL]->stride; 597117f1b4Smrg const GLfloat *normal = (GLfloat *)VB->AttribPtr[_TNL_ATTRIB_NORMAL]->data; 607117f1b4Smrg 617117f1b4Smrg GLfloat (*Fcolor)[4] = (GLfloat (*)[4]) store->LitColor[0].data; 627117f1b4Smrg GLfloat (*Fspec)[4] = (GLfloat (*)[4]) store->LitSecondary[0].data; 637117f1b4Smrg#if IDX & LIGHT_TWOSIDE 647117f1b4Smrg GLfloat (*Bcolor)[4] = (GLfloat (*)[4]) store->LitColor[1].data; 657117f1b4Smrg GLfloat (*Bspec)[4] = (GLfloat (*)[4]) store->LitSecondary[1].data; 667117f1b4Smrg#endif 677117f1b4Smrg 687117f1b4Smrg const GLuint nr = VB->Count; 697117f1b4Smrg 707117f1b4Smrg#ifdef TRACE 7101e04c3fSmrg fprintf(stderr, "%s\n", __func__ ); 727117f1b4Smrg#endif 737117f1b4Smrg 74cdc920a0Smrg VB->AttribPtr[_TNL_ATTRIB_COLOR0] = &store->LitColor[0]; 75cdc920a0Smrg VB->AttribPtr[_TNL_ATTRIB_COLOR1] = &store->LitSecondary[0]; 767117f1b4Smrg sumA[0] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE][3]; 777117f1b4Smrg 787117f1b4Smrg#if IDX & LIGHT_TWOSIDE 79cdc920a0Smrg VB->BackfaceColorPtr = &store->LitColor[1]; 80cdc920a0Smrg VB->BackfaceSecondaryColorPtr = &store->LitSecondary[1]; 817117f1b4Smrg sumA[1] = ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_DIFFUSE][3]; 827117f1b4Smrg#endif 837117f1b4Smrg 847117f1b4Smrg 857117f1b4Smrg store->LitColor[0].stride = 16; 867117f1b4Smrg store->LitColor[1].stride = 16; 877117f1b4Smrg 887117f1b4Smrg for (j = 0; j < nr; j++,STRIDE_F(vertex,vstride),STRIDE_F(normal,nstride)) { 897117f1b4Smrg GLfloat sum[2][3], spec[2][3]; 9001e04c3fSmrg GLbitfield mask; 917117f1b4Smrg 927117f1b4Smrg#if IDX & LIGHT_MATERIAL 937117f1b4Smrg update_materials( ctx, store ); 947117f1b4Smrg sumA[0] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE][3]; 957117f1b4Smrg#if IDX & LIGHT_TWOSIDE 967117f1b4Smrg sumA[1] = ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_DIFFUSE][3]; 977117f1b4Smrg#endif 987117f1b4Smrg#endif 997117f1b4Smrg 1007117f1b4Smrg COPY_3V(sum[0], base[0]); 1017117f1b4Smrg ZERO_3V(spec[0]); 1027117f1b4Smrg 1037117f1b4Smrg#if IDX & LIGHT_TWOSIDE 1047117f1b4Smrg COPY_3V(sum[1], base[1]); 1057117f1b4Smrg ZERO_3V(spec[1]); 1067117f1b4Smrg#endif 1077117f1b4Smrg 1087117f1b4Smrg /* Add contribution from each enabled light source */ 10901e04c3fSmrg mask = ctx->Light._EnabledLights; 11001e04c3fSmrg while (mask) { 11101e04c3fSmrg const int l = u_bit_scan(&mask); 11201e04c3fSmrg struct gl_light *light = &ctx->Light.Light[l]; 1137ec681f3Smrg struct gl_light_uniforms *lu = &ctx->Light.LightSource[l]; 1147117f1b4Smrg GLfloat n_dot_h; 1157117f1b4Smrg GLfloat correction; 1167117f1b4Smrg GLint side; 1177117f1b4Smrg GLfloat contrib[3]; 1187117f1b4Smrg GLfloat attenuation; 11901e04c3fSmrg GLfloat VP[3]; /* unit vector from vertex to light */ 1207117f1b4Smrg GLfloat n_dot_VP; /* n dot VP */ 1217117f1b4Smrg GLfloat *h; 1227117f1b4Smrg 1237117f1b4Smrg /* compute VP and attenuation */ 1247117f1b4Smrg if (!(light->_Flags & LIGHT_POSITIONAL)) { 1257117f1b4Smrg /* directional light */ 1267117f1b4Smrg COPY_3V(VP, light->_VP_inf_norm); 1277117f1b4Smrg attenuation = light->_VP_inf_spot_attenuation; 1287117f1b4Smrg } 1297117f1b4Smrg else { 1307117f1b4Smrg GLfloat d; /* distance from vertex to light */ 1317117f1b4Smrg 1327117f1b4Smrg SUB_3V(VP, light->_Position, vertex); 1337117f1b4Smrg 1347117f1b4Smrg d = (GLfloat) LEN_3FV( VP ); 1357117f1b4Smrg 13601e04c3fSmrg if (d > 1e-6F) { 1377117f1b4Smrg GLfloat invd = 1.0F / d; 1387117f1b4Smrg SELF_SCALE_SCALAR_3V(VP, invd); 1397117f1b4Smrg } 1407117f1b4Smrg 1417ec681f3Smrg attenuation = 1.0F / (lu->ConstantAttenuation + d * 1427ec681f3Smrg (lu->LinearAttenuation + d * 1437ec681f3Smrg lu->QuadraticAttenuation)); 1447117f1b4Smrg 1457117f1b4Smrg /* spotlight attenuation */ 1467117f1b4Smrg if (light->_Flags & LIGHT_SPOT) { 1474a49301eSmrg GLfloat PV_dot_dir = - DOT3(VP, light->_NormSpotDirection); 1487117f1b4Smrg 1497ec681f3Smrg if (PV_dot_dir<lu->_CosCutoff) { 1507117f1b4Smrg continue; /* this light makes no contribution */ 1517117f1b4Smrg } 1527117f1b4Smrg else { 1537ec681f3Smrg GLfloat spot = powf(PV_dot_dir, lu->SpotExponent); 1547117f1b4Smrg attenuation *= spot; 1557117f1b4Smrg } 1567117f1b4Smrg } 1577117f1b4Smrg } 1587117f1b4Smrg 15901e04c3fSmrg if (attenuation < 1e-3F) 1607117f1b4Smrg continue; /* this light makes no contribution */ 1617117f1b4Smrg 1627117f1b4Smrg /* Compute dot product or normal and vector from V to light pos */ 1637117f1b4Smrg n_dot_VP = DOT3( normal, VP ); 1647117f1b4Smrg 1657117f1b4Smrg /* Which side gets the diffuse & specular terms? */ 1667117f1b4Smrg if (n_dot_VP < 0.0F) { 1677117f1b4Smrg ACC_SCALE_SCALAR_3V(sum[0], attenuation, light->_MatAmbient[0]); 1687117f1b4Smrg#if IDX & LIGHT_TWOSIDE 1697117f1b4Smrg side = 1; 1707117f1b4Smrg correction = -1; 1717117f1b4Smrg n_dot_VP = -n_dot_VP; 1727117f1b4Smrg#else 1737117f1b4Smrg continue; 1747117f1b4Smrg#endif 1757117f1b4Smrg } 1767117f1b4Smrg else { 1777117f1b4Smrg#if IDX & LIGHT_TWOSIDE 1787117f1b4Smrg ACC_SCALE_SCALAR_3V( sum[1], attenuation, light->_MatAmbient[1]); 1797117f1b4Smrg#endif 1807117f1b4Smrg side = 0; 1817117f1b4Smrg correction = 1; 1827117f1b4Smrg } 1837117f1b4Smrg 1847117f1b4Smrg /* diffuse term */ 1857117f1b4Smrg COPY_3V(contrib, light->_MatAmbient[side]); 1867117f1b4Smrg ACC_SCALE_SCALAR_3V(contrib, n_dot_VP, light->_MatDiffuse[side]); 1877117f1b4Smrg ACC_SCALE_SCALAR_3V(sum[side], attenuation, contrib ); 1887117f1b4Smrg 1897117f1b4Smrg /* specular term - cannibalize VP... */ 1907117f1b4Smrg if (ctx->Light.Model.LocalViewer) { 1917117f1b4Smrg GLfloat v[3]; 1927117f1b4Smrg COPY_3V(v, vertex); 1937117f1b4Smrg NORMALIZE_3FV(v); 1947117f1b4Smrg SUB_3V(VP, VP, v); /* h = VP + VPe */ 1957117f1b4Smrg h = VP; 1967117f1b4Smrg NORMALIZE_3FV(h); 1977117f1b4Smrg } 1987117f1b4Smrg else if (light->_Flags & LIGHT_POSITIONAL) { 1997117f1b4Smrg h = VP; 2007117f1b4Smrg ACC_3V(h, ctx->_EyeZDir); 2017117f1b4Smrg NORMALIZE_3FV(h); 2027117f1b4Smrg } 2037117f1b4Smrg else { 2047117f1b4Smrg h = light->_h_inf_norm; 2057117f1b4Smrg } 2067117f1b4Smrg 2077117f1b4Smrg n_dot_h = correction * DOT3(normal, h); 2087117f1b4Smrg 2097117f1b4Smrg if (n_dot_h > 0.0F) { 210af69d88dSmrg GLfloat spec_coef = lookup_shininess(ctx, side, n_dot_h); 21101e04c3fSmrg if (spec_coef > 1.0e-10F) { 2127117f1b4Smrg spec_coef *= attenuation; 2137117f1b4Smrg ACC_SCALE_SCALAR_3V( spec[side], spec_coef, 2147117f1b4Smrg light->_MatSpecular[side]); 2157117f1b4Smrg } 2167117f1b4Smrg } 2177117f1b4Smrg } /*loop over lights*/ 2187117f1b4Smrg 2197117f1b4Smrg COPY_3V( Fcolor[j], sum[0] ); 2207117f1b4Smrg COPY_3V( Fspec[j], spec[0] ); 2217117f1b4Smrg Fcolor[j][3] = sumA[0]; 2227117f1b4Smrg 2237117f1b4Smrg#if IDX & LIGHT_TWOSIDE 2247117f1b4Smrg COPY_3V( Bcolor[j], sum[1] ); 2257117f1b4Smrg COPY_3V( Bspec[j], spec[1] ); 2267117f1b4Smrg Bcolor[j][3] = sumA[1]; 2277117f1b4Smrg#endif 2287117f1b4Smrg } 2297117f1b4Smrg} 2307117f1b4Smrg 2317117f1b4Smrg 2323464ebd5Sriastradhstatic void TAG(light_rgba)( struct gl_context *ctx, 2337117f1b4Smrg struct vertex_buffer *VB, 2347117f1b4Smrg struct tnl_pipeline_stage *stage, 2357117f1b4Smrg GLvector4f *input ) 2367117f1b4Smrg{ 2377117f1b4Smrg struct light_stage_data *store = LIGHT_STAGE_DATA(stage); 2387117f1b4Smrg GLuint j; 2397117f1b4Smrg 2407117f1b4Smrg GLfloat (*base)[3] = ctx->Light._BaseColor; 2417117f1b4Smrg GLfloat sumA[2]; 2427117f1b4Smrg 2437117f1b4Smrg const GLuint vstride = input->stride; 2447117f1b4Smrg const GLfloat *vertex = (GLfloat *) input->data; 2457117f1b4Smrg const GLuint nstride = VB->AttribPtr[_TNL_ATTRIB_NORMAL]->stride; 2467117f1b4Smrg const GLfloat *normal = (GLfloat *)VB->AttribPtr[_TNL_ATTRIB_NORMAL]->data; 2477117f1b4Smrg 2487117f1b4Smrg GLfloat (*Fcolor)[4] = (GLfloat (*)[4]) store->LitColor[0].data; 2497117f1b4Smrg#if IDX & LIGHT_TWOSIDE 2507117f1b4Smrg GLfloat (*Bcolor)[4] = (GLfloat (*)[4]) store->LitColor[1].data; 2517117f1b4Smrg#endif 2527117f1b4Smrg 2537117f1b4Smrg const GLuint nr = VB->Count; 2547117f1b4Smrg 2557117f1b4Smrg#ifdef TRACE 25601e04c3fSmrg fprintf(stderr, "%s\n", __func__ ); 2577117f1b4Smrg#endif 2587117f1b4Smrg 259cdc920a0Smrg VB->AttribPtr[_TNL_ATTRIB_COLOR0] = &store->LitColor[0]; 2607117f1b4Smrg sumA[0] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE][3]; 2617117f1b4Smrg 2627117f1b4Smrg#if IDX & LIGHT_TWOSIDE 263cdc920a0Smrg VB->BackfaceColorPtr = &store->LitColor[1]; 2647117f1b4Smrg sumA[1] = ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_DIFFUSE][3]; 2657117f1b4Smrg#endif 2667117f1b4Smrg 2677117f1b4Smrg store->LitColor[0].stride = 16; 2687117f1b4Smrg store->LitColor[1].stride = 16; 2697117f1b4Smrg 2707117f1b4Smrg for (j = 0; j < nr; j++,STRIDE_F(vertex,vstride),STRIDE_F(normal,nstride)) { 2717117f1b4Smrg GLfloat sum[2][3]; 27201e04c3fSmrg GLbitfield mask; 2737117f1b4Smrg 2747117f1b4Smrg#if IDX & LIGHT_MATERIAL 2757117f1b4Smrg update_materials( ctx, store ); 2767117f1b4Smrg sumA[0] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE][3]; 2777117f1b4Smrg#if IDX & LIGHT_TWOSIDE 2787117f1b4Smrg sumA[1] = ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_DIFFUSE][3]; 2797117f1b4Smrg#endif 2807117f1b4Smrg#endif 2817117f1b4Smrg 2827117f1b4Smrg COPY_3V(sum[0], base[0]); 2837117f1b4Smrg 2847117f1b4Smrg#if IDX & LIGHT_TWOSIDE 2857117f1b4Smrg COPY_3V(sum[1], base[1]); 2867117f1b4Smrg#endif 2877117f1b4Smrg 2887117f1b4Smrg /* Add contribution from each enabled light source */ 28901e04c3fSmrg mask = ctx->Light._EnabledLights; 29001e04c3fSmrg while (mask) { 29101e04c3fSmrg const int l = u_bit_scan(&mask); 29201e04c3fSmrg struct gl_light *light = &ctx->Light.Light[l]; 2937ec681f3Smrg struct gl_light_uniforms *lu = &ctx->Light.LightSource[l]; 2947117f1b4Smrg GLfloat n_dot_h; 2957117f1b4Smrg GLfloat correction; 2967117f1b4Smrg GLint side; 2977117f1b4Smrg GLfloat contrib[3]; 29801e04c3fSmrg GLfloat attenuation; 2997117f1b4Smrg GLfloat VP[3]; /* unit vector from vertex to light */ 3007117f1b4Smrg GLfloat n_dot_VP; /* n dot VP */ 3017117f1b4Smrg GLfloat *h; 3027117f1b4Smrg 3037117f1b4Smrg /* compute VP and attenuation */ 3047117f1b4Smrg if (!(light->_Flags & LIGHT_POSITIONAL)) { 3057117f1b4Smrg /* directional light */ 3067117f1b4Smrg COPY_3V(VP, light->_VP_inf_norm); 3077117f1b4Smrg attenuation = light->_VP_inf_spot_attenuation; 3087117f1b4Smrg } 3097117f1b4Smrg else { 3107117f1b4Smrg GLfloat d; /* distance from vertex to light */ 3117117f1b4Smrg 3127117f1b4Smrg SUB_3V(VP, light->_Position, vertex); 3137117f1b4Smrg 3147117f1b4Smrg d = (GLfloat) LEN_3FV( VP ); 3157117f1b4Smrg 31601e04c3fSmrg if (d > 1e-6F) { 3177117f1b4Smrg GLfloat invd = 1.0F / d; 3187117f1b4Smrg SELF_SCALE_SCALAR_3V(VP, invd); 3197117f1b4Smrg } 3207117f1b4Smrg 3217ec681f3Smrg attenuation = 1.0F / (lu->ConstantAttenuation + d * 3227ec681f3Smrg (lu->LinearAttenuation + d * 3237ec681f3Smrg lu->QuadraticAttenuation)); 3247117f1b4Smrg 3257117f1b4Smrg /* spotlight attenuation */ 3267117f1b4Smrg if (light->_Flags & LIGHT_SPOT) { 3274a49301eSmrg GLfloat PV_dot_dir = - DOT3(VP, light->_NormSpotDirection); 3287117f1b4Smrg 3297ec681f3Smrg if (PV_dot_dir<lu->_CosCutoff) { 3307117f1b4Smrg continue; /* this light makes no contribution */ 3317117f1b4Smrg } 3327117f1b4Smrg else { 3337ec681f3Smrg GLfloat spot = powf(PV_dot_dir, lu->SpotExponent); 3347117f1b4Smrg attenuation *= spot; 3357117f1b4Smrg } 3367117f1b4Smrg } 3377117f1b4Smrg } 3387117f1b4Smrg 33901e04c3fSmrg if (attenuation < 1e-3F) 3407117f1b4Smrg continue; /* this light makes no contribution */ 3417117f1b4Smrg 3427117f1b4Smrg /* Compute dot product or normal and vector from V to light pos */ 3437117f1b4Smrg n_dot_VP = DOT3( normal, VP ); 3447117f1b4Smrg 3457117f1b4Smrg /* which side are we lighting? */ 3467117f1b4Smrg if (n_dot_VP < 0.0F) { 3477117f1b4Smrg ACC_SCALE_SCALAR_3V(sum[0], attenuation, light->_MatAmbient[0]); 3487117f1b4Smrg#if IDX & LIGHT_TWOSIDE 3497117f1b4Smrg side = 1; 3507117f1b4Smrg correction = -1; 3517117f1b4Smrg n_dot_VP = -n_dot_VP; 3527117f1b4Smrg#else 3537117f1b4Smrg continue; 3547117f1b4Smrg#endif 3557117f1b4Smrg } 3567117f1b4Smrg else { 3577117f1b4Smrg#if IDX & LIGHT_TWOSIDE 3587117f1b4Smrg ACC_SCALE_SCALAR_3V( sum[1], attenuation, light->_MatAmbient[1]); 3597117f1b4Smrg#endif 3607117f1b4Smrg side = 0; 3617117f1b4Smrg correction = 1; 3627117f1b4Smrg } 3637117f1b4Smrg 3647117f1b4Smrg COPY_3V(contrib, light->_MatAmbient[side]); 3657117f1b4Smrg 3667117f1b4Smrg /* diffuse term */ 3677117f1b4Smrg ACC_SCALE_SCALAR_3V(contrib, n_dot_VP, light->_MatDiffuse[side]); 3687117f1b4Smrg 3697117f1b4Smrg /* specular term - cannibalize VP... */ 3707117f1b4Smrg { 3717117f1b4Smrg if (ctx->Light.Model.LocalViewer) { 3727117f1b4Smrg GLfloat v[3]; 3737117f1b4Smrg COPY_3V(v, vertex); 3747117f1b4Smrg NORMALIZE_3FV(v); 3757117f1b4Smrg SUB_3V(VP, VP, v); /* h = VP + VPe */ 3767117f1b4Smrg h = VP; 3777117f1b4Smrg NORMALIZE_3FV(h); 3787117f1b4Smrg } 3797117f1b4Smrg else if (light->_Flags & LIGHT_POSITIONAL) { 3807117f1b4Smrg h = VP; 3817117f1b4Smrg ACC_3V(h, ctx->_EyeZDir); 3827117f1b4Smrg NORMALIZE_3FV(h); 3837117f1b4Smrg } 3847117f1b4Smrg else { 3857117f1b4Smrg h = light->_h_inf_norm; 3867117f1b4Smrg } 3877117f1b4Smrg 3887117f1b4Smrg n_dot_h = correction * DOT3(normal, h); 3897117f1b4Smrg 390af69d88dSmrg if (n_dot_h > 0.0F) { 391af69d88dSmrg GLfloat spec_coef = lookup_shininess(ctx, side, n_dot_h); 3927117f1b4Smrg ACC_SCALE_SCALAR_3V( contrib, spec_coef, 3937117f1b4Smrg light->_MatSpecular[side]); 3947117f1b4Smrg } 3957117f1b4Smrg } 3967117f1b4Smrg 3977117f1b4Smrg ACC_SCALE_SCALAR_3V( sum[side], attenuation, contrib ); 3987117f1b4Smrg } 3997117f1b4Smrg 4007117f1b4Smrg COPY_3V( Fcolor[j], sum[0] ); 4017117f1b4Smrg Fcolor[j][3] = sumA[0]; 4027117f1b4Smrg 4037117f1b4Smrg#if IDX & LIGHT_TWOSIDE 4047117f1b4Smrg COPY_3V( Bcolor[j], sum[1] ); 4057117f1b4Smrg Bcolor[j][3] = sumA[1]; 4067117f1b4Smrg#endif 4077117f1b4Smrg } 4087117f1b4Smrg} 4097117f1b4Smrg 4107117f1b4Smrg 4117117f1b4Smrg 4127117f1b4Smrg 4137117f1b4Smrg/* As below, but with just a single light. 4147117f1b4Smrg */ 4153464ebd5Sriastradhstatic void TAG(light_fast_rgba_single)( struct gl_context *ctx, 4167117f1b4Smrg struct vertex_buffer *VB, 4177117f1b4Smrg struct tnl_pipeline_stage *stage, 4187117f1b4Smrg GLvector4f *input ) 4197117f1b4Smrg 4207117f1b4Smrg{ 4217117f1b4Smrg struct light_stage_data *store = LIGHT_STAGE_DATA(stage); 4227117f1b4Smrg const GLuint nstride = VB->AttribPtr[_TNL_ATTRIB_NORMAL]->stride; 4237117f1b4Smrg const GLfloat *normal = (GLfloat *)VB->AttribPtr[_TNL_ATTRIB_NORMAL]->data; 4247117f1b4Smrg GLfloat (*Fcolor)[4] = (GLfloat (*)[4]) store->LitColor[0].data; 4257117f1b4Smrg#if IDX & LIGHT_TWOSIDE 4267117f1b4Smrg GLfloat (*Bcolor)[4] = (GLfloat (*)[4]) store->LitColor[1].data; 4277117f1b4Smrg#endif 42801e04c3fSmrg const struct gl_light *light = 42901e04c3fSmrg &ctx->Light.Light[ffs(ctx->Light._EnabledLights) - 1]; 4307117f1b4Smrg GLuint j = 0; 4317117f1b4Smrg GLfloat base[2][4]; 4327117f1b4Smrg#if IDX & LIGHT_MATERIAL 4337117f1b4Smrg const GLuint nr = VB->Count; 4347117f1b4Smrg#else 4357117f1b4Smrg const GLuint nr = VB->AttribPtr[_TNL_ATTRIB_NORMAL]->count; 4367117f1b4Smrg#endif 4377117f1b4Smrg 4387117f1b4Smrg#ifdef TRACE 43901e04c3fSmrg fprintf(stderr, "%s\n", __func__ ); 4407117f1b4Smrg#endif 4417117f1b4Smrg 4427117f1b4Smrg (void) input; /* doesn't refer to Eye or Obj */ 4437117f1b4Smrg 444cdc920a0Smrg VB->AttribPtr[_TNL_ATTRIB_COLOR0] = &store->LitColor[0]; 4457117f1b4Smrg#if IDX & LIGHT_TWOSIDE 446cdc920a0Smrg VB->BackfaceColorPtr = &store->LitColor[1]; 4477117f1b4Smrg#endif 4487117f1b4Smrg 4497117f1b4Smrg if (nr > 1) { 4507117f1b4Smrg store->LitColor[0].stride = 16; 4517117f1b4Smrg store->LitColor[1].stride = 16; 4527117f1b4Smrg } 4537117f1b4Smrg else { 4547117f1b4Smrg store->LitColor[0].stride = 0; 4557117f1b4Smrg store->LitColor[1].stride = 0; 4567117f1b4Smrg } 4577117f1b4Smrg 4587117f1b4Smrg for (j = 0; j < nr; j++, STRIDE_F(normal,nstride)) { 4597117f1b4Smrg 4607117f1b4Smrg GLfloat n_dot_VP; 4617117f1b4Smrg 4627117f1b4Smrg#if IDX & LIGHT_MATERIAL 4637117f1b4Smrg update_materials( ctx, store ); 4647117f1b4Smrg#endif 4657117f1b4Smrg 4667117f1b4Smrg /* No attenuation, so incoporate _MatAmbient into base color. 4677117f1b4Smrg */ 4687117f1b4Smrg#if !(IDX & LIGHT_MATERIAL) 4697117f1b4Smrg if ( j == 0 ) 4707117f1b4Smrg#endif 4717117f1b4Smrg { 4727117f1b4Smrg COPY_3V(base[0], light->_MatAmbient[0]); 4737117f1b4Smrg ACC_3V(base[0], ctx->Light._BaseColor[0] ); 4747117f1b4Smrg base[0][3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE][3]; 4757117f1b4Smrg 4767117f1b4Smrg#if IDX & LIGHT_TWOSIDE 4777117f1b4Smrg COPY_3V(base[1], light->_MatAmbient[1]); 4787117f1b4Smrg ACC_3V(base[1], ctx->Light._BaseColor[1]); 4797117f1b4Smrg base[1][3] = ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_DIFFUSE][3]; 4807117f1b4Smrg#endif 4817117f1b4Smrg } 4827117f1b4Smrg 4837117f1b4Smrg n_dot_VP = DOT3(normal, light->_VP_inf_norm); 4847117f1b4Smrg 4857117f1b4Smrg if (n_dot_VP < 0.0F) { 4867117f1b4Smrg#if IDX & LIGHT_TWOSIDE 4877117f1b4Smrg GLfloat n_dot_h = -DOT3(normal, light->_h_inf_norm); 4887117f1b4Smrg GLfloat sum[3]; 4897117f1b4Smrg COPY_3V(sum, base[1]); 4907117f1b4Smrg ACC_SCALE_SCALAR_3V(sum, -n_dot_VP, light->_MatDiffuse[1]); 4917117f1b4Smrg if (n_dot_h > 0.0F) { 492af69d88dSmrg GLfloat spec = lookup_shininess(ctx, 1, n_dot_h); 4937117f1b4Smrg ACC_SCALE_SCALAR_3V(sum, spec, light->_MatSpecular[1]); 4947117f1b4Smrg } 4957117f1b4Smrg COPY_3V(Bcolor[j], sum ); 4967117f1b4Smrg Bcolor[j][3] = base[1][3]; 4977117f1b4Smrg#endif 4987117f1b4Smrg COPY_4FV(Fcolor[j], base[0]); 4997117f1b4Smrg } 5007117f1b4Smrg else { 5017117f1b4Smrg GLfloat n_dot_h = DOT3(normal, light->_h_inf_norm); 5027117f1b4Smrg GLfloat sum[3]; 5037117f1b4Smrg COPY_3V(sum, base[0]); 5047117f1b4Smrg ACC_SCALE_SCALAR_3V(sum, n_dot_VP, light->_MatDiffuse[0]); 5057117f1b4Smrg if (n_dot_h > 0.0F) { 506af69d88dSmrg GLfloat spec = lookup_shininess(ctx, 0, n_dot_h); 5077117f1b4Smrg ACC_SCALE_SCALAR_3V(sum, spec, light->_MatSpecular[0]); 5087117f1b4Smrg } 5097117f1b4Smrg COPY_3V(Fcolor[j], sum ); 5107117f1b4Smrg Fcolor[j][3] = base[0][3]; 5117117f1b4Smrg#if IDX & LIGHT_TWOSIDE 5127117f1b4Smrg COPY_4FV(Bcolor[j], base[1]); 5137117f1b4Smrg#endif 5147117f1b4Smrg } 5157117f1b4Smrg } 5167117f1b4Smrg} 5177117f1b4Smrg 5187117f1b4Smrg 5197117f1b4Smrg/* Light infinite lights 5207117f1b4Smrg */ 5213464ebd5Sriastradhstatic void TAG(light_fast_rgba)( struct gl_context *ctx, 5227117f1b4Smrg struct vertex_buffer *VB, 5237117f1b4Smrg struct tnl_pipeline_stage *stage, 5247117f1b4Smrg GLvector4f *input ) 5257117f1b4Smrg{ 5267117f1b4Smrg struct light_stage_data *store = LIGHT_STAGE_DATA(stage); 5277117f1b4Smrg GLfloat sumA[2]; 5287117f1b4Smrg const GLuint nstride = VB->AttribPtr[_TNL_ATTRIB_NORMAL]->stride; 5297117f1b4Smrg const GLfloat *normal = (GLfloat *)VB->AttribPtr[_TNL_ATTRIB_NORMAL]->data; 5307117f1b4Smrg GLfloat (*Fcolor)[4] = (GLfloat (*)[4]) store->LitColor[0].data; 5317117f1b4Smrg#if IDX & LIGHT_TWOSIDE 5327117f1b4Smrg GLfloat (*Bcolor)[4] = (GLfloat (*)[4]) store->LitColor[1].data; 5337117f1b4Smrg#endif 5347117f1b4Smrg GLuint j = 0; 5357117f1b4Smrg#if IDX & LIGHT_MATERIAL 5367117f1b4Smrg const GLuint nr = VB->Count; 5377117f1b4Smrg#else 5387117f1b4Smrg const GLuint nr = VB->AttribPtr[_TNL_ATTRIB_NORMAL]->count; 5397117f1b4Smrg#endif 5407117f1b4Smrg 5417117f1b4Smrg#ifdef TRACE 54201e04c3fSmrg fprintf(stderr, "%s %d\n", __func__, nr ); 5437117f1b4Smrg#endif 5447117f1b4Smrg 5457117f1b4Smrg (void) input; 5467117f1b4Smrg 5477117f1b4Smrg sumA[0] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE][3]; 5487117f1b4Smrg sumA[1] = ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_DIFFUSE][3]; 5497117f1b4Smrg 550cdc920a0Smrg VB->AttribPtr[_TNL_ATTRIB_COLOR0] = &store->LitColor[0]; 5517117f1b4Smrg#if IDX & LIGHT_TWOSIDE 552cdc920a0Smrg VB->BackfaceColorPtr = &store->LitColor[1]; 5537117f1b4Smrg#endif 5547117f1b4Smrg 5557117f1b4Smrg if (nr > 1) { 5567117f1b4Smrg store->LitColor[0].stride = 16; 5577117f1b4Smrg store->LitColor[1].stride = 16; 5587117f1b4Smrg } 5597117f1b4Smrg else { 5607117f1b4Smrg store->LitColor[0].stride = 0; 5617117f1b4Smrg store->LitColor[1].stride = 0; 5627117f1b4Smrg } 5637117f1b4Smrg 5647117f1b4Smrg for (j = 0; j < nr; j++, STRIDE_F(normal,nstride)) { 5657117f1b4Smrg 5667117f1b4Smrg GLfloat sum[2][3]; 56701e04c3fSmrg GLbitfield mask; 5687117f1b4Smrg 5697117f1b4Smrg#if IDX & LIGHT_MATERIAL 5707117f1b4Smrg update_materials( ctx, store ); 5717117f1b4Smrg 5727117f1b4Smrg sumA[0] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE][3]; 5737117f1b4Smrg#if IDX & LIGHT_TWOSIDE 5747117f1b4Smrg sumA[1] = ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_DIFFUSE][3]; 5757117f1b4Smrg#endif 5767117f1b4Smrg#endif 5777117f1b4Smrg 5787117f1b4Smrg 5797117f1b4Smrg COPY_3V(sum[0], ctx->Light._BaseColor[0]); 5807117f1b4Smrg#if IDX & LIGHT_TWOSIDE 5817117f1b4Smrg COPY_3V(sum[1], ctx->Light._BaseColor[1]); 5827117f1b4Smrg#endif 5837117f1b4Smrg 58401e04c3fSmrg mask = ctx->Light._EnabledLights; 58501e04c3fSmrg while (mask) { 58601e04c3fSmrg const int l = u_bit_scan(&mask); 58701e04c3fSmrg const struct gl_light *light = &ctx->Light.Light[l]; 5887117f1b4Smrg GLfloat n_dot_h, n_dot_VP, spec; 5897117f1b4Smrg 5907117f1b4Smrg ACC_3V(sum[0], light->_MatAmbient[0]); 5917117f1b4Smrg#if IDX & LIGHT_TWOSIDE 5927117f1b4Smrg ACC_3V(sum[1], light->_MatAmbient[1]); 5937117f1b4Smrg#endif 5947117f1b4Smrg 5957117f1b4Smrg n_dot_VP = DOT3(normal, light->_VP_inf_norm); 5967117f1b4Smrg 5977117f1b4Smrg if (n_dot_VP > 0.0F) { 5987117f1b4Smrg ACC_SCALE_SCALAR_3V(sum[0], n_dot_VP, light->_MatDiffuse[0]); 5997117f1b4Smrg n_dot_h = DOT3(normal, light->_h_inf_norm); 6007117f1b4Smrg if (n_dot_h > 0.0F) { 601af69d88dSmrg spec = lookup_shininess(ctx, 0, n_dot_h); 6027117f1b4Smrg ACC_SCALE_SCALAR_3V( sum[0], spec, light->_MatSpecular[0]); 6037117f1b4Smrg } 6047117f1b4Smrg } 6057117f1b4Smrg#if IDX & LIGHT_TWOSIDE 6067117f1b4Smrg else { 6077117f1b4Smrg ACC_SCALE_SCALAR_3V(sum[1], -n_dot_VP, light->_MatDiffuse[1]); 6087117f1b4Smrg n_dot_h = -DOT3(normal, light->_h_inf_norm); 6097117f1b4Smrg if (n_dot_h > 0.0F) { 610af69d88dSmrg spec = lookup_shininess(ctx, 1, n_dot_h); 6117117f1b4Smrg ACC_SCALE_SCALAR_3V( sum[1], spec, light->_MatSpecular[1]); 6127117f1b4Smrg } 6137117f1b4Smrg } 6147117f1b4Smrg#endif 6157117f1b4Smrg } 6167117f1b4Smrg 6177117f1b4Smrg COPY_3V( Fcolor[j], sum[0] ); 6187117f1b4Smrg Fcolor[j][3] = sumA[0]; 6197117f1b4Smrg 6207117f1b4Smrg#if IDX & LIGHT_TWOSIDE 6217117f1b4Smrg COPY_3V( Bcolor[j], sum[1] ); 6227117f1b4Smrg Bcolor[j][3] = sumA[1]; 6237117f1b4Smrg#endif 6247117f1b4Smrg } 6257117f1b4Smrg} 6267117f1b4Smrg 6277117f1b4Smrg 6287117f1b4Smrg 6297117f1b4Smrg 6307117f1b4Smrgstatic void TAG(init_light_tab)( void ) 6317117f1b4Smrg{ 6327117f1b4Smrg _tnl_light_tab[IDX] = TAG(light_rgba); 6337117f1b4Smrg _tnl_light_fast_tab[IDX] = TAG(light_fast_rgba); 6347117f1b4Smrg _tnl_light_fast_single_tab[IDX] = TAG(light_fast_rgba_single); 6357117f1b4Smrg _tnl_light_spec_tab[IDX] = TAG(light_rgba_spec); 6367117f1b4Smrg} 6377117f1b4Smrg 6387117f1b4Smrg 6397117f1b4Smrg#undef TAG 6407117f1b4Smrg#undef IDX 6417117f1b4Smrg#undef NR_SIDES 642