]> git.xonotic.org Git - xonotic/netradiant.git/blobdiff - tools/quake3/q3map2/light_ydnar.c
Merge commit '39f598c5f44010cc32f1b445b12cb088a72bbc50' into master-merge
[xonotic/netradiant.git] / tools / quake3 / q3map2 / light_ydnar.c
index 19d1b31a6d1f8c291fbd2188a38b4151e8fea31e..aa71f82696ea053be8900fa8a2b5da80b535fba0 100644 (file)
@@ -55,6 +55,8 @@ void ColorToBytes( const float *color, byte *colorBytes, float scale ){
        if ( scale <= 0.0f ) {
                scale = 1.0f;
        }
+       /* globally */
+       scale *= lightmapBrightness;
 
        /* make a local copy */
        VectorScale( color, scale, sample );
@@ -119,6 +121,23 @@ void ColorToBytes( const float *color, byte *colorBytes, float scale ){
        /* compensate for ingame overbrighting/bitshifting */
        VectorScale( sample, ( 1.0f / lightmapCompensate ), sample );
 
+       /* contrast */
+       if ( lightmapContrast != 1.0f ){
+               for ( i = 0; i < 3; i++ ){
+                       sample[i] = lightmapContrast * ( sample[i] - 128 ) + 128;
+                       if ( sample[i] < 0 ){
+                               sample[i] = 0;
+                       }
+               }
+               if ( ( sample[0] > 255 ) || ( sample[1] > 255 ) || ( sample[2] > 255 ) ) {
+                       max = sample[0] > sample[1] ? sample[0] : sample[1];
+                       max = max > sample[2] ? max : sample[2];
+                       sample[0] = sample[0] * 255 / max;
+                       sample[1] = sample[1] * 255 / max;
+                       sample[2] = sample[2] * 255 / max;
+               }
+       }
+
        /* sRGB lightmaps */
        if ( lightmapsRGB ) {
                sample[0] = floor( Image_sRGBFloatFromLinearFloat( sample[0] * ( 1.0 / 255.0 ) ) * 255.0 + 0.5 );
@@ -132,6 +151,19 @@ void ColorToBytes( const float *color, byte *colorBytes, float scale ){
        colorBytes[ 2 ] = sample[ 2 ];
 }
 
+/*
+ * Same as ColorToBytes, but if the output color will never contain zero
+ * components. Used to avoid returning 0 0 0 due to an ioq3 issue. Reason
+ * to also map 0 0 1 to 1 1 1 is to ensure monotonicity in the color mapping
+ * to prevent banding-like artifacts on lightmaps.
+ */
+void ColorToBytesNonZero( const float *color, byte *colorBytes, float scale) {
+       int i;
+       ColorToBytes(color, colorBytes, scale);
+       for (i = 0; i < 3; ++i)
+               if (colorBytes[i] == 0)
+                       colorBytes[i] = 1;
+}
 
 
 /* -------------------------------------------------------------------------------
@@ -1762,7 +1794,7 @@ static qboolean SubmapRawLuxel( rawLightmap_t *lm, int x, int y, float bx, float
                origin2 = SUPER_ORIGIN( x, y );
                //%     normal2 = SUPER_NORMAL( x, y );
        }
-       else {
+       else{
                Sys_FPrintf( SYS_WRN, "WARNING: Spurious lightmap T vector\n" );
        }
 
@@ -2206,13 +2238,13 @@ void IlluminateRawLightmap( int rawLightmapNum ){
                        }
 
                        /* set luxel filter radius */
-                       luxelFilterRadius = lm->sampleSize != 0 ? superSample * filterRadius / lm->sampleSize : 0;
+                       luxelFilterRadius = superSample * filterRadius / lm->sampleSize;
                        if ( luxelFilterRadius == 0 && ( filterRadius > 0.0f || filter ) ) {
                                luxelFilterRadius = 1;
                        }
 
                        /* allocate sampling flags storage */
-                       if ( ( lightSamples > 1 || lightRandomSamples ) && luxelFilterRadius == 0 ) {
+                       if ( lightSamples > 1 || lightRandomSamples ) {
                                size = lm->sw * lm->sh * SUPER_LUXEL_SIZE * sizeof( unsigned char );
                                if ( lm->superFlags == NULL ) {
                                        lm->superFlags = safe_malloc( size );
@@ -2256,8 +2288,8 @@ void IlluminateRawLightmap( int rawLightmapNum ){
                                        }
 
                                        /* check for evilness */
-                                       if ( trace.forceSubsampling > 1.0f && ( lightSamples > 1 || lightRandomSamples ) && luxelFilterRadius == 0 ) {
-                                                       totalLighted++;
+                                       if ( trace.forceSubsampling > 1.0f && ( lightSamples > 1 || lightRandomSamples ) ) {
+                                               totalLighted++;
                                                *flag |= FLAG_FORCE_SUBSAMPLING; /* force */
                                        }
                                        /* add to count */
@@ -2274,7 +2306,7 @@ void IlluminateRawLightmap( int rawLightmapNum ){
 
                        /* secondary pass, adaptive supersampling (fixme: use a contrast function to determine if subsampling is necessary) */
                        /* 2003-09-27: changed it so filtering disamples supersampling, as it would waste time */
-                       if ( ( lightSamples > 1 || lightRandomSamples ) && luxelFilterRadius == 0 ) {
+                       if ( lightSamples > 1 || lightRandomSamples ) {
                                /* walk luxels */
                                for ( y = 0; y < ( lm->sh - 1 ); y++ )
                                {
@@ -2727,7 +2759,7 @@ void IlluminateVertexes( int num ){
        int i, x, y, z, x1, y1, z1, sx, sy, radius, maxRadius, *cluster;
        int lightmapNum, numAvg;
        float samples, *vertLuxel, *radVertLuxel, *luxel, dirt;
-       vec3_t origin, temp, temp2, colors[ MAX_LIGHTMAPS ], avgColors[ MAX_LIGHTMAPS ];
+       vec3_t temp, temp2, colors[ MAX_LIGHTMAPS ], avgColors[ MAX_LIGHTMAPS ];
        bspDrawSurface_t    *ds;
        surfaceInfo_t       *info;
        rawLightmap_t       *lm;
@@ -2782,7 +2814,7 @@ void IlluminateVertexes( int num ){
                        else if ( debugOrigin ) {
                                VectorSubtract( info->maxs, info->mins, temp );
                                VectorScale( temp, ( 1.0f / 255.0f ), temp );
-                               VectorSubtract( origin, lm->mins, temp2 );
+                               VectorSubtract( verts[ i ].xyz, info->mins, temp2 );
                                radVertLuxel[ 0 ] = info->mins[ 0 ] + ( temp[ 0 ] * temp2[ 0 ] );
                                radVertLuxel[ 1 ] = info->mins[ 1 ] + ( temp[ 1 ] * temp2[ 1 ] );
                                radVertLuxel[ 2 ] = info->mins[ 2 ] + ( temp[ 2 ] * temp2[ 2 ] );
@@ -2795,6 +2827,14 @@ void IlluminateVertexes( int num ){
                                radVertLuxel[ 2 ] = ( verts[ i ].normal[ 2 ] + 1.0f ) * 127.5f;
                        }
 
+                       else if ( info->si->noVertexLight ) {
+                               VectorSet( radVertLuxel, 127.5f, 127.5f, 127.5f );
+                       }
+
+                       else if ( noVertexLighting > 0 ) {
+                               VectorSet( radVertLuxel, 127.5f * noVertexLighting, 127.5f * noVertexLighting, 127.5f * noVertexLighting );
+                       }
+
                        /* illuminate the vertex */
                        else
                        {
@@ -2868,7 +2908,7 @@ void IlluminateVertexes( int num ){
                                                                trace.origin[ 2 ] = verts[ i ].xyz[ 2 ] + ( VERTEX_NUDGE * z1 );
 
                                                                /* try at nudged origin */
-                                                               trace.cluster = ClusterForPointExtFilter( origin, VERTEX_EPSILON, info->numSurfaceClusters, &surfaceClusters[ info->firstSurfaceCluster ] );
+                                                               trace.cluster = ClusterForPointExtFilter( trace.origin, VERTEX_EPSILON, info->numSurfaceClusters, &surfaceClusters[ info->firstSurfaceCluster ] );
                                                                if ( trace.cluster < 0 ) {
                                                                        continue;
                                                                }
@@ -3044,6 +3084,14 @@ void IlluminateVertexes( int num ){
                                VectorCopy( debugColors[ num % 12 ], radVertLuxel );
                        }
 
+                       else if ( info->si->noVertexLight ) {
+                               VectorSet( radVertLuxel, 127.5f, 127.5f, 127.5f );
+                       }
+
+                       else if ( noVertexLighting > 0 ) {
+                               VectorSet( radVertLuxel, 127.5f * noVertexLighting, 127.5f * noVertexLighting, 127.5f * noVertexLighting );
+                       }
+
                        /* divine color from the superluxels */
                        else
                        {
@@ -4421,6 +4469,9 @@ void FloodlightIlluminateLightmap( rawLightmap_t *lm ){
                        continue;
                }
 
+               if( lm->styles[lightmapNum] != LS_NORMAL && lm->styles[lightmapNum] != LS_NONE ) // isStyleLight
+                       continue;
+
                /* apply floodlight to each luxel */
                for ( y = 0; y < lm->sh; y++ )
                {