Added _mesa_light() helper function so we can avoid transforming then
authorBrian Paul <brian.paul@tungstengraphics.com>
Sat, 12 Nov 2005 18:58:12 +0000 (18:58 +0000)
committerBrian Paul <brian.paul@tungstengraphics.com>
Sat, 12 Nov 2005 18:58:12 +0000 (18:58 +0000)
un-transforming light positions and spot directions when popping light
state off the attribute stack.

src/mesa/main/attrib.c
src/mesa/main/dd.h
src/mesa/main/light.c
src/mesa/main/light.h

index 9c2a666e478b813ed152e7eb186dc60d002e151d..b4a587efb04d433a18cc8e6fda44ea895f77cb80 100644 (file)
@@ -951,25 +951,21 @@ _mesa_PopAttrib(void)
                   _math_matrix_analyse( ctx->ModelviewMatrixStack.Top );
               
                for (i = 0; i < ctx->Const.MaxLights; i++) {
-                  GLenum lgt = (GLenum) (GL_LIGHT0 + i);
                  const struct gl_light *l = &light->Light[i];
-                 GLfloat tmp[4];
-                  _mesa_set_enable(ctx, lgt, l->Enabled);
-                 _mesa_Lightfv( lgt, GL_AMBIENT, l->Ambient );
-                 _mesa_Lightfv( lgt, GL_DIFFUSE, l->Diffuse );
-                 _mesa_Lightfv( lgt, GL_SPECULAR, l->Specular );
-                 TRANSFORM_POINT( tmp, ctx->ModelviewMatrixStack.Top->inv, l->EyePosition );
-                 _mesa_Lightfv( lgt, GL_POSITION, tmp );
-                 TRANSFORM_NORMAL( tmp, l->EyeDirection, ctx->ModelviewMatrixStack.Top->m );
-                 _mesa_Lightfv( lgt, GL_SPOT_DIRECTION, tmp );
-                 _mesa_Lightfv( lgt, GL_SPOT_EXPONENT, &l->SpotExponent );
-                 _mesa_Lightfv( lgt, GL_SPOT_CUTOFF, &l->SpotCutoff );
-                 _mesa_Lightfv( lgt, GL_CONSTANT_ATTENUATION, 
-                                &l->ConstantAttenuation );
-                 _mesa_Lightfv( lgt, GL_LINEAR_ATTENUATION, 
-                                &l->LinearAttenuation );
-                 _mesa_Lightfv( lgt, GL_QUADRATIC_ATTENUATION, 
-                                &l->QuadraticAttenuation );
+                  _mesa_set_enable(ctx, GL_LIGHT0 + i, l->Enabled);
+                 _mesa_light(ctx, i, GL_AMBIENT, l->Ambient);
+                 _mesa_light(ctx, i, GL_DIFFUSE, l->Diffuse);
+                 _mesa_light(ctx, i, GL_SPECULAR, l->Specular );
+                 _mesa_light(ctx, i, GL_POSITION, l->EyePosition);
+                 _mesa_light(ctx, i, GL_SPOT_DIRECTION, l->EyeDirection);
+                 _mesa_light(ctx, i, GL_SPOT_EXPONENT, &l->SpotExponent);
+                 _mesa_light(ctx, i, GL_SPOT_CUTOFF, &l->SpotCutoff);
+                 _mesa_light(ctx, i, GL_CONSTANT_ATTENUATION,
+                              &l->ConstantAttenuation);
+                 _mesa_light(ctx, i, GL_LINEAR_ATTENUATION,
+                              &l->LinearAttenuation);
+                 _mesa_light(ctx, i, GL_QUADRATIC_ATTENUATION,
+                              &l->QuadraticAttenuation);
                }
                /* light model */
                _mesa_LightModelfv(GL_LIGHT_MODEL_AMBIENT,
index d07a7a26b1e7ae0736b9cb8654da6f6c7a8f661f..f150cf99e251618c0957dcebafb6da9cb558050d 100644 (file)
@@ -658,7 +658,10 @@ struct dd_function_table {
    void (*Hint)(GLcontext *ctx, GLenum target, GLenum mode);
    /** Control the writing of individual bits in the color index buffers */
    void (*IndexMask)(GLcontext *ctx, GLuint mask);
-   /** Set light source parameters */
+   /** Set light source parameters.
+    * Note: for GL_POSITION and GL_SPOT_DIRECTION, params will have already
+    * been transformed to eye-space.
+    */
    void (*Lightfv)(GLcontext *ctx, GLenum light,
                   GLenum pname, const GLfloat *params );
    /** Set the lighting model parameters */
index f9b59089acdc3260f41f0e0d35fa340092439295..8fa62eb873434a25b919f6463ba75fcfd58245f0 100644 (file)
@@ -59,6 +59,112 @@ _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 )
 {
@@ -71,124 +177,69 @@ _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 (_math_matrix_is_dirty(ctx->ModelviewMatrixStack.Top)) {
-        _math_matrix_analyse( 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);
 }
 
 
index 1f19019450fcd710644213e5bae4d30158116087..f47fe58a83932f832b9a4358d8ba3f5c1521f1cc 100644 (file)
@@ -78,6 +78,10 @@ extern void GLAPIENTRY
 _mesa_GetMaterialiv( GLenum face, GLenum pname, GLint *params );
 
 
+extern void
+_mesa_light(GLcontext *ctx, GLuint lnum, GLenum pname, const GLfloat *params);
+
+
 /* Lerp between adjacent values in the f(x) lookup table, giving a
  * continuous function, with adequeate overall accuracy.  (Though
  * still pretty good compared to a straight lookup).