]> de.git.xonotic.org Git - xonotic/netradiant.git/blobdiff - tools/quake3/q3map2/lightmaps_ydnar.c
Merge branch 'master' of ssh://icculus.org/netradiant
[xonotic/netradiant.git] / tools / quake3 / q3map2 / lightmaps_ydnar.c
index e680fc0ce98d2c9b96472f306d63c0cfff8681cb..080f26ec5002441991c97388ecb5028fc577fa08 100644 (file)
@@ -291,8 +291,8 @@ static int CompareLightSurface( const void *a, const void *b )
        
        
        /* get shaders */
-       asi = surfaceInfos[ *((int*) a) ].si;
-       bsi = surfaceInfos[ *((int*) b) ].si;
+       asi = surfaceInfos[ *((const int*) a) ].si;
+       bsi = surfaceInfos[ *((const int*) b) ].si;
        
        /* dummy check */
        if( asi == NULL )
@@ -678,14 +678,14 @@ qboolean AddSurfaceToRawLightmap( int num, rawLightmap_t *lm )
                size[ i ] = (maxs[ i ] - mins[ i ]) / sampleSize + 1.0f;
                
                /* hack (god this sucks) */
-               if( size[ i ] > lm->customWidth || size[ i ] > lm->customHeight )
+               if( size[ i ] > lm->customWidth || size[ i ] > lm->customHeight  || (lmLimitSize && size[i] > lmLimitSize))
                {
                        i = -1;
                        sampleSize += 1.0f;
                }
        }
 
-       if(sampleSize != lm->sampleSize)
+       if(sampleSize != lm->sampleSize && lmLimitSize == 0)
        {
                Sys_FPrintf(SYS_VRB,"WARNING: surface at (%6.0f %6.0f %6.0f) (%6.0f %6.0f %6.0f) too large for desired samplesize/lightmapsize/lightmapscale combination, increased samplesize from %d to %d\n",
                        info->mins[0],
@@ -861,8 +861,8 @@ static int CompareSurfaceInfo( const void *a, const void *b )
        
 
        /* get surface info */
-       aInfo = &surfaceInfos[ *((int*) a) ];
-       bInfo = &surfaceInfos[ *((int*) b) ];
+       aInfo = &surfaceInfos[ *((const int*) a) ];
+       bInfo = &surfaceInfos[ *((const int*) b) ];
        
        /* model first */
        if( aInfo->modelindex < bInfo->modelindex )
@@ -1921,7 +1921,7 @@ for a given surface lightmap, find output lightmap pages and positions for it
 #define LIGHTMAP_RESERVE_COUNT 1
 static void FindOutLightmaps( rawLightmap_t *lm )
 {
-       int                                     i, j, k, lightmapNum, xMax, yMax, x, y, sx, sy, ox, oy, offset, temp;
+       int                                     i, j, k, lightmapNum, xMax, yMax, x, y, sx, sy, ox, oy, offset;
        outLightmap_t           *olm;
        surfaceInfo_t           *info;
        float                           *luxel, *deluxel;
@@ -2016,7 +2016,11 @@ static void FindOutLightmaps( rawLightmap_t *lm )
                        y = 0;
                        
                        /* walk the list of lightmap pages */
-                       for( i = noLightmapSearch ? numOutLightmaps - LIGHTMAP_RESERVE_COUNT : 0; i < numOutLightmaps; i++ )
+                       if(lightmapSearchBlockSize <= 0 || numOutLightmaps < LIGHTMAP_RESERVE_COUNT)
+                               i = 0;
+                       else
+                               i = ((numOutLightmaps - LIGHTMAP_RESERVE_COUNT) / lightmapSearchBlockSize) * lightmapSearchBlockSize;
+                       for( ; i < numOutLightmaps; i++ )
                        {
                                /* get the output lightmap */
                                olm = &outLightmaps[ i ];
@@ -2075,7 +2079,7 @@ static void FindOutLightmaps( rawLightmap_t *lm )
                        olm = safe_malloc( numOutLightmaps * sizeof( outLightmap_t ) );
                        if( outLightmaps != NULL && numOutLightmaps > LIGHTMAP_RESERVE_COUNT )
                        {
-                               memcpy( olm, outLightmaps, (numOutLightmaps - 2) * sizeof( outLightmap_t ) );
+                               memcpy( olm, outLightmaps, (numOutLightmaps - LIGHTMAP_RESERVE_COUNT) * sizeof( outLightmap_t ) );
                                free( outLightmaps );
                        }
                        outLightmaps = olm;
@@ -2189,21 +2193,12 @@ static void FindOutLightmaps( rawLightmap_t *lm )
                                if( deluxemap )
                                {
                                        /* normalize average light direction */
-                                       if( VectorNormalize( deluxel, direction ) )
-                                       {
-                                               /* encode [-1,1] in [0,255] */
-                                               pixel = olm->bspDirBytes + (((oy * olm->customWidth) + ox) * 3);
-                                               for( i = 0; i < 3; i++ )
-                                               {
-                                                       temp = (direction[ i ] + 1.0f) * 127.5f;
-                                                       if( temp < 0 )
-                                                               pixel[ i ] = 0;
-                                                       else if( temp > 255 )
-                                                               pixel[ i ] = 255;
-                                                       else
-                                                               pixel[ i ] = temp;
-                                               }
-                                       }
+                                       pixel = olm->bspDirBytes + (((oy * olm->customWidth) + ox) * 3);
+                                       VectorScale( deluxel, 1000.0f, direction );
+                                       VectorNormalize( direction, direction );
+                                       VectorScale( direction, 127.5f, direction );
+                                       for( i = 0; i < 3; i++ )
+                                               pixel[ i ] = (byte)( 127.5f + direction[ i ] );
                                }
                        }
                }
@@ -2225,8 +2220,8 @@ static int CompareRawLightmap( const void *a, const void *b )
        
        
        /* get lightmaps */
-       alm = &rawLightmaps[ *((int*) a) ];
-       blm = &rawLightmaps[ *((int*) b) ];
+       alm = &rawLightmaps[ *((const int*) a) ];
+       blm = &rawLightmaps[ *((const int*) b) ];
        
        /* get min number of surfaces */
        min = (alm->numLightSurfaces < blm->numLightSurfaces ? alm->numLightSurfaces : blm->numLightSurfaces);
@@ -2260,7 +2255,112 @@ static int CompareRawLightmap( const void *a, const void *b )
        return 0;
 }
 
+void FillOutLightmap(outLightmap_t *olm)
+{
+       int x, y;
+       int ofs;
+       vec3_t dir_sum, light_sum;
+       int cnt, filled;
+       byte *lightBitsNew = NULL;
+       byte *lightBytesNew = NULL;
+       byte *dirBytesNew = NULL;
+
+       lightBitsNew = safe_malloc((olm->customWidth * olm->customHeight + 8) / 8);
+       lightBytesNew = safe_malloc(olm->customWidth * olm->customHeight * 3);
+       if(deluxemap)
+               dirBytesNew = safe_malloc(olm->customWidth * olm->customHeight * 3);
+
+       /*
+       memset(olm->lightBits, 0, (olm->customWidth * olm->customHeight + 8) / 8);
+               olm->lightBits[0] |= 1;
+               olm->lightBits[(10 * olm->customWidth + 30) >> 3] |= 1 << ((10 * olm->customWidth + 30) & 7);
+       memset(olm->bspLightBytes, 0, olm->customWidth * olm->customHeight * 3);
+               olm->bspLightBytes[0] = 255;
+               olm->bspLightBytes[(10 * olm->customWidth + 30) * 3 + 2] = 255;
+       */
+
+       memcpy(lightBitsNew, olm->lightBits, (olm->customWidth * olm->customHeight + 8) / 8);
+       memcpy(lightBytesNew, olm->bspLightBytes, olm->customWidth * olm->customHeight * 3);
+       if(deluxemap)
+               memcpy(dirBytesNew, olm->bspDirBytes, olm->customWidth * olm->customHeight * 3);
+
+       for(;;)
+       {
+               filled = 0;
+               for(y = 0; y < olm->customHeight; ++y)
+               {
+                       for(x = 0; x < olm->customWidth; ++x)
+                       {
+                               ofs = y * olm->customWidth + x;
+                               if(olm->lightBits[ofs >> 3] & (1 << (ofs & 7))) /* already filled */
+                                       continue;
+                               cnt = 0;
+                               VectorClear(dir_sum);
+                               VectorClear(light_sum);
+
+                               /* try all four neighbors */
+                               ofs = ((y + olm->customHeight - 1) % olm->customHeight) * olm->customWidth + x;
+                               if(olm->lightBits[ofs >> 3] & (1 << (ofs & 7))) /* already filled */
+                               {
+                                       ++cnt;
+                                       VectorAdd(light_sum, olm->bspLightBytes + ofs * 3, light_sum);
+                                       if(deluxemap)
+                                               VectorAdd(dir_sum, olm->bspDirBytes + ofs * 3, dir_sum);
+                               }
+
+                               ofs = ((y + 1) % olm->customHeight) * olm->customWidth + x;
+                               if(olm->lightBits[ofs >> 3] & (1 << (ofs & 7))) /* already filled */
+                               {
+                                       ++cnt;
+                                       VectorAdd(light_sum, olm->bspLightBytes + ofs * 3, light_sum);
+                                       if(deluxemap)
+                                               VectorAdd(dir_sum, olm->bspDirBytes + ofs * 3, dir_sum);
+                               }
+
+                               ofs = y * olm->customWidth + (x + olm->customWidth - 1) % olm->customWidth;
+                               if(olm->lightBits[ofs >> 3] & (1 << (ofs & 7))) /* already filled */
+                               {
+                                       ++cnt;
+                                       VectorAdd(light_sum, olm->bspLightBytes + ofs * 3, light_sum);
+                                       if(deluxemap)
+                                               VectorAdd(dir_sum, olm->bspDirBytes + ofs * 3, dir_sum);
+                               }
 
+                               ofs = y * olm->customWidth + (x + 1) % olm->customWidth;
+                               if(olm->lightBits[ofs >> 3] & (1 << (ofs & 7))) /* already filled */
+                               {
+                                       ++cnt;
+                                       VectorAdd(light_sum, olm->bspLightBytes + ofs * 3, light_sum);
+                                       if(deluxemap)
+                                               VectorAdd(dir_sum, olm->bspDirBytes + ofs * 3, dir_sum);
+                               }
+
+                               if(cnt)
+                               {
+                                       ++filled;
+                                       ofs = y * olm->customWidth + x;
+                                       lightBitsNew[ofs >> 3] |= (1 << (ofs & 7));
+                                       VectorScale(light_sum, 1.0/cnt, lightBytesNew + ofs * 3);
+                                       if(deluxemap)
+                                               VectorScale(dir_sum, 1.0/cnt, dirBytesNew + ofs * 3);
+                               }
+                       }
+               }
+
+               if(!filled)
+                       break;
+
+               memcpy(olm->lightBits, lightBitsNew, (olm->customWidth * olm->customHeight + 8) / 8);
+               memcpy(olm->bspLightBytes, lightBytesNew, olm->customWidth * olm->customHeight * 3);
+               if(deluxemap)
+                       memcpy(olm->bspDirBytes, dirBytesNew, olm->customWidth * olm->customHeight * 3);
+       }
+
+       free(lightBitsNew);
+       free(lightBytesNew);
+       if(deluxemap)
+               free(dirBytesNew);
+}
 
 /*
 StoreSurfaceLightmaps()
@@ -2286,8 +2386,8 @@ void StoreSurfaceLightmaps( void )
        char                            dirname[ 1024 ], filename[ 1024 ];
        shaderInfo_t            *csi;
        char                            lightmapName[ 128 ];
-       char                            *rgbGenValues[ 256 ];
-       char                            *alphaGenValues[ 256 ];
+       const char                              *rgbGenValues[ 256 ];
+       const char                              *alphaGenValues[ 256 ];
        
        
        /* note it */
@@ -2942,6 +3042,10 @@ void StoreSurfaceLightmaps( void )
        {
                /* get output lightmap */
                olm = &outLightmaps[ i ];
+
+               /* fill output lightmap */
+               if(lightmapFill)
+                       FillOutLightmap(olm);
                
                /* is this a valid bsp lightmap? */
                if( olm->lightmapNum >= 0 && !externalLightmaps )
@@ -3175,7 +3279,7 @@ void StoreSurfaceLightmaps( void )
                                if( rgbGenValues[ style ] == NULL )
                                {
                                        sprintf( key, "_style%drgbgen", style );
-                                       rgbGenValues[ style ] = (char*) ValueForKey( &entities[ 0 ], key );
+                                       rgbGenValues[ style ] = ValueForKey( &entities[ 0 ], key );
                                        if( rgbGenValues[ style ][ 0 ] == '\0' )
                                                rgbGenValues[ style ] = "wave noise 0.5 1 0 5.37";
                                }
@@ -3189,7 +3293,7 @@ void StoreSurfaceLightmaps( void )
                                if( alphaGenValues[ style ] == NULL )
                                {
                                        sprintf( key, "_style%dalphagen", style );
-                                       alphaGenValues[ style ] = (char*) ValueForKey( &entities[ 0 ], key );
+                                       alphaGenValues[ style ] = ValueForKey( &entities[ 0 ], key );
                                }
                                if( alphaGenValues[ style ][ 0 ] != '\0' )
                                        sprintf( alphaGen, "\t\talphaGen %s // style %d\n", alphaGenValues[ style ], style );