2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 Lightmap allocation has to be done after all flood filling and
28 visible surface determination.
33 mapDrawSurface_t *surfsOnShader[ MAX_MAP_SHADERS ];
36 int allocated[ LIGHTMAP_WIDTH ];
39 int c_exactLightmap = 0;
40 int c_planarPatch = 0;
41 int c_nonplanarLightmap = 0;
44 void PrepareNewLightmap( void ) {
45 memset( allocated, 0, sizeof( allocated ) );
53 returns a texture number and the position inside it
56 qboolean AllocLMBlock (int w, int h, int *x, int *y)
61 best = LIGHTMAP_HEIGHT;
63 for ( i=0 ; i <= LIGHTMAP_WIDTH-w ; i++ ) {
66 for (j=0 ; j<w ; j++) {
67 if (allocated[i+j] >= best) {
70 if (allocated[i+j] > best2) {
71 best2 = allocated[i+j];
74 if (j == w) { // this is a valid spot
80 if (best + h > LIGHTMAP_HEIGHT) {
84 for (i=0 ; i<w ; i++) {
85 allocated[*x + i] = best + h;
94 AllocateLightmapForPatch
97 //#define LIGHTMAP_PATCHSHIFT
99 void AllocateLightmapForPatch( mapDrawSurface_t *ds )
106 mesh_t mesh, *subdividedMesh, *tempMesh, *newmesh;
107 int widthtable[LIGHTMAP_WIDTH], heighttable[LIGHTMAP_HEIGHT], ssize;
111 mesh.width = ds->patchWidth;
112 mesh.height = ds->patchHeight;
114 newmesh = SubdivideMesh( mesh, 8, 999 );
116 PutMeshOnCurve( *newmesh );
117 tempMesh = RemoveLinearMeshColumnsRows( newmesh );
120 /* get sample size */
121 ssize = ds->sampleSize;
124 #ifdef LIGHTMAP_PATCHSHIFT
125 subdividedMesh = SubdivideMeshQuads( tempMesh, ssize, LIGHTMAP_WIDTH-1, widthtable, heighttable );
127 subdividedMesh = SubdivideMeshQuads( tempMesh, ssize, LIGHTMAP_WIDTH, widthtable, heighttable );
130 w = subdividedMesh->width;
131 h = subdividedMesh->height;
133 #ifdef LIGHTMAP_PATCHSHIFT
138 FreeMesh(subdividedMesh);
140 // allocate the lightmap
141 c_exactLightmap += w * h;
143 if ( !AllocLMBlock( w, h, &x, &y ) ) {
144 PrepareNewLightmap();
145 if ( !AllocLMBlock( w, h, &x, &y ) )
147 Error("Entity %i, brush %i: Lightmap allocation failed",
148 ds->mapBrush->entitynum, ds->mapBrush->brushnum );
152 #ifdef LIGHTMAP_PATCHSHIFT
157 // set the lightmap texture coordinates in the drawVerts
158 ds->lightmapNum = numLightmaps - 1;
159 ds->lightmapWidth = w;
160 ds->lightmapHeight = h;
164 for ( i = 0 ; i < ds->patchWidth ; i++ ) {
165 for ( k = 0 ; k < w ; k++ ) {
166 if ( originalWidths[k] >= i ) {
173 for ( j = 0 ; j < ds->patchHeight ; j++ ) {
174 for ( k = 0 ; k < h ; k++ ) {
175 if ( originalHeights[k] >= j ) {
182 verts[i + j * ds->patchWidth].lightmap[0] = ( s + 0.5 ) / LIGHTMAP_WIDTH;
183 verts[i + j * ds->patchWidth].lightmap[1] = ( t + 0.5 ) / LIGHTMAP_HEIGHT;
191 AllocateLightmapForSurface
195 //#define LIGHTMAP_BLOCK 16
197 void AllocateLightmapForSurface( mapDrawSurface_t *ds )
199 vec3_t mins, maxs, size, exactSize, delta;
214 if( ds->type == SURF_META && ds->planar == qfalse )
215 Sys_Printf( "NPMS: %3d vertexes, %s\n", ds->numVerts, ds->shaderInfo->shader );
216 else if( ds->type == SURF_META && ds->planar == qtrue )
217 Sys_Printf( "PMS: %3d vertexes, %s\n", ds->numVerts, ds->shaderInfo->shader );
220 /* ydnar: handle planar patches */
221 if( noPatchFix == qtrue || (ds->type == SURF_PATCH && ds->planeNum < 0) )
223 AllocateLightmapForPatch( ds );
227 /* get sample size */
228 ssize = ds->sampleSize;
230 /* bound the surface */
231 ClearBounds( mins, maxs );
233 for ( i = 0 ; i < ds->numVerts ; i++ )
234 AddPointToBounds( verts[i].xyz, mins, maxs );
236 /* round to the lightmap resolution */
237 for( i = 0; i < 3; i++ )
239 exactSize[i] = maxs[i] - mins[i];
240 mins[i] = ssize * floor( mins[i] / ssize );
241 maxs[i] = ssize * ceil( maxs[i] / ssize );
242 size[i] = (maxs[i] - mins[i]) / ssize + 1;
245 /* ydnar: lightmap projection axis is already stored */
246 memset( vecs, 0, sizeof( vecs ) );
248 /* classify the plane (x y or z major) (ydnar: biased to z axis projection) */
249 if( ds->lightmapAxis[ 2 ] >= ds->lightmapAxis[ 0 ] && ds->lightmapAxis[ 2 ] >= ds->lightmapAxis[ 1 ] )
254 vecs[ 0 ][ 0 ] = 1.0 / ssize;
255 vecs[ 1 ][ 1 ] = 1.0 / ssize;
257 else if( ds->lightmapAxis[ 0 ] >= ds->lightmapAxis[ 1 ] && ds->lightmapAxis[ 0 ] >= ds->lightmapAxis[ 2 ] )
262 vecs[ 0 ][ 1 ] = 1.0 / ssize;
263 vecs[ 1 ][ 2 ] = 1.0 / ssize;
270 vecs[ 0 ][ 0 ] = 1.0 / ssize;
271 vecs[ 1 ][ 2 ] = 1.0 / ssize;
274 /* odd check, given projection is now precalculated */
275 if( ds->lightmapAxis[ axis ] == 0 )
276 Error( "Chose a 0 valued axis" );
278 /* clamp to lightmap texture resolution */
279 if( w > LIGHTMAP_WIDTH )
281 VectorScale ( vecs[0], (float) LIGHTMAP_WIDTH / w, vecs[0] );
284 if( h > LIGHTMAP_HEIGHT )
286 VectorScale ( vecs[1], (float) LIGHTMAP_HEIGHT / h, vecs[1] );
292 if( ds->planar == qfalse )
293 c_nonplanarLightmap += w * h;
294 c_exactLightmap += w * h;
297 if( !AllocLMBlock( w, h, &x, &y ) )
299 PrepareNewLightmap();
300 if ( !AllocLMBlock( w, h, &x, &y ) )
302 Error( "Entity %i, brush %i: Lightmap allocation failed",
303 ds->mapBrush->entitynum, ds->mapBrush->brushnum );
307 /* set the lightmap texture coordinates in the drawVerts */
308 ds->lightmapNum = numLightmaps - 1;
309 ds->lightmapWidth = w;
310 ds->lightmapHeight = h;
313 for ( i = 0 ; i < ds->numVerts ; i++ )
315 VectorSubtract( verts[i].xyz, mins, delta );
316 s = DotProduct( delta, vecs[0] ) + x + 0.5;
317 t = DotProduct( delta, vecs[1] ) + y + 0.5;
318 verts[i].lightmap[0] = s / LIGHTMAP_WIDTH;
319 verts[i].lightmap[1] = t / LIGHTMAP_HEIGHT;
322 /* calculate the world coordinates of the lightmap samples */
324 /* construct a plane from the first vert and clear bounding box */
326 /* project mins onto plane to get origin */
327 VectorCopy( ds->lightmapVecs[ 2 ], plane );
328 plane[ 3 ] = DotProduct( ds->verts[ 0 ].xyz, plane );
329 d = DotProduct( mins, plane ) - plane[ 3 ];
332 //% d = DotProduct( mins, plane->normal ) - plane->dist;
333 //% d /= plane->normal[ axis ];
334 VectorCopy( mins, origin );
337 /* project stepped lightmap blocks and subtract to get planevecs */
338 for( i = 0; i < 2; i++ )
343 len = VectorNormalize( vecs[i], normalized );
344 VectorScale( normalized, (1.0/len), vecs[i] );
345 d = DotProduct( vecs[i], plane );
347 //%d = DotProduct( vecs[i], plane->normal );
348 //%d /= plane->normal[ axis ];
352 /* store lightmap origin and vectors (fixme: make this work right) */
353 VectorCopy( origin, ds->lightmapOrigin );
354 //% VectorCopy( plane->normal, ds->lightmapVecs[ 2 ] );
356 /* ydnar: lightmap vectors 0 and 1 are used for lod bounds, so don't overwrite */
357 if( ds->type == SURF_PATCH )
360 /* store lightmap vectors */
361 VectorCopy( vecs[ 0 ], ds->lightmapVecs[ 0 ] );
362 VectorCopy( vecs[ 1 ], ds->lightmapVecs[ 1 ] );
364 /* ydnar: print some stats */
365 //Sys_FPrintf( SYS_VRB, "Lightmap block %3d (%3d, %3d) (%3d x %3d) emitted\n", (numLightmaps - 1), x, y, w, h );
374 void AllocateLightmaps( entity_t *e )
377 mapDrawSurface_t *ds;
382 Sys_FPrintf( SYS_VRB,"--- AllocateLightmaps ---\n" );
385 /* sort all surfaces by shader so common shaders will usually be in the same lightmap */
386 /* ydnar: this is done in two passes, because of an odd bug with lightmapped terrain */
388 for( i = e->firstDrawSurf; i < numMapDrawSurfs; i++ )
390 /* get surface and early out if possible */
391 ds = &mapDrawSurfs[ i ];
393 if( si->surfaceFlags & SURF_VERTEXLIT )
395 if( ds->numVerts <= 0 )
398 /* ydnar: handle brush faces and patches first */
399 if( ds->type != SURF_FACE && ds->type != SURF_PATCH )
402 /* ydnar: this is unecessary because it should already be set */
403 //% VectorCopy( ds->plane.normal, ds->lightmapVecs[ 2 ] );
405 /* search for this shader */
406 for( j = 0 ; j < numSortShaders; j++ )
408 if( ds->shaderInfo == surfsOnShader[ j ]->shaderInfo )
410 ds->nextOnShader = surfsOnShader[ j ];
411 surfsOnShader[ j ] = ds;
417 if( j == numSortShaders )
419 if( numSortShaders >= MAX_MAP_SHADERS )
420 Error( "MAX_MAP_SHADERS" );
421 surfsOnShader[ j ] = ds;
422 ds->nextOnShader = NULL;
427 /* second pass, to allocate lightmapped terrain last */
428 for( i = e->firstDrawSurf; i < numMapDrawSurfs; i++ )
430 /* get surface and early out if possible */
431 ds = &mapDrawSurfs[ i ];
433 if( si->surfaceFlags & SURF_VERTEXLIT )
435 if( ds->numVerts <= 0 )
438 /* ydnar: this only handles metasurfaces and terrain */
439 if( ds->type != SURF_TERRAIN && ds->type != SURF_META )
442 /* ydnar: a lightmap projection should be pre-stored for anything but excessively curved patches */
443 if( VectorLength( ds->lightmapAxis ) <= 0 )
446 /* search for this shader */
447 for( j = 0; j < numSortShaders; j++ )
449 if( ds->shaderInfo == surfsOnShader[ j ]->shaderInfo )
451 ds->nextOnShader = surfsOnShader[ j ];
452 surfsOnShader[ j ] = ds;
458 if( j == numSortShaders )
460 if( numSortShaders >= MAX_MAP_SHADERS )
461 Error( "MAX_MAP_SHADERS" );
462 surfsOnShader[ j ] = ds;
463 ds->nextOnShader = NULL;
468 /* tot up shader count */
469 Sys_FPrintf( SYS_VRB, "%9d unique shaders\n", numSortShaders );
471 /* for each shader, allocate lightmaps for each surface */
472 for( i = 0; i < numSortShaders; i++ )
474 si = surfsOnShader[ i ]->shaderInfo;
475 for( ds = surfsOnShader[ i ]; ds; ds = ds->nextOnShader )
477 /* ydnar: promoting pointlight above nolightmap */
478 if( si->surfaceFlags & SURF_POINTLIGHT )
479 ds->lightmapNum = -3;
480 else if( si->surfaceFlags & SURF_NOLIGHTMAP )
481 ds->lightmapNum = -1;
483 AllocateLightmapForSurface( ds );
487 /* emit some statistics */
488 Sys_FPrintf( SYS_VRB, "%9d exact lightmap texels\n", c_exactLightmap );
489 Sys_FPrintf( SYS_VRB, "%9d block lightmap texels\n", numLightmaps * LIGHTMAP_WIDTH * LIGHTMAP_HEIGHT );
490 Sys_FPrintf( SYS_VRB, "%9d non-planar or terrain lightmap texels\n", c_nonplanarLightmap );
491 Sys_FPrintf( SYS_VRB, "%9d planar patch lightmaps\n", c_planarPatch );
492 Sys_FPrintf( SYS_VRB, "%9d lightmap textures, size: %d Kbytes\n", numLightmaps, (numLightmaps * LIGHTMAP_WIDTH * LIGHTMAP_HEIGHT * 3) / 1024 );