mesa: Remove _CosCutoffNeg from light state.
[mesa.git] / src / mesa / main / light.c
index 1c8a081e9a66583f731da9e938af5fd6838ce5ef..552efa7f0a671aaa1b158ac1ca64331eb960eb47 100644 (file)
@@ -103,7 +103,7 @@ _mesa_ProvokingVertexEXT(GLenum mode)
  * Also, all error checking should have already been done.
  */
 void
-_mesa_light(GLcontext *ctx, GLuint lnum, GLenum pname, const GLfloat *params)
+_mesa_light(struct gl_context *ctx, GLuint lnum, GLenum pname, const GLfloat *params)
 {
    struct gl_light *light;
 
@@ -154,7 +154,6 @@ _mesa_light(GLcontext *ctx, GLuint lnum, GLenum pname, const GLfloat *params)
         return;
       FLUSH_VERTICES(ctx, _NEW_LIGHT);
       light->SpotExponent = params[0];
-      _mesa_invalidate_spot_exp_table(light);
       break;
    case GL_SPOT_CUTOFF:
       ASSERT(params[0] == 180.0 || (params[0] >= 0.0 && params[0] <= 90.0));
@@ -162,11 +161,9 @@ _mesa_light(GLcontext *ctx, GLuint lnum, GLenum pname, const GLfloat *params)
          return;
       FLUSH_VERTICES(ctx, _NEW_LIGHT);
       light->SpotCutoff = params[0];
-      light->_CosCutoffNeg = (GLfloat) (_mesa_cos(light->SpotCutoff * DEG2RAD));
-      if (light->_CosCutoffNeg < 0)
+      light->_CosCutoff = (GLfloat) (cos(light->SpotCutoff * DEG2RAD));
+      if (light->_CosCutoff < 0)
          light->_CosCutoff = 0;
-      else
-         light->_CosCutoff = light->_CosCutoffNeg;
       if (light->SpotCutoff != 180.0F)
          light->_Flags |= LIGHT_SPOT;
       else
@@ -206,7 +203,10 @@ _mesa_light(GLcontext *ctx, GLuint lnum, GLenum pname, const GLfloat *params)
 void GLAPIENTRY
 _mesa_Lightf( GLenum light, GLenum pname, GLfloat param )
 {
-   _mesa_Lightfv( light, pname, &param );
+   GLfloat fparam[4];
+   fparam[0] = param;
+   fparam[1] = fparam[2] = fparam[3] = 0.0F;
+   _mesa_Lightfv( light, pname, fparam );
 }
 
 
@@ -285,7 +285,10 @@ _mesa_Lightfv( GLenum light, GLenum pname, const GLfloat *params )
 void GLAPIENTRY
 _mesa_Lighti( GLenum light, GLenum pname, GLint param )
 {
-   _mesa_Lightiv( light, pname, &param );
+   GLint iparam[4];
+   iparam[0] = param;
+   iparam[1] = iparam[2] = iparam[3] = 0;
+   _mesa_Lightiv( light, pname, iparam );
 }
 
 
@@ -537,14 +540,20 @@ _mesa_LightModeliv( GLenum pname, const GLint *params )
 void GLAPIENTRY
 _mesa_LightModeli( GLenum pname, GLint param )
 {
-   _mesa_LightModeliv( pname, &param );
+   GLint iparam[4];
+   iparam[0] = param;
+   iparam[1] = iparam[2] = iparam[3] = 0;
+   _mesa_LightModeliv( pname, iparam );
 }
 
 
 void GLAPIENTRY
 _mesa_LightModelf( GLenum pname, GLfloat param )
 {
-   _mesa_LightModelfv( pname, &param );
+   GLfloat fparam[4];
+   fparam[0] = param;
+   fparam[1] = fparam[2] = fparam[3] = 0.0F;
+   _mesa_LightModelfv( pname, fparam );
 }
 
 
@@ -557,7 +566,7 @@ _mesa_LightModelf( GLenum pname, GLfloat param )
  * of the targeted material values.
  */
 GLuint
-_mesa_material_bitmask( GLcontext *ctx, GLenum face, GLenum pname,
+_mesa_material_bitmask( struct gl_context *ctx, GLenum face, GLenum pname,
                         GLuint legal, const char *where )
 {
    GLuint bitmask = 0;
@@ -587,7 +596,7 @@ _mesa_material_bitmask( GLcontext *ctx, GLenum face, GLenum pname,
          bitmask |= MAT_BIT_FRONT_INDEXES  | MAT_BIT_BACK_INDEXES;
          break;
       default:
-         _mesa_error( ctx, GL_INVALID_ENUM, where );
+         _mesa_error( ctx, GL_INVALID_ENUM, "%s", where );
          return 0;
    }
 
@@ -598,12 +607,12 @@ _mesa_material_bitmask( GLcontext *ctx, GLenum face, GLenum pname,
       bitmask &= BACK_MATERIAL_BITS;
    }
    else if (face != GL_FRONT_AND_BACK) {
-      _mesa_error( ctx, GL_INVALID_ENUM, where );
+      _mesa_error( ctx, GL_INVALID_ENUM, "%s", where );
       return 0;
    }
 
    if (bitmask & ~legal) {
-      _mesa_error( ctx, GL_INVALID_ENUM, where );
+      _mesa_error( ctx, GL_INVALID_ENUM, "%s", where );
       return 0;
    }
 
@@ -612,26 +621,15 @@ _mesa_material_bitmask( GLcontext *ctx, GLenum face, GLenum pname,
 
 
 
-/* Perform a straight copy between materials.
- */
-void
-_mesa_copy_materials( struct gl_material *dst,
-                      const struct gl_material *src,
-                      GLuint bitmask )
-{
-   int i;
-
-   for (i = 0 ; i < MAT_ATTRIB_MAX ; i++) 
-      if (bitmask & (1<<i))
-        COPY_4FV( dst->Attrib[i], src->Attrib[i] );
-}
+static void
+invalidate_shine_table( struct gl_context *ctx, GLuint side );
 
 
 
 /* Update derived values following a change in ctx->Light.Material
  */
 void
-_mesa_update_material( GLcontext *ctx, GLuint bitmask )
+_mesa_update_material( struct gl_context *ctx, GLuint bitmask )
 {
    struct gl_light *light, *list = &ctx->Light.EnabledList;
    GLfloat (*mat)[4] = ctx->Light.Material.Attrib;
@@ -701,11 +699,11 @@ _mesa_update_material( GLcontext *ctx, GLuint bitmask )
    }
 
    if (bitmask & MAT_BIT_FRONT_SHININESS) {
-      _mesa_invalidate_shine_table( ctx, 0 );
+      invalidate_shine_table( ctx, 0 );
    }
 
    if (bitmask & MAT_BIT_BACK_SHININESS) {
-      _mesa_invalidate_shine_table( ctx, 1 );
+      invalidate_shine_table( ctx, 1 );
    }
 }
 
@@ -716,7 +714,7 @@ _mesa_update_material( GLcontext *ctx, GLuint bitmask )
  * set by glColorMaterial().
  */
 void
-_mesa_update_color_material( GLcontext *ctx, const GLfloat color[4] )
+_mesa_update_color_material( struct gl_context *ctx, const GLfloat color[4] )
 {
    GLuint bitmask = ctx->Light.ColorMaterialBitmask;
    struct gl_material *mat = &ctx->Light.Material;
@@ -915,52 +913,12 @@ _mesa_GetMaterialiv( GLenum face, GLenum pname, GLint *params )
 
 
 
-/*
- * Whenever the spotlight exponent for a light changes we must call
- * this function to recompute the exponent lookup table.
- */
-void
-_mesa_invalidate_spot_exp_table( struct gl_light *l )
-{
-   l->_SpotExpTable[0][0] = -1;
-}
-
-
-static void
-validate_spot_exp_table( struct gl_light *l )
-{
-   GLint i;
-   GLdouble exponent = l->SpotExponent;
-   GLdouble tmp = 0;
-   GLint clamp = 0;
-
-   l->_SpotExpTable[0][0] = 0.0;
-
-   for (i = EXP_TABLE_SIZE - 1; i > 0 ;i--) {
-      if (clamp == 0) {
-        tmp = _mesa_pow(i / (GLdouble) (EXP_TABLE_SIZE - 1), exponent);
-        if (tmp < FLT_MIN * 100.0) {
-           tmp = 0.0;
-           clamp = 1;
-        }
-      }
-      l->_SpotExpTable[i][0] = (GLfloat) tmp;
-   }
-   for (i = 0; i < EXP_TABLE_SIZE - 1; i++) {
-      l->_SpotExpTable[i][1] = (l->_SpotExpTable[i+1][0] -
-                               l->_SpotExpTable[i][0]);
-   }
-   l->_SpotExpTable[EXP_TABLE_SIZE-1][1] = 0.0;
-}
-
-
-
 /* Calculate a new shine table.  Doing this here saves a branch in
  * lighting, and the cost of doing it early may be partially offset
  * by keeping a MRU cache of shine tables for various shine values.
  */
-void
-_mesa_invalidate_shine_table( GLcontext *ctx, GLuint side )
+static void
+invalidate_shine_table( struct gl_context *ctx, GLuint side )
 {
    ASSERT(side < 2);
    if (ctx->_ShineTable[side])
@@ -970,7 +928,7 @@ _mesa_invalidate_shine_table( GLcontext *ctx, GLuint side )
 
 
 static void
-validate_shine_table( GLcontext *ctx, GLuint side, GLfloat shininess )
+validate_shine_table( struct gl_context *ctx, GLuint side, GLfloat shininess )
 {
    struct gl_shine_tab *list = ctx->_ShineTabList;
    struct gl_shine_tab *s;
@@ -1000,7 +958,7 @@ validate_shine_table( GLcontext *ctx, GLuint side, GLfloat shininess )
             GLdouble t, x = j / (GLfloat) (SHINE_TABLE_SIZE - 1);
             if (x < 0.005) /* underflow check */
                x = 0.005;
-            t = _mesa_pow(x, shininess);
+            t = pow(x, shininess);
            if (t > 1e-20)
               m[j] = (GLfloat) t;
            else
@@ -1022,9 +980,8 @@ validate_shine_table( GLcontext *ctx, GLuint side, GLfloat shininess )
 
 
 void
-_mesa_validate_all_lighting_tables( GLcontext *ctx )
+_mesa_validate_all_lighting_tables( struct gl_context *ctx )
 {
-   GLuint i;
    GLfloat shininess;
    
    shininess = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SHININESS][0];
@@ -1034,10 +991,6 @@ _mesa_validate_all_lighting_tables( GLcontext *ctx )
    shininess = ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_SHININESS][0];
    if (!ctx->_ShineTable[1] || ctx->_ShineTable[1]->shininess != shininess)
       validate_shine_table( ctx, 1, shininess );
-
-   for (i = 0; i < ctx->Const.MaxLights; i++)
-      if (ctx->Light.Light[i]._SpotExpTable[0][0] == -1)
-        validate_spot_exp_table( &ctx->Light.Light[i] );
 }
 
 
@@ -1048,7 +1001,7 @@ _mesa_validate_all_lighting_tables( GLcontext *ctx )
  * source and material ambient, diffuse and specular coefficients.
  */
 void
-_mesa_update_lighting( GLcontext *ctx )
+_mesa_update_lighting( struct gl_context *ctx )
 {
    struct gl_light *light;
    ctx->Light._NeedEyeCoords = GL_FALSE;
@@ -1081,31 +1034,22 @@ _mesa_update_lighting( GLcontext *ctx )
     * FLUSH_UPDATE_CURRENT, as when any outstanding material changes
     * are flushed, they will update the derived state at that time.
     */
-   if (ctx->Visual.rgbMode) {
-      if (ctx->Light.Model.TwoSide)
-        _mesa_update_material( ctx, 
-                               MAT_BIT_FRONT_EMISSION |
-                               MAT_BIT_FRONT_AMBIENT |
-                               MAT_BIT_FRONT_DIFFUSE | 
-                               MAT_BIT_FRONT_SPECULAR |
-                               MAT_BIT_BACK_EMISSION |
-                               MAT_BIT_BACK_AMBIENT |
-                               MAT_BIT_BACK_DIFFUSE | 
-                               MAT_BIT_BACK_SPECULAR);
-      else
-        _mesa_update_material( ctx, 
-                               MAT_BIT_FRONT_EMISSION |
-                               MAT_BIT_FRONT_AMBIENT |
-                               MAT_BIT_FRONT_DIFFUSE | 
-                               MAT_BIT_FRONT_SPECULAR);
-   }
-   else {
-      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);
-      }
-   }
+   if (ctx->Light.Model.TwoSide)
+      _mesa_update_material(ctx,
+                           MAT_BIT_FRONT_EMISSION |
+                           MAT_BIT_FRONT_AMBIENT |
+                           MAT_BIT_FRONT_DIFFUSE |
+                           MAT_BIT_FRONT_SPECULAR |
+                           MAT_BIT_BACK_EMISSION |
+                           MAT_BIT_BACK_AMBIENT |
+                           MAT_BIT_BACK_DIFFUSE |
+                           MAT_BIT_BACK_SPECULAR);
+   else
+      _mesa_update_material(ctx,
+                           MAT_BIT_FRONT_EMISSION |
+                           MAT_BIT_FRONT_AMBIENT |
+                           MAT_BIT_FRONT_DIFFUSE |
+                           MAT_BIT_FRONT_SPECULAR);
 }
 
 
@@ -1120,7 +1064,7 @@ _mesa_update_lighting( GLcontext *ctx )
  * Also update on lighting space changes.
  */
 static void
-compute_light_positions( GLcontext *ctx )
+compute_light_positions( struct gl_context *ctx )
 {
    struct gl_light *light;
    static const GLfloat eye_z[3] = { 0, 0, 1 };
@@ -1135,6 +1079,9 @@ compute_light_positions( GLcontext *ctx )
       TRANSFORM_NORMAL( ctx->_EyeZDir, eye_z, ctx->ModelviewMatrixStack.Top->m );
    }
 
+   /* Make sure all the light tables are updated before the computation */
+   _mesa_validate_all_lighting_tables(ctx);
+
    foreach (light, &ctx->Light.EnabledList) {
 
       if (ctx->_NeedEyeCoords) {
@@ -1190,11 +1137,8 @@ compute_light_positions( GLcontext *ctx )
                                        light->_NormSpotDirection);
 
            if (PV_dot_dir > light->_CosCutoff) {
-              double x = PV_dot_dir * (EXP_TABLE_SIZE-1);
-              int k = (int) x;
               light->_VP_inf_spot_attenuation =
-                 (GLfloat) (light->_SpotExpTable[k][0] +
-                  (x-k)*light->_SpotExpTable[k][1]);
+                  powf(PV_dot_dir, light->SpotExponent);
            }
            else {
               light->_VP_inf_spot_attenuation = 0;
@@ -1207,7 +1151,7 @@ compute_light_positions( GLcontext *ctx )
 
 
 static void
-update_modelview_scale( GLcontext *ctx )
+update_modelview_scale( struct gl_context *ctx )
 {
    ctx->_ModelViewInvScale = 1.0F;
    if (!_math_matrix_is_length_preserving(ctx->ModelviewMatrixStack.Top)) {
@@ -1226,7 +1170,7 @@ update_modelview_scale( GLcontext *ctx )
  * Bring up to date any state that relies on _NeedEyeCoords.
  */
 void
-_mesa_update_tnl_spaces( GLcontext *ctx, GLuint new_state )
+_mesa_update_tnl_spaces( struct gl_context *ctx, GLuint new_state )
 {
    const GLuint oldneedeyecoords = ctx->_NeedEyeCoords;
 
@@ -1275,7 +1219,7 @@ _mesa_update_tnl_spaces( GLcontext *ctx, GLuint new_state )
  * light-in-modelspace optimization.  It's also useful for debugging.
  */
 void
-_mesa_allow_light_in_model( GLcontext *ctx, GLboolean flag )
+_mesa_allow_light_in_model( struct gl_context *ctx, GLboolean flag )
 {
    ctx->_ForceEyeCoords = !flag;
    ctx->NewState |= _NEW_POINT;        /* one of the bits from
@@ -1313,9 +1257,7 @@ init_light( struct gl_light *l, GLuint n )
    ASSIGN_4V( l->EyePosition, 0.0, 0.0, 1.0, 0.0 );
    ASSIGN_3V( l->SpotDirection, 0.0, 0.0, -1.0 );
    l->SpotExponent = 0.0;
-   _mesa_invalidate_spot_exp_table( l );
    l->SpotCutoff = 180.0;
-   l->_CosCutoffNeg = -1.0f;
    l->_CosCutoff = 0.0;                /* KW: -ve values not admitted */
    l->ConstantAttenuation = 1.0;
    l->LinearAttenuation = 0.0;
@@ -1367,7 +1309,7 @@ init_material( struct gl_material *m )
  * Initialize all lighting state for the given context.
  */
 void
-_mesa_init_lighting( GLcontext *ctx )
+_mesa_init_lighting( struct gl_context *ctx )
 {
    GLuint i;
 
@@ -1415,13 +1357,13 @@ _mesa_init_lighting( GLcontext *ctx )
  * Deallocate malloc'd lighting state attached to given context.
  */
 void
-_mesa_free_lighting_data( GLcontext *ctx )
+_mesa_free_lighting_data( struct gl_context *ctx )
 {
    struct gl_shine_tab *s, *tmps;
 
    /* Free lighting shininess exponentiation table */
    foreach_s( s, tmps, ctx->_ShineTabList ) {
-      _mesa_free( s );
+      free( s );
    }
-   _mesa_free( ctx->_ShineTabList );
+   free( ctx->_ShineTabList );
 }