]> de.git.xonotic.org Git - xonotic/netradiant.git/blobdiff - tools/quake3/q3map2/light.c
half lambert lighting by jal (option: -lightanglehl)
[xonotic/netradiant.git] / tools / quake3 / q3map2 / light.c
index 6d3b4c687c773cc09b74e9ebaf5bcd858f603630..deff96777d84b699abe112e972f1aaaaee2f182f 100644 (file)
@@ -865,7 +865,33 @@ int LightContributionToSample( trace_t *trace )
                        dist = 16.0f;
                
                /* angle attenuation */
-               angle = (light->flags & LIGHT_ATTEN_ANGLE) ? DotProduct( trace->normal, trace->direction ) : 1.0f;
+               if( light->flags & LIGHT_ATTEN_ANGLE )
+               {
+                       /* standard Lambert attenuation */
+                       float dot = DotProduct( trace->normal, trace->direction ); 
+
+                       /* twosided lighting */
+                       if( trace->twoSided )
+                               dot = fabs( dot );
+
+                       /* jal: optional half Lambert attenuation (http://developer.valvesoftware.com/wiki/Half_Lambert) */
+                       if( lightAngleHL )
+                       {
+                               if( dot > 0.001f ) // skip coplanar
+                               {
+                                       if( dot > 1.0f ) dot = 1.0f;
+                                       dot = ( dot * 0.5f ) + 0.5f;
+                                       dot *= dot;
+                               }
+                               else
+                                       dot = 0;
+                       }
+
+                       angle = dot;
+               }
+               else
+                       angle = 1.0f;
+
                if( light->angleScale != 0.0f )
                {
                        angle /= light->angleScale;
@@ -873,10 +899,6 @@ int LightContributionToSample( trace_t *trace )
                                angle = 1.0f;
                }
                
-               /* twosided lighting */
-               if( trace->twoSided )
-                       angle = fabs( angle );
-               
                /* attenuate */
                if( light->flags & LIGHT_ATTEN_LINEAR )
                {
@@ -918,15 +940,34 @@ int LightContributionToSample( trace_t *trace )
                /* get origin and direction */
                VectorAdd( trace->origin, light->origin, trace->end );
                dist = SetupTrace( trace );
-               
+
                /* angle attenuation */
-               angle = (light->flags & LIGHT_ATTEN_ANGLE)
-                       ? DotProduct( trace->normal, trace->direction )
-                       : 1.0f;
-               
-               /* twosided lighting */
-               if( trace->twoSided )
-                       angle = fabs( angle );
+               if( light->flags & LIGHT_ATTEN_ANGLE )
+               {
+                       /* standard Lambert attenuation */
+                       float dot = DotProduct( trace->normal, trace->direction ); 
+
+                       /* twosided lighting */
+                       if( trace->twoSided )
+                               dot = fabs( dot );
+
+                       /* jal: optional half Lambert attenuation (http://developer.valvesoftware.com/wiki/Half_Lambert) */
+                       if( lightAngleHL )
+                       {
+                               if( dot > 0.001f ) // skip coplanar
+                               {
+                                       if( dot > 1.0f ) dot = 1.0f;
+                                       dot = ( dot * 0.5f ) + 0.5f;
+                                       dot *= dot;
+                               }
+                               else
+                                       dot = 0;
+                       }
+                       
+                       angle = dot;
+               }
+               else
+                       angle = 1.0f;
                
                /* attenuate */
                add = light->photons * angle;
@@ -1272,15 +1313,14 @@ contribution_t;
 
 void TraceGrid( int num )
 {
-       int                                             i, j, x, y, z, mod, step, numCon, numStyles;
-       float                                   d;
+       int                                             i, j, x, y, z, mod, numCon, numStyles;
+       float                                   d, step;
        vec3_t                                  baseOrigin, cheapColor, color, thisdir;
        rawGridPoint_t                  *gp;
        bspGridPoint_t                  *bgp;
        contribution_t                  contributions[ MAX_CONTRIBUTIONS ];
        trace_t                                 trace;
        
-       
        /* get grid points */
        gp = &rawGridPoints[ num ];
        bgp = &bspGridPoints[ num ];
@@ -1311,38 +1351,21 @@ void TraceGrid( int num )
        {
                /* try to nudge the origin around to find a valid point */
                VectorCopy( trace.origin, baseOrigin );
-               for( step = 0.05; step <= 0.5; step += 0.05 )
+               for( step = 0; (step += 0.005) <= 1.0; )
                {
-                       for( i = 0; i < 8; i++ )
-                       {
-                               VectorCopy( baseOrigin, trace.origin );
-                               if( i & 1 )
-                                       trace.origin[ 0 ] += step * gridSize[0];
-                               else
-                                       trace.origin[ 0 ] -= step * gridSize[0];
+                       VectorCopy( baseOrigin, trace.origin );
+                       trace.origin[ 0 ] += step * (Random() - 0.5) * gridSize[0];
+                       trace.origin[ 1 ] += step * (Random() - 0.5) * gridSize[1];
+                       trace.origin[ 2 ] += step * (Random() - 0.5) * gridSize[2];
                                
-                               if( i & 2 )
-                                       trace.origin[ 1 ] += step * gridSize[1];
-                               else
-                                       trace.origin[ 1 ] -= step * gridSize[1];
-                               
-                               if( i & 4 )
-                                       trace.origin[ 2 ] += step * gridSize[2];
-                               else
-                                       trace.origin[ 2 ] -= step * gridSize[2];
-                               
-                               /* ydnar: changed to find cluster num */
-                               trace.cluster = ClusterForPointExt( trace.origin, VERTEX_EPSILON );
-                               if( trace.cluster >= 0 )
-                                       break;
-                       }
-                       
-                       if( i != 8 )
+                       /* ydnar: changed to find cluster num */
+                       trace.cluster = ClusterForPointExt( trace.origin, VERTEX_EPSILON );
+                       if( trace.cluster >= 0 )
                                break;
                }
                
                /* can't find a valid point at all */
-               if( step > 0.5 )
+               if( step > 1.0 )
                        return;
        }
        
@@ -1645,7 +1668,7 @@ void LightWorld( void )
        vec3_t          color;
        float           f;
        int                     b, bt;
-       qboolean        minVertex, minGrid;
+       qboolean        minVertex, minGrid, ps;
        const char      *value;
        
 
@@ -1719,7 +1742,9 @@ void LightWorld( void )
                SetupEnvelopes( qtrue, fastgrid );
                
                Sys_Printf( "--- TraceGrid ---\n" );
+               inGrid = qtrue;
                RunThreadsOnIndividual( numRawGridPoints, qtrue, TraceGrid );
+               inGrid = qfalse;
                Sys_Printf( "%d x %d x %d = %d grid\n",
                        gridBounds[ 0 ], gridBounds[ 1 ], gridBounds[ 2 ], numBSPGridPoints );
                
@@ -1815,7 +1840,9 @@ void LightWorld( void )
                        gridBoundsCulled = 0;
                        
                        Sys_Printf( "--- BounceGrid ---\n" );
+                       inGrid = qtrue;
                        RunThreadsOnIndividual( numRawGridPoints, qtrue, TraceGrid );
+                       inGrid = qfalse;
                        Sys_FPrintf( SYS_VRB, "%9d grid points envelope culled\n", gridEnvelopeCulled );
                        Sys_FPrintf( SYS_VRB, "%9d grid points bounds culled\n", gridBoundsCulled );
                }
@@ -1894,6 +1921,10 @@ int LightMain( int argc, char **argv )
        gridAmbientScale = game->gridAmbientScale;
        Sys_Printf( " lightgrid ambient scale: %f\n", gridAmbientScale );
 
+       lightAngleHL = game->lightAngleHL;
+       if( lightAngleHL )
+               Sys_Printf( " half lambert light angle attenuation enabled \n" );
+
        noStyles = game->noStyles;
        if (noStyles == qtrue)
                Sys_Printf( " shader lightstyles hack: disabled\n" );
@@ -2430,6 +2461,17 @@ int LightMain( int argc, char **argv )
                        loMem = qtrue;
                        Sys_Printf( "Enabling low-memory (potentially slower) lighting mode\n" );
                }
+               else if( !strcmp( argv[ i ], "-lightanglehl" ) )
+               {
+                       if( ( atoi( argv[ i + 1 ] ) != 0 ) != lightAngleHL )
+                       {
+                               lightAngleHL = ( atoi( argv[ i + 1 ] ) != 0 );
+                               if( lightAngleHL )
+                                       Sys_Printf( "Enabling half lambert light angle attenuation\n" );
+                               else
+                                       Sys_Printf( "Disabling half lambert light angle attenuation\n" );
+                       }
+               }
                else if( !strcmp( argv[ i ], "-nostyle" ) || !strcmp( argv[ i ], "-nostyles" ) )
                {
                        noStyles = qtrue;