Fix pow <small> and a very stypid bug with dummy srcs(0 equals to tmp0.x)</small...
[mesa.git] / src / mesa / main / light.c
index c9e0fbd73a8cbd32b17097f990c6db1cee0dcd2c..8fa62eb873434a25b919f6463ba75fcfd58245f0 100644 (file)
@@ -1,9 +1,8 @@
-
 /*
  * Mesa 3-D graphics library
- * Version:  5.1
+ * Version:  6.5
  *
- * Copyright (C) 1999-2003  Brian Paul   All Rights Reserved.
+ * Copyright (C) 1999-2005  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"),
 
 #include "glheader.h"
 #include "imports.h"
-#include "colormac.h"
 #include "context.h"
 #include "enums.h"
 #include "light.h"
 #include "macros.h"
 #include "simple_list.h"
 #include "mtypes.h"
-#include "math/m_xform.h"
 #include "math/m_matrix.h"
 
 
-/* XXX this is a bit of a hack needed for compilation within XFree86 */
-#ifndef FLT_MIN
-#define FLT_MIN 1e-37
-#endif
-
-
-void
+void GLAPIENTRY
 _mesa_ShadeModel( GLenum mode )
 {
    GET_CURRENT_CONTEXT(ctx);
@@ -68,148 +59,198 @@ _mesa_ShadeModel( GLenum mode )
 }
 
 
-
+/**
+ * Helper function called by _mesa_Lightfv and _mesa_PopAttrib to set
+ * per-light state.
+ * For GL_POSITION and GL_SPOT_DIRECTION the params position/direction
+ * will have already been transformed by the modelview matrix!
+ * Also, all error checking should have already been done.
+ */
 void
+_mesa_light(GLcontext *ctx, GLuint lnum, GLenum pname, const GLfloat *params)
+{
+   struct gl_light *light;
+
+   ASSERT(lnum < MAX_LIGHTS);
+   light = &ctx->Light.Light[lnum];
+
+   switch (pname) {
+   case GL_AMBIENT:
+      if (TEST_EQ_4V(light->Ambient, params))
+        return;
+      FLUSH_VERTICES(ctx, _NEW_LIGHT);
+      COPY_4V( light->Ambient, params );
+      break;
+   case GL_DIFFUSE:
+      if (TEST_EQ_4V(light->Diffuse, params))
+        return;
+      FLUSH_VERTICES(ctx, _NEW_LIGHT);
+      COPY_4V( light->Diffuse, params );
+      break;
+   case GL_SPECULAR:
+      if (TEST_EQ_4V(light->Specular, params))
+        return;
+      FLUSH_VERTICES(ctx, _NEW_LIGHT);
+      COPY_4V( light->Specular, params );
+      break;
+   case GL_POSITION:
+      /* NOTE: position has already been transformed by ModelView! */
+      if (TEST_EQ_4V(light->EyePosition, params))
+        return;
+      FLUSH_VERTICES(ctx, _NEW_LIGHT);
+      COPY_4V(light->EyePosition, params);
+      if (light->EyePosition[3] != 0.0F)
+        light->_Flags |= LIGHT_POSITIONAL;
+      else
+        light->_Flags &= ~LIGHT_POSITIONAL;
+      break;
+   case GL_SPOT_DIRECTION:
+      /* NOTE: Direction already transformed by inverse ModelView! */
+      if (TEST_EQ_3V(light->EyeDirection, params))
+        return;
+      FLUSH_VERTICES(ctx, _NEW_LIGHT);
+      COPY_3V(light->EyeDirection, params);
+      break;
+   case GL_SPOT_EXPONENT:
+      ASSERT(params[0] >= 0.0);
+      ASSERT(params[0] <= ctx->Const.MaxSpotExponent);
+      if (light->SpotExponent == params[0])
+        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));
+      if (light->SpotCutoff == params[0])
+        return;
+      FLUSH_VERTICES(ctx, _NEW_LIGHT);
+      light->SpotCutoff = params[0];
+      light->_CosCutoff = (GLfloat) _mesa_cos(params[0]*DEG2RAD);
+      if (light->_CosCutoff < 0)
+        light->_CosCutoff = 0;
+      if (light->SpotCutoff != 180.0F)
+        light->_Flags |= LIGHT_SPOT;
+      else
+        light->_Flags &= ~LIGHT_SPOT;
+      break;
+   case GL_CONSTANT_ATTENUATION:
+      ASSERT(params[0] >= 0.0);
+      if (light->ConstantAttenuation == params[0])
+        return;
+      FLUSH_VERTICES(ctx, _NEW_LIGHT);
+      light->ConstantAttenuation = params[0];
+      break;
+   case GL_LINEAR_ATTENUATION:
+      ASSERT(params[0] >= 0.0);
+      if (light->LinearAttenuation == params[0])
+        return;
+      FLUSH_VERTICES(ctx, _NEW_LIGHT);
+      light->LinearAttenuation = params[0];
+      break;
+   case GL_QUADRATIC_ATTENUATION:
+      ASSERT(params[0] >= 0.0);
+      if (light->QuadraticAttenuation == params[0])
+        return;
+      FLUSH_VERTICES(ctx, _NEW_LIGHT);
+      light->QuadraticAttenuation = params[0];
+      break;
+   default:
+      _mesa_problem(ctx, "Unexpected pname in _mesa_light()");
+      return;
+   }
+
+   if (ctx->Driver.Lightfv)
+      ctx->Driver.Lightfv( ctx, GL_LIGHT0 + lnum, pname, params );
+}
+
+
+void GLAPIENTRY
 _mesa_Lightf( GLenum light, GLenum pname, GLfloat param )
 {
    _mesa_Lightfv( light, pname, &param );
 }
 
 
-void
+void GLAPIENTRY
 _mesa_Lightfv( GLenum light, GLenum pname, const GLfloat *params )
 {
    GET_CURRENT_CONTEXT(ctx);
    GLint i = (GLint) (light - GL_LIGHT0);
-   struct gl_light *l = &ctx->Light.Light[i];
+   GLfloat temp[4];
 
    if (i < 0 || i >= (GLint) ctx->Const.MaxLights) {
       _mesa_error( ctx, GL_INVALID_ENUM, "glLight(light=0x%x)", light );
       return;
    }
 
+   /* do particular error checks, transformations */
    switch (pname) {
    case GL_AMBIENT:
-      if (TEST_EQ_4V(l->Ambient, params))
-        return;
-      FLUSH_VERTICES(ctx, _NEW_LIGHT);
-      COPY_4V( l->Ambient, params );
-      break;
    case GL_DIFFUSE:
-      if (TEST_EQ_4V(l->Diffuse, params))
-        return;
-      FLUSH_VERTICES(ctx, _NEW_LIGHT);
-      COPY_4V( l->Diffuse, params );
-      break;
    case GL_SPECULAR:
-      if (TEST_EQ_4V(l->Specular, params))
-        return;
-      FLUSH_VERTICES(ctx, _NEW_LIGHT);
-      COPY_4V( l->Specular, params );
+      /* nothing */
       break;
-   case GL_POSITION: {
-      GLfloat tmp[4];
+   case GL_POSITION:
       /* transform position by ModelView matrix */
-      TRANSFORM_POINT( tmp, ctx->ModelviewMatrixStack.Top->m, params );
-      if (TEST_EQ_4V(l->EyePosition, tmp))
-        return;
-      FLUSH_VERTICES(ctx, _NEW_LIGHT);
-      COPY_4V(l->EyePosition, tmp);
-      if (l->EyePosition[3] != 0.0F)
-        l->_Flags |= LIGHT_POSITIONAL;
-      else
-        l->_Flags &= ~LIGHT_POSITIONAL;
+      TRANSFORM_POINT(temp, ctx->ModelviewMatrixStack.Top->m, params);
+      params = temp;
       break;
-   }
-   case GL_SPOT_DIRECTION: {
-      GLfloat tmp[4];
+   case GL_SPOT_DIRECTION:
       /* transform direction by inverse modelview */
-      if (ctx->ModelviewMatrixStack.Top->flags & MAT_DIRTY_INVERSE) {
-        _math_matrix_analyse( ctx->ModelviewMatrixStack.Top );
+      if (_math_matrix_is_dirty(ctx->ModelviewMatrixStack.Top)) {
+        _math_matrix_analyse(ctx->ModelviewMatrixStack.Top);
       }
-      TRANSFORM_NORMAL( tmp, params, ctx->ModelviewMatrixStack.Top->inv );
-      if (TEST_EQ_3V(l->EyeDirection, tmp))
-        return;
-      FLUSH_VERTICES(ctx, _NEW_LIGHT);
-      COPY_3V(l->EyeDirection, tmp);
+      TRANSFORM_NORMAL(temp, params, ctx->ModelviewMatrixStack.Top->inv);
+      params = temp;
       break;
-   }
    case GL_SPOT_EXPONENT:
-      if (params[0]<0.0 || params[0]>ctx->Const.MaxSpotExponent) {
-        _mesa_error( ctx, GL_INVALID_VALUE, "glLight" );
+      if (params[0] < 0.0 || params[0] > ctx->Const.MaxSpotExponent) {
+        _mesa_error(ctx, GL_INVALID_VALUE, "glLight");
         return;
       }
-      if (l->SpotExponent == params[0])
-        return;
-      FLUSH_VERTICES(ctx, _NEW_LIGHT);
-      l->SpotExponent = params[0];
-      _mesa_invalidate_spot_exp_table( l );
       break;
    case GL_SPOT_CUTOFF:
-      if ((params[0]<0.0 || params[0]>90.0) && params[0]!=180.0) {
-        _mesa_error( ctx, GL_INVALID_VALUE, "glLight" );
+      if ((params[0] < 0.0 || params[0] > 90.0) && params[0] != 180.0) {
+        _mesa_error(ctx, GL_INVALID_VALUE, "glLight");
         return;
       }
-      if (l->SpotCutoff == params[0])
-        return;
-      FLUSH_VERTICES(ctx, _NEW_LIGHT);
-      l->SpotCutoff = params[0];
-      l->_CosCutoff = (GLfloat) _mesa_cos(params[0]*DEG2RAD);
-      if (l->_CosCutoff < 0)
-        l->_CosCutoff = 0;
-      if (l->SpotCutoff != 180.0F)
-        l->_Flags |= LIGHT_SPOT;
-      else
-        l->_Flags &= ~LIGHT_SPOT;
       break;
    case GL_CONSTANT_ATTENUATION:
-      if (params[0]<0.0) {
-        _mesa_error( ctx, GL_INVALID_VALUE, "glLight" );
+      if (params[0] < 0.0) {
+        _mesa_error(ctx, GL_INVALID_VALUE, "glLight");
         return;
       }
-      if (l->ConstantAttenuation == params[0])
-        return;
-      FLUSH_VERTICES(ctx, _NEW_LIGHT);
-      l->ConstantAttenuation = params[0];
       break;
    case GL_LINEAR_ATTENUATION:
-      if (params[0]<0.0) {
-        _mesa_error( ctx, GL_INVALID_VALUE, "glLight" );
+      if (params[0] < 0.0) {
+        _mesa_error(ctx, GL_INVALID_VALUE, "glLight");
         return;
       }
-      if (l->LinearAttenuation == params[0])
-        return;
-      FLUSH_VERTICES(ctx, _NEW_LIGHT);
-      l->LinearAttenuation = params[0];
       break;
    case GL_QUADRATIC_ATTENUATION:
-      if (params[0]<0.0) {
-        _mesa_error( ctx, GL_INVALID_VALUE, "glLight" );
+      if (params[0] < 0.0) {
+        _mesa_error(ctx, GL_INVALID_VALUE, "glLight");
         return;
       }
-      if (l->QuadraticAttenuation == params[0])
-        return;
-      FLUSH_VERTICES(ctx, _NEW_LIGHT);
-      l->QuadraticAttenuation = params[0];
       break;
    default:
-      _mesa_error( ctx, GL_INVALID_ENUM, "glLight(pname=0x%x)", pname );
+      _mesa_error(ctx, GL_INVALID_ENUM, "glLight(pname=0x%x)", pname);
       return;
    }
 
-   if (ctx->Driver.Lightfv)
-      ctx->Driver.Lightfv( ctx, light, pname, params );
+   _mesa_light(ctx, i, pname, params);
 }
 
 
-void
+void GLAPIENTRY
 _mesa_Lighti( GLenum light, GLenum pname, GLint param )
 {
    _mesa_Lightiv( light, pname, &param );
 }
 
 
-void
+void GLAPIENTRY
 _mesa_Lightiv( GLenum light, GLenum pname, const GLint *params )
 {
    GLfloat fparam[4];
@@ -251,7 +292,7 @@ _mesa_Lightiv( GLenum light, GLenum pname, const GLint *params )
 
 
 
-void
+void GLAPIENTRY
 _mesa_GetLightfv( GLenum light, GLenum pname, GLfloat *params )
 {
    GET_CURRENT_CONTEXT(ctx);
@@ -301,8 +342,7 @@ _mesa_GetLightfv( GLenum light, GLenum pname, GLfloat *params )
 }
 
 
-
-void
+void GLAPIENTRY
 _mesa_GetLightiv( GLenum light, GLenum pname, GLint *params )
 {
    GET_CURRENT_CONTEXT(ctx);
@@ -372,7 +412,7 @@ _mesa_GetLightiv( GLenum light, GLenum pname, GLint *params )
 /**********************************************************************/
 
 
-void
+void GLAPIENTRY
 _mesa_LightModelfv( GLenum pname, const GLfloat *params )
 {
    GLenum newenum;
@@ -420,14 +460,6 @@ _mesa_LightModelfv( GLenum pname, const GLfloat *params )
            return;
         FLUSH_VERTICES(ctx, _NEW_LIGHT);
         ctx->Light.Model.ColorControl = newenum;
-
-        if ((ctx->Light.Enabled &&
-             ctx->Light.Model.ColorControl==GL_SEPARATE_SPECULAR_COLOR)
-            || ctx->Fog.ColorSumEnabled)
-           ctx->_TriangleCaps |= DD_SEPARATE_SPECULAR;
-        else
-           ctx->_TriangleCaps &= ~DD_SEPARATE_SPECULAR;
-
          break;
       default:
          _mesa_error( ctx, GL_INVALID_ENUM, "glLightModel(pname=0x%x)", pname );
@@ -439,7 +471,7 @@ _mesa_LightModelfv( GLenum pname, const GLfloat *params )
 }
 
 
-void
+void GLAPIENTRY
 _mesa_LightModeliv( GLenum pname, const GLint *params )
 {
    GLfloat fparam[4];
@@ -464,14 +496,14 @@ _mesa_LightModeliv( GLenum pname, const GLint *params )
 }
 
 
-void
+void GLAPIENTRY
 _mesa_LightModeli( GLenum pname, GLint param )
 {
    _mesa_LightModeliv( pname, &param );
 }
 
 
-void
+void GLAPIENTRY
 _mesa_LightModelf( GLenum pname, GLfloat param )
 {
    _mesa_LightModelfv( pname, &param );
@@ -544,9 +576,10 @@ _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 )
+void
+_mesa_copy_materials( struct gl_material *dst,
+                      const struct gl_material *src,
+                      GLuint bitmask )
 {
    int i;
 
@@ -559,7 +592,8 @@ void _mesa_copy_materials( struct gl_material *dst,
 
 /* Update derived values following a change in ctx->Light.Material
  */
-void _mesa_update_material( GLcontext *ctx, GLuint bitmask )
+void
+_mesa_update_material( GLcontext *ctx, GLuint bitmask )
 {
    struct gl_light *light, *list = &ctx->Light.EnabledList;
    GLfloat (*mat)[4] = ctx->Light.Material.Attrib;
@@ -643,8 +677,8 @@ void _mesa_update_material( GLcontext *ctx, GLuint bitmask )
  * according to the bitmask in ColorMaterialBitmask, which is
  * set by glColorMaterial().
  */
-void _mesa_update_color_material( GLcontext *ctx,
-                                 const GLfloat color[4] )
+void
+_mesa_update_color_material( GLcontext *ctx, const GLfloat color[4] )
 {
    GLuint bitmask = ctx->Light.ColorMaterialBitmask;
    struct gl_material *mat = &ctx->Light.Material;
@@ -658,9 +692,7 @@ void _mesa_update_color_material( GLcontext *ctx,
 }
 
 
-
-
-void
+void GLAPIENTRY
 _mesa_ColorMaterial( GLenum face, GLenum mode )
 {
    GET_CURRENT_CONTEXT(ctx);
@@ -698,10 +730,7 @@ _mesa_ColorMaterial( GLenum face, GLenum mode )
 }
 
 
-
-
-
-void
+void GLAPIENTRY
 _mesa_GetMaterialfv( GLenum face, GLenum pname, GLfloat *params )
 {
    GET_CURRENT_CONTEXT(ctx);
@@ -709,6 +738,8 @@ _mesa_GetMaterialfv( GLenum face, GLenum pname, GLfloat *params )
    GLfloat (*mat)[4] = ctx->Light.Material.Attrib;
    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); /* update materials */
 
+   FLUSH_CURRENT(ctx, 0); /* update ctx->Light.Material from vertex buffer */
+
    if (face==GL_FRONT) {
       f = 0;
    }
@@ -747,8 +778,7 @@ _mesa_GetMaterialfv( GLenum face, GLenum pname, GLfloat *params )
 }
 
 
-
-void
+void GLAPIENTRY
 _mesa_GetMaterialiv( GLenum face, GLenum pname, GLint *params )
 {
    GET_CURRENT_CONTEXT(ctx);
@@ -756,6 +786,8 @@ _mesa_GetMaterialiv( GLenum face, GLenum pname, GLint *params )
    GLfloat (*mat)[4] = ctx->Light.Material.Attrib;
    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); /* update materials */
 
+   FLUSH_CURRENT(ctx, 0); /* update ctx->Light.Material from vertex buffer */
+
    if (face==GL_FRONT) {
       f = 0;
    }
@@ -806,7 +838,6 @@ _mesa_GetMaterialiv( GLenum face, GLenum pname, GLint *params )
 
 
 
-
 /**********************************************************************/
 /*****                  Lighting computation                      *****/
 /**********************************************************************/
@@ -856,7 +887,9 @@ _mesa_invalidate_spot_exp_table( struct gl_light *l )
    l->_SpotExpTable[0][0] = -1;
 }
 
-static void validate_spot_exp_table( struct gl_light *l )
+
+static void
+validate_spot_exp_table( struct gl_light *l )
 {
    GLint i;
    GLdouble exponent = l->SpotExponent;
@@ -884,24 +917,28 @@ static void validate_spot_exp_table( struct gl_light *l )
 
 
 
-
 /* 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 i )
+_mesa_invalidate_shine_table( GLcontext *ctx, GLuint side )
 {
-   if (ctx->_ShineTable[i])
-      ctx->_ShineTable[i]->refcount--;
-   ctx->_ShineTable[i] = 0;
+   ASSERT(side < 2);
+   if (ctx->_ShineTable[side])
+      ctx->_ShineTable[side]->refcount--;
+   ctx->_ShineTable[side] = NULL;
 }
 
-static void validate_shine_table( GLcontext *ctx, GLuint i, GLfloat shininess )
+
+static void
+validate_shine_table( GLcontext *ctx, GLuint side, GLfloat shininess )
 {
    struct gl_shine_tab *list = ctx->_ShineTabList;
    struct gl_shine_tab *s;
 
+   ASSERT(side < 2);
+
    foreach(s, list)
       if ( s->shininess == shininess )
         break;
@@ -937,18 +974,19 @@ static void validate_shine_table( GLcontext *ctx, GLuint i, GLfloat shininess )
       s->shininess = shininess;
    }
 
-   if (ctx->_ShineTable[i])
-      ctx->_ShineTable[i]->refcount--;
+   if (ctx->_ShineTable[side])
+      ctx->_ShineTable[side]->refcount--;
 
-   ctx->_ShineTable[i] = s;
+   ctx->_ShineTable[side] = s;
    move_to_tail( list, s );
    s->refcount++;
 }
 
+
 void
 _mesa_validate_all_lighting_tables( GLcontext *ctx )
 {
-   GLint i;
+   GLuint i;
    GLfloat shininess;
    
    shininess = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SHININESS][0];
@@ -959,15 +997,13 @@ _mesa_validate_all_lighting_tables( GLcontext *ctx )
    if (!ctx->_ShineTable[1] || ctx->_ShineTable[1]->shininess != shininess)
       validate_shine_table( ctx, 1, shininess );
 
-   for (i = 0 ; i < MAX_LIGHTS ; i++)
+   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] );
 }
 
 
-
-
-/*
+/**
  * Examine current lighting parameters to determine if the optimized lighting
  * function can be used.
  * Also, precompute some lighting values such as the products of light
@@ -977,7 +1013,7 @@ void
 _mesa_update_lighting( GLcontext *ctx )
 {
    struct gl_light *light;
-   ctx->Light._NeedEyeCoords = 0;
+   ctx->Light._NeedEyeCoords = GL_FALSE;
    ctx->Light._Flags = 0;
 
    if (!ctx->Light.Enabled)
@@ -995,8 +1031,6 @@ _mesa_update_lighting( GLcontext *ctx )
    ctx->Light._NeedEyeCoords = ((ctx->Light._Flags & LIGHT_POSITIONAL) ||
                                ctx->Light.Model.LocalViewer);
 
-
-
    /* XXX: This test is overkill & needs to be fixed both for software and
     * hardware t&l drivers.  The above should be sufficient & should
     * be tested to verify this.
@@ -1004,7 +1038,6 @@ _mesa_update_lighting( GLcontext *ctx )
    if (ctx->Light._NeedVertices)
       ctx->Light._NeedEyeCoords = GL_TRUE;
 
-
    /* Precompute some shading values.  Although we reference
     * Light.Material here, we can get away without flushing
     * FLUSH_UPDATE_CURRENT, as when any outstanding material changes
@@ -1038,15 +1071,18 @@ _mesa_update_lighting( GLcontext *ctx )
 }
 
 
-/* _NEW_MODELVIEW
- * _NEW_LIGHT
- * _TNL_NEW_NEED_EYE_COORDS
+/**
+ * Update state derived from light position, spot direction.
+ * Called upon:
+ *   _NEW_MODELVIEW
+ *   _NEW_LIGHT
+ *   _TNL_NEW_NEED_EYE_COORDS
  *
  * Update on (_NEW_MODELVIEW | _NEW_LIGHT) when lighting is enabled.
  * Also update on lighting space changes.
  */
 static void
-_mesa_compute_light_positions( GLcontext *ctx )
+compute_light_positions( GLcontext *ctx )
 {
    struct gl_light *light;
    static const GLfloat eye_z[3] = { 0, 0, 1 };
@@ -1064,9 +1100,11 @@ _mesa_compute_light_positions( GLcontext *ctx )
    foreach (light, &ctx->Light.EnabledList) {
 
       if (ctx->_NeedEyeCoords) {
+         /* _Position is in eye coordinate space */
         COPY_4FV( light->_Position, light->EyePosition );
       }
       else {
+         /* _Position is in object coordinate space */
         TRANSFORM_POINT( light->_Position, ctx->ModelviewMatrixStack.Top->inv,
                          light->EyePosition );
       }
@@ -1117,16 +1155,11 @@ _mesa_compute_light_positions( GLcontext *ctx )
 
 
 
-
-
 static void
 update_modelview_scale( GLcontext *ctx )
 {
    ctx->_ModelViewInvScale = 1.0F;
-   if (ctx->ModelviewMatrixStack.Top->flags & (MAT_FLAG_UNIFORM_SCALE |
-                              MAT_FLAG_GENERAL_SCALE |
-                              MAT_FLAG_GENERAL_3D |
-                              MAT_FLAG_GENERAL) ) {
+   if (!_math_matrix_is_length_preserving(ctx->ModelviewMatrixStack.Top)) {
       const GLfloat *m = ctx->ModelviewMatrixStack.Top->inv;
       GLfloat f = m[2] * m[2] + m[6] * m[6] + m[10] * m[10];
       if (f < 1e-12) f = 1.0;
@@ -1138,25 +1171,26 @@ update_modelview_scale( GLcontext *ctx )
 }
 
 
-/* Bring uptodate any state that relies on _NeedEyeCoords.
+/**
+ * Bring up to date any state that relies on _NeedEyeCoords.
  */
-void _mesa_update_tnl_spaces( GLcontext *ctx, GLuint new_state )
+void
+_mesa_update_tnl_spaces( GLcontext *ctx, GLuint new_state )
 {
    const GLuint oldneedeyecoords = ctx->_NeedEyeCoords;
 
-   ctx->_NeedEyeCoords = 0;
+   (void) new_state;
+   ctx->_NeedEyeCoords = GL_FALSE;
 
    if (ctx->_ForceEyeCoords ||
        (ctx->Texture._GenFlags & TEXGEN_NEED_EYE_COORD) ||
        ctx->Point._Attenuated ||
        ctx->Light._NeedEyeCoords)
-      ctx->_NeedEyeCoords = 1;
+      ctx->_NeedEyeCoords = GL_TRUE;
 
    if (ctx->Light.Enabled &&
-       !TEST_MAT_FLAGS( ctx->ModelviewMatrixStack.Top, 
-                       MAT_FLAGS_LENGTH_PRESERVING))
-      ctx->_NeedEyeCoords = 1;
-
+       !_math_matrix_is_length_preserving(ctx->ModelviewMatrixStack.Top))
+      ctx->_NeedEyeCoords = GL_TRUE;
 
    /* Check if the truth-value interpretations of the bitfields have
     * changed:
@@ -1165,7 +1199,7 @@ void _mesa_update_tnl_spaces( GLcontext *ctx, GLuint new_state )
       /* Recalculate all state that depends on _NeedEyeCoords.
        */
       update_modelview_scale(ctx);
-      _mesa_compute_light_positions( ctx );
+      compute_light_positions( ctx );
 
       if (ctx->Driver.LightingSpaceChange)
         ctx->Driver.LightingSpaceChange( ctx );
@@ -1180,18 +1214,19 @@ void _mesa_update_tnl_spaces( GLcontext *ctx, GLuint new_state )
         update_modelview_scale(ctx);
 
       if (new_state & (_NEW_LIGHT|_NEW_MODELVIEW))
-        _mesa_compute_light_positions( ctx );
+        compute_light_positions( ctx );
    }
 }
 
 
-/* Drivers may need this if the hardware tnl unit doesn't support the
+/**
+ * Drivers may need this if the hardware tnl unit doesn't support the
  * light-in-modelspace optimization.  It's also useful for debugging.
  */
 void
 _mesa_allow_light_in_model( GLcontext *ctx, GLboolean flag )
 {
-   ctx->_ForceEyeCoords = flag;
+   ctx->_ForceEyeCoords = !flag;
    ctx->NewState |= _NEW_POINT;        /* one of the bits from
                                 * _MESA_NEW_NEED_EYE_COORDS.
                                 */
@@ -1236,6 +1271,7 @@ init_light( struct gl_light *l, GLuint n )
    l->Enabled = GL_FALSE;
 }
 
+
 /**
  * Initialize the light model data structure.
  *
@@ -1250,6 +1286,7 @@ init_lightmodel( struct gl_lightmodel *lm )
    lm->ColorControl = GL_SINGLE_COLOR;
 }
 
+
 /**
  * Initialize the material data structure.
  * 
@@ -1274,12 +1311,16 @@ init_material( struct gl_material *m )
 }
 
 
-void _mesa_init_lighting( GLcontext *ctx )
+/**
+ * Initialize all lighting state for the given context.
+ */
+void
+_mesa_init_lighting( GLcontext *ctx )
 {
-   int i;
+   GLuint i;
 
    /* Lighting group */
-   for (i=0;i<MAX_LIGHTS;i++) {
+   for (i = 0; i < MAX_LIGHTS; i++) {
       init_light( &ctx->Light.Light[i], i );
    }
    make_empty_list( &ctx->Light.EnabledList );
@@ -1292,13 +1333,15 @@ void _mesa_init_lighting( GLcontext *ctx )
    ctx->Light.ColorMaterialMode = GL_AMBIENT_AND_DIFFUSE;
    ctx->Light.ColorMaterialBitmask = _mesa_material_bitmask( ctx,
                                                GL_FRONT_AND_BACK,
-                                               GL_AMBIENT_AND_DIFFUSE, ~0, 0 );
+                                               GL_AMBIENT_AND_DIFFUSE, ~0,
+                                               NULL );
 
    ctx->Light.ColorMaterialEnabled = GL_FALSE;
 
    /* Lighting miscellaneous */
    ctx->_ShineTabList = MALLOC_STRUCT( gl_shine_tab );
    make_empty_list( ctx->_ShineTabList );
+   /* Allocate 10 (arbitrary) shininess lookup tables */
    for (i = 0 ; i < 10 ; i++) {
       struct gl_shine_tab *s = MALLOC_STRUCT( gl_shine_tab );
       s->shininess = -1;
@@ -1306,22 +1349,24 @@ void _mesa_init_lighting( GLcontext *ctx )
       insert_at_tail( ctx->_ShineTabList, s );
    }
 
-   
-
    /* Miscellaneous */
-   ctx->Light._NeedEyeCoords = 0;
-   ctx->_NeedEyeCoords = 0;
+   ctx->Light._NeedEyeCoords = GL_FALSE;
+   ctx->_NeedEyeCoords = GL_FALSE;
    ctx->_ModelViewInvScale = 1.0;
 }
 
 
-void _mesa_free_lighting_data( GLcontext *ctx )
+/**
+ * Deallocate malloc'd lighting state attached to given context.
+ */
+void
+_mesa_free_lighting_data( GLcontext *ctx )
 {
    struct gl_shine_tab *s, *tmps;
 
    /* Free lighting shininess exponentiation table */
    foreach_s( s, tmps, ctx->_ShineTabList ) {
-      FREE( s );
+      _mesa_free( s );
    }
-   FREE( ctx->_ShineTabList );
+   _mesa_free( ctx->_ShineTabList );
 }