/* Only 3 elements are used, the 4th is ignored */ void x86_opengl_vector_unit(GLfloat *dst, GLfloat *p1, GLfloat *p2) { if (p1[3] == 0 && p2[3] !=0) ACC_SCALE_SCALAR_3V(dst, -1.0f, p1); else if (p1[3] != 0 && p2[3] ==0) COPY_3V(dst, p2); else if (p1[3] == 0 && p2[3] == 0) SUB_3V(dst, p2, p1); else SUB_3V(dst, p2, p1); NORMALIZE_3FV(dst); }
/** * 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); }
/** * Determine type and flags from scratch. * * \param mat matrix. * * This is expensive enough to only want to do it once. */ static void analyse_from_scratch( GLmatrix *mat ) { const GLfloat *m = mat->m; GLuint mask = 0; GLuint i; for (i = 0 ; i < 16 ; i++) { if (m[i] == 0.0) mask |= (1<<i); } if (m[0] == 1.0F) mask |= (1<<16); if (m[5] == 1.0F) mask |= (1<<21); if (m[10] == 1.0F) mask |= (1<<26); if (m[15] == 1.0F) mask |= (1<<31); mat->flags &= ~MAT_FLAGS_GEOMETRY; /* Check for translation - no-one really cares */ if ((mask & MASK_NO_TRX) != MASK_NO_TRX) mat->flags |= MAT_FLAG_TRANSLATION; /* Do the real work */ if (mask == (GLuint) MASK_IDENTITY) { mat->type = MATRIX_IDENTITY; } else if ((mask & MASK_2D_NO_ROT) == (GLuint) MASK_2D_NO_ROT) { mat->type = MATRIX_2D_NO_ROT; if ((mask & MASK_NO_2D_SCALE) != MASK_NO_2D_SCALE) mat->flags |= MAT_FLAG_GENERAL_SCALE; } else if ((mask & MASK_2D) == (GLuint) MASK_2D) { GLfloat mm = DOT2(m, m); GLfloat m4m4 = DOT2(m+4,m+4); GLfloat mm4 = DOT2(m,m+4); mat->type = MATRIX_2D; /* Check for scale */ if (SQ(mm-1) > SQ(1e-6) || SQ(m4m4-1) > SQ(1e-6)) mat->flags |= MAT_FLAG_GENERAL_SCALE; /* Check for rotation */ if (SQ(mm4) > SQ(1e-6)) mat->flags |= MAT_FLAG_GENERAL_3D; else mat->flags |= MAT_FLAG_ROTATION; } else if ((mask & MASK_3D_NO_ROT) == (GLuint) MASK_3D_NO_ROT) { mat->type = MATRIX_3D_NO_ROT; /* Check for scale */ if (SQ(m[0]-m[5]) < SQ(1e-6) && SQ(m[0]-m[10]) < SQ(1e-6)) { if (SQ(m[0]-1.0) > SQ(1e-6)) { mat->flags |= MAT_FLAG_UNIFORM_SCALE; } } else { mat->flags |= MAT_FLAG_GENERAL_SCALE; } } else if ((mask & MASK_3D) == (GLuint) MASK_3D) { GLfloat c1 = DOT3(m,m); GLfloat c2 = DOT3(m+4,m+4); GLfloat c3 = DOT3(m+8,m+8); GLfloat d1 = DOT3(m, m+4); GLfloat cp[3]; mat->type = MATRIX_3D; /* Check for scale */ if (SQ(c1-c2) < SQ(1e-6) && SQ(c1-c3) < SQ(1e-6)) { if (SQ(c1-1.0) > SQ(1e-6)) mat->flags |= MAT_FLAG_UNIFORM_SCALE; /* else no scale at all */ } else { mat->flags |= MAT_FLAG_GENERAL_SCALE; } /* Check for rotation */ if (SQ(d1) < SQ(1e-6)) { CROSS3( cp, m, m+4 ); SUB_3V( cp, cp, (m+8) ); if (LEN_SQUARED_3FV(cp) < SQ(1e-6)) mat->flags |= MAT_FLAG_ROTATION; else mat->flags |= MAT_FLAG_GENERAL_3D; } else { mat->flags |= MAT_FLAG_GENERAL_3D; /* shear, etc */ } } else if ((mask & MASK_PERSPECTIVE) == MASK_PERSPECTIVE && m[11]==-1.0F) { mat->type = MATRIX_PERSPECTIVE; mat->flags |= MAT_FLAG_GENERAL; } else { mat->type = MATRIX_GENERAL; mat->flags |= MAT_FLAG_GENERAL; } }