X-Git-Url: https://de.git.xonotic.org/?a=blobdiff_plain;f=tools%2Fquake3%2Fq3map2%2Flightmaps_ydnar.c;h=2802ecaf4370c223b2d8c3e29ce6b8061154ca9d;hb=ca0295d3b8e3340c18de1fe13f41265ba32612d4;hp=ea36b921f8bd6a41b75be713a64286de5ae7019c;hpb=8e83f5d7ed0e40452949289681c2b89919008405;p=xonotic%2Fnetradiant.git diff --git a/tools/quake3/q3map2/lightmaps_ydnar.c b/tools/quake3/q3map2/lightmaps_ydnar.c index ea36b921..2802ecaf 100644 --- a/tools/quake3/q3map2/lightmaps_ydnar.c +++ b/tools/quake3/q3map2/lightmaps_ydnar.c @@ -67,6 +67,8 @@ void WriteTGA24( char *filename, byte *data, int width, int height, qboolean fli /* allocate a buffer and set it up */ buffer = safe_malloc( width * height * 3 + 18 ); + /* we may also use safe_malloc0 on the whole instead, + * this would just be a bit slower */ memset( buffer, 0, 18 ); buffer[ 2 ] = 2; buffer[ 12 ] = width & 255; @@ -153,7 +155,7 @@ void ExportLightmaps( void ){ int ExportLightmapsMain( int argc, char **argv ){ /* arg checking */ - if ( argc < 1 ) { + if ( argc < 2 ) { Sys_Printf( "Usage: q3map -export [-v] \n" ); return 0; } @@ -188,7 +190,7 @@ int ImportLightmapsMain( int argc, char **argv ){ /* arg checking */ - if ( argc < 1 ) { + if ( argc < 2 ) { Sys_Printf( "Usage: q3map -import [-v] \n" ); return 0; } @@ -826,8 +828,7 @@ qboolean AddSurfaceToRawLightmap( int num, rawLightmap_t *lm ){ /* for planar surfaces, create lightmap vectors for st->xyz conversion */ if ( VectorLength( ds->lightmapVecs[ 2 ] ) || 1 ) { /* ydnar: can't remember what exactly i was thinking here... */ /* allocate space for the vectors */ - lm->vecs = safe_malloc( 3 * sizeof( vec3_t ) ); - memset( lm->vecs, 0, 3 * sizeof( vec3_t ) ); + lm->vecs = safe_malloc0( 3 * sizeof( vec3_t ) ); VectorCopy( ds->lightmapVecs[ 2 ], lm->vecs[ 2 ] ); /* project stepped lightmap blocks and subtract to get planevecs */ @@ -997,18 +998,15 @@ void SetupSurfaceLightmaps( void ){ /* allocate a list of surface clusters */ numSurfaceClusters = 0; maxSurfaceClusters = numBSPLeafSurfaces; - surfaceClusters = safe_malloc( maxSurfaceClusters * sizeof( *surfaceClusters ) ); - memset( surfaceClusters, 0, maxSurfaceClusters * sizeof( *surfaceClusters ) ); + surfaceClusters = safe_malloc0( maxSurfaceClusters * sizeof( *surfaceClusters ) ); /* allocate a list for per-surface info */ - surfaceInfos = safe_malloc( numBSPDrawSurfaces * sizeof( *surfaceInfos ) ); - memset( surfaceInfos, 0, numBSPDrawSurfaces * sizeof( *surfaceInfos ) ); + surfaceInfos = safe_malloc0( numBSPDrawSurfaces * sizeof( *surfaceInfos ) ); for ( i = 0; i < numBSPDrawSurfaces; i++ ) surfaceInfos[ i ].childSurfaceNum = -1; /* allocate a list of surface indexes to be sorted */ - sortSurfaces = safe_malloc( numBSPDrawSurfaces * sizeof( int ) ); - memset( sortSurfaces, 0, numBSPDrawSurfaces * sizeof( int ) ); + sortSurfaces = safe_malloc0( numBSPDrawSurfaces * sizeof( int ) ); /* walk each model in the bsp */ for ( i = 0; i < numBSPModels; i++ ) @@ -1129,14 +1127,12 @@ void SetupSurfaceLightmaps( void ){ /* allocate a list of surfaces that would go into raw lightmaps */ numLightSurfaces = 0; - lightSurfaces = safe_malloc( numSurfsLightmapped * sizeof( int ) ); - memset( lightSurfaces, 0, numSurfsLightmapped * sizeof( int ) ); + lightSurfaces = safe_malloc0( numSurfsLightmapped * sizeof( int ) ); /* allocate a list of raw lightmaps */ numRawSuperLuxels = 0; numRawLightmaps = 0; - rawLightmaps = safe_malloc( numSurfsLightmapped * sizeof( *rawLightmaps ) ); - memset( rawLightmaps, 0, numSurfsLightmapped * sizeof( *rawLightmaps ) ); + rawLightmaps = safe_malloc0( numSurfsLightmapped * sizeof( *rawLightmaps ) ); /* walk the list of sorted surfaces */ for ( i = 0; i < numBSPDrawSurfaces; i++ ) @@ -1225,10 +1221,8 @@ void SetupSurfaceLightmaps( void ){ /* allocate vertex luxel storage */ for ( k = 0; k < MAX_LIGHTMAPS; k++ ) { - vertexLuxels[ k ] = safe_malloc( numBSPDrawVerts * VERTEX_LUXEL_SIZE * sizeof( float ) ); - memset( vertexLuxels[ k ], 0, numBSPDrawVerts * VERTEX_LUXEL_SIZE * sizeof( float ) ); - radVertexLuxels[ k ] = safe_malloc( numBSPDrawVerts * VERTEX_LUXEL_SIZE * sizeof( float ) ); - memset( radVertexLuxels[ k ], 0, numBSPDrawVerts * VERTEX_LUXEL_SIZE * sizeof( float ) ); + vertexLuxels[ k ] = safe_malloc0( numBSPDrawVerts * VERTEX_LUXEL_SIZE * sizeof( float ) ); + radVertexLuxels[ k ] = safe_malloc0( numBSPDrawVerts * VERTEX_LUXEL_SIZE * sizeof( float ) ); } /* emit some stats */ @@ -1979,13 +1973,10 @@ static void SetupOutLightmap( rawLightmap_t *lm, outLightmap_t *olm ){ olm->numShaders = 0; /* allocate buffers */ - olm->lightBits = safe_malloc( ( olm->customWidth * olm->customHeight / 8 ) + 8 ); - memset( olm->lightBits, 0, ( olm->customWidth * olm->customHeight / 8 ) + 8 ); - olm->bspLightBytes = safe_malloc( olm->customWidth * olm->customHeight * 3 ); - memset( olm->bspLightBytes, 0, olm->customWidth * olm->customHeight * 3 ); + olm->lightBits = safe_malloc0( ( olm->customWidth * olm->customHeight / 8 ) + 8 ); + olm->bspLightBytes = safe_malloc0( olm->customWidth * olm->customHeight * 3 ); if ( deluxemap ) { - olm->bspDirBytes = safe_malloc( olm->customWidth * olm->customHeight * 3 ); - memset( olm->bspDirBytes, 0, olm->customWidth * olm->customHeight * 3 ); + olm->bspDirBytes = safe_malloc0( olm->customWidth * olm->customHeight * 3 ); } } @@ -2007,7 +1998,6 @@ static void FindOutLightmaps( rawLightmap_t *lm, qboolean fastAllocate ){ qboolean ok; int xIncrement, yIncrement; - /* set default lightmap number (-3 = LIGHTMAP_BY_VERTEX) */ for ( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ ) lm->outLightmapNums[ lightmapNum ] = -3; @@ -2335,6 +2325,16 @@ static int CompareRawLightmap( const void *a, const void *b ){ /* get min number of surfaces */ min = ( alm->numLightSurfaces < blm->numLightSurfaces ? alm->numLightSurfaces : blm->numLightSurfaces ); +//#define allocate_bigger_first +#ifdef allocate_bigger_first + /* compare size, allocate bigger first */ + // fastAllocate commit part: can kick fps by unique lightmap/shader combinations*=~2 + bigger compile time + //return -diff; makes packing faster and rough + diff = ( blm->w * blm->h ) - ( alm->w * alm->h ); + if ( diff != 0 ) { + return diff; + } +#endif /* iterate */ for ( i = 0; i < min; i++ ) { @@ -2356,13 +2356,13 @@ static int CompareRawLightmap( const void *a, const void *b ){ if ( diff ) { return diff; } - +#ifndef allocate_bigger_first /* compare size */ diff = ( blm->w * blm->h ) - ( alm->w * alm->h ); if ( diff != 0 ) { return diff; } - +#endif /* must be equivalent */ return 0; } @@ -2481,15 +2481,13 @@ void FillOutLightmap( outLightmap_t *olm ){ } } - - /* StoreSurfaceLightmaps() stores the surface lightmaps into the bsp as byte rgb triplets */ -void StoreSurfaceLightmaps( qboolean fastAllocate ){ - int i, j, k, x, y, lx, ly, sx, sy, *cluster, mappedSamples; +void StoreSurfaceLightmaps( qboolean fastAllocate, qboolean storeForReal ){ + int i, j, k, x, y, lx, ly, sx, sy, *cluster, mappedSamples, timer_start; int style, size, lightmapNum, lightmapNum2; float *normal, *luxel, *bspLuxel, *bspLuxel2, *radLuxel, samples, occludedSamples; vec3_t sample, occludedSample, dirSample, colorMins, colorMaxs; @@ -2532,6 +2530,8 @@ void StoreSurfaceLightmaps( qboolean fastAllocate ){ /* note it */ Sys_FPrintf( SYS_VRB, "Subsampling..." ); + timer_start = I_FloatTime(); + /* walk the list of raw lightmaps */ numUsed = 0; numTwins = 0; @@ -2553,8 +2553,7 @@ void StoreSurfaceLightmaps( qboolean fastAllocate ){ /* allocate bsp luxel storage */ if ( lm->bspLuxels[ lightmapNum ] == NULL ) { size = lm->w * lm->h * BSP_LUXEL_SIZE * sizeof( float ); - lm->bspLuxels[ lightmapNum ] = safe_malloc( size ); - memset( lm->bspLuxels[ lightmapNum ], 0, size ); + lm->bspLuxels[ lightmapNum ] = safe_malloc0( size ); } /* allocate radiosity lightmap storage */ @@ -2866,6 +2865,8 @@ void StoreSurfaceLightmaps( qboolean fastAllocate ){ } } + Sys_FPrintf( SYS_VRB, "%d.", (int) ( I_FloatTime() - timer_start ) ); + /* ----------------------------------------------------------------- convert modelspace deluxemaps to tangentspace ----------------------------------------------------------------- */ @@ -2875,6 +2876,8 @@ void StoreSurfaceLightmaps( qboolean fastAllocate ){ vec3_t worldUp, myNormal, myTangent, myBinormal; float dist; + timer_start = I_FloatTime(); + Sys_Printf( "converting..." ); for ( i = 0; i < numRawLightmaps; i++ ) @@ -2944,6 +2947,8 @@ void StoreSurfaceLightmaps( qboolean fastAllocate ){ } } } + + Sys_FPrintf( SYS_VRB, "%d.", (int) ( I_FloatTime() - timer_start ) ); } } @@ -2996,10 +3001,12 @@ void StoreSurfaceLightmaps( qboolean fastAllocate ){ collapse non-unique lightmaps ----------------------------------------------------------------- */ - if ( noCollapse == qfalse && deluxemap == qfalse ) { + if ( storeForReal && noCollapse == qfalse && deluxemap == qfalse ) { /* note it */ Sys_FPrintf( SYS_VRB, "collapsing..." ); + timer_start = I_FloatTime(); + /* set all twin refs to null */ for ( i = 0; i < numRawLightmaps; i++ ) { @@ -3060,6 +3067,8 @@ void StoreSurfaceLightmaps( qboolean fastAllocate ){ } } } + + Sys_FPrintf( SYS_VRB, "%d.", (int) ( I_FloatTime() - timer_start ) ); } /* ----------------------------------------------------------------- @@ -3069,6 +3078,8 @@ void StoreSurfaceLightmaps( qboolean fastAllocate ){ /* note it */ Sys_FPrintf( SYS_VRB, "sorting..." ); + timer_start = I_FloatTime(); + /* allocate a new sorted list */ if ( sortLightmaps == NULL ) { sortLightmaps = safe_malloc( numRawLightmaps * sizeof( int ) ); @@ -3079,433 +3090,452 @@ void StoreSurfaceLightmaps( qboolean fastAllocate ){ sortLightmaps[ i ] = i; qsort( sortLightmaps, numRawLightmaps, sizeof( int ), CompareRawLightmap ); + Sys_FPrintf( SYS_VRB, "%d.", (int) ( I_FloatTime() - timer_start ) ); + /* ----------------------------------------------------------------- allocate output lightmaps ----------------------------------------------------------------- */ - /* note it */ - Sys_FPrintf( SYS_VRB, "allocating..." ); + if ( storeForReal ) { + /* note it */ + Sys_FPrintf( SYS_VRB, "allocating..." ); - /* kill all existing output lightmaps */ - if ( outLightmaps != NULL ) { - for ( i = 0; i < numOutLightmaps; i++ ) - { - free( outLightmaps[ i ].lightBits ); - free( outLightmaps[ i ].bspLightBytes ); + timer_start = I_FloatTime(); + + /* kill all existing output lightmaps */ + if ( outLightmaps != NULL ) { + for ( i = 0; i < numOutLightmaps; i++ ) + { + free( outLightmaps[ i ].lightBits ); + free( outLightmaps[ i ].bspLightBytes ); + } + free( outLightmaps ); + outLightmaps = NULL; } - free( outLightmaps ); - outLightmaps = NULL; - } - numLightmapShaders = 0; - numOutLightmaps = 0; - numBSPLightmaps = 0; - numExtLightmaps = 0; + numLightmapShaders = 0; + numOutLightmaps = 0; + numBSPLightmaps = 0; + numExtLightmaps = 0; - /* find output lightmap */ - for ( i = 0; i < numRawLightmaps; i++ ) - { - lm = &rawLightmaps[ sortLightmaps[ i ] ]; - FindOutLightmaps( lm, fastAllocate ); - } - - /* set output numbers in twinned lightmaps */ - for ( i = 0; i < numRawLightmaps; i++ ) - { - /* get lightmap */ - lm = &rawLightmaps[ sortLightmaps[ i ] ]; + /* find output lightmap */ + for ( i = 0; i < numRawLightmaps; i++ ) + { + lm = &rawLightmaps[ sortLightmaps[ i ] ]; + FindOutLightmaps( lm, fastAllocate ); + } - /* walk lightmaps */ - for ( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ ) + /* set output numbers in twinned lightmaps */ + for ( i = 0; i < numRawLightmaps; i++ ) { - /* get twin */ - lm2 = lm->twins[ lightmapNum ]; - if ( lm2 == NULL ) { - continue; - } - lightmapNum2 = lm->twinNums[ lightmapNum ]; + /* get lightmap */ + lm = &rawLightmaps[ sortLightmaps[ i ] ]; - /* find output lightmap from twin */ - lm->outLightmapNums[ lightmapNum ] = lm2->outLightmapNums[ lightmapNum2 ]; - lm->lightmapX[ lightmapNum ] = lm2->lightmapX[ lightmapNum2 ]; - lm->lightmapY[ lightmapNum ] = lm2->lightmapY[ lightmapNum2 ]; + /* walk lightmaps */ + for ( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ ) + { + /* get twin */ + lm2 = lm->twins[ lightmapNum ]; + if ( lm2 == NULL ) { + continue; + } + lightmapNum2 = lm->twinNums[ lightmapNum ]; + + /* find output lightmap from twin */ + lm->outLightmapNums[ lightmapNum ] = lm2->outLightmapNums[ lightmapNum2 ]; + lm->lightmapX[ lightmapNum ] = lm2->lightmapX[ lightmapNum2 ]; + lm->lightmapY[ lightmapNum ] = lm2->lightmapY[ lightmapNum2 ]; + } } } + Sys_FPrintf( SYS_VRB, "%d.", (int) ( I_FloatTime() - timer_start ) ); + /* ----------------------------------------------------------------- store output lightmaps ----------------------------------------------------------------- */ - /* note it */ - Sys_FPrintf( SYS_VRB, "storing..." ); - - /* count the bsp lightmaps and allocate space */ - if ( bspLightBytes != NULL ) { - free( bspLightBytes ); - } - if ( numBSPLightmaps == 0 || externalLightmaps ) { - numBSPLightBytes = 0; - bspLightBytes = NULL; - } - else - { - numBSPLightBytes = ( numBSPLightmaps * game->lightmapSize * game->lightmapSize * 3 ); - bspLightBytes = safe_malloc( numBSPLightBytes ); - memset( bspLightBytes, 0, numBSPLightBytes ); - } + if ( storeForReal ) { + /* note it */ + Sys_FPrintf( SYS_VRB, "storing..." ); - /* walk the list of output lightmaps */ - for ( i = 0; i < numOutLightmaps; i++ ) - { - /* get output lightmap */ - olm = &outLightmaps[ i ]; + timer_start = I_FloatTime(); - /* fill output lightmap */ - if ( lightmapFill ) { - FillOutLightmap( olm ); + /* count the bsp lightmaps and allocate space */ + if ( bspLightBytes != NULL ) { + free( bspLightBytes ); + } + if ( numBSPLightmaps == 0 || externalLightmaps ) { + numBSPLightBytes = 0; + bspLightBytes = NULL; + } + else + { + numBSPLightBytes = ( numBSPLightmaps * game->lightmapSize * game->lightmapSize * 3 ); + bspLightBytes = safe_malloc0( numBSPLightBytes ); } - /* is this a valid bsp lightmap? */ - if ( olm->lightmapNum >= 0 && !externalLightmaps ) { - /* copy lighting data */ - lb = bspLightBytes + ( olm->lightmapNum * game->lightmapSize * game->lightmapSize * 3 ); - memcpy( lb, olm->bspLightBytes, game->lightmapSize * game->lightmapSize * 3 ); + /* walk the list of output lightmaps */ + for ( i = 0; i < numOutLightmaps; i++ ) + { + /* get output lightmap */ + olm = &outLightmaps[ i ]; - /* copy direction data */ - if ( deluxemap ) { - lb = bspLightBytes + ( ( olm->lightmapNum + 1 ) * game->lightmapSize * game->lightmapSize * 3 ); - memcpy( lb, olm->bspDirBytes, game->lightmapSize * game->lightmapSize * 3 ); + /* fill output lightmap */ + if ( lightmapFill ) { + FillOutLightmap( olm ); } - } - /* external lightmap? */ - if ( olm->lightmapNum < 0 || olm->extLightmapNum >= 0 || externalLightmaps ) { - /* make a directory for the lightmaps */ - Q_mkdir( dirname ); + /* is this a valid bsp lightmap? */ + if ( olm->lightmapNum >= 0 && !externalLightmaps ) { + /* copy lighting data */ + lb = bspLightBytes + ( olm->lightmapNum * game->lightmapSize * game->lightmapSize * 3 ); + memcpy( lb, olm->bspLightBytes, game->lightmapSize * game->lightmapSize * 3 ); - /* set external lightmap number */ - olm->extLightmapNum = numExtLightmaps; + /* copy direction data */ + if ( deluxemap ) { + lb = bspLightBytes + ( ( olm->lightmapNum + 1 ) * game->lightmapSize * game->lightmapSize * 3 ); + memcpy( lb, olm->bspDirBytes, game->lightmapSize * game->lightmapSize * 3 ); + } + } - /* write lightmap */ - sprintf( filename, "%s/" EXTERNAL_LIGHTMAP, dirname, numExtLightmaps ); - Sys_FPrintf( SYS_VRB, "\nwriting %s", filename ); - WriteTGA24( filename, olm->bspLightBytes, olm->customWidth, olm->customHeight, qtrue ); - numExtLightmaps++; + /* external lightmap? */ + if ( olm->lightmapNum < 0 || olm->extLightmapNum >= 0 || externalLightmaps ) { + /* make a directory for the lightmaps */ + Q_mkdir( dirname ); - /* write deluxemap */ - if ( deluxemap ) { + /* set external lightmap number */ + olm->extLightmapNum = numExtLightmaps; + + /* write lightmap */ sprintf( filename, "%s/" EXTERNAL_LIGHTMAP, dirname, numExtLightmaps ); Sys_FPrintf( SYS_VRB, "\nwriting %s", filename ); - WriteTGA24( filename, olm->bspDirBytes, olm->customWidth, olm->customHeight, qtrue ); + WriteTGA24( filename, olm->bspLightBytes, olm->customWidth, olm->customHeight, qtrue ); numExtLightmaps++; - if ( debugDeluxemap ) { - olm->extLightmapNum++; + /* write deluxemap */ + if ( deluxemap ) { + sprintf( filename, "%s/" EXTERNAL_LIGHTMAP, dirname, numExtLightmaps ); + Sys_FPrintf( SYS_VRB, "\nwriting %s", filename ); + WriteTGA24( filename, olm->bspDirBytes, olm->customWidth, olm->customHeight, qtrue ); + numExtLightmaps++; + + if ( debugDeluxemap ) { + olm->extLightmapNum++; + } } } } - } - if ( numExtLightmaps > 0 ) { - Sys_FPrintf( SYS_VRB, "\n" ); - } - - /* delete unused external lightmaps */ - for ( i = numExtLightmaps; i; i++ ) - { - /* determine if file exists */ - sprintf( filename, "%s/" EXTERNAL_LIGHTMAP, dirname, i ); - if ( !FileExists( filename ) ) { - break; + if ( numExtLightmaps > 0 ) { + Sys_FPrintf( SYS_VRB, "\n" ); } - /* delete it */ - remove( filename ); + /* delete unused external lightmaps */ + for ( i = numExtLightmaps; i; i++ ) + { + /* determine if file exists */ + sprintf( filename, "%s/" EXTERNAL_LIGHTMAP, dirname, i ); + if ( !FileExists( filename ) ) { + break; + } + + /* delete it */ + remove( filename ); + } } + Sys_FPrintf( SYS_VRB, "%d.", (int) ( I_FloatTime() - timer_start ) ); + /* ----------------------------------------------------------------- project the lightmaps onto the bsp surfaces ----------------------------------------------------------------- */ - /* note it */ - Sys_FPrintf( SYS_VRB, "projecting..." ); - - /* walk the list of surfaces */ - for ( i = 0; i < numBSPDrawSurfaces; i++ ) - { - /* get the surface and info */ - ds = &bspDrawSurfaces[ i ]; - info = &surfaceInfos[ i ]; - lm = info->lm; - olm = NULL; - - /* handle surfaces with identical parent */ - if ( info->parentSurfaceNum >= 0 ) { - /* preserve original data and get parent */ - parent = &bspDrawSurfaces[ info->parentSurfaceNum ]; - memcpy( &dsTemp, ds, sizeof( *ds ) ); - - /* overwrite child with parent data */ - memcpy( ds, parent, sizeof( *ds ) ); - - /* restore key parts */ - ds->fogNum = dsTemp.fogNum; - ds->firstVert = dsTemp.firstVert; - ds->firstIndex = dsTemp.firstIndex; - memcpy( ds->lightmapVecs, dsTemp.lightmapVecs, sizeof( dsTemp.lightmapVecs ) ); - - /* set vertex data */ - dv = &bspDrawVerts[ ds->firstVert ]; - dvParent = &bspDrawVerts[ parent->firstVert ]; - for ( j = 0; j < ds->numVerts; j++ ) - { - memcpy( dv[ j ].lightmap, dvParent[ j ].lightmap, sizeof( dv[ j ].lightmap ) ); - memcpy( dv[ j ].color, dvParent[ j ].color, sizeof( dv[ j ].color ) ); - } - - /* skip the rest */ - continue; - } + if ( storeForReal ) { + /* note it */ + Sys_FPrintf( SYS_VRB, "projecting..." ); - /* handle vertex lit or approximated surfaces */ - else if ( lm == NULL || lm->outLightmapNums[ 0 ] < 0 ) { - for ( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ ) - { - ds->lightmapNum[ lightmapNum ] = -3; - ds->lightmapStyles[ lightmapNum ] = ds->vertexStyles[ lightmapNum ]; - } - } + timer_start = I_FloatTime(); - /* handle lightmapped surfaces */ - else + /* walk the list of surfaces */ + for ( i = 0; i < numBSPDrawSurfaces; i++ ) { - /* walk lightmaps */ - for ( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ ) - { - /* set style */ - ds->lightmapStyles[ lightmapNum ] = lm->styles[ lightmapNum ]; + /* get the surface and info */ + ds = &bspDrawSurfaces[ i ]; + info = &surfaceInfos[ i ]; + lm = info->lm; + olm = NULL; - /* handle unused style */ - if ( lm->styles[ lightmapNum ] == LS_NONE || lm->outLightmapNums[ lightmapNum ] < 0 ) { - ds->lightmapNum[ lightmapNum ] = -3; - continue; - } + /* handle surfaces with identical parent */ + if ( info->parentSurfaceNum >= 0 ) { + /* preserve original data and get parent */ + parent = &bspDrawSurfaces[ info->parentSurfaceNum ]; + memcpy( &dsTemp, ds, sizeof( *ds ) ); - /* get output lightmap */ - olm = &outLightmaps[ lm->outLightmapNums[ lightmapNum ] ]; + /* overwrite child with parent data */ + memcpy( ds, parent, sizeof( *ds ) ); - /* set bsp lightmap number */ - ds->lightmapNum[ lightmapNum ] = olm->lightmapNum; + /* restore key parts */ + ds->fogNum = dsTemp.fogNum; + ds->firstVert = dsTemp.firstVert; + ds->firstIndex = dsTemp.firstIndex; + memcpy( ds->lightmapVecs, dsTemp.lightmapVecs, sizeof( dsTemp.lightmapVecs ) ); - /* deluxemap debugging makes the deluxemap visible */ - if ( deluxemap && debugDeluxemap && lightmapNum == 0 ) { - ds->lightmapNum[ lightmapNum ]++; + /* set vertex data */ + dv = &bspDrawVerts[ ds->firstVert ]; + dvParent = &bspDrawVerts[ parent->firstVert ]; + for ( j = 0; j < ds->numVerts; j++ ) + { + memcpy( dv[ j ].lightmap, dvParent[ j ].lightmap, sizeof( dv[ j ].lightmap ) ); + memcpy( dv[ j ].color, dvParent[ j ].color, sizeof( dv[ j ].color ) ); } - /* calc lightmap origin in texture space */ - lmx = (float) lm->lightmapX[ lightmapNum ] / (float) olm->customWidth; - lmy = (float) lm->lightmapY[ lightmapNum ] / (float) olm->customHeight; + /* skip the rest */ + continue; + } - /* calc lightmap st coords */ - dv = &bspDrawVerts[ ds->firstVert ]; - ydv = &yDrawVerts[ ds->firstVert ]; - for ( j = 0; j < ds->numVerts; j++ ) + /* handle vertex lit or approximated surfaces */ + else if ( lm == NULL || lm->outLightmapNums[ 0 ] < 0 ) { + for ( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ ) { - if ( lm->solid[ lightmapNum ] ) { - dv[ j ].lightmap[ lightmapNum ][ 0 ] = lmx + ( 0.5f / (float) olm->customWidth ); - dv[ j ].lightmap[ lightmapNum ][ 1 ] = lmy + ( 0.5f / (float) olm->customWidth ); - } - else - { - dv[ j ].lightmap[ lightmapNum ][ 0 ] = lmx + ( ydv[ j ].lightmap[ 0 ][ 0 ] / ( superSample * olm->customWidth ) ); - dv[ j ].lightmap[ lightmapNum ][ 1 ] = lmy + ( ydv[ j ].lightmap[ 0 ][ 1 ] / ( superSample * olm->customHeight ) ); - } + ds->lightmapNum[ lightmapNum ] = -3; + ds->lightmapStyles[ lightmapNum ] = ds->vertexStyles[ lightmapNum ]; } } - } - /* store vertex colors */ - dv = &bspDrawVerts[ ds->firstVert ]; - for ( j = 0; j < ds->numVerts; j++ ) - { - /* walk lightmaps */ - for ( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ ) + /* handle lightmapped surfaces */ + else { - /* handle unused style */ - if ( ds->vertexStyles[ lightmapNum ] == LS_NONE ) { - VectorClear( color ); - } - else + /* walk lightmaps */ + for ( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ ) { - /* get vertex color */ - luxel = VERTEX_LUXEL( lightmapNum, ds->firstVert + j ); - VectorCopy( luxel, color ); + /* set style */ + ds->lightmapStyles[ lightmapNum ] = lm->styles[ lightmapNum ]; - /* set minimum light */ - if ( lightmapNum == 0 ) { - for ( k = 0; k < 3; k++ ) - if ( color[ k ] < minVertexLight[ k ] ) { - color[ k ] = minVertexLight[ k ]; - } + /* handle unused style */ + if ( lm->styles[ lightmapNum ] == LS_NONE || lm->outLightmapNums[ lightmapNum ] < 0 ) { + ds->lightmapNum[ lightmapNum ] = -3; + continue; } - } - /* store to bytes */ - if ( !info->si->noVertexLight ) { - ColorToBytes( color, dv[ j ].color[ lightmapNum ], info->si->vertexScale ); - } - } - } + /* get output lightmap */ + olm = &outLightmaps[ lm->outLightmapNums[ lightmapNum ] ]; - /* surfaces with styled lightmaps and a style marker get a custom generated shader (fixme: make this work with external lightmaps) */ - if ( olm != NULL && lm != NULL && lm->styles[ 1 ] != LS_NONE && game->load != LoadRBSPFile ) { //% info->si->styleMarker > 0 ) - qboolean dfEqual; - char key[ 32 ], styleStage[ 512 ], styleStages[ 4096 ], rgbGen[ 128 ], alphaGen[ 128 ]; + /* set bsp lightmap number */ + ds->lightmapNum[ lightmapNum ] = olm->lightmapNum; + /* deluxemap debugging makes the deluxemap visible */ + if ( deluxemap && debugDeluxemap && lightmapNum == 0 ) { + ds->lightmapNum[ lightmapNum ]++; + } - /* setup */ - sprintf( styleStages, "\n\t// Q3Map2 custom lightstyle stage(s)\n" ); - dv = &bspDrawVerts[ ds->firstVert ]; + /* calc lightmap origin in texture space */ + lmx = (float) lm->lightmapX[ lightmapNum ] / (float) olm->customWidth; + lmy = (float) lm->lightmapY[ lightmapNum ] / (float) olm->customHeight; - /* depthFunc equal? */ - if ( info->si->styleMarker == 2 || info->si->implicitMap == IM_MASKED ) { - dfEqual = qtrue; - } - else{ - dfEqual = qfalse; + /* calc lightmap st coords */ + dv = &bspDrawVerts[ ds->firstVert ]; + ydv = &yDrawVerts[ ds->firstVert ]; + for ( j = 0; j < ds->numVerts; j++ ) + { + if ( lm->solid[ lightmapNum ] ) { + dv[ j ].lightmap[ lightmapNum ][ 0 ] = lmx + ( 0.5f / (float) olm->customWidth ); + dv[ j ].lightmap[ lightmapNum ][ 1 ] = lmy + ( 0.5f / (float) olm->customWidth ); + } + else + { + dv[ j ].lightmap[ lightmapNum ][ 0 ] = lmx + ( ydv[ j ].lightmap[ 0 ][ 0 ] / ( superSample * olm->customWidth ) ); + dv[ j ].lightmap[ lightmapNum ][ 1 ] = lmy + ( ydv[ j ].lightmap[ 0 ][ 1 ] / ( superSample * olm->customHeight ) ); + } + } + } } - /* generate stages for styled lightmaps */ - for ( lightmapNum = 1; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ ) + /* store vertex colors */ + dv = &bspDrawVerts[ ds->firstVert ]; + for ( j = 0; j < ds->numVerts; j++ ) { - /* early out */ - style = lm->styles[ lightmapNum ]; - if ( style == LS_NONE || lm->outLightmapNums[ lightmapNum ] < 0 ) { - continue; + /* walk lightmaps */ + for ( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ ) + { + /* handle unused style */ + if ( ds->vertexStyles[ lightmapNum ] == LS_NONE ) { + VectorClear( color ); + } + else + { + /* get vertex color */ + luxel = VERTEX_LUXEL( lightmapNum, ds->firstVert + j ); + VectorCopy( luxel, color ); + + /* set minimum light */ + if ( lightmapNum == 0 ) { + for ( k = 0; k < 3; k++ ) + if ( color[ k ] < minVertexLight[ k ] ) { + color[ k ] = minVertexLight[ k ]; + } + } + } + + /* store to bytes */ + if ( !info->si->noVertexLight ) { + ColorToBytes( color, dv[ j ].color[ lightmapNum ], info->si->vertexScale ); + } } + } - /* get output lightmap */ - olm = &outLightmaps[ lm->outLightmapNums[ lightmapNum ] ]; + /* surfaces with styled lightmaps and a style marker get a custom generated shader (fixme: make this work with external lightmaps) */ + if ( olm != NULL && lm != NULL && lm->styles[ 1 ] != LS_NONE && game->load != LoadRBSPFile ) { //% info->si->styleMarker > 0 ) + qboolean dfEqual; + char key[ 32 ], styleStage[ 512 ], styleStages[ 4096 ], rgbGen[ 128 ], alphaGen[ 128 ]; - /* lightmap name */ - if ( lm->outLightmapNums[ lightmapNum ] == lm->outLightmapNums[ 0 ] ) { - strcpy( lightmapName, "$lightmap" ); + + /* setup */ + sprintf( styleStages, "\n\t// Q3Map2 custom lightstyle stage(s)\n" ); + dv = &bspDrawVerts[ ds->firstVert ]; + + /* depthFunc equal? */ + if ( info->si->styleMarker == 2 || info->si->implicitMap == IM_MASKED ) { + dfEqual = qtrue; } else{ - sprintf( lightmapName, "maps/%s/" EXTERNAL_LIGHTMAP, mapName, olm->extLightmapNum ); + dfEqual = qfalse; } - /* get rgbgen string */ - if ( rgbGenValues[ style ] == NULL ) { - sprintf( key, "_style%drgbgen", style ); - rgbGenValues[ style ] = ValueForKey( &entities[ 0 ], key ); - if ( rgbGenValues[ style ][ 0 ] == '\0' ) { - rgbGenValues[ style ] = "wave noise 0.5 1 0 5.37"; + /* generate stages for styled lightmaps */ + for ( lightmapNum = 1; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ ) + { + /* early out */ + style = lm->styles[ lightmapNum ]; + if ( style == LS_NONE || lm->outLightmapNums[ lightmapNum ] < 0 ) { + continue; + } + + /* get output lightmap */ + olm = &outLightmaps[ lm->outLightmapNums[ lightmapNum ] ]; + + /* lightmap name */ + if ( lm->outLightmapNums[ lightmapNum ] == lm->outLightmapNums[ 0 ] ) { + strcpy( lightmapName, "$lightmap" ); + } + else{ + sprintf( lightmapName, "maps/%s/" EXTERNAL_LIGHTMAP, mapName, olm->extLightmapNum ); + } + + /* get rgbgen string */ + if ( rgbGenValues[ style ] == NULL ) { + sprintf( key, "_style%drgbgen", style ); + rgbGenValues[ style ] = ValueForKey( &entities[ 0 ], key ); + if ( rgbGenValues[ style ][ 0 ] == '\0' ) { + rgbGenValues[ style ] = "wave noise 0.5 1 0 5.37"; + } } - } - rgbGen[ 0 ] = '\0'; - if ( rgbGenValues[ style ][ 0 ] != '\0' ) { - sprintf( rgbGen, "\t\trgbGen %s // style %d\n", rgbGenValues[ style ], style ); - } - else{ rgbGen[ 0 ] = '\0'; - } + if ( rgbGenValues[ style ][ 0 ] != '\0' ) { + sprintf( rgbGen, "\t\trgbGen %s // style %d\n", rgbGenValues[ style ], style ); + } + else{ + rgbGen[ 0 ] = '\0'; + } - /* get alphagen string */ - if ( alphaGenValues[ style ] == NULL ) { - sprintf( key, "_style%dalphagen", style ); - alphaGenValues[ style ] = ValueForKey( &entities[ 0 ], key ); - } - if ( alphaGenValues[ style ][ 0 ] != '\0' ) { - sprintf( alphaGen, "\t\talphaGen %s // style %d\n", alphaGenValues[ style ], style ); - } - else{ - alphaGen[ 0 ] = '\0'; - } + /* get alphagen string */ + if ( alphaGenValues[ style ] == NULL ) { + sprintf( key, "_style%dalphagen", style ); + alphaGenValues[ style ] = ValueForKey( &entities[ 0 ], key ); + } + if ( alphaGenValues[ style ][ 0 ] != '\0' ) { + sprintf( alphaGen, "\t\talphaGen %s // style %d\n", alphaGenValues[ style ], style ); + } + else{ + alphaGen[ 0 ] = '\0'; + } + + /* calculate st offset */ + lmx = dv[ 0 ].lightmap[ lightmapNum ][ 0 ] - dv[ 0 ].lightmap[ 0 ][ 0 ]; + lmy = dv[ 0 ].lightmap[ lightmapNum ][ 1 ] - dv[ 0 ].lightmap[ 0 ][ 1 ]; + + /* create additional stage */ + if ( lmx == 0.0f && lmy == 0.0f ) { + sprintf( styleStage, "\t{\n" + "\t\tmap %s\n" /* lightmap */ + "\t\tblendFunc GL_SRC_ALPHA GL_ONE\n" + "%s" /* depthFunc equal */ + "%s" /* rgbGen */ + "%s" /* alphaGen */ + "\t\ttcGen lightmap\n" + "\t}\n", + lightmapName, + ( dfEqual ? "\t\tdepthFunc equal\n" : "" ), + rgbGen, + alphaGen ); + } + else + { + sprintf( styleStage, "\t{\n" + "\t\tmap %s\n" /* lightmap */ + "\t\tblendFunc GL_SRC_ALPHA GL_ONE\n" + "%s" /* depthFunc equal */ + "%s" /* rgbGen */ + "%s" /* alphaGen */ + "\t\ttcGen lightmap\n" + "\t\ttcMod transform 1 0 0 1 %1.5f %1.5f\n" /* st offset */ + "\t}\n", + lightmapName, + ( dfEqual ? "\t\tdepthFunc equal\n" : "" ), + rgbGen, + alphaGen, + lmx, lmy ); + + } - /* calculate st offset */ - lmx = dv[ 0 ].lightmap[ lightmapNum ][ 0 ] - dv[ 0 ].lightmap[ 0 ][ 0 ]; - lmy = dv[ 0 ].lightmap[ lightmapNum ][ 1 ] - dv[ 0 ].lightmap[ 0 ][ 1 ]; - - /* create additional stage */ - if ( lmx == 0.0f && lmy == 0.0f ) { - sprintf( styleStage, "\t{\n" - "\t\tmap %s\n" /* lightmap */ - "\t\tblendFunc GL_SRC_ALPHA GL_ONE\n" - "%s" /* depthFunc equal */ - "%s" /* rgbGen */ - "%s" /* alphaGen */ - "\t\ttcGen lightmap\n" - "\t}\n", - lightmapName, - ( dfEqual ? "\t\tdepthFunc equal\n" : "" ), - rgbGen, - alphaGen ); + /* concatenate */ + strcat( styleStages, styleStage ); } - else - { - sprintf( styleStage, "\t{\n" - "\t\tmap %s\n" /* lightmap */ - "\t\tblendFunc GL_SRC_ALPHA GL_ONE\n" - "%s" /* depthFunc equal */ - "%s" /* rgbGen */ - "%s" /* alphaGen */ - "\t\ttcGen lightmap\n" - "\t\ttcMod transform 1 0 0 1 %1.5f %1.5f\n" /* st offset */ - "\t}\n", - lightmapName, - ( dfEqual ? "\t\tdepthFunc equal\n" : "" ), - rgbGen, - alphaGen, - lmx, lmy ); + /* create custom shader */ + if ( info->si->styleMarker == 2 ) { + csi = CustomShader( info->si, "q3map_styleMarker2", styleStages ); + } + else{ + csi = CustomShader( info->si, "q3map_styleMarker", styleStages ); } - /* concatenate */ - strcat( styleStages, styleStage ); - } + /* emit remap command */ + //% EmitVertexRemapShader( csi->shader, info->si->shader ); - /* create custom shader */ - if ( info->si->styleMarker == 2 ) { - csi = CustomShader( info->si, "q3map_styleMarker2", styleStages ); + /* store it */ + //% Sys_Printf( "Emitting: %s (%d", csi->shader, strlen( csi->shader ) ); + ds->shaderNum = EmitShader( csi->shader, &bspShaders[ ds->shaderNum ].contentFlags, &bspShaders[ ds->shaderNum ].surfaceFlags ); + //% Sys_Printf( ")\n" ); } - else{ - csi = CustomShader( info->si, "q3map_styleMarker", styleStages ); - } - - /* emit remap command */ - //% EmitVertexRemapShader( csi->shader, info->si->shader ); - - /* store it */ - //% Sys_Printf( "Emitting: %s (%d", csi->shader, strlen( csi->shader ) ); - ds->shaderNum = EmitShader( csi->shader, &bspShaders[ ds->shaderNum ].contentFlags, &bspShaders[ ds->shaderNum ].surfaceFlags ); - //% Sys_Printf( ")\n" ); - } - /* devise a custom shader for this surface (fixme: make this work with light styles) */ - else if ( olm != NULL && lm != NULL && !externalLightmaps && - ( olm->customWidth != game->lightmapSize || olm->customHeight != game->lightmapSize ) ) { - /* get output lightmap */ - olm = &outLightmaps[ lm->outLightmapNums[ 0 ] ]; + /* devise a custom shader for this surface (fixme: make this work with light styles) */ + else if ( olm != NULL && lm != NULL && !externalLightmaps && + ( olm->customWidth != game->lightmapSize || olm->customHeight != game->lightmapSize ) ) { + /* get output lightmap */ + olm = &outLightmaps[ lm->outLightmapNums[ 0 ] ]; - /* do some name mangling */ - sprintf( lightmapName, "maps/%s/" EXTERNAL_LIGHTMAP, mapName, olm->extLightmapNum ); + /* do some name mangling */ + sprintf( lightmapName, "maps/%s/" EXTERNAL_LIGHTMAP, mapName, olm->extLightmapNum ); - /* create custom shader */ - csi = CustomShader( info->si, "$lightmap", lightmapName ); + /* create custom shader */ + csi = CustomShader( info->si, "$lightmap", lightmapName ); - /* store it */ - //% Sys_Printf( "Emitting: %s (%d", csi->shader, strlen( csi->shader ) ); - ds->shaderNum = EmitShader( csi->shader, &bspShaders[ ds->shaderNum ].contentFlags, &bspShaders[ ds->shaderNum ].surfaceFlags ); - //% Sys_Printf( ")\n" ); - } + /* store it */ + //% Sys_Printf( "Emitting: %s (%d", csi->shader, strlen( csi->shader ) ); + ds->shaderNum = EmitShader( csi->shader, &bspShaders[ ds->shaderNum ].contentFlags, &bspShaders[ ds->shaderNum ].surfaceFlags ); + //% Sys_Printf( ")\n" ); + } - /* use the normal plain-jane shader */ - else{ - ds->shaderNum = EmitShader( info->si->shader, &bspShaders[ ds->shaderNum ].contentFlags, &bspShaders[ ds->shaderNum ].surfaceFlags ); + /* use the normal plain-jane shader */ + else{ + ds->shaderNum = EmitShader( info->si->shader, &bspShaders[ ds->shaderNum ].contentFlags, &bspShaders[ ds->shaderNum ].surfaceFlags ); + } } } + Sys_FPrintf( SYS_VRB, "%d.", (int) ( I_FloatTime() - timer_start ) ); + /* finish */ Sys_FPrintf( SYS_VRB, "done.\n" ); @@ -3515,17 +3545,19 @@ void StoreSurfaceLightmaps( qboolean fastAllocate ){ ? 0 : (float) numUsed / (float) numStored; - /* print stats */ - Sys_Printf( "%9d luxels used\n", numUsed ); - Sys_Printf( "%9d luxels stored (%3.2f percent efficiency)\n", numStored, efficiency * 100.0f ); - Sys_Printf( "%9d solid surface lightmaps\n", numSolidLightmaps ); - Sys_Printf( "%9d identical surface lightmaps, using %d luxels\n", numTwins, numTwinLuxels ); - Sys_Printf( "%9d vertex forced surfaces\n", numSurfsVertexForced ); - Sys_Printf( "%9d vertex approximated surfaces\n", numSurfsVertexApproximated ); - Sys_Printf( "%9d BSP lightmaps\n", numBSPLightmaps ); - Sys_Printf( "%9d total lightmaps\n", numOutLightmaps ); - Sys_Printf( "%9d unique lightmap/shader combinations\n", numLightmapShaders ); - - /* write map shader file */ - WriteMapShaderFile(); + if ( storeForReal ) { + /* print stats */ + Sys_Printf( "%9d luxels used\n", numUsed ); + Sys_Printf( "%9d luxels stored (%3.2f percent efficiency)\n", numStored, efficiency * 100.0f ); + Sys_Printf( "%9d solid surface lightmaps\n", numSolidLightmaps ); + Sys_Printf( "%9d identical surface lightmaps, using %d luxels\n", numTwins, numTwinLuxels ); + Sys_Printf( "%9d vertex forced surfaces\n", numSurfsVertexForced ); + Sys_Printf( "%9d vertex approximated surfaces\n", numSurfsVertexApproximated ); + Sys_Printf( "%9d BSP lightmaps\n", numBSPLightmaps ); + Sys_Printf( "%9d total lightmaps\n", numOutLightmaps ); + Sys_Printf( "%9d unique lightmap/shader combinations\n", numLightmapShaders ); + + /* write map shader file */ + WriteMapShaderFile(); + } }