-/* $Id: light.c,v 1.13 2000/06/19 14:35:17 keithw Exp $ */
+/* $Id: light.c,v 1.14 2000/06/26 23:37:46 brianp Exp $ */
/*
* Mesa 3-D graphics library
-
-
-
/*
* Check if the global material has to be updated with info that was
* associated with a vertex via glMaterial.
* glBegin/glEnd either by calling glMaterial() or by calling glColor()
* when GL_COLOR_MATERIAL is enabled.
*
+ * src[0] is front material, src[1] is back material
+ *
* KW: Added code here to keep the precomputed variables uptodate.
* This means we can use the faster shade functions when using
* GL_COLOR_MATERIAL, and we can also now use the precomputed
* the cost of doing this here.
*/
void gl_update_material( GLcontext *ctx,
- struct gl_material *src,
+ const struct gl_material src[2],
GLuint bitmask )
{
struct gl_light *light, *list = &ctx->Light.EnabledList;
- GLfloat tmp[4];
if (ctx->Light.ColorMaterialEnabled)
bitmask &= ~ctx->Light.ColorMaterialBitmask;
if (MESA_VERBOSE&VERBOSE_IMMEDIATE)
- fprintf(stderr, "gl_update_material, mask %x\n", bitmask);
+ fprintf(stderr, "gl_update_material, mask 0x%x\n", bitmask);
if (!bitmask)
return;
+ /* update material emission */
+ if (bitmask & FRONT_EMISSION_BIT) {
+ struct gl_material *mat = &ctx->Light.Material[0];
+ COPY_4FV( mat->Emission, src[0].Emission );
+ }
+ if (bitmask & BACK_EMISSION_BIT) {
+ struct gl_material *mat = &ctx->Light.Material[1];
+ COPY_4FV( mat->Emission, src[1].Emission );
+ }
+
+ /* update material ambience */
if (bitmask & FRONT_AMBIENT_BIT) {
struct gl_material *mat = &ctx->Light.Material[0];
- SUB_3V( tmp, src[0].Ambient, mat->Ambient );
- ACC_SCALE_3V( ctx->Light.BaseColor[0], ctx->Light.Model.Ambient, tmp);
+ COPY_4FV( mat->Ambient, src[0].Ambient );
foreach (light, list) {
- ACC_SCALE_3V( ctx->Light.BaseColor[0], light->Ambient, tmp );
+ SCALE_3V( light->MatAmbient[0], light->Ambient, src[0].Ambient);
}
- COPY_4FV( mat->Ambient, src[0].Ambient );
}
if (bitmask & BACK_AMBIENT_BIT) {
struct gl_material *mat = &ctx->Light.Material[1];
- SUB_3V( tmp, src[1].Ambient, mat->Ambient );
- ACC_SCALE_3V( ctx->Light.BaseColor[1], ctx->Light.Model.Ambient, tmp);
+ COPY_4FV( mat->Ambient, src[1].Ambient );
foreach (light, list) {
- ACC_SCALE_3V( ctx->Light.BaseColor[0], light->Ambient, tmp );
+ SCALE_3V( light->MatAmbient[1], light->Ambient, src[1].Ambient);
}
- COPY_4FV( mat->Ambient, src[1].Ambient );
}
+
+ /* update BaseColor = emission + scene's ambience * material's ambience */
+ if (bitmask & (FRONT_EMISSION_BIT | FRONT_AMBIENT_BIT)) {
+ struct gl_material *mat = &ctx->Light.Material[0];
+ COPY_3V( ctx->Light.BaseColor[0], mat->Emission );
+ ACC_SCALE_3V( ctx->Light.BaseColor[0], mat->Ambient, ctx->Light.Model.Ambient );
+ }
+ if (bitmask & (BACK_EMISSION_BIT | BACK_AMBIENT_BIT)) {
+ struct gl_material *mat = &ctx->Light.Material[1];
+ COPY_3V( ctx->Light.BaseColor[1], mat->Emission );
+ ACC_SCALE_3V( ctx->Light.BaseColor[1], mat->Ambient, ctx->Light.Model.Ambient );
+ }
+
+ /* update material diffuse values */
if (bitmask & FRONT_DIFFUSE_BIT) {
struct gl_material *mat = &ctx->Light.Material[0];
+ GLfloat tmp[4];
SUB_3V( tmp, src[0].Diffuse, mat->Diffuse );
foreach (light, list) {
ACC_SCALE_3V( light->MatDiffuse[0], light->Diffuse, tmp );
}
if (bitmask & BACK_DIFFUSE_BIT) {
struct gl_material *mat = &ctx->Light.Material[1];
+ GLfloat tmp[4];
SUB_3V( tmp, src[1].Diffuse, mat->Diffuse );
foreach (light, list) {
ACC_SCALE_3V( light->MatDiffuse[1], light->Diffuse, tmp );
COPY_4FV( mat->Diffuse, src[1].Diffuse );
FLOAT_COLOR_TO_UBYTE_COLOR(ctx->Light.BaseAlpha[1], mat->Diffuse[3]);
}
+
+ /* update material specular values */
if (bitmask & FRONT_SPECULAR_BIT) {
struct gl_material *mat = &ctx->Light.Material[0];
+ GLfloat tmp[4];
SUB_3V( tmp, src[0].Specular, mat->Specular );
foreach (light, list) {
if (light->Flags & LIGHT_SPECULAR) {
}
if (bitmask & BACK_SPECULAR_BIT) {
struct gl_material *mat = &ctx->Light.Material[1];
+ GLfloat tmp[4];
SUB_3V( tmp, src[1].Specular, mat->Specular );
foreach (light, list) {
if (light->Flags & LIGHT_SPECULAR) {
}
COPY_4FV( mat->Specular, src[1].Specular );
}
- if (bitmask & FRONT_EMISSION_BIT) {
- struct gl_material *mat = &ctx->Light.Material[0];
- SUB_3V( tmp, src[0].Emission, mat->Emission );
- ACC_3V( ctx->Light.BaseColor[0], tmp );
- COPY_4FV( mat->Emission, src[0].Emission );
- }
- if (bitmask & BACK_EMISSION_BIT) {
- struct gl_material *mat = &ctx->Light.Material[1];
- SUB_3V( tmp, src[1].Emission, mat->Emission );
- ACC_3V( ctx->Light.BaseColor[1], tmp );
- COPY_4FV( mat->Emission, src[1].Emission );
- }
+
if (bitmask & FRONT_SHININESS_BIT) {
GLfloat shininess = ctx->Light.Material[0].Shininess = src[0].Shininess;
gl_compute_shine_table( ctx, 0, shininess );
gl_compute_shine_table( ctx, 1, shininess );
gl_compute_shine_table( ctx, 3, shininess * .5 );
}
+
if (bitmask & FRONT_INDEXES_BIT) {
ctx->Light.Material[0].AmbientIndex = src[0].AmbientIndex;
ctx->Light.Material[0].DiffuseIndex = src[0].DiffuseIndex;
-
-
+/*
+ * Update the current materials from the given rgba color
+ * according to the bitmask in ColorMaterialBitmask, which is
+ * set by glColorMaterial().
+ */
void gl_update_color_material( GLcontext *ctx,
const GLubyte rgba[4] )
{
struct gl_light *light, *list = &ctx->Light.EnabledList;
GLuint bitmask = ctx->Light.ColorMaterialBitmask;
- GLfloat tmp[4], color[4];
+ GLfloat color[4];
UBYTE_RGBA_TO_FLOAT_RGBA( color, rgba );
if (MESA_VERBOSE&VERBOSE_IMMEDIATE)
- fprintf(stderr, "gl_update_color_material, mask %x\n", bitmask);
+ fprintf(stderr, "gl_update_color_material, mask 0x%x\n", bitmask);
+ /* update emissive colors */
+ if (bitmask & FRONT_EMISSION_BIT) {
+ struct gl_material *mat = &ctx->Light.Material[0];
+ COPY_4FV( mat->Emission, color );
+ }
+
+ if (bitmask & BACK_EMISSION_BIT) {
+ struct gl_material *mat = &ctx->Light.Material[1];
+ COPY_4FV( mat->Emission, color );
+ }
+ /* update light->MatAmbient = light's ambient * material's ambient */
if (bitmask & FRONT_AMBIENT_BIT) {
struct gl_material *mat = &ctx->Light.Material[0];
- SUB_3V( tmp, color, mat->Ambient );
- ACC_SCALE_3V( ctx->Light.BaseColor[0], ctx->Light.Model.Ambient, tmp);
foreach (light, list) {
- ACC_SCALE_3V( ctx->Light.BaseColor[0], light->Ambient, tmp );
+ SCALE_3V( light->MatAmbient[0], light->Ambient, color);
}
COPY_4FV( mat->Ambient, color );
}
if (bitmask & BACK_AMBIENT_BIT) {
struct gl_material *mat = &ctx->Light.Material[1];
- SUB_3V( tmp, color, mat->Ambient );
- ACC_SCALE_3V( ctx->Light.BaseColor[1], ctx->Light.Model.Ambient, tmp);
foreach (light, list) {
- ACC_SCALE_3V( ctx->Light.BaseColor[1], light->Ambient, tmp );
+ SCALE_3V( light->MatAmbient[1], light->Ambient, color);
}
COPY_4FV( mat->Ambient, color );
}
+ /* update BaseColor = emission + scene's ambience * material's ambience */
+ if (bitmask & (FRONT_EMISSION_BIT | FRONT_AMBIENT_BIT)) {
+ struct gl_material *mat = &ctx->Light.Material[0];
+ COPY_3V( ctx->Light.BaseColor[0], mat->Emission );
+ ACC_SCALE_3V( ctx->Light.BaseColor[0], mat->Ambient, ctx->Light.Model.Ambient );
+ }
+
+ if (bitmask & (BACK_EMISSION_BIT | BACK_AMBIENT_BIT)) {
+ struct gl_material *mat = &ctx->Light.Material[1];
+ COPY_3V( ctx->Light.BaseColor[1], mat->Emission );
+ ACC_SCALE_3V( ctx->Light.BaseColor[1], mat->Ambient, ctx->Light.Model.Ambient );
+ }
+
+ /* update light->MatDiffuse = light's diffuse * material's diffuse */
if (bitmask & FRONT_DIFFUSE_BIT) {
struct gl_material *mat = &ctx->Light.Material[0];
+ GLfloat tmp[4];
SUB_3V( tmp, color, mat->Diffuse );
foreach (light, list) {
ACC_SCALE_3V( light->MatDiffuse[0], light->Diffuse, tmp );
if (bitmask & BACK_DIFFUSE_BIT) {
struct gl_material *mat = &ctx->Light.Material[1];
+ GLfloat tmp[4];
SUB_3V( tmp, color, mat->Diffuse );
foreach (light, list) {
ACC_SCALE_3V( light->MatDiffuse[1], light->Diffuse, tmp );
FLOAT_COLOR_TO_UBYTE_COLOR(ctx->Light.BaseAlpha[1], mat->Diffuse[3]);
}
+ /* update light->MatSpecular = light's specular * material's specular */
if (bitmask & FRONT_SPECULAR_BIT) {
struct gl_material *mat = &ctx->Light.Material[0];
+ GLfloat tmp[4];
SUB_3V( tmp, color, mat->Specular );
foreach (light, list) {
if (light->Flags & LIGHT_SPECULAR) {
}
COPY_4FV( mat->Specular, color );
}
+
if (bitmask & BACK_SPECULAR_BIT) {
struct gl_material *mat = &ctx->Light.Material[1];
+ GLfloat tmp[4];
SUB_3V( tmp, color, mat->Specular );
foreach (light, list) {
if (light->Flags & LIGHT_SPECULAR) {
}
COPY_4FV( mat->Specular, color );
}
- if (bitmask & FRONT_EMISSION_BIT) {
- struct gl_material *mat = &ctx->Light.Material[0];
- SUB_3V( tmp, color, mat->Emission );
- ACC_3V( ctx->Light.BaseColor[0], tmp );
- COPY_4FV( mat->Emission, color );
- }
- if (bitmask & BACK_EMISSION_BIT) {
- struct gl_material *mat = &ctx->Light.Material[1];
- SUB_3V( tmp, color, mat->Emission );
- ACC_3V( ctx->Light.BaseColor[1], tmp );
- COPY_4FV( mat->Emission, color );
- }
if (0)
{
{
struct gl_light *light;
- if (ctx->Light.NeedVertices && !ctx->Light.Model.LocalViewer) {
- GLfloat eye_z[3] = { 0, 0, 1 };
- if (!ctx->NeedEyeCoords) {
- TRANSFORM_NORMAL( ctx->EyeZDir, eye_z, ctx->ModelView.m );
- } else {
+ if (1 /*ctx->Light.NeedVertices && !ctx->Light.Model.LocalViewer*/) {
+ static const GLfloat eye_z[3] = { 0, 0, 1 };
+ if (ctx->NeedEyeCoords) {
COPY_3V( ctx->EyeZDir, eye_z );
}
+ else {
+ TRANSFORM_NORMAL( ctx->EyeZDir, eye_z, ctx->ModelView.m );
+ }
}
foreach (light, &ctx->Light.EnabledList) {
- if (!ctx->NeedEyeCoords) {
+ if (ctx->NeedEyeCoords) {
+ COPY_4FV( light->Position, light->EyePosition );
+ }
+ else {
TRANSFORM_POINT( light->Position, ctx->ModelView.inv,
light->EyePosition );
- } else {
- COPY_4FV( light->Position, light->EyePosition );
}
- if (!(light->Flags & LIGHT_POSITIONAL))
- {
+ if (!(light->Flags & LIGHT_POSITIONAL)) {
/* VP (VP) = Normalize( Position ) */
COPY_3V( light->VP_inf_norm, light->Position );
NORMALIZE_3FV( light->VP_inf_norm );
- if (!ctx->Light.Model.LocalViewer)
- {
+ if (!ctx->Light.Model.LocalViewer) {
/* h_inf_norm = Normalize( V_to_P + <0,0,1> ) */
ADD_3V( light->h_inf_norm, light->VP_inf_norm, ctx->EyeZDir);
NORMALIZE_3FV( light->h_inf_norm );
}
-
light->VP_inf_spot_attenuation = 1.0;
}
- if (light->Flags & LIGHT_SPOT)
- {
+ if (light->Flags & LIGHT_SPOT) {
if (ctx->NeedEyeNormals) {
COPY_3V( light->NormDirection, light->EyeDirection );
- } else {
+ }
+ else {
TRANSFORM_NORMAL( light->NormDirection,
light->EyeDirection,
ctx->ModelView.m);
(light->SpotExpTable[k][0] +
(x-k)*light->SpotExpTable[k][1]);
}
- else
+ else {
light->VP_inf_spot_attenuation = 0;
+ }
}
}
}