More GL_EXT_framebuffer_object: rename some things, added device driver hooks.
[mesa.git] / src / mesa / main / light.c
index c9e0fbd73a8cbd32b17097f990c6db1cee0dcd2c..4c39d2ff4c97befd75b5670857ebc28a411f6667 100644 (file)
@@ -1,9 +1,8 @@
-
 /*
  * Mesa 3-D graphics library
- * Version:  5.1
+ * Version:  6.1
  *
- * Copyright (C) 1999-2003  Brian Paul   All Rights Reserved.
+ * Copyright (C) 1999-2004  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,15 +59,14 @@ _mesa_ShadeModel( GLenum mode )
 }
 
 
-
-void
+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);
@@ -202,14 +192,14 @@ _mesa_Lightfv( GLenum light, GLenum pname, const GLfloat *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 +241,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 +291,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 +361,7 @@ _mesa_GetLightiv( GLenum light, GLenum pname, GLint *params )
 /**********************************************************************/
 
 
-void
+void GLAPIENTRY
 _mesa_LightModelfv( GLenum pname, const GLfloat *params )
 {
    GLenum newenum;
@@ -420,14 +409,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 +420,7 @@ _mesa_LightModelfv( GLenum pname, const GLfloat *params )
 }
 
 
-void
+void GLAPIENTRY
 _mesa_LightModeliv( GLenum pname, const GLint *params )
 {
    GLfloat fparam[4];
@@ -464,14 +445,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 +525,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 +541,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 +626,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 +641,7 @@ void _mesa_update_color_material( GLcontext *ctx,
 }
 
 
-
-
-void
+void GLAPIENTRY
 _mesa_ColorMaterial( GLenum face, GLenum mode )
 {
    GET_CURRENT_CONTEXT(ctx);
@@ -698,10 +679,7 @@ _mesa_ColorMaterial( GLenum face, GLenum mode )
 }
 
 
-
-
-
-void
+void GLAPIENTRY
 _mesa_GetMaterialfv( GLenum face, GLenum pname, GLfloat *params )
 {
    GET_CURRENT_CONTEXT(ctx);
@@ -709,6 +687,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 +727,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 +735,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 +787,6 @@ _mesa_GetMaterialiv( GLenum face, GLenum pname, GLint *params )
 
 
 
-
 /**********************************************************************/
 /*****                  Lighting computation                      *****/
 /**********************************************************************/
@@ -856,7 +836,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 +866,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 +923,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];
@@ -966,7 +953,6 @@ _mesa_validate_all_lighting_tables( GLcontext *ctx )
 
 
 
-
 /*
  * Examine current lighting parameters to determine if the optimized lighting
  * function can be used.
@@ -1046,7 +1032,7 @@ _mesa_update_lighting( GLcontext *ctx )
  * 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 };
@@ -1117,8 +1103,6 @@ _mesa_compute_light_positions( GLcontext *ctx )
 
 
 
-
-
 static void
 update_modelview_scale( GLcontext *ctx )
 {
@@ -1140,10 +1124,12 @@ update_modelview_scale( GLcontext *ctx )
 
 /* Bring uptodate 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;
 
+   (void) new_state;
    ctx->_NeedEyeCoords = 0;
 
    if (ctx->_ForceEyeCoords ||
@@ -1165,7 +1151,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,7 +1166,7 @@ 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 );
    }
 }
 
@@ -1191,7 +1177,7 @@ void _mesa_update_tnl_spaces( GLcontext *ctx, GLuint new_state )
 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 +1222,7 @@ init_light( struct gl_light *l, GLuint n )
    l->Enabled = GL_FALSE;
 }
 
+
 /**
  * Initialize the light model data structure.
  *
@@ -1250,6 +1237,7 @@ init_lightmodel( struct gl_lightmodel *lm )
    lm->ColorControl = GL_SINGLE_COLOR;
 }
 
+
 /**
  * Initialize the material data structure.
  * 
@@ -1274,12 +1262,13 @@ init_material( struct gl_material *m )
 }
 
 
-void _mesa_init_lighting( GLcontext *ctx )
+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 +1281,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,8 +1297,6 @@ void _mesa_init_lighting( GLcontext *ctx )
       insert_at_tail( ctx->_ShineTabList, s );
    }
 
-   
-
    /* Miscellaneous */
    ctx->Light._NeedEyeCoords = 0;
    ctx->_NeedEyeCoords = 0;
@@ -1315,7 +1304,8 @@ void _mesa_init_lighting( GLcontext *ctx )
 }
 
 
-void _mesa_free_lighting_data( GLcontext *ctx )
+void
+_mesa_free_lighting_data( GLcontext *ctx )
 {
    struct gl_shine_tab *s, *tmps;