2007-11-04 03:34:51 +00:00
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
Copyright (C) 1999-2007 id Software, Inc. and contributors.
|
|
|
|
For a list of contributors, see the accompanying CONTRIBUTORS file.
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
This file is part of GtkRadiant.
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
GtkRadiant is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
GtkRadiant is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with GtkRadiant; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
#include "qbsp.h"
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
Lightmap allocation has to be done after all flood filling and
|
|
|
|
visible surface determination.
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
*/
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int numSortShaders;
|
|
|
|
mapDrawSurface_t *surfsOnShader[ MAX_MAP_SHADERS ];
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int allocated[ LIGHTMAP_WIDTH ];
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int numLightmaps = 1;
|
|
|
|
int c_exactLightmap = 0;
|
|
|
|
int c_planarPatch = 0;
|
|
|
|
int c_nonplanarLightmap = 0;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
void PrepareNewLightmap( void ) {
|
|
|
|
memset( allocated, 0, sizeof( allocated ) );
|
|
|
|
numLightmaps++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
===============
|
|
|
|
AllocLMBlock
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
returns a texture number and the position inside it
|
|
|
|
===============
|
|
|
|
*/
|
|
|
|
qboolean AllocLMBlock( int w, int h, int *x, int *y ){
|
|
|
|
int i, j;
|
|
|
|
int best, best2;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
best = LIGHTMAP_HEIGHT;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0 ; i <= LIGHTMAP_WIDTH - w ; i++ ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
best2 = 0;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( j = 0 ; j < w ; j++ ) {
|
|
|
|
if ( allocated[i + j] >= best ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( allocated[i + j] > best2 ) {
|
|
|
|
best2 = allocated[i + j];
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( j == w ) { // this is a valid spot
|
2007-11-04 03:34:51 +00:00
|
|
|
*x = i;
|
|
|
|
*y = best = best2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( best + h > LIGHTMAP_HEIGHT ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return qfalse;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0 ; i < w ; i++ ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
allocated[*x + i] = best + h;
|
|
|
|
}
|
|
|
|
|
|
|
|
return qtrue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
===================
|
|
|
|
AllocateLightmapForPatch
|
|
|
|
===================
|
|
|
|
*/
|
2007-11-04 03:34:51 +00:00
|
|
|
//#define LIGHTMAP_PATCHSHIFT
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void AllocateLightmapForPatch( mapDrawSurface_t *ds ){
|
|
|
|
int i, j, k;
|
|
|
|
drawVert_t *verts;
|
|
|
|
int w, h;
|
|
|
|
int x, y;
|
|
|
|
float s, t;
|
|
|
|
mesh_t mesh, *subdividedMesh, *tempMesh, *newmesh;
|
|
|
|
int widthtable[LIGHTMAP_WIDTH], heighttable[LIGHTMAP_HEIGHT], ssize;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
verts = ds->verts;
|
|
|
|
|
|
|
|
mesh.width = ds->patchWidth;
|
|
|
|
mesh.height = ds->patchHeight;
|
|
|
|
mesh.verts = verts;
|
|
|
|
newmesh = SubdivideMesh( mesh, 8, 999 );
|
|
|
|
|
|
|
|
PutMeshOnCurve( *newmesh );
|
|
|
|
tempMesh = RemoveLinearMeshColumnsRows( newmesh );
|
2012-03-17 20:01:54 +00:00
|
|
|
FreeMesh( newmesh );
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* get sample size */
|
2012-03-17 20:01:54 +00:00
|
|
|
ssize = ds->sampleSize;
|
|
|
|
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
#ifdef LIGHTMAP_PATCHSHIFT
|
2012-03-17 20:01:54 +00:00
|
|
|
subdividedMesh = SubdivideMeshQuads( tempMesh, ssize, LIGHTMAP_WIDTH - 1, widthtable, heighttable );
|
2007-11-04 03:34:51 +00:00
|
|
|
#else
|
2012-03-17 20:01:54 +00:00
|
|
|
subdividedMesh = SubdivideMeshQuads( tempMesh, ssize, LIGHTMAP_WIDTH, widthtable, heighttable );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
w = subdividedMesh->width;
|
|
|
|
h = subdividedMesh->height;
|
|
|
|
|
|
|
|
#ifdef LIGHTMAP_PATCHSHIFT
|
|
|
|
w++;
|
|
|
|
h++;
|
|
|
|
#endif
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
FreeMesh( subdividedMesh );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// allocate the lightmap
|
|
|
|
c_exactLightmap += w * h;
|
|
|
|
|
|
|
|
if ( !AllocLMBlock( w, h, &x, &y ) ) {
|
|
|
|
PrepareNewLightmap();
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !AllocLMBlock( w, h, &x, &y ) ) {
|
|
|
|
Error( "Entity %i, brush %i: Lightmap allocation failed",
|
|
|
|
ds->mapBrush->entitynum, ds->mapBrush->brushnum );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef LIGHTMAP_PATCHSHIFT
|
|
|
|
w--;
|
|
|
|
h--;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// set the lightmap texture coordinates in the drawVerts
|
|
|
|
ds->lightmapNum = numLightmaps - 1;
|
|
|
|
ds->lightmapWidth = w;
|
|
|
|
ds->lightmapHeight = h;
|
|
|
|
ds->lightmapX = x;
|
|
|
|
ds->lightmapY = y;
|
|
|
|
|
|
|
|
for ( i = 0 ; i < ds->patchWidth ; i++ ) {
|
|
|
|
for ( k = 0 ; k < w ; k++ ) {
|
|
|
|
if ( originalWidths[k] >= i ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( k >= w ) {
|
|
|
|
k = w - 1;
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
s = x + k;
|
|
|
|
for ( j = 0 ; j < ds->patchHeight ; j++ ) {
|
|
|
|
for ( k = 0 ; k < h ; k++ ) {
|
|
|
|
if ( originalHeights[k] >= j ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( k >= h ) {
|
|
|
|
k = h - 1;
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
t = y + k;
|
|
|
|
verts[i + j * ds->patchWidth].lightmap[0] = ( s + 0.5 ) / LIGHTMAP_WIDTH;
|
|
|
|
verts[i + j * ds->patchWidth].lightmap[1] = ( t + 0.5 ) / LIGHTMAP_HEIGHT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
===================
|
|
|
|
AllocateLightmapForSurface
|
|
|
|
===================
|
|
|
|
*/
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
//#define LIGHTMAP_BLOCK 16
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void AllocateLightmapForSurface( mapDrawSurface_t *ds ){
|
|
|
|
vec3_t mins, maxs, size, exactSize, delta;
|
|
|
|
int i;
|
|
|
|
drawVert_t *verts;
|
|
|
|
int w, h;
|
|
|
|
int x, y, ssize;
|
|
|
|
int axis;
|
|
|
|
vec3_t vecs[ 2 ];
|
|
|
|
float s, t;
|
|
|
|
vec3_t origin;
|
|
|
|
vec4_t plane;
|
|
|
|
float d;
|
|
|
|
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* debug code */
|
|
|
|
#if 0
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ds->type == SURF_META && ds->planar == qfalse ) {
|
|
|
|
Sys_Printf( "NPMS: %3d vertexes, %s\n", ds->numVerts, ds->shaderInfo->shader );
|
|
|
|
}
|
|
|
|
else if ( ds->type == SURF_META && ds->planar == qtrue ) {
|
|
|
|
Sys_Printf( "PMS: %3d vertexes, %s\n", ds->numVerts, ds->shaderInfo->shader );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* ydnar: handle planar patches */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( noPatchFix == qtrue || ( ds->type == SURF_PATCH && ds->planeNum < 0 ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
AllocateLightmapForPatch( ds );
|
|
|
|
return;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* get sample size */
|
2012-03-17 20:01:54 +00:00
|
|
|
ssize = ds->sampleSize;
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* bound the surface */
|
|
|
|
ClearBounds( mins, maxs );
|
|
|
|
verts = ds->verts;
|
|
|
|
for ( i = 0 ; i < ds->numVerts ; i++ )
|
|
|
|
AddPointToBounds( verts[i].xyz, mins, maxs );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* round to the lightmap resolution */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < 3; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
exactSize[i] = maxs[i] - mins[i];
|
2012-03-17 20:01:54 +00:00
|
|
|
mins[i] = ssize * floor( mins[i] / ssize );
|
|
|
|
maxs[i] = ssize * ceil( maxs[i] / ssize );
|
|
|
|
size[i] = ( maxs[i] - mins[i] ) / ssize + 1;
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* ydnar: lightmap projection axis is already stored */
|
|
|
|
memset( vecs, 0, sizeof( vecs ) );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* classify the plane (x y or z major) (ydnar: biased to z axis projection) */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ds->lightmapAxis[ 2 ] >= ds->lightmapAxis[ 0 ] && ds->lightmapAxis[ 2 ] >= ds->lightmapAxis[ 1 ] ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
w = size[ 0 ];
|
|
|
|
h = size[ 1 ];
|
|
|
|
axis = 2;
|
|
|
|
vecs[ 0 ][ 0 ] = 1.0 / ssize;
|
|
|
|
vecs[ 1 ][ 1 ] = 1.0 / ssize;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
else if ( ds->lightmapAxis[ 0 ] >= ds->lightmapAxis[ 1 ] && ds->lightmapAxis[ 0 ] >= ds->lightmapAxis[ 2 ] ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
w = size[ 1 ];
|
|
|
|
h = size[ 2 ];
|
|
|
|
axis = 0;
|
|
|
|
vecs[ 0 ][ 1 ] = 1.0 / ssize;
|
|
|
|
vecs[ 1 ][ 2 ] = 1.0 / ssize;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
w = size[ 0 ];
|
|
|
|
h = size[ 2 ];
|
|
|
|
axis = 1;
|
|
|
|
vecs[ 0 ][ 0 ] = 1.0 / ssize;
|
|
|
|
vecs[ 1 ][ 2 ] = 1.0 / ssize;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* odd check, given projection is now precalculated */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ds->lightmapAxis[ axis ] == 0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
Error( "Chose a 0 valued axis" );
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* clamp to lightmap texture resolution */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( w > LIGHTMAP_WIDTH ) {
|
|
|
|
VectorScale( vecs[0], (float) LIGHTMAP_WIDTH / w, vecs[0] );
|
2007-11-04 03:34:51 +00:00
|
|
|
w = LIGHTMAP_WIDTH;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( h > LIGHTMAP_HEIGHT ) {
|
|
|
|
VectorScale( vecs[1], (float) LIGHTMAP_HEIGHT / h, vecs[1] );
|
2007-11-04 03:34:51 +00:00
|
|
|
h = LIGHTMAP_HEIGHT;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* ydnar */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ds->planar == qfalse ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
c_nonplanarLightmap += w * h;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
c_exactLightmap += w * h;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
if ( !AllocLMBlock( w, h, &x, &y ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
PrepareNewLightmap();
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !AllocLMBlock( w, h, &x, &y ) ) {
|
|
|
|
Error( "Entity %i, brush %i: Lightmap allocation failed",
|
|
|
|
ds->mapBrush->entitynum, ds->mapBrush->brushnum );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set the lightmap texture coordinates in the drawVerts */
|
|
|
|
ds->lightmapNum = numLightmaps - 1;
|
|
|
|
ds->lightmapWidth = w;
|
|
|
|
ds->lightmapHeight = h;
|
|
|
|
ds->lightmapX = x;
|
|
|
|
ds->lightmapY = y;
|
|
|
|
for ( i = 0 ; i < ds->numVerts ; i++ )
|
|
|
|
{
|
|
|
|
VectorSubtract( verts[i].xyz, mins, delta );
|
|
|
|
s = DotProduct( delta, vecs[0] ) + x + 0.5;
|
|
|
|
t = DotProduct( delta, vecs[1] ) + y + 0.5;
|
|
|
|
verts[i].lightmap[0] = s / LIGHTMAP_WIDTH;
|
|
|
|
verts[i].lightmap[1] = t / LIGHTMAP_HEIGHT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* calculate the world coordinates of the lightmap samples */
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* construct a plane from the first vert and clear bounding box */
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* project mins onto plane to get origin */
|
|
|
|
VectorCopy( ds->lightmapVecs[ 2 ], plane );
|
|
|
|
plane[ 3 ] = DotProduct( ds->verts[ 0 ].xyz, plane );
|
|
|
|
d = DotProduct( mins, plane ) - plane[ 3 ];
|
|
|
|
d /= plane[ axis ];
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
//% d = DotProduct( mins, plane->normal ) - plane->dist;
|
|
|
|
//% d /= plane->normal[ axis ];
|
|
|
|
VectorCopy( mins, origin );
|
|
|
|
origin[ axis ] -= d;
|
|
|
|
|
|
|
|
/* project stepped lightmap blocks and subtract to get planevecs */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < 2; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
vec3_t normalized;
|
|
|
|
float len;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
len = VectorNormalize( vecs[i], normalized );
|
2012-03-17 20:01:54 +00:00
|
|
|
VectorScale( normalized, ( 1.0 / len ), vecs[i] );
|
2007-11-04 03:34:51 +00:00
|
|
|
d = DotProduct( vecs[i], plane );
|
|
|
|
d /= plane[ axis ];
|
|
|
|
//%d = DotProduct( vecs[i], plane->normal );
|
|
|
|
//%d /= plane->normal[ axis ];
|
|
|
|
vecs[i][axis] -= d;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* store lightmap origin and vectors (fixme: make this work right) */
|
|
|
|
VectorCopy( origin, ds->lightmapOrigin );
|
|
|
|
//% VectorCopy( plane->normal, ds->lightmapVecs[ 2 ] );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* ydnar: lightmap vectors 0 and 1 are used for lod bounds, so don't overwrite */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ds->type == SURF_PATCH ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
c_planarPatch++;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* store lightmap vectors */
|
|
|
|
VectorCopy( vecs[ 0 ], ds->lightmapVecs[ 0 ] );
|
|
|
|
VectorCopy( vecs[ 1 ], ds->lightmapVecs[ 1 ] );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* ydnar: print some stats */
|
|
|
|
//Sys_FPrintf( SYS_VRB, "Lightmap block %3d (%3d, %3d) (%3d x %3d) emitted\n", (numLightmaps - 1), x, y, w, h );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
===================
|
|
|
|
AllocateLightmaps
|
|
|
|
===================
|
|
|
|
*/
|
|
|
|
void AllocateLightmaps( entity_t *e ){
|
|
|
|
int i, j;
|
|
|
|
mapDrawSurface_t *ds;
|
|
|
|
shaderInfo_t *si;
|
|
|
|
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* note it */
|
|
|
|
Sys_FPrintf( SYS_VRB,"--- AllocateLightmaps ---\n" );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* sort all surfaces by shader so common shaders will usually be in the same lightmap */
|
|
|
|
/* ydnar: this is done in two passes, because of an odd bug with lightmapped terrain */
|
|
|
|
numSortShaders = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = e->firstDrawSurf; i < numMapDrawSurfs; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
/* get surface and early out if possible */
|
|
|
|
ds = &mapDrawSurfs[ i ];
|
|
|
|
si = ds->shaderInfo;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( si->surfaceFlags & SURF_VERTEXLIT ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( ds->numVerts <= 0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* ydnar: handle brush faces and patches first */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ds->type != SURF_FACE && ds->type != SURF_PATCH ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* ydnar: this is unecessary because it should already be set */
|
|
|
|
//% VectorCopy( ds->plane.normal, ds->lightmapVecs[ 2 ] );
|
|
|
|
|
|
|
|
/* search for this shader */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( j = 0 ; j < numSortShaders; j++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ds->shaderInfo == surfsOnShader[ j ]->shaderInfo ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
ds->nextOnShader = surfsOnShader[ j ];
|
|
|
|
surfsOnShader[ j ] = ds;
|
|
|
|
break;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* new shader */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( j == numSortShaders ) {
|
|
|
|
if ( numSortShaders >= MAX_MAP_SHADERS ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
Error( "MAX_MAP_SHADERS" );
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
surfsOnShader[ j ] = ds;
|
|
|
|
ds->nextOnShader = NULL;
|
|
|
|
numSortShaders++;
|
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* second pass, to allocate lightmapped terrain last */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = e->firstDrawSurf; i < numMapDrawSurfs; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
/* get surface and early out if possible */
|
|
|
|
ds = &mapDrawSurfs[ i ];
|
|
|
|
si = ds->shaderInfo;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( si->surfaceFlags & SURF_VERTEXLIT ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( ds->numVerts <= 0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* ydnar: this only handles metasurfaces and terrain */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ds->type != SURF_TERRAIN && ds->type != SURF_META ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* ydnar: a lightmap projection should be pre-stored for anything but excessively curved patches */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( VectorLength( ds->lightmapAxis ) <= 0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* search for this shader */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( j = 0; j < numSortShaders; j++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ds->shaderInfo == surfsOnShader[ j ]->shaderInfo ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
ds->nextOnShader = surfsOnShader[ j ];
|
|
|
|
surfsOnShader[ j ] = ds;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* new shader */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( j == numSortShaders ) {
|
|
|
|
if ( numSortShaders >= MAX_MAP_SHADERS ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
Error( "MAX_MAP_SHADERS" );
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
surfsOnShader[ j ] = ds;
|
|
|
|
ds->nextOnShader = NULL;
|
|
|
|
numSortShaders++;
|
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* tot up shader count */
|
|
|
|
Sys_FPrintf( SYS_VRB, "%9d unique shaders\n", numSortShaders );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* for each shader, allocate lightmaps for each surface */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < numSortShaders; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
si = surfsOnShader[ i ]->shaderInfo;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( ds = surfsOnShader[ i ]; ds; ds = ds->nextOnShader )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
/* ydnar: promoting pointlight above nolightmap */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( si->surfaceFlags & SURF_POINTLIGHT ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
ds->lightmapNum = -3;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
else if ( si->surfaceFlags & SURF_NOLIGHTMAP ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
ds->lightmapNum = -1;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
else{
|
2007-11-04 03:34:51 +00:00
|
|
|
AllocateLightmapForSurface( ds );
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* emit some statistics */
|
|
|
|
Sys_FPrintf( SYS_VRB, "%9d exact lightmap texels\n", c_exactLightmap );
|
|
|
|
Sys_FPrintf( SYS_VRB, "%9d block lightmap texels\n", numLightmaps * LIGHTMAP_WIDTH * LIGHTMAP_HEIGHT );
|
|
|
|
Sys_FPrintf( SYS_VRB, "%9d non-planar or terrain lightmap texels\n", c_nonplanarLightmap );
|
|
|
|
Sys_FPrintf( SYS_VRB, "%9d planar patch lightmaps\n", c_planarPatch );
|
2012-03-17 20:01:54 +00:00
|
|
|
Sys_FPrintf( SYS_VRB, "%9d lightmap textures, size: %d Kbytes\n", numLightmaps, ( numLightmaps * LIGHTMAP_WIDTH * LIGHTMAP_HEIGHT * 3 ) / 1024 );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|