stores the surface lightmaps into the bsp as byte rgb triplets
*/
-void StoreSurfaceLightmaps( qboolean fastAllocate ){
+void StoreSurfaceLightmaps( qboolean fastAllocate, qboolean storeForReal ){
- int i, j, k, x, y, lx, ly, sx, sy, *cluster, mappedSamples;
+ 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;
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;
- /* find output lightmap */
- for ( i = 0; i < numRawLightmaps; i++ )
- {
- lm = &rawLightmaps[ sortLightmaps[ i ] ];
- FindOutLightmaps( lm, fastAllocate );
- }
+ numLightmapShaders = 0;
+ numOutLightmaps = 0;
+ numBSPLightmaps = 0;
+ numExtLightmaps = 0;
- /* 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 ];
+ }
}
}
store output lightmaps
----------------------------------------------------------------- */
- /* note it */
- Sys_FPrintf( SYS_VRB, "storing..." );
+ if ( storeForReal ) {
+ /* 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 );
- }
-
- /* walk the list of output lightmaps */
- for ( i = 0; i < numOutLightmaps; i++ )
- {
- /* get output lightmap */
- olm = &outLightmaps[ i ];
-
- /* fill output lightmap */
- if ( lightmapFill ) {
- FillOutLightmap( olm );
+ timer_start = I_FloatTime();
+
+ /* 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 );
+ }
+ }
+
+ /* external lightmap? */
+ if ( olm->lightmapNum < 0 || olm->extLightmapNum >= 0 || externalLightmaps ) {
+ /* make a directory for the lightmaps */
+ Q_mkdir( dirname );
- /* 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++;
+ /* set external lightmap number */
+ olm->extLightmapNum = numExtLightmaps;
- /* write deluxemap */
- if ( deluxemap ) {
+ /* 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..." );
+ if ( storeForReal ) {
+ /* 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 ) );
- }
+ timer_start = I_FloatTime();
+
+ /* 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;
- /* skip the rest */
- 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 ) );
- /* 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 ];
- }
- }
+ /* overwrite child with parent data */
+ memcpy( ds, parent, sizeof( *ds ) );
- /* handle lightmapped surfaces */
- else
- {
- /* walk lightmaps */
- for ( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
- {
- /* set style */
- ds->lightmapStyles[ lightmapNum ] = lm->styles[ lightmapNum ];
+ /* restore key parts */
+ ds->fogNum = dsTemp.fogNum;
+ ds->firstVert = dsTemp.firstVert;
+ ds->firstIndex = dsTemp.firstIndex;
+ memcpy( ds->lightmapVecs, dsTemp.lightmapVecs, sizeof( dsTemp.lightmapVecs ) );
- /* handle unused style */
- if ( lm->styles[ lightmapNum ] == LS_NONE || lm->outLightmapNums[ lightmapNum ] < 0 ) {
+ /* 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;
+ }
+
+ /* 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;
- continue;
+ ds->lightmapStyles[ lightmapNum ] = ds->vertexStyles[ lightmapNum ];
}
+ }
- /* get output lightmap */
- olm = &outLightmaps[ lm->outLightmapNums[ lightmapNum ] ];
+ /* handle lightmapped surfaces */
+ else
+ {
+ /* walk lightmaps */
+ for ( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
+ {
+ /* set style */
+ ds->lightmapStyles[ lightmapNum ] = lm->styles[ lightmapNum ];
- /* set bsp lightmap number */
- ds->lightmapNum[ lightmapNum ] = olm->lightmapNum;
+ /* handle unused style */
+ if ( lm->styles[ lightmapNum ] == LS_NONE || lm->outLightmapNums[ lightmapNum ] < 0 ) {
+ ds->lightmapNum[ lightmapNum ] = -3;
+ continue;
+ }
- /* deluxemap debugging makes the deluxemap visible */
- if ( deluxemap && debugDeluxemap && lightmapNum == 0 ) {
- ds->lightmapNum[ lightmapNum ]++;
- }
+ /* get output lightmap */
+ olm = &outLightmaps[ lm->outLightmapNums[ lightmapNum ] ];
- /* calc lightmap origin in texture space */
- lmx = (float) lm->lightmapX[ lightmapNum ] / (float) olm->customWidth;
- lmy = (float) lm->lightmapY[ lightmapNum ] / (float) olm->customHeight;
+ /* set bsp lightmap number */
+ ds->lightmapNum[ lightmapNum ] = olm->lightmapNum;
- /* 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 );
+ /* deluxemap debugging makes the deluxemap visible */
+ if ( deluxemap && debugDeluxemap && lightmapNum == 0 ) {
+ ds->lightmapNum[ lightmapNum ]++;
}
- else
+
+ /* calc lightmap origin in texture space */
+ lmx = (float) lm->lightmapX[ lightmapNum ] / (float) olm->customWidth;
+ lmy = (float) lm->lightmapY[ lightmapNum ] / (float) olm->customHeight;
+
+ /* calc lightmap st coords */
+ dv = &bspDrawVerts[ ds->firstVert ];
+ ydv = &yDrawVerts[ ds->firstVert ];
+ for ( j = 0; j < ds->numVerts; j++ )
{
- 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 ) );
+ 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 ) );
+ }
}
}
}