Committing in .
[mesa.git] / src / mesa / main / light.c
index ed449b9f58fbe7433fbbb56298969a12e61d085c..b50eba7e9d07a95a73e9bb8795aa9e03b3819df7 100644 (file)
@@ -1,10 +1,10 @@
-/* $Id: light.c,v 1.41 2001/03/12 00:48:38 gareth Exp $ */
+/* $Id: light.c,v 1.50 2002/06/13 04:28:29 brianp Exp $ */
 
 /*
  * Mesa 3-D graphics library
- * Version:  3.5
+ * Version:  4.1
  *
- * Copyright (C) 1999-2001  Brian Paul   All Rights Reserved.
+ * Copyright (C) 1999-2002  Brian Paul   All Rights Reserved.
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
@@ -57,7 +57,7 @@ _mesa_ShadeModel( GLenum mode )
    ASSERT_OUTSIDE_BEGIN_END(ctx);
 
    if (MESA_VERBOSE & VERBOSE_API)
-      fprintf(stderr, "glShadeModel %s\n", _mesa_lookup_enum_by_nr(mode));
+      _mesa_debug("glShadeModel %s\n", _mesa_lookup_enum_by_nr(mode));
 
    if (mode != GL_FLAT && mode != GL_SMOOTH) {
       _mesa_error( ctx, GL_INVALID_ENUM, "glShadeModel" );
@@ -117,7 +117,7 @@ _mesa_Lightfv( GLenum light, GLenum pname, const GLfloat *params )
    case GL_POSITION: {
       GLfloat tmp[4];
       /* transform position by ModelView matrix */
-      TRANSFORM_POINT( tmp, ctx->ModelView.m, params );
+      TRANSFORM_POINT( tmp, ctx->ModelviewMatrixStack.Top->m, params );
       if (TEST_EQ_4V(l->EyePosition, tmp))
         return;
       FLUSH_VERTICES(ctx, _NEW_LIGHT);
@@ -131,10 +131,10 @@ _mesa_Lightfv( GLenum light, GLenum pname, const GLfloat *params )
    case GL_SPOT_DIRECTION: {
       GLfloat tmp[4];
       /* transform direction by inverse modelview */
-      if (ctx->ModelView.flags & MAT_DIRTY_INVERSE) {
-        _math_matrix_analyse( &ctx->ModelView );
+      if (ctx->ModelviewMatrixStack.Top->flags & MAT_DIRTY_INVERSE) {
+        _math_matrix_analyse( ctx->ModelviewMatrixStack.Top );
       }
-      TRANSFORM_NORMAL( tmp, params, ctx->ModelView.inv );
+      TRANSFORM_NORMAL( tmp, params, ctx->ModelviewMatrixStack.Top->inv );
       if (TEST_EQ_3V(l->EyeDirection, tmp))
         return;
       FLUSH_VERTICES(ctx, _NEW_LIGHT);
@@ -161,7 +161,7 @@ _mesa_Lightfv( GLenum light, GLenum pname, const GLfloat *params )
         return;
       FLUSH_VERTICES(ctx, _NEW_LIGHT);
       l->SpotCutoff = params[0];
-      l->_CosCutoff = cos(params[0]*DEG2RAD);
+      l->_CosCutoff = (GLfloat) cos(params[0]*DEG2RAD);
       if (l->_CosCutoff < 0)
         l->_CosCutoff = 0;
       if (l->SpotCutoff != 180.0F)
@@ -407,6 +407,11 @@ _mesa_LightModelfv( GLenum pname, const GLfloat *params )
            return;
         FLUSH_VERTICES(ctx, _NEW_LIGHT);
         ctx->Light.Model.TwoSide = newbool;
+
+        if (ctx->Light.Enabled && ctx->Light.Model.TwoSide)
+           ctx->_TriangleCaps |= DD_TRI_LIGHT_TWOSIDE;
+        else
+           ctx->_TriangleCaps &= ~DD_TRI_LIGHT_TWOSIDE;
          break;
       case GL_LIGHT_MODEL_COLOR_CONTROL:
          if (params[0] == (GLfloat) GL_SINGLE_COLOR)
@@ -425,9 +430,9 @@ _mesa_LightModelfv( GLenum pname, const GLfloat *params )
         if ((ctx->Light.Enabled &&
              ctx->Light.Model.ColorControl==GL_SEPARATE_SPECULAR_COLOR)
             || ctx->Fog.ColorSumEnabled)
-           ctx->_TriangleCaps |= DD_SEPERATE_SPECULAR;
+           ctx->_TriangleCaps |= DD_SEPARATE_SPECULAR;
         else
-           ctx->_TriangleCaps &= ~DD_SEPERATE_SPECULAR;
+           ctx->_TriangleCaps &= ~DD_SEPARATE_SPECULAR;
 
          break;
       default:
@@ -612,7 +617,7 @@ void _mesa_update_material( GLcontext *ctx,
       bitmask &= ~ctx->Light.ColorMaterialBitmask;
 
    if (MESA_VERBOSE&VERBOSE_IMMEDIATE)
-      fprintf(stderr, "_mesa_update_material, mask 0x%x\n", bitmask);
+      _mesa_debug("_mesa_update_material, mask 0x%x\n", bitmask);
 
    if (!bitmask)
       return;
@@ -664,7 +669,6 @@ void _mesa_update_material( GLcontext *ctx,
       foreach (light, list) {
         SCALE_3V( light->_MatDiffuse[0], light->Diffuse, mat->Diffuse );
       }
-      UNCLAMPED_FLOAT_TO_CHAN(ctx->Light._BaseAlpha[0], mat->Diffuse[3]);
    }
    if (bitmask & BACK_DIFFUSE_BIT) {
       struct gl_material *mat = &ctx->Light.Material[1];
@@ -672,7 +676,6 @@ void _mesa_update_material( GLcontext *ctx,
       foreach (light, list) {
         SCALE_3V( light->_MatDiffuse[1], light->Diffuse, mat->Diffuse );
       }
-      UNCLAMPED_FLOAT_TO_CHAN(ctx->Light._BaseAlpha[1], mat->Diffuse[3]);
    }
 
    /* update material specular values */
@@ -714,19 +717,19 @@ void _mesa_update_material( GLcontext *ctx,
    if (0)
    {
       struct gl_material *mat = &ctx->Light.Material[0];
-      fprintf(stderr, "update_mat  emission : %f %f %f\n",
+      _mesa_debug("update_mat  emission : %f %f %f\n",
              mat->Emission[0],
              mat->Emission[1],
              mat->Emission[2]);
-      fprintf(stderr, "update_mat  specular : %f %f %f\n",
+      _mesa_debug("update_mat  specular : %f %f %f\n",
              mat->Specular[0],
              mat->Specular[1],
              mat->Specular[2]);
-      fprintf(stderr, "update_mat  diffuse : %f %f %f\n",
+      _mesa_debug("update_mat  diffuse : %f %f %f\n",
              mat->Diffuse[0],
              mat->Diffuse[1],
              mat->Diffuse[2]);
-      fprintf(stderr, "update_mat  ambient : %f %f %f\n",
+      _mesa_debug("update_mat  ambient : %f %f %f\n",
              mat->Ambient[0],
              mat->Ambient[1],
              mat->Ambient[2]);
@@ -745,19 +748,13 @@ void _mesa_update_material( GLcontext *ctx,
  * set by glColorMaterial().
  */
 void _mesa_update_color_material( GLcontext *ctx,
-                              const GLchan rgba[4] )
+                                 const GLfloat color[4] )
 {
    struct gl_light *light, *list = &ctx->Light.EnabledList;
    GLuint bitmask = ctx->Light.ColorMaterialBitmask;
-   GLfloat color[4];
-
-   color[0] = CHAN_TO_FLOAT(rgba[0]);
-   color[1] = CHAN_TO_FLOAT(rgba[1]);
-   color[2] = CHAN_TO_FLOAT(rgba[2]);
-   color[3] = CHAN_TO_FLOAT(rgba[3]);
 
    if (MESA_VERBOSE&VERBOSE_IMMEDIATE)
-      fprintf(stderr, "_mesa_update_color_material, mask 0x%x\n", bitmask);
+      _mesa_debug("_mesa_update_color_material, mask 0x%x\n", bitmask);
 
    /* update emissive colors */
    if (bitmask & FRONT_EMISSION_BIT) {
@@ -807,7 +804,6 @@ void _mesa_update_color_material( GLcontext *ctx,
       foreach (light, list) {
         SCALE_3V( light->_MatDiffuse[0], light->Diffuse, mat->Diffuse );
       }
-      UNCLAMPED_FLOAT_TO_CHAN(ctx->Light._BaseAlpha[0], mat->Diffuse[3]);
    }
 
    if (bitmask & BACK_DIFFUSE_BIT) {
@@ -816,7 +812,6 @@ void _mesa_update_color_material( GLcontext *ctx,
       foreach (light, list) {
         SCALE_3V( light->_MatDiffuse[1], light->Diffuse, mat->Diffuse );
       }
-      UNCLAMPED_FLOAT_TO_CHAN(ctx->Light._BaseAlpha[1], mat->Diffuse[3]);
    }
 
    /* update light->_MatSpecular = light's specular * material's specular */
@@ -839,19 +834,19 @@ void _mesa_update_color_material( GLcontext *ctx,
    if (0)
    {
       struct gl_material *mat = &ctx->Light.Material[0];
-      fprintf(stderr, "update_color_mat  emission : %f %f %f\n",
+      _mesa_debug("update_color_mat  emission : %f %f %f\n",
              mat->Emission[0],
              mat->Emission[1],
              mat->Emission[2]);
-      fprintf(stderr, "update_color_mat  specular : %f %f %f\n",
+      _mesa_debug("update_color_mat  specular : %f %f %f\n",
              mat->Specular[0],
              mat->Specular[1],
              mat->Specular[2]);
-      fprintf(stderr, "update_color_mat  diffuse : %f %f %f\n",
+      _mesa_debug("update_color_mat  diffuse : %f %f %f\n",
              mat->Diffuse[0],
              mat->Diffuse[1],
              mat->Diffuse[2]);
-      fprintf(stderr, "update_color_mat  ambient : %f %f %f\n",
+      _mesa_debug("update_color_mat  ambient : %f %f %f\n",
              mat->Ambient[0],
              mat->Ambient[1],
              mat->Ambient[2]);
@@ -873,7 +868,7 @@ _mesa_ColorMaterial( GLenum face, GLenum mode )
    ASSERT_OUTSIDE_BEGIN_END(ctx);
 
    if (MESA_VERBOSE&VERBOSE_API)
-      fprintf(stderr, "glColorMaterial %s %s\n",
+      _mesa_debug("glColorMaterial %s %s\n",
              _mesa_lookup_enum_by_nr(face),
              _mesa_lookup_enum_by_nr(mode));
 
@@ -891,8 +886,11 @@ _mesa_ColorMaterial( GLenum face, GLenum mode )
 
    if (ctx->Light.ColorMaterialEnabled) {
       FLUSH_CURRENT( ctx, 0 );
-      _mesa_update_color_material( ctx, ctx->Current.Color );
+      _mesa_update_color_material(ctx,ctx->Current.Attrib[VERT_ATTRIB_COLOR0]);
    }
+
+   if (ctx->Driver.ColorMaterial)
+      (*ctx->Driver.ColorMaterial)( ctx, face, mode );
 }
 
 
@@ -1068,7 +1066,7 @@ static void validate_spot_exp_table( struct gl_light *l )
            clamp = 1;
         }
       }
-      l->_SpotExpTable[i][0] = tmp;
+      l->_SpotExpTable[i][0] = (GLfloat) tmp;
    }
    for (i = 0; i < EXP_TABLE_SIZE - 1; i++) {
       l->_SpotExpTable[i][1] = (l->_SpotExpTable[i+1][0] -
@@ -1122,7 +1120,7 @@ static void validate_shine_table( GLcontext *ctx, GLuint i, GLfloat shininess )
                x = 0.005;
             t = pow(x, shininess);
            if (t > 1e-20)
-              m[j] = t;
+              m[j] = (GLfloat) t;
            else
               m[j] = 0.0;
         }
@@ -1172,7 +1170,6 @@ void
 _mesa_update_lighting( GLcontext *ctx )
 {
    struct gl_light *light;
-   ctx->_TriangleCaps &= ~DD_TRI_LIGHT_TWOSIDE;
    ctx->_NeedEyeCoords &= ~NEED_EYE_LIGHT;
    ctx->_NeedNormals &= ~NEED_NORMALS_LIGHT;
    ctx->Light._Flags = 0;
@@ -1182,9 +1179,6 @@ _mesa_update_lighting( GLcontext *ctx )
 
    ctx->_NeedNormals |= NEED_NORMALS_LIGHT;
 
-   if (ctx->Light.Model.TwoSide)
-      ctx->_TriangleCaps |= DD_TRI_LIGHT_TWOSIDE;
-
    foreach(light, &ctx->Light.EnabledList) {
       ctx->Light._Flags |= light->_Flags;
    }
@@ -1222,9 +1216,6 @@ _mesa_update_lighting( GLcontext *ctx )
         ACC_SCALE_3V(ctx->Light._BaseColor[side],
                      ctx->Light.Model.Ambient,
                      mat->Ambient);
-
-        UNCLAMPED_FLOAT_TO_CHAN(ctx->Light._BaseAlpha[side],
-                                 ctx->Light.Material[side].Diffuse[3] );
       }
 
       foreach (light, &ctx->Light.EnabledList) {
@@ -1238,7 +1229,7 @@ _mesa_update_lighting( GLcontext *ctx )
       }
    }
    else {
-      static const GLfloat ci[3] = { .30, .59, .11 };
+      static const GLfloat ci[3] = { .30F, .59F, .11F };
       foreach(light, &ctx->Light.EnabledList) {
         light->_dli = DOT3(ci, light->Diffuse);
         light->_sli = DOT3(ci, light->Specular);
@@ -1267,7 +1258,7 @@ _mesa_compute_light_positions( GLcontext *ctx )
       COPY_3V( ctx->_EyeZDir, eye_z );
    }
    else {
-      TRANSFORM_NORMAL( ctx->_EyeZDir, eye_z, ctx->ModelView.m );
+      TRANSFORM_NORMAL( ctx->_EyeZDir, eye_z, ctx->ModelviewMatrixStack.Top->m );
    }
 
    foreach (light, &ctx->Light.EnabledList) {
@@ -1276,7 +1267,7 @@ _mesa_compute_light_positions( GLcontext *ctx )
         COPY_4FV( light->_Position, light->EyePosition );
       }
       else {
-        TRANSFORM_POINT( light->_Position, ctx->ModelView.inv,
+        TRANSFORM_POINT( light->_Position, ctx->ModelviewMatrixStack.Top->inv,
                          light->EyePosition );
       }
 
@@ -1300,7 +1291,7 @@ _mesa_compute_light_positions( GLcontext *ctx )
          else {
            TRANSFORM_NORMAL( light->_NormDirection,
                              light->EyeDirection,
-                             ctx->ModelView.m);
+                             ctx->ModelviewMatrixStack.Top->m);
         }
 
         NORMALIZE_3FV( light->_NormDirection );
@@ -1313,7 +1304,7 @@ _mesa_compute_light_positions( GLcontext *ctx )
               double x = PV_dot_dir * (EXP_TABLE_SIZE-1);
               int k = (int) x;
               light->_VP_inf_spot_attenuation =
-                 (light->_SpotExpTable[k][0] +
+                 (GLfloat) (light->_SpotExpTable[k][0] +
                   (x-k)*light->_SpotExpTable[k][1]);
            }
            else {