static void build_normal_lengths( struct tnl_vertex_list *node ) { GLuint i; GLfloat *len; GLfloat *n = node->buffer; GLuint stride = node->vertex_size; GLuint count = node->count; len = node->normal_lengths = (GLfloat *) MALLOC( count * sizeof(GLfloat) ); if (!len) return; /* Find the normal of the first vertex: */ for (i = 0 ; i < _TNL_ATTRIB_NORMAL ; i++) n += node->attrsz[i]; for (i = 0 ; i < count ; i++, n += stride) { len[i] = LEN_3FV( n ); if (len[i] > 0.0F) len[i] = 1.0F / len[i]; } }
/** * Compute lighting for the raster position. RGB modes computed. * \param ctx the context * \param vertex vertex location * \param normal normal vector * \param Rcolor returned color * \param Rspec returned specular color (if separate specular enabled) */ static void shade_rastpos(struct gl_context *ctx, const GLfloat vertex[4], const GLfloat normal[3], GLfloat Rcolor[4], GLfloat Rspec[4]) { /*const*/ GLfloat (*base)[3] = ctx->Light._BaseColor; GLbitfield mask; GLfloat diffuseColor[4], specularColor[4]; /* for RGB mode only */ COPY_3V(diffuseColor, base[0]); diffuseColor[3] = CLAMP( ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE][3], 0.0F, 1.0F ); ASSIGN_4V(specularColor, 0.0, 0.0, 0.0, 1.0); mask = ctx->Light._EnabledLights; while (mask) { const int i = u_bit_scan(&mask); struct gl_light *light = &ctx->Light.Light[i]; GLfloat attenuation = 1.0; GLfloat VP[3]; /* vector from vertex to light pos */ GLfloat n_dot_VP; GLfloat diffuseContrib[3], specularContrib[3]; if (!(light->_Flags & LIGHT_POSITIONAL)) { /* light at infinity */ COPY_3V(VP, light->_VP_inf_norm); attenuation = light->_VP_inf_spot_attenuation; } else { /* local/positional light */ GLfloat d; /* VP = vector from vertex pos to light[i].pos */ SUB_3V(VP, light->_Position, vertex); /* d = length(VP) */ d = (GLfloat) LEN_3FV( VP ); if (d > 1.0e-6F) { /* normalize VP */ GLfloat invd = 1.0F / d; SELF_SCALE_SCALAR_3V(VP, invd); } /* atti */ attenuation = 1.0F / (light->ConstantAttenuation + d * (light->LinearAttenuation + d * light->QuadraticAttenuation)); if (light->_Flags & LIGHT_SPOT) { GLfloat PV_dot_dir = - DOT3(VP, light->_NormSpotDirection); if (PV_dot_dir<light->_CosCutoff) { continue; } else { GLfloat spot = powf(PV_dot_dir, light->SpotExponent); attenuation *= spot; } } } if (attenuation < 1e-3F) continue; n_dot_VP = DOT3( normal, VP ); if (n_dot_VP < 0.0F) { ACC_SCALE_SCALAR_3V(diffuseColor, attenuation, light->_MatAmbient[0]); continue; } /* Ambient + diffuse */ COPY_3V(diffuseContrib, light->_MatAmbient[0]); ACC_SCALE_SCALAR_3V(diffuseContrib, n_dot_VP, light->_MatDiffuse[0]); /* Specular */ { const GLfloat *h; GLfloat n_dot_h; ASSIGN_3V(specularContrib, 0.0, 0.0, 0.0); if (ctx->Light.Model.LocalViewer) { GLfloat v[3]; COPY_3V(v, vertex); NORMALIZE_3FV(v); SUB_3V(VP, VP, v); NORMALIZE_3FV(VP); h = VP; } else if (light->_Flags & LIGHT_POSITIONAL) { ACC_3V(VP, ctx->_EyeZDir); NORMALIZE_3FV(VP); h = VP; } else { h = light->_h_inf_norm; } n_dot_h = DOT3(normal, h); if (n_dot_h > 0.0F) { GLfloat shine; GLfloat spec_coef; shine = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SHININESS][0]; spec_coef = powf(n_dot_h, shine); if (spec_coef > 1.0e-10F) { if (ctx->Light.Model.ColorControl==GL_SEPARATE_SPECULAR_COLOR) { ACC_SCALE_SCALAR_3V( specularContrib, spec_coef, light->_MatSpecular[0]); } else { ACC_SCALE_SCALAR_3V( diffuseContrib, spec_coef, light->_MatSpecular[0]); } } } } ACC_SCALE_SCALAR_3V( diffuseColor, attenuation, diffuseContrib ); ACC_SCALE_SCALAR_3V( specularColor, attenuation, specularContrib ); } Rcolor[0] = CLAMP(diffuseColor[0], 0.0F, 1.0F); Rcolor[1] = CLAMP(diffuseColor[1], 0.0F, 1.0F); Rcolor[2] = CLAMP(diffuseColor[2], 0.0F, 1.0F); Rcolor[3] = CLAMP(diffuseColor[3], 0.0F, 1.0F); Rspec[0] = CLAMP(specularColor[0], 0.0F, 1.0F); Rspec[1] = CLAMP(specularColor[1], 0.0F, 1.0F); Rspec[2] = CLAMP(specularColor[2], 0.0F, 1.0F); Rspec[3] = CLAMP(specularColor[3], 0.0F, 1.0F); }