2008-07-25 07:31:37 +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
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
----------------------------------------------------------------------------------
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
This code has been altered significantly from its original form, to support
|
|
|
|
several games based on the Quake III Arena engine, in the form of "Q3Map2."
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
------------------------------------------------------------------------------- */
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* marker */
|
|
|
|
#define LIGHT_BOUNCE_C
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* dependencies */
|
|
|
|
#include "q3map2.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* functions */
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
RadFreeLights()
|
|
|
|
deletes any existing lights, freeing up memory for the next bounce
|
|
|
|
*/
|
|
|
|
|
|
|
|
void RadFreeLights( void ){
|
|
|
|
light_t *light, *next;
|
|
|
|
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* delete lights */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( light = lights; light; light = next )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
next = light->next;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( light->w != NULL ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
FreeWinding( light->w );
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
free( light );
|
|
|
|
}
|
|
|
|
numLights = 0;
|
|
|
|
lights = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
RadClipWindingEpsilon()
|
|
|
|
clips a rad winding by a plane
|
|
|
|
based off the regular clip winding code
|
|
|
|
*/
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
static void RadClipWindingEpsilon( radWinding_t *in, vec3_t normal, vec_t dist,
|
2012-03-17 20:01:54 +00:00
|
|
|
vec_t epsilon, radWinding_t *front, radWinding_t *back, clipWork_t *cw ){
|
|
|
|
vec_t *dists;
|
|
|
|
int *sides;
|
|
|
|
int counts[ 3 ];
|
|
|
|
vec_t dot; /* ydnar: changed from static b/c of threading */ /* VC 4.2 optimizer bug if not static? */
|
|
|
|
int i, j, k;
|
|
|
|
radVert_t *v1, *v2, mid;
|
|
|
|
int maxPoints;
|
|
|
|
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* crutch */
|
|
|
|
dists = cw->dists;
|
|
|
|
sides = cw->sides;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* clear counts */
|
|
|
|
counts[ 0 ] = counts[ 1 ] = counts[ 2 ] = 0;
|
|
|
|
|
|
|
|
/* determine sides for each point */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < in->numVerts; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
dot = DotProduct( in->verts[ i ].xyz, normal );
|
|
|
|
dot -= dist;
|
|
|
|
dists[ i ] = dot;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( dot > epsilon ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
sides[ i ] = SIDE_FRONT;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
else if ( dot < -epsilon ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
sides[ i ] = SIDE_BACK;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
else{
|
2007-11-04 03:34:51 +00:00
|
|
|
sides[ i ] = SIDE_ON;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
counts[ sides[ i ] ]++;
|
|
|
|
}
|
|
|
|
sides[ i ] = sides[ 0 ];
|
|
|
|
dists[ i ] = dists[ 0 ];
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* clear front and back */
|
|
|
|
front->numVerts = back->numVerts = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* handle all on one side cases */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( counts[ 0 ] == 0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
memcpy( back, in, sizeof( radWinding_t ) );
|
|
|
|
return;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( counts[ 1 ] == 0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
memcpy( front, in, sizeof( radWinding_t ) );
|
|
|
|
return;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* setup windings */
|
|
|
|
maxPoints = in->numVerts + 4;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* do individual verts */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < in->numVerts; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
/* do simple vertex copies first */
|
|
|
|
v1 = &in->verts[ i ];
|
2012-03-17 20:01:54 +00:00
|
|
|
|
|
|
|
if ( sides[ i ] == SIDE_ON ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
memcpy( &front->verts[ front->numVerts++ ], v1, sizeof( radVert_t ) );
|
|
|
|
memcpy( &back->verts[ back->numVerts++ ], v1, sizeof( radVert_t ) );
|
|
|
|
continue;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
|
|
|
if ( sides[ i ] == SIDE_FRONT ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
memcpy( &front->verts[ front->numVerts++ ], v1, sizeof( radVert_t ) );
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( sides[ i ] == SIDE_BACK ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
memcpy( &back->verts[ back->numVerts++ ], v1, sizeof( radVert_t ) );
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( sides[ i + 1 ] == SIDE_ON || sides[ i + 1 ] == sides[ i ] ) {
|
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
|
|
|
/* generate a split vertex */
|
2012-03-17 20:01:54 +00:00
|
|
|
v2 = &in->verts[ ( i + 1 ) % in->numVerts ];
|
|
|
|
|
|
|
|
dot = dists[ i ] / ( dists[ i ] - dists[ i + 1 ] );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* average vertex values */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( j = 0; j < 4; j++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
/* color */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( j < 4 ) {
|
|
|
|
for ( k = 0; k < MAX_LIGHTMAPS; k++ )
|
|
|
|
mid.color[ k ][ j ] = v1->color[ k ][ j ] + dot * ( v2->color[ k ][ j ] - v1->color[ k ][ j ] );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* xyz, normal */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( j < 3 ) {
|
|
|
|
mid.xyz[ j ] = v1->xyz[ j ] + dot * ( v2->xyz[ j ] - v1->xyz[ j ] );
|
|
|
|
mid.normal[ j ] = v1->normal[ j ] + dot * ( v2->normal[ j ] - v1->normal[ j ] );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* st, lightmap */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( j < 2 ) {
|
|
|
|
mid.st[ j ] = v1->st[ j ] + dot * ( v2->st[ j ] - v1->st[ j ] );
|
|
|
|
for ( k = 0; k < MAX_LIGHTMAPS; k++ )
|
|
|
|
mid.lightmap[ k ][ j ] = v1->lightmap[ k ][ j ] + dot * ( v2->lightmap[ k ][ j ] - v1->lightmap[ k ][ j ] );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* normalize the averaged normal */
|
|
|
|
VectorNormalize( mid.normal, mid.normal );
|
|
|
|
|
|
|
|
/* copy the midpoint to both windings */
|
|
|
|
memcpy( &front->verts[ front->numVerts++ ], &mid, sizeof( radVert_t ) );
|
|
|
|
memcpy( &back->verts[ back->numVerts++ ], &mid, sizeof( radVert_t ) );
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* error check */
|
2013-11-01 00:24:58 +00:00
|
|
|
if ( front->numVerts > maxPoints ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
Error( "RadClipWindingEpsilon: points exceeded estimate" );
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2013-11-01 00:24:58 +00:00
|
|
|
if ( front->numVerts > MAX_POINTS_ON_WINDING ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
Error( "RadClipWindingEpsilon: MAX_POINTS_ON_WINDING" );
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
RadSampleImage()
|
|
|
|
samples a texture image for a given color
|
|
|
|
returns qfalse if pixels are bad
|
|
|
|
*/
|
|
|
|
|
|
|
|
qboolean RadSampleImage( byte *pixels, int width, int height, float st[ 2 ], float color[ 4 ] ){
|
|
|
|
float sto[ 2 ];
|
|
|
|
int x, y;
|
|
|
|
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* clear color first */
|
|
|
|
color[ 0 ] = color[ 1 ] = color[ 2 ] = color[ 3 ] = 255;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* dummy check */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( pixels == NULL || width < 1 || height < 1 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return qfalse;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* bias st */
|
|
|
|
sto[ 0 ] = st[ 0 ];
|
2012-03-17 20:01:54 +00:00
|
|
|
while ( sto[ 0 ] < 0.0f )
|
2007-11-04 03:34:51 +00:00
|
|
|
sto[ 0 ] += 1.0f;
|
|
|
|
sto[ 1 ] = st[ 1 ];
|
2012-03-17 20:01:54 +00:00
|
|
|
while ( sto[ 1 ] < 0.0f )
|
2007-11-04 03:34:51 +00:00
|
|
|
sto[ 1 ] += 1.0f;
|
|
|
|
|
|
|
|
/* get offsets */
|
2012-03-17 20:01:54 +00:00
|
|
|
x = ( (float) width * sto[ 0 ] ) + 0.5f;
|
2007-11-04 03:34:51 +00:00
|
|
|
x %= width;
|
2012-03-17 20:01:54 +00:00
|
|
|
y = ( (float) height * sto[ 1 ] ) + 0.5f;
|
2007-11-04 03:34:51 +00:00
|
|
|
y %= height;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* get pixel */
|
2012-03-17 20:01:54 +00:00
|
|
|
pixels += ( y * width * 4 ) + ( x * 4 );
|
2007-11-04 03:34:51 +00:00
|
|
|
VectorCopy( pixels, color );
|
|
|
|
color[ 3 ] = pixels[ 3 ];
|
|
|
|
return qtrue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
RadSample()
|
|
|
|
samples a fragment's lightmap or vertex color and returns an
|
|
|
|
average color and a color gradient for the sample
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define MAX_SAMPLES 150
|
|
|
|
#define SAMPLE_GRANULARITY 6
|
|
|
|
|
|
|
|
static void RadSample( int lightmapNum, bspDrawSurface_t *ds, rawLightmap_t *lm, shaderInfo_t *si, radWinding_t *rw, vec3_t average, vec3_t gradient, int *style ){
|
|
|
|
int i, j, k, l, v, x, y, samples;
|
|
|
|
vec3_t color, mins, maxs;
|
|
|
|
vec4_t textureColor;
|
|
|
|
float alpha, alphaI, bf;
|
|
|
|
vec3_t blend;
|
|
|
|
float st[ 2 ], lightmap[ 2 ], *radLuxel;
|
|
|
|
radVert_t *rv[ 3 ];
|
|
|
|
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* initial setup */
|
|
|
|
ClearBounds( mins, maxs );
|
|
|
|
VectorClear( average );
|
|
|
|
VectorClear( gradient );
|
|
|
|
alpha = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* dummy check */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( rw == NULL || rw->numVerts < 3 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* start sampling */
|
|
|
|
samples = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* sample vertex colors if no lightmap or this is the initial pass */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( lm == NULL || lm->radLuxels[ lightmapNum ] == NULL || bouncing == qfalse ) {
|
|
|
|
for ( samples = 0; samples < rw->numVerts; samples++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
/* multiply by texture color */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !RadSampleImage( si->lightImage->pixels, si->lightImage->width, si->lightImage->height, rw->verts[ samples ].st, textureColor ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
VectorCopy( si->averageColor, textureColor );
|
2013-11-01 00:24:58 +00:00
|
|
|
textureColor[ 3 ] = 255.0f;
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < 3; i++ )
|
|
|
|
color[ i ] = ( textureColor[ i ] / 255 ) * ( rw->verts[ samples ].color[ lightmapNum ][ i ] / 255.0f );
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
AddPointToBounds( color, mins, maxs );
|
|
|
|
VectorAdd( average, color, average );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* get alpha */
|
2012-03-17 20:01:54 +00:00
|
|
|
alpha += ( textureColor[ 3 ] / 255.0f ) * ( rw->verts[ samples ].color[ lightmapNum ][ 3 ] / 255.0f );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* set style */
|
|
|
|
*style = ds->vertexStyles[ lightmapNum ];
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* sample lightmap */
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* fracture the winding into a fan (including degenerate tris) */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( v = 1; v < ( rw->numVerts - 1 ) && samples < MAX_SAMPLES; v++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
/* get a triangle */
|
|
|
|
rv[ 0 ] = &rw->verts[ 0 ];
|
|
|
|
rv[ 1 ] = &rw->verts[ v ];
|
|
|
|
rv[ 2 ] = &rw->verts[ v + 1 ];
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* this code is embarassing (really should just rasterize the triangle) */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 1; i < SAMPLE_GRANULARITY && samples < MAX_SAMPLES; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( j = 1; j < SAMPLE_GRANULARITY && samples < MAX_SAMPLES; j++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( k = 1; k < SAMPLE_GRANULARITY && samples < MAX_SAMPLES; k++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
/* create a blend vector (barycentric coordinates) */
|
|
|
|
blend[ 0 ] = i;
|
|
|
|
blend[ 1 ] = j;
|
|
|
|
blend[ 2 ] = k;
|
2012-03-17 20:01:54 +00:00
|
|
|
bf = ( 1.0 / ( blend[ 0 ] + blend[ 1 ] + blend[ 2 ] ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
VectorScale( blend, bf, blend );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* create a blended sample */
|
|
|
|
st[ 0 ] = st[ 1 ] = 0.0f;
|
|
|
|
lightmap[ 0 ] = lightmap[ 1 ] = 0.0f;
|
|
|
|
alphaI = 0.0f;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( l = 0; l < 3; l++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
st[ 0 ] += ( rv[ l ]->st[ 0 ] * blend[ l ] );
|
|
|
|
st[ 1 ] += ( rv[ l ]->st[ 1 ] * blend[ l ] );
|
|
|
|
lightmap[ 0 ] += ( rv[ l ]->lightmap[ lightmapNum ][ 0 ] * blend[ l ] );
|
|
|
|
lightmap[ 1 ] += ( rv[ l ]->lightmap[ lightmapNum ][ 1 ] * blend[ l ] );
|
|
|
|
alphaI += ( rv[ l ]->color[ lightmapNum ][ 3 ] * blend[ l ] );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* get lightmap xy coords */
|
|
|
|
x = lightmap[ 0 ] / (float) superSample;
|
|
|
|
y = lightmap[ 1 ] / (float) superSample;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( x < 0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
x = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
else if ( x >= lm->w ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
x = lm->w - 1;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( y < 0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
y = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
else if ( y >= lm->h ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
y = lm->h - 1;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* get radiosity luxel */
|
|
|
|
radLuxel = RAD_LUXEL( lightmapNum, x, y );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* ignore unlit/unused luxels */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( radLuxel[ 0 ] < 0.0f ) {
|
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
|
|
|
/* inc samples */
|
|
|
|
samples++;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* multiply by texture color */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !RadSampleImage( si->lightImage->pixels, si->lightImage->width, si->lightImage->height, st, textureColor ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
VectorCopy( si->averageColor, textureColor );
|
2013-11-01 00:24:58 +00:00
|
|
|
textureColor[ 3 ] = 255;
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < 3; i++ )
|
|
|
|
color[ i ] = ( textureColor[ i ] / 255 ) * ( radLuxel[ i ] / 255 );
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
AddPointToBounds( color, mins, maxs );
|
|
|
|
VectorAdd( average, color, average );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* get alpha */
|
2012-03-17 20:01:54 +00:00
|
|
|
alpha += ( textureColor[ 3 ] / 255 ) * ( alphaI / 255 );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* set style */
|
|
|
|
*style = ds->lightmapStyles[ lightmapNum ];
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* any samples? */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( samples <= 0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* average the color */
|
2012-03-17 20:01:54 +00:00
|
|
|
VectorScale( average, ( 1.0 / samples ), average );
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* create the color gradient */
|
|
|
|
//% VectorSubtract( maxs, mins, delta );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* new: color gradient will always be 0-1.0, expressed as the range of light relative to overall light */
|
|
|
|
//% gradient[ 0 ] = maxs[ 0 ] > 0.0f ? (maxs[ 0 ] - mins[ 0 ]) / maxs[ 0 ] : 0.0f;
|
|
|
|
//% gradient[ 1 ] = maxs[ 1 ] > 0.0f ? (maxs[ 1 ] - mins[ 1 ]) / maxs[ 1 ] : 0.0f;
|
|
|
|
//% gradient[ 2 ] = maxs[ 2 ] > 0.0f ? (maxs[ 2 ] - mins[ 2 ]) / maxs[ 2 ] : 0.0f;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* newer: another contrast function */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < 3; i++ )
|
|
|
|
gradient[ i ] = ( maxs[ i ] - mins[ i ] ) * maxs[ i ];
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
RadSubdivideDiffuseLight()
|
|
|
|
subdivides a radiosity winding until it is smaller than subdivide, then generates an area light
|
|
|
|
*/
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
#define RADIOSITY_MAX_GRADIENT 0.75f //% 0.25f
|
|
|
|
#define RADIOSITY_VALUE 500.0f
|
|
|
|
#define RADIOSITY_MIN 0.0001f
|
|
|
|
#define RADIOSITY_CLIP_EPSILON 0.125f
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
static void RadSubdivideDiffuseLight( int lightmapNum, bspDrawSurface_t *ds, rawLightmap_t *lm, shaderInfo_t *si,
|
2012-03-17 20:01:54 +00:00
|
|
|
float scale, float subdivide, qboolean original, radWinding_t *rw, clipWork_t *cw ){
|
|
|
|
int i, style;
|
|
|
|
float dist, area, value;
|
|
|
|
vec3_t mins, maxs, normal, d1, d2, cross, color, gradient;
|
|
|
|
light_t *light, *splash;
|
|
|
|
winding_t *w;
|
|
|
|
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* dummy check */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( rw == NULL || rw->numVerts < 3 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* get bounds for winding */
|
|
|
|
ClearBounds( mins, maxs );
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < rw->numVerts; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
AddPointToBounds( rw->verts[ i ].xyz, mins, maxs );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* subdivide if necessary */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < 3; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( maxs[ i ] - mins[ i ] > subdivide ) {
|
|
|
|
radWinding_t front, back;
|
|
|
|
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* make axial plane */
|
|
|
|
VectorClear( normal );
|
|
|
|
normal[ i ] = 1;
|
2012-03-17 20:01:54 +00:00
|
|
|
dist = ( maxs[ i ] + mins[ i ] ) * 0.5f;
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* clip the winding */
|
|
|
|
RadClipWindingEpsilon( rw, normal, dist, RADIOSITY_CLIP_EPSILON, &front, &back, cw );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* recurse */
|
|
|
|
RadSubdivideDiffuseLight( lightmapNum, ds, lm, si, scale, subdivide, qfalse, &front, cw );
|
|
|
|
RadSubdivideDiffuseLight( lightmapNum, ds, lm, si, scale, subdivide, qfalse, &back, cw );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* check area */
|
|
|
|
area = 0.0f;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 2; i < rw->numVerts; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
VectorSubtract( rw->verts[ i - 1 ].xyz, rw->verts[ 0 ].xyz, d1 );
|
|
|
|
VectorSubtract( rw->verts[ i ].xyz, rw->verts[ 0 ].xyz, d2 );
|
|
|
|
CrossProduct( d1, d2, cross );
|
|
|
|
area += 0.5f * VectorLength( cross );
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( area < 1.0f || area > 20000000.0f ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* more subdivision may be necessary */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( bouncing ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
/* get color sample for the surface fragment */
|
|
|
|
RadSample( lightmapNum, ds, lm, si, rw, color, gradient, &style );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* if color gradient is too high, subdivide again */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( subdivide > minDiffuseSubdivide &&
|
|
|
|
( gradient[ 0 ] > RADIOSITY_MAX_GRADIENT || gradient[ 1 ] > RADIOSITY_MAX_GRADIENT || gradient[ 2 ] > RADIOSITY_MAX_GRADIENT ) ) {
|
|
|
|
RadSubdivideDiffuseLight( lightmapNum, ds, lm, si, scale, ( subdivide / 2.0f ), qfalse, rw, cw );
|
2007-11-04 03:34:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* create a regular winding and an average normal */
|
|
|
|
w = AllocWinding( rw->numVerts );
|
|
|
|
w->numpoints = rw->numVerts;
|
|
|
|
VectorClear( normal );
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < rw->numVerts; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
VectorCopy( rw->verts[ i ].xyz, w->p[ i ] );
|
|
|
|
VectorAdd( normal, rw->verts[ i ].normal, normal );
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
VectorScale( normal, ( 1.0f / rw->numVerts ), normal );
|
|
|
|
if ( VectorNormalize( normal, normal ) == 0.0f ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* early out? */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( bouncing && VectorLength( color ) < RADIOSITY_MIN ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* debug code */
|
|
|
|
//% Sys_Printf( "Size: %d %d %d\n", (int) (maxs[ 0 ] - mins[ 0 ]), (int) (maxs[ 1 ] - mins[ 1 ]), (int) (maxs[ 2 ] - mins[ 2 ]) );
|
|
|
|
//% Sys_Printf( "Grad: %f %f %f\n", gradient[ 0 ], gradient[ 1 ], gradient[ 2 ] );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* increment counts */
|
|
|
|
numDiffuseLights++;
|
2012-03-17 20:01:54 +00:00
|
|
|
switch ( ds->surfaceType )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
case MST_PLANAR:
|
|
|
|
numBrushDiffuseLights++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MST_TRIANGLE_SOUP:
|
|
|
|
numTriangleDiffuseLights++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MST_PATCH:
|
|
|
|
numPatchDiffuseLights++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* create a light */
|
|
|
|
light = safe_malloc( sizeof( *light ) );
|
|
|
|
memset( light, 0, sizeof( *light ) );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* attach it */
|
|
|
|
ThreadLock();
|
|
|
|
light->next = lights;
|
|
|
|
lights = light;
|
|
|
|
ThreadUnlock();
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* initialize the light */
|
|
|
|
light->flags = LIGHT_AREA_DEFAULT;
|
|
|
|
light->type = EMIT_AREA;
|
|
|
|
light->si = si;
|
|
|
|
light->fade = 1.0f;
|
|
|
|
light->w = w;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* set falloff threshold */
|
|
|
|
light->falloffTolerance = falloffTolerance;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* bouncing light? */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( bouncing == qfalse ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
/* handle first-pass lights in normal q3a style */
|
|
|
|
value = si->value;
|
|
|
|
light->photons = value * area * areaScale;
|
|
|
|
light->add = value * formFactorValueScale * areaScale;
|
|
|
|
VectorCopy( si->color, light->color );
|
|
|
|
VectorScale( light->color, light->add, light->emitColor );
|
2008-07-25 07:31:37 +00:00
|
|
|
light->style = noStyles ? LS_NORMAL : si->lightStyle;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( light->style < LS_NORMAL || light->style >= LS_NONE ) {
|
2008-07-25 07:31:37 +00:00
|
|
|
light->style = LS_NORMAL;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* set origin */
|
|
|
|
VectorAdd( mins, maxs, light->origin );
|
|
|
|
VectorScale( light->origin, 0.5f, light->origin );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* nudge it off the plane a bit */
|
|
|
|
VectorCopy( normal, light->normal );
|
|
|
|
VectorMA( light->origin, 1.0f, light->normal, light->origin );
|
|
|
|
light->dist = DotProduct( light->origin, normal );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* optionally create a point splashsplash light for first pass */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( original && si->backsplashFraction > 0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
/* allocate a new point light */
|
|
|
|
splash = safe_malloc( sizeof( *splash ) );
|
|
|
|
memset( splash, 0, sizeof( *splash ) );
|
|
|
|
splash->next = lights;
|
|
|
|
lights = splash;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* set it up */
|
|
|
|
splash->flags = LIGHT_Q3A_DEFAULT;
|
|
|
|
splash->type = EMIT_POINT;
|
|
|
|
splash->photons = light->photons * si->backsplashFraction;
|
|
|
|
splash->fade = 1.0f;
|
|
|
|
splash->si = si;
|
|
|
|
VectorMA( light->origin, si->backsplashDistance, normal, splash->origin );
|
|
|
|
VectorCopy( si->color, splash->color );
|
|
|
|
splash->falloffTolerance = falloffTolerance;
|
2008-07-25 07:31:37 +00:00
|
|
|
splash->style = noStyles ? LS_NORMAL : light->style;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* add to counts */
|
|
|
|
numPointLights++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* handle bounced light (radiosity) a little differently */
|
|
|
|
value = RADIOSITY_VALUE * si->bounceScale * 0.375f;
|
|
|
|
light->photons = value * area * bounceScale;
|
|
|
|
light->add = value * formFactorValueScale * bounceScale;
|
|
|
|
VectorCopy( color, light->color );
|
|
|
|
VectorScale( light->color, light->add, light->emitColor );
|
2008-07-25 07:31:37 +00:00
|
|
|
light->style = noStyles ? LS_NORMAL : style;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( light->style < LS_NORMAL || light->style >= LS_NONE ) {
|
2008-07-25 07:31:37 +00:00
|
|
|
light->style = LS_NORMAL;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* set origin */
|
|
|
|
WindingCenter( w, light->origin );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* nudge it off the plane a bit */
|
|
|
|
VectorCopy( normal, light->normal );
|
|
|
|
VectorMA( light->origin, 1.0f, light->normal, light->origin );
|
|
|
|
light->dist = DotProduct( light->origin, normal );
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* emit light from both sides? */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( si->compileFlags & C_FOG || si->twoSided ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
light->flags |= LIGHT_TWOSIDED;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
//% Sys_Printf( "\nAL: C: (%6f, %6f, %6f) [%6f] N: (%6f, %6f, %6f) %s\n",
|
|
|
|
//% light->color[ 0 ], light->color[ 1 ], light->color[ 2 ], light->add,
|
|
|
|
//% light->normal[ 0 ], light->normal[ 1 ], light->normal[ 2 ],
|
|
|
|
//% light->si->shader );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
RadLightForTriangles()
|
|
|
|
creates unbounced diffuse lights for triangle soup (misc_models, etc)
|
|
|
|
*/
|
|
|
|
|
|
|
|
void RadLightForTriangles( int num, int lightmapNum, rawLightmap_t *lm, shaderInfo_t *si, float scale, float subdivide, clipWork_t *cw ){
|
|
|
|
int i, j, k, v;
|
|
|
|
bspDrawSurface_t *ds;
|
|
|
|
surfaceInfo_t *info;
|
|
|
|
float *radVertexLuxel;
|
|
|
|
radWinding_t rw;
|
|
|
|
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* get surface */
|
|
|
|
ds = &bspDrawSurfaces[ num ];
|
|
|
|
info = &surfaceInfos[ num ];
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* each triangle is a potential emitter */
|
|
|
|
rw.numVerts = 3;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < ds->numIndexes; i += 3 )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
/* copy each vert */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( j = 0; j < 3; j++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
/* get vertex index and rad vertex luxel */
|
|
|
|
v = ds->firstVert + bspDrawIndexes[ ds->firstIndex + i + j ];
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* get most everything */
|
|
|
|
memcpy( &rw.verts[ j ], &yDrawVerts[ v ], sizeof( bspDrawVert_t ) );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* fix colors */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( k = 0; k < MAX_LIGHTMAPS; k++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
radVertexLuxel = RAD_VERTEX_LUXEL( k, ds->firstVert + bspDrawIndexes[ ds->firstIndex + i + j ] );
|
|
|
|
VectorCopy( radVertexLuxel, rw.verts[ j ].color[ k ] );
|
|
|
|
rw.verts[ j ].color[ k ][ 3 ] = yDrawVerts[ v ].color[ k ][ 3 ];
|
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* subdivide into area lights */
|
|
|
|
RadSubdivideDiffuseLight( lightmapNum, ds, lm, si, scale, subdivide, qtrue, &rw, cw );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
RadLightForPatch()
|
|
|
|
creates unbounced diffuse lights for patches
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define PLANAR_EPSILON 0.1f
|
|
|
|
|
|
|
|
void RadLightForPatch( int num, int lightmapNum, rawLightmap_t *lm, shaderInfo_t *si, float scale, float subdivide, clipWork_t *cw ){
|
|
|
|
int i, x, y, v, t, pw[ 5 ], r;
|
|
|
|
bspDrawSurface_t *ds;
|
|
|
|
surfaceInfo_t *info;
|
|
|
|
bspDrawVert_t *bogus;
|
|
|
|
bspDrawVert_t *dv[ 4 ];
|
|
|
|
mesh_t src, *subdivided, *mesh;
|
|
|
|
float *radVertexLuxel;
|
|
|
|
float dist;
|
|
|
|
vec4_t plane;
|
|
|
|
qboolean planar;
|
|
|
|
radWinding_t rw;
|
|
|
|
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* get surface */
|
|
|
|
ds = &bspDrawSurfaces[ num ];
|
|
|
|
info = &surfaceInfos[ num ];
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* construct a bogus vert list with color index stuffed into color[ 0 ] */
|
|
|
|
bogus = safe_malloc( ds->numVerts * sizeof( bspDrawVert_t ) );
|
|
|
|
memcpy( bogus, &yDrawVerts[ ds->firstVert ], ds->numVerts * sizeof( bspDrawVert_t ) );
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < ds->numVerts; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
bogus[ i ].color[ 0 ][ 0 ] = i;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* build a subdivided mesh identical to shadow facets for this patch */
|
|
|
|
/* this MUST MATCH FacetsForPatch() identically! */
|
|
|
|
src.width = ds->patchWidth;
|
|
|
|
src.height = ds->patchHeight;
|
|
|
|
src.verts = bogus;
|
|
|
|
//% subdivided = SubdivideMesh( src, 8, 512 );
|
|
|
|
subdivided = SubdivideMesh2( src, info->patchIterations );
|
|
|
|
PutMeshOnCurve( *subdivided );
|
|
|
|
//% MakeMeshNormals( *subdivided );
|
|
|
|
mesh = RemoveLinearMeshColumnsRows( subdivided );
|
|
|
|
FreeMesh( subdivided );
|
|
|
|
free( bogus );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* FIXME: build interpolation table into color[ 1 ] */
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* fix up color indexes */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < ( mesh->width * mesh->height ); i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
dv[ 0 ] = &mesh->verts[ i ];
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( dv[ 0 ]->color[ 0 ][ 0 ] >= ds->numVerts ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
dv[ 0 ]->color[ 0 ][ 0 ] = ds->numVerts - 1;
|
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
|
|
|
/* iterate through the mesh quads */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( y = 0; y < ( mesh->height - 1 ); y++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( x = 0; x < ( mesh->width - 1 ); x++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
/* set indexes */
|
2012-03-17 20:01:54 +00:00
|
|
|
pw[ 0 ] = x + ( y * mesh->width );
|
|
|
|
pw[ 1 ] = x + ( ( y + 1 ) * mesh->width );
|
|
|
|
pw[ 2 ] = x + 1 + ( ( y + 1 ) * mesh->width );
|
|
|
|
pw[ 3 ] = x + 1 + ( y * mesh->width );
|
|
|
|
pw[ 4 ] = x + ( y * mesh->width ); /* same as pw[ 0 ] */
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* set radix */
|
2012-03-17 20:01:54 +00:00
|
|
|
r = ( x + y ) & 1;
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* get drawverts */
|
|
|
|
dv[ 0 ] = &mesh->verts[ pw[ r + 0 ] ];
|
|
|
|
dv[ 1 ] = &mesh->verts[ pw[ r + 1 ] ];
|
|
|
|
dv[ 2 ] = &mesh->verts[ pw[ r + 2 ] ];
|
|
|
|
dv[ 3 ] = &mesh->verts[ pw[ r + 3 ] ];
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* planar? */
|
|
|
|
planar = PlaneFromPoints( plane, dv[ 0 ]->xyz, dv[ 1 ]->xyz, dv[ 2 ]->xyz );
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( planar ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
dist = DotProduct( dv[ 1 ]->xyz, plane ) - plane[ 3 ];
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( fabs( dist ) > PLANAR_EPSILON ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
planar = qfalse;
|
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
|
|
|
/* generate a quad */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( planar ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
rw.numVerts = 4;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( v = 0; v < 4; v++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
/* get most everything */
|
|
|
|
memcpy( &rw.verts[ v ], dv[ v ], sizeof( bspDrawVert_t ) );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* fix colors */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < MAX_LIGHTMAPS; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
radVertexLuxel = RAD_VERTEX_LUXEL( i, ds->firstVert + dv[ v ]->color[ 0 ][ 0 ] );
|
|
|
|
VectorCopy( radVertexLuxel, rw.verts[ v ].color[ i ] );
|
|
|
|
rw.verts[ v ].color[ i ][ 3 ] = dv[ v ]->color[ i ][ 3 ];
|
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* subdivide into area lights */
|
|
|
|
RadSubdivideDiffuseLight( lightmapNum, ds, lm, si, scale, subdivide, qtrue, &rw, cw );
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* generate 2 tris */
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rw.numVerts = 3;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( t = 0; t < 2; t++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( v = 0; v < 3 + t; v++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
/* get "other" triangle (stupid hacky logic, but whatevah) */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( v == 1 && t == 1 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
v++;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* get most everything */
|
|
|
|
memcpy( &rw.verts[ v ], dv[ v ], sizeof( bspDrawVert_t ) );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* fix colors */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < MAX_LIGHTMAPS; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
radVertexLuxel = RAD_VERTEX_LUXEL( i, ds->firstVert + dv[ v ]->color[ 0 ][ 0 ] );
|
|
|
|
VectorCopy( radVertexLuxel, rw.verts[ v ].color[ i ] );
|
|
|
|
rw.verts[ v ].color[ i ][ 3 ] = dv[ v ]->color[ i ][ 3 ];
|
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* subdivide into area lights */
|
|
|
|
RadSubdivideDiffuseLight( lightmapNum, ds, lm, si, scale, subdivide, qtrue, &rw, cw );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* free the mesh */
|
|
|
|
FreeMesh( mesh );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
RadLight()
|
|
|
|
creates unbounced diffuse lights for a given surface
|
|
|
|
*/
|
|
|
|
|
|
|
|
void RadLight( int num ){
|
|
|
|
int lightmapNum;
|
|
|
|
float scale, subdivide;
|
|
|
|
int contentFlags, surfaceFlags, compileFlags;
|
|
|
|
bspDrawSurface_t *ds;
|
|
|
|
surfaceInfo_t *info;
|
|
|
|
rawLightmap_t *lm;
|
|
|
|
shaderInfo_t *si;
|
|
|
|
clipWork_t cw;
|
|
|
|
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* get drawsurface, lightmap, and shader info */
|
|
|
|
ds = &bspDrawSurfaces[ num ];
|
|
|
|
info = &surfaceInfos[ num ];
|
|
|
|
lm = info->lm;
|
|
|
|
si = info->si;
|
|
|
|
scale = si->bounceScale;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* find nodraw bit */
|
|
|
|
contentFlags = surfaceFlags = compileFlags = 0;
|
|
|
|
ApplySurfaceParm( "nodraw", &contentFlags, &surfaceFlags, &compileFlags );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* early outs? */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( scale <= 0.0f || ( si->compileFlags & C_SKY ) || si->autosprite ||
|
|
|
|
( bspShaders[ ds->shaderNum ].contentFlags & contentFlags ) || ( bspShaders[ ds->shaderNum ].surfaceFlags & surfaceFlags ) ||
|
|
|
|
( si->compileFlags & compileFlags ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* determine how much we need to chop up the surface */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( si->lightSubdivide ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
subdivide = si->lightSubdivide;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
else{
|
2007-11-04 03:34:51 +00:00
|
|
|
subdivide = diffuseSubdivide;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* inc counts */
|
|
|
|
numDiffuseSurfaces++;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* iterate through styles (this could be more efficient, yes) */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
/* switch on type */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ds->lightmapStyles[ lightmapNum ] != LS_NONE && ds->lightmapStyles[ lightmapNum ] != LS_UNUSED ) {
|
|
|
|
switch ( ds->surfaceType )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
case MST_PLANAR:
|
|
|
|
case MST_TRIANGLE_SOUP:
|
|
|
|
RadLightForTriangles( num, lightmapNum, lm, si, scale, subdivide, &cw );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MST_PATCH:
|
|
|
|
RadLightForPatch( num, lightmapNum, lm, si, scale, subdivide, &cw );
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
RadCreateDiffuseLights()
|
|
|
|
creates lights for unbounced light on surfaces in the bsp
|
|
|
|
*/
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int iterations = 0;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void RadCreateDiffuseLights( void ){
|
2007-11-04 03:34:51 +00:00
|
|
|
/* startup */
|
|
|
|
Sys_FPrintf( SYS_VRB, "--- RadCreateDiffuseLights ---\n" );
|
|
|
|
numDiffuseSurfaces = 0;
|
|
|
|
numDiffuseLights = 0;
|
|
|
|
numBrushDiffuseLights = 0;
|
|
|
|
numTriangleDiffuseLights = 0;
|
|
|
|
numPatchDiffuseLights = 0;
|
|
|
|
numAreaLights = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* hit every surface (threaded) */
|
|
|
|
RunThreadsOnIndividual( numBSPDrawSurfaces, qtrue, RadLight );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* dump the lights generated to a file */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( dump ) {
|
|
|
|
char dumpName[ 1024 ], ext[ 64 ];
|
|
|
|
FILE *file;
|
|
|
|
light_t *light;
|
|
|
|
|
2015-09-08 01:09:48 +00:00
|
|
|
Q_strncpyz( dumpName, source, sizeof( dumpName ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
StripExtension( dumpName );
|
2015-09-08 01:09:48 +00:00
|
|
|
snprintf( ext, sizeof( ext ), "_bounce_%03d.map", iterations );
|
|
|
|
strncat( dumpName, ext, sizeof( dumpName ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
file = fopen( dumpName, "wb" );
|
|
|
|
Sys_Printf( "Writing %s...\n", dumpName );
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( file ) {
|
|
|
|
for ( light = lights; light; light = light->next )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
fprintf( file,
|
2012-03-17 20:01:54 +00:00
|
|
|
"{\n"
|
|
|
|
"\"classname\" \"light\"\n"
|
|
|
|
"\"light\" \"%d\"\n"
|
|
|
|
"\"origin\" \"%.0f %.0f %.0f\"\n"
|
|
|
|
"\"_color\" \"%.3f %.3f %.3f\"\n"
|
|
|
|
"}\n",
|
|
|
|
|
|
|
|
(int) light->add,
|
|
|
|
|
|
|
|
light->origin[ 0 ],
|
|
|
|
light->origin[ 1 ],
|
|
|
|
light->origin[ 2 ],
|
|
|
|
|
|
|
|
light->color[ 0 ],
|
|
|
|
light->color[ 1 ],
|
|
|
|
light->color[ 2 ] );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
fclose( file );
|
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* increment */
|
|
|
|
iterations++;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* print counts */
|
|
|
|
Sys_Printf( "%8d diffuse surfaces\n", numDiffuseSurfaces );
|
|
|
|
Sys_FPrintf( SYS_VRB, "%8d total diffuse lights\n", numDiffuseLights );
|
|
|
|
Sys_FPrintf( SYS_VRB, "%8d brush diffuse lights\n", numBrushDiffuseLights );
|
|
|
|
Sys_FPrintf( SYS_VRB, "%8d patch diffuse lights\n", numPatchDiffuseLights );
|
|
|
|
Sys_FPrintf( SYS_VRB, "%8d triangle diffuse lights\n", numTriangleDiffuseLights );
|
|
|
|
}
|