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 "qrad.h"
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
#define MAX_LSTYLES 256
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
dface_t *faces[2];
|
|
|
|
qboolean coplanar;
|
2007-11-04 03:34:51 +00:00
|
|
|
} edgeshare_t;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
edgeshare_t edgeshare[MAX_MAP_EDGES];
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int facelinks[MAX_MAP_FACES];
|
|
|
|
int planelinks[2][MAX_MAP_PLANES];
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
============
|
|
|
|
LinkPlaneFaces
|
|
|
|
============
|
|
|
|
*/
|
|
|
|
void LinkPlaneFaces( void ){
|
|
|
|
int i;
|
|
|
|
dface_t *f;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
f = dfaces;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0 ; i < numfaces ; i++, f++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
facelinks[i] = planelinks[f->side][f->planenum];
|
|
|
|
planelinks[f->side][f->planenum] = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
============
|
|
|
|
PairEdges
|
|
|
|
============
|
|
|
|
*/
|
|
|
|
void PairEdges( void ){
|
|
|
|
int i, j, k;
|
|
|
|
dface_t *f;
|
|
|
|
edgeshare_t *e;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
f = dfaces;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0 ; i < numfaces ; i++, f++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( j = 0 ; j < f->numedges ; j++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
k = dsurfedges[f->firstedge + j];
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( k < 0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
e = &edgeshare[-k];
|
|
|
|
e->faces[1] = f;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
e = &edgeshare[k];
|
|
|
|
e->faces[0] = f;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( e->faces[0] && e->faces[1] ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
// determine if coplanar
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( e->faces[0]->planenum == e->faces[1]->planenum ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
e->coplanar = true;
|
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
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
POINT TRIANGULATION
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
=================================================================
|
|
|
|
*/
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
typedef struct triedge_s
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
int p0, p1;
|
|
|
|
vec3_t normal;
|
|
|
|
vec_t dist;
|
|
|
|
struct triangle_s *tri;
|
2007-11-04 03:34:51 +00:00
|
|
|
} triedge_t;
|
|
|
|
|
|
|
|
typedef struct triangle_s
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
triedge_t *edges[3];
|
2007-11-04 03:34:51 +00:00
|
|
|
} triangle_t;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
#define MAX_TRI_POINTS 1024
|
|
|
|
#define MAX_TRI_EDGES ( MAX_TRI_POINTS * 6 )
|
|
|
|
#define MAX_TRI_TRIS ( MAX_TRI_POINTS * 2 )
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
int numpoints;
|
|
|
|
int numedges;
|
|
|
|
int numtris;
|
|
|
|
dplane_t *plane;
|
|
|
|
triedge_t *edgematrix[MAX_TRI_POINTS][MAX_TRI_POINTS];
|
|
|
|
patch_t *points[MAX_TRI_POINTS];
|
|
|
|
triedge_t edges[MAX_TRI_EDGES];
|
|
|
|
triangle_t tris[MAX_TRI_TRIS];
|
2007-11-04 03:34:51 +00:00
|
|
|
} triangulation_t;
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
===============
|
|
|
|
AllocTriangulation
|
|
|
|
===============
|
|
|
|
*/
|
|
|
|
triangulation_t *AllocTriangulation( dplane_t *plane ){
|
|
|
|
triangulation_t *t;
|
|
|
|
|
|
|
|
t = malloc( sizeof( triangulation_t ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
t->numpoints = 0;
|
|
|
|
t->numedges = 0;
|
|
|
|
t->numtris = 0;
|
|
|
|
|
|
|
|
t->plane = plane;
|
|
|
|
|
|
|
|
// memset (t->edgematrix, 0, sizeof(t->edgematrix));
|
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
===============
|
|
|
|
FreeTriangulation
|
|
|
|
===============
|
|
|
|
*/
|
|
|
|
void FreeTriangulation( triangulation_t *tr ){
|
|
|
|
free( tr );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
triedge_t *FindEdge( triangulation_t *trian, int p0, int p1 ){
|
|
|
|
triedge_t *e, *be;
|
|
|
|
vec3_t v1;
|
|
|
|
vec3_t normal;
|
|
|
|
vec_t dist;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( trian->edgematrix[p0][p1] ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return trian->edgematrix[p0][p1];
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( trian->numedges > MAX_TRI_EDGES - 2 ) {
|
|
|
|
Error( "trian->numedges > MAX_TRI_EDGES-2" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
VectorSubtract( trian->points[p1]->origin, trian->points[p0]->origin, v1 );
|
|
|
|
VectorNormalize( v1, v1 );
|
|
|
|
CrossProduct( v1, trian->plane->normal, normal );
|
|
|
|
dist = DotProduct( trian->points[p0]->origin, normal );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
e = &trian->edges[trian->numedges];
|
|
|
|
e->p0 = p0;
|
|
|
|
e->p1 = p1;
|
|
|
|
e->tri = NULL;
|
2012-03-17 20:01:54 +00:00
|
|
|
VectorCopy( normal, e->normal );
|
2007-11-04 03:34:51 +00:00
|
|
|
e->dist = dist;
|
|
|
|
trian->numedges++;
|
|
|
|
trian->edgematrix[p0][p1] = e;
|
|
|
|
|
|
|
|
be = &trian->edges[trian->numedges];
|
|
|
|
be->p0 = p1;
|
|
|
|
be->p1 = p0;
|
|
|
|
be->tri = NULL;
|
2012-03-17 20:01:54 +00:00
|
|
|
VectorSubtract( vec3_origin, normal, be->normal );
|
2007-11-04 03:34:51 +00:00
|
|
|
be->dist = -dist;
|
|
|
|
trian->numedges++;
|
|
|
|
trian->edgematrix[p1][p0] = be;
|
|
|
|
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
triangle_t *AllocTriangle( triangulation_t *trian ){
|
|
|
|
triangle_t *t;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( trian->numtris >= MAX_TRI_TRIS ) {
|
|
|
|
Error( "trian->numtris >= MAX_TRI_TRIS" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
t = &trian->tris[trian->numtris];
|
|
|
|
trian->numtris++;
|
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
============
|
|
|
|
TriEdge_r
|
|
|
|
============
|
|
|
|
*/
|
|
|
|
void TriEdge_r( triangulation_t *trian, triedge_t *e ){
|
|
|
|
int i, bestp;
|
|
|
|
vec3_t v1, v2;
|
|
|
|
vec_t *p0, *p1, *p;
|
|
|
|
vec_t best, ang;
|
|
|
|
triangle_t *nt;
|
|
|
|
|
|
|
|
if ( e->tri ) {
|
|
|
|
return; // allready connected by someone
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
// find the point with the best angle
|
|
|
|
p0 = trian->points[e->p0]->origin;
|
|
|
|
p1 = trian->points[e->p1]->origin;
|
|
|
|
best = 1.1;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0 ; i < trian->numpoints ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
p = trian->points[i]->origin;
|
|
|
|
// a 0 dist will form a degenerate triangle
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( DotProduct( p, e->normal ) - e->dist < 0 ) {
|
|
|
|
continue; // behind edge
|
|
|
|
}
|
|
|
|
VectorSubtract( p0, p, v1 );
|
|
|
|
VectorSubtract( p1, p, v2 );
|
|
|
|
if ( !VectorNormalize( v1,v1 ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( !VectorNormalize( v2,v2 ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
ang = DotProduct( v1, v2 );
|
|
|
|
if ( ang < best ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
best = ang;
|
|
|
|
bestp = i;
|
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( best >= 1 ) {
|
|
|
|
return; // edge doesn't match anything
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
// make a new triangle
|
2012-03-17 20:01:54 +00:00
|
|
|
nt = AllocTriangle( trian );
|
2007-11-04 03:34:51 +00:00
|
|
|
nt->edges[0] = e;
|
2012-03-17 20:01:54 +00:00
|
|
|
nt->edges[1] = FindEdge( trian, e->p1, bestp );
|
|
|
|
nt->edges[2] = FindEdge( trian, bestp, e->p0 );
|
|
|
|
for ( i = 0 ; i < 3 ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
nt->edges[i]->tri = nt;
|
2012-03-17 20:01:54 +00:00
|
|
|
TriEdge_r( trian, FindEdge( trian, bestp, e->p1 ) );
|
|
|
|
TriEdge_r( trian, FindEdge( trian, e->p0, bestp ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
============
|
|
|
|
TriangulatePoints
|
|
|
|
============
|
|
|
|
*/
|
|
|
|
void TriangulatePoints( triangulation_t *trian ){
|
|
|
|
vec_t d, bestd;
|
|
|
|
vec3_t v1;
|
|
|
|
int bp1, bp2, i, j;
|
|
|
|
vec_t *p1, *p2;
|
|
|
|
triedge_t *e, *e2;
|
|
|
|
|
|
|
|
if ( trian->numpoints < 2 ) {
|
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
|
|
|
|
|
|
|
// find the two closest points
|
|
|
|
bestd = 9999;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0 ; i < trian->numpoints ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
p1 = trian->points[i]->origin;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( j = i + 1 ; j < trian->numpoints ; j++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
p2 = trian->points[j]->origin;
|
2012-03-17 20:01:54 +00:00
|
|
|
VectorSubtract( p2, p1, v1 );
|
|
|
|
d = VectorLength( v1 );
|
|
|
|
if ( d < bestd ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
bestd = d;
|
|
|
|
bp1 = i;
|
|
|
|
bp2 = j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
e = FindEdge( trian, bp1, bp2 );
|
|
|
|
e2 = FindEdge( trian, bp2, bp1 );
|
|
|
|
TriEdge_r( trian, e );
|
|
|
|
TriEdge_r( trian, e2 );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
===============
|
|
|
|
AddPointToTriangulation
|
|
|
|
===============
|
|
|
|
*/
|
|
|
|
void AddPointToTriangulation( patch_t *patch, triangulation_t *trian ){
|
|
|
|
int pnum;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
pnum = trian->numpoints;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( pnum == MAX_TRI_POINTS ) {
|
|
|
|
Error( "trian->numpoints == MAX_TRI_POINTS" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
trian->points[pnum] = patch;
|
|
|
|
trian->numpoints++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
===============
|
|
|
|
LerpTriangle
|
|
|
|
===============
|
|
|
|
*/
|
|
|
|
void LerpTriangle( triangulation_t *trian, triangle_t *t, vec3_t point, vec3_t color ){
|
|
|
|
patch_t *p1, *p2, *p3;
|
|
|
|
vec3_t base, d1, d2;
|
|
|
|
float x, y, x1, y1, x2, y2;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
p1 = trian->points[t->edges[0]->p0];
|
|
|
|
p2 = trian->points[t->edges[1]->p0];
|
|
|
|
p3 = trian->points[t->edges[2]->p0];
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
VectorCopy( p1->totallight, base );
|
|
|
|
VectorSubtract( p2->totallight, base, d1 );
|
|
|
|
VectorSubtract( p3->totallight, base, d2 );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
x = DotProduct( point, t->edges[0]->normal ) - t->edges[0]->dist;
|
|
|
|
y = DotProduct( point, t->edges[2]->normal ) - t->edges[2]->dist;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
x1 = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
y1 = DotProduct( p2->origin, t->edges[2]->normal ) - t->edges[2]->dist;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
x2 = DotProduct( p3->origin, t->edges[0]->normal ) - t->edges[0]->dist;
|
2007-11-04 03:34:51 +00:00
|
|
|
y2 = 0;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( fabs( y1 ) < ON_EPSILON || fabs( x2 ) < ON_EPSILON ) {
|
|
|
|
VectorCopy( base, color );
|
2007-11-04 03:34:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
VectorMA( base, x / x2, d2, color );
|
|
|
|
VectorMA( color, y / y1, d1, color );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
qboolean PointInTriangle( vec3_t point, triangle_t *t ){
|
|
|
|
int i;
|
|
|
|
triedge_t *e;
|
|
|
|
vec_t d;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0 ; i < 3 ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
e = t->edges[i];
|
2012-03-17 20:01:54 +00:00
|
|
|
d = DotProduct( e->normal, point ) - e->dist;
|
|
|
|
if ( d < 0 ) {
|
|
|
|
return false; // not inside
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
===============
|
|
|
|
SampleTriangulation
|
|
|
|
===============
|
|
|
|
*/
|
|
|
|
void SampleTriangulation( vec3_t point, triangulation_t *trian, vec3_t color ){
|
|
|
|
triangle_t *t;
|
|
|
|
triedge_t *e;
|
|
|
|
vec_t d, best;
|
|
|
|
patch_t *p0, *p1;
|
|
|
|
vec3_t v1, v2;
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
if ( trian->numpoints == 0 ) {
|
|
|
|
VectorClear( color );
|
2007-11-04 03:34:51 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( trian->numpoints == 1 ) {
|
|
|
|
VectorCopy( trian->points[0]->totallight, color );
|
2007-11-04 03:34:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// search for triangles
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( t = trian->tris, j = 0 ; j < trian->numtris ; t++, j++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !PointInTriangle( point, t ) ) {
|
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
|
|
|
|
|
|
|
// this is it
|
2012-03-17 20:01:54 +00:00
|
|
|
LerpTriangle( trian, t, point, color );
|
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
|
|
|
// search for exterior edge
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( e = trian->edges, j = 0 ; j < trian->numedges ; e++, j++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( e->tri ) {
|
|
|
|
continue; // not an exterior edge
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
d = DotProduct( point, e->normal ) - e->dist;
|
|
|
|
if ( d < 0 ) {
|
|
|
|
continue; // not in front of edge
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
p0 = trian->points[e->p0];
|
|
|
|
p1 = trian->points[e->p1];
|
2012-03-17 20:01:54 +00:00
|
|
|
|
|
|
|
VectorSubtract( p1->origin, p0->origin, v1 );
|
|
|
|
VectorNormalize( v1, v1 );
|
|
|
|
VectorSubtract( point, p0->origin, v2 );
|
|
|
|
d = DotProduct( v2, v1 );
|
|
|
|
if ( d < 0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( d > 1 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
for ( i = 0 ; i < 3 ; i++ )
|
|
|
|
color[i] = p0->totallight[i] + d * ( p1->totallight[i] - p0->totallight[i] );
|
2007-11-04 03:34:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// search for nearest point
|
|
|
|
best = 99999;
|
|
|
|
p1 = NULL;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( j = 0 ; j < trian->numpoints ; j++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
p0 = trian->points[j];
|
2012-03-17 20:01:54 +00:00
|
|
|
VectorSubtract( point, p0->origin, v1 );
|
|
|
|
d = VectorLength( v1 );
|
|
|
|
if ( d < best ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
best = d;
|
|
|
|
p1 = p0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !p1 ) {
|
|
|
|
Error( "SampleTriangulation: no points" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
VectorCopy( p1->totallight, color );
|
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
|
|
|
LIGHTMAP SAMPLE GENERATION
|
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
|
|
|
#define SINGLEMAP ( 64 * 64 * 4 )
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
vec_t facedist;
|
|
|
|
vec3_t facenormal;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int numsurfpt;
|
|
|
|
vec3_t surfpt[SINGLEMAP];
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
vec3_t modelorg; // for origined bmodels
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
vec3_t texorg;
|
|
|
|
vec3_t worldtotex[2]; // s = (world - texorg) . worldtotex[0]
|
|
|
|
vec3_t textoworld[2]; // world = texorg + s * textoworld[0]
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
vec_t exactmins[2], exactmaxs[2];
|
|
|
|
|
|
|
|
int texmins[2], texsize[2];
|
|
|
|
int surfnum;
|
|
|
|
dface_t *face;
|
2007-11-04 03:34:51 +00:00
|
|
|
} lightinfo_t;
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
================
|
|
|
|
CalcFaceExtents
|
|
|
|
|
|
|
|
Fills in s->texmins[] and s->texsize[]
|
|
|
|
also sets exactmins[] and exactmaxs[]
|
|
|
|
================
|
|
|
|
*/
|
|
|
|
void CalcFaceExtents( lightinfo_t *l ){
|
2007-11-04 03:34:51 +00:00
|
|
|
dface_t *s;
|
2012-03-17 20:01:54 +00:00
|
|
|
vec_t mins[2], maxs[2], val;
|
|
|
|
int i,j, e;
|
|
|
|
dvertex_t *v;
|
|
|
|
texinfo_t *tex;
|
|
|
|
vec3_t vt;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
s = l->face;
|
|
|
|
|
|
|
|
mins[0] = mins[1] = 999999;
|
|
|
|
maxs[0] = maxs[1] = -99999;
|
|
|
|
|
|
|
|
tex = &texinfo[s->texinfo];
|
2012-03-17 20:01:54 +00:00
|
|
|
|
|
|
|
for ( i = 0 ; i < s->numedges ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
e = dsurfedges[s->firstedge + i];
|
|
|
|
if ( e >= 0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
v = dvertexes + dedges[e].v[0];
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
else{
|
2007-11-04 03:34:51 +00:00
|
|
|
v = dvertexes + dedges[-e].v[1];
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
// VectorAdd (v->point, l->modelorg, vt);
|
2012-03-17 20:01:54 +00:00
|
|
|
VectorCopy( v->point, vt );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( j = 0 ; j < 2 ; j++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
val = DotProduct( vt, tex->vecs[j] ) + tex->vecs[j][3];
|
|
|
|
if ( val < mins[j] ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
mins[j] = val;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( val > maxs[j] ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
maxs[j] = val;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0 ; i < 2 ; i++ )
|
|
|
|
{
|
2007-11-04 03:34:51 +00:00
|
|
|
l->exactmins[i] = mins[i];
|
|
|
|
l->exactmaxs[i] = maxs[i];
|
2012-03-17 20:01:54 +00:00
|
|
|
|
|
|
|
mins[i] = floor( mins[i] / 16 );
|
|
|
|
maxs[i] = ceil( maxs[i] / 16 );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
l->texmins[i] = mins[i];
|
|
|
|
l->texsize[i] = maxs[i] - mins[i];
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( l->texsize[0] * l->texsize[1] > SINGLEMAP / 4 ) { // div 4 for extrasamples
|
|
|
|
Error( "Surface to large to map" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
================
|
|
|
|
CalcFaceVectors
|
|
|
|
|
|
|
|
Fills in texorg, worldtotex. and textoworld
|
|
|
|
================
|
|
|
|
*/
|
|
|
|
void CalcFaceVectors( lightinfo_t *l ){
|
|
|
|
texinfo_t *tex;
|
|
|
|
int i, j;
|
|
|
|
vec3_t texnormal;
|
|
|
|
vec_t distscale;
|
|
|
|
vec_t dist, len;
|
|
|
|
int w, h;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
tex = &texinfo[l->face->texinfo];
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
// convert from float to double
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0 ; i < 2 ; i++ )
|
|
|
|
for ( j = 0 ; j < 3 ; j++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
l->worldtotex[i][j] = tex->vecs[i][j];
|
|
|
|
|
|
|
|
// calculate a normal to the texture axis. points can be moved along this
|
|
|
|
// without changing their S/T
|
2012-03-17 20:01:54 +00:00
|
|
|
texnormal[0] = tex->vecs[1][1] * tex->vecs[0][2]
|
|
|
|
- tex->vecs[1][2] * tex->vecs[0][1];
|
|
|
|
texnormal[1] = tex->vecs[1][2] * tex->vecs[0][0]
|
|
|
|
- tex->vecs[1][0] * tex->vecs[0][2];
|
|
|
|
texnormal[2] = tex->vecs[1][0] * tex->vecs[0][1]
|
|
|
|
- tex->vecs[1][1] * tex->vecs[0][0];
|
|
|
|
VectorNormalize( texnormal, texnormal );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// flip it towards plane normal
|
2012-03-17 20:01:54 +00:00
|
|
|
distscale = DotProduct( texnormal, l->facenormal );
|
|
|
|
if ( !distscale ) {
|
|
|
|
Sys_FPrintf( SYS_VRB, "WARNING: Texture axis perpendicular to face\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
distscale = 1;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( distscale < 0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
distscale = -distscale;
|
2012-03-17 20:01:54 +00:00
|
|
|
VectorSubtract( vec3_origin, texnormal, texnormal );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// distscale is the ratio of the distance along the texture normal to
|
|
|
|
// the distance along the plane normal
|
2012-03-17 20:01:54 +00:00
|
|
|
distscale = 1 / distscale;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
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
|
|
|
len = VectorLength( l->worldtotex[i] );
|
|
|
|
dist = DotProduct( l->worldtotex[i], l->facenormal );
|
2007-11-04 03:34:51 +00:00
|
|
|
dist *= distscale;
|
2012-03-17 20:01:54 +00:00
|
|
|
VectorMA( l->worldtotex[i], -dist, texnormal, l->textoworld[i] );
|
|
|
|
VectorScale( l->textoworld[i], ( 1 / len ) * ( 1 / len ), l->textoworld[i] );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// calculate texorg on the texture plane
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0 ; i < 3 ; i++ )
|
|
|
|
l->texorg[i] = -tex->vecs[0][3] * l->textoworld[0][i] - tex->vecs[1][3] * l->textoworld[1][i];
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// project back to the face plane
|
2012-03-17 20:01:54 +00:00
|
|
|
dist = DotProduct( l->texorg, l->facenormal ) - l->facedist - 1;
|
2007-11-04 03:34:51 +00:00
|
|
|
dist *= distscale;
|
2012-03-17 20:01:54 +00:00
|
|
|
VectorMA( l->texorg, -dist, texnormal, l->texorg );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// compensate for org'd bmodels
|
2012-03-17 20:01:54 +00:00
|
|
|
VectorAdd( l->texorg, l->modelorg, l->texorg );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// total sample count
|
2012-03-17 20:01:54 +00:00
|
|
|
h = l->texsize[1] + 1;
|
|
|
|
w = l->texsize[0] + 1;
|
2007-11-04 03:34:51 +00:00
|
|
|
l->numsurfpt = w * h;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
=================
|
|
|
|
CalcPoints
|
|
|
|
|
|
|
|
For each texture aligned grid point, back project onto the plane
|
|
|
|
to get the world xyz value of the sample point
|
|
|
|
=================
|
|
|
|
*/
|
|
|
|
void CalcPoints( lightinfo_t *l, float sofs, float tofs ){
|
|
|
|
int i;
|
|
|
|
int s, t, j;
|
|
|
|
int w, h, step;
|
|
|
|
vec_t starts, startt, us, ut;
|
|
|
|
vec_t *surf;
|
|
|
|
vec_t mids, midt;
|
|
|
|
vec3_t facemid;
|
|
|
|
dleaf_t *leaf;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
surf = l->surfpt[0];
|
2012-03-17 20:01:54 +00:00
|
|
|
mids = ( l->exactmaxs[0] + l->exactmins[0] ) / 2;
|
|
|
|
midt = ( l->exactmaxs[1] + l->exactmins[1] ) / 2;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( j = 0 ; j < 3 ; j++ )
|
|
|
|
facemid[j] = l->texorg[j] + l->textoworld[0][j] * mids + l->textoworld[1][j] * midt;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
h = l->texsize[1] + 1;
|
|
|
|
w = l->texsize[0] + 1;
|
2007-11-04 03:34:51 +00:00
|
|
|
l->numsurfpt = w * h;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
starts = l->texmins[0] * 16;
|
|
|
|
startt = l->texmins[1] * 16;
|
2007-11-04 03:34:51 +00:00
|
|
|
step = 16;
|
|
|
|
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( t = 0 ; t < h ; t++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( s = 0 ; s < w ; s++, surf += 3 )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
us = starts + ( s + sofs ) * step;
|
|
|
|
ut = startt + ( t + tofs ) * step;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
// if a line can be traced from surf to facemid, the point is good
|
|
|
|
for ( i = 0 ; i < 6 ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
// calculate texture point
|
|
|
|
for ( j = 0 ; j < 3 ; j++ )
|
|
|
|
surf[j] = l->texorg[j] + l->textoworld[0][j] * us
|
|
|
|
+ l->textoworld[1][j] * ut;
|
|
|
|
|
|
|
|
leaf = Rad_PointInLeaf( surf );
|
|
|
|
if ( leaf->contents != CONTENTS_SOLID ) {
|
|
|
|
if ( !TestLine_r( 0, facemid, surf ) ) {
|
|
|
|
break; // got it
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// nudge it
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( i & 1 ) {
|
|
|
|
if ( us > mids ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
us -= 8;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( us < mids ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
us = mids;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
us += 8;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( us > mids ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
us = mids;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ut > midt ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
ut -= 8;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ut < midt ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
ut = midt;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ut += 8;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ut > midt ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
ut = midt;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//==============================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
#define MAX_STYLES 32
|
2007-11-04 03:34:51 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
int numsamples;
|
|
|
|
float *origins;
|
|
|
|
int numstyles;
|
|
|
|
int stylenums[MAX_STYLES];
|
|
|
|
float *samples[MAX_STYLES];
|
2007-11-04 03:34:51 +00:00
|
|
|
} facelight_t;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
directlight_t *directlights[MAX_MAP_LEAFS];
|
|
|
|
facelight_t facelight[MAX_MAP_FACES];
|
|
|
|
int numdlights;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
==================
|
|
|
|
FindTargetEntity
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
entity_t *FindTargetEntity( char *target ){
|
|
|
|
int i;
|
|
|
|
char *n;
|
|
|
|
|
|
|
|
for ( i = 0 ; i < num_entities ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
n = ValueForKey( &entities[i], "targetname" );
|
|
|
|
if ( !strcmp( n, target ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return &entities[i];
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
//#define DIRECT_LIGHT 3000
|
2012-03-17 20:01:54 +00:00
|
|
|
#define DIRECT_LIGHT 3
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
=============
|
|
|
|
CreateDirectLights
|
|
|
|
=============
|
|
|
|
*/
|
|
|
|
void CreateDirectLights( void ){
|
|
|
|
int i;
|
|
|
|
patch_t *p;
|
|
|
|
directlight_t *dl;
|
|
|
|
dleaf_t *leaf;
|
|
|
|
int cluster;
|
|
|
|
entity_t *e, *e2;
|
|
|
|
char *name;
|
|
|
|
char *target;
|
|
|
|
float angle;
|
|
|
|
vec3_t dest;
|
|
|
|
char *_color;
|
|
|
|
float intensity;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// surfaces
|
|
|
|
//
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0, p = patches ; i < (int) num_patches ; i++, p++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( p->totallight[0] < DIRECT_LIGHT
|
|
|
|
&& p->totallight[1] < DIRECT_LIGHT
|
|
|
|
&& p->totallight[2] < DIRECT_LIGHT ) {
|
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
|
|
|
|
|
|
|
numdlights++;
|
2012-03-17 20:01:54 +00:00
|
|
|
dl = malloc( sizeof( directlight_t ) );
|
|
|
|
memset( dl, 0, sizeof( *dl ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
VectorCopy( p->origin, dl->origin );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
leaf = Rad_PointInLeaf( dl->origin );
|
2007-11-04 03:34:51 +00:00
|
|
|
cluster = leaf->cluster;
|
|
|
|
dl->next = directlights[cluster];
|
|
|
|
directlights[cluster] = dl;
|
|
|
|
|
|
|
|
dl->type = emit_surface;
|
2012-03-17 20:01:54 +00:00
|
|
|
VectorCopy( p->plane->normal, dl->normal );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
dl->intensity = ColorNormalize( p->totallight, dl->color );
|
2007-11-04 03:34:51 +00:00
|
|
|
dl->intensity *= p->area * direct_scale;
|
2012-03-17 20:01:54 +00:00
|
|
|
VectorClear( p->totallight ); // all sent now
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// entities
|
|
|
|
//
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0 ; i < num_entities ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
e = &entities[i];
|
2012-03-17 20:01:54 +00:00
|
|
|
name = ValueForKey( e, "classname" );
|
|
|
|
if ( strncmp( name, "light", 5 ) ) {
|
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
|
|
|
|
|
|
|
numdlights++;
|
2012-03-17 20:01:54 +00:00
|
|
|
dl = malloc( sizeof( directlight_t ) );
|
|
|
|
memset( dl, 0, sizeof( *dl ) );
|
|
|
|
|
|
|
|
GetVectorForKey( e, "origin", dl->origin );
|
|
|
|
dl->style = FloatForKey( e, "_style" );
|
|
|
|
if ( !dl->style ) {
|
|
|
|
dl->style = FloatForKey( e, "style" );
|
|
|
|
}
|
|
|
|
if ( dl->style < 0 || dl->style >= MAX_LSTYLES ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
dl->style = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
leaf = Rad_PointInLeaf( dl->origin );
|
2007-11-04 03:34:51 +00:00
|
|
|
cluster = leaf->cluster;
|
|
|
|
|
|
|
|
dl->next = directlights[cluster];
|
|
|
|
directlights[cluster] = dl;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
intensity = FloatForKey( e, "light" );
|
|
|
|
if ( !intensity ) {
|
|
|
|
intensity = FloatForKey( e, "_light" );
|
|
|
|
}
|
|
|
|
if ( !intensity ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
intensity = 300;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
_color = ValueForKey( e, "_color" );
|
|
|
|
if ( _color && _color[0] ) {
|
|
|
|
sscanf( _color, "%f %f %f", &dl->color[0],&dl->color[1],&dl->color[2] );
|
|
|
|
ColorNormalize( dl->color, dl->color );
|
|
|
|
}
|
|
|
|
else{
|
2007-11-04 03:34:51 +00:00
|
|
|
dl->color[0] = dl->color[1] = dl->color[2] = 1.0;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
dl->intensity = intensity * entity_scale;
|
2007-11-04 03:34:51 +00:00
|
|
|
dl->type = emit_point;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
target = ValueForKey( e, "target" );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !strcmp( name, "light_spot" ) || target[0] ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
dl->type = emit_spotlight;
|
2012-03-17 20:01:54 +00:00
|
|
|
dl->stopdot = FloatForKey( e, "_cone" );
|
|
|
|
if ( !dl->stopdot ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
dl->stopdot = 10;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
dl->stopdot = cos( dl->stopdot / 180 * 3.14159 );
|
|
|
|
if ( target[0] ) { // point towards target
|
|
|
|
e2 = FindTargetEntity( target );
|
|
|
|
if ( !e2 ) {
|
|
|
|
Sys_Printf( "WARNING: light at (%i %i %i) has missing target\n",
|
|
|
|
(int)dl->origin[0], (int)dl->origin[1], (int)dl->origin[2] );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
else
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
GetVectorForKey( e2, "origin", dest );
|
|
|
|
VectorSubtract( dest, dl->origin, dl->normal );
|
|
|
|
VectorNormalize( dl->normal, dl->normal );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2012-03-17 20:01:54 +00:00
|
|
|
{ // point down angle
|
|
|
|
angle = FloatForKey( e, "angle" );
|
|
|
|
if ( angle == ANGLE_UP ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
dl->normal[0] = dl->normal[1] = 0;
|
|
|
|
dl->normal[2] = 1;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
else if ( angle == ANGLE_DOWN ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
dl->normal[0] = dl->normal[1] = 0;
|
|
|
|
dl->normal[2] = -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dl->normal[2] = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
dl->normal[0] = cos( angle / 180 * 3.14159 );
|
|
|
|
dl->normal[1] = sin( angle / 180 * 3.14159 );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
Sys_FPrintf( SYS_VRB, "%i direct lights\n", numdlights );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
=============
|
|
|
|
GatherSampleLight
|
|
|
|
|
|
|
|
Lightscale is the normalizer for multisampling
|
|
|
|
=============
|
|
|
|
*/
|
|
|
|
void GatherSampleLight( vec3_t pos, vec3_t normal,
|
|
|
|
float **styletable, int offset, int mapsize, float lightscale ){
|
|
|
|
int i;
|
|
|
|
directlight_t *l;
|
|
|
|
byte pvs[( MAX_MAP_LEAFS + 7 ) / 8];
|
|
|
|
vec3_t delta;
|
|
|
|
float dot, dot2;
|
|
|
|
float dist;
|
|
|
|
float scale;
|
|
|
|
float *dest;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// get the PVS for the pos to limit the number of checks
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !PvsForOrigin( pos, pvs ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0 ; i < dvis->numclusters ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !( pvs[ i >> 3] & ( 1 << ( i & 7 ) ) ) ) {
|
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
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( l = directlights[i] ; l ; l = l->next )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
VectorSubtract( l->origin, pos, delta );
|
|
|
|
dist = VectorNormalize( delta, delta );
|
|
|
|
dot = DotProduct( delta, normal );
|
|
|
|
if ( dot <= 0.001 ) {
|
|
|
|
continue; // behind sample surface
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
switch ( l->type )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
case emit_point:
|
|
|
|
// linear falloff
|
2012-03-17 20:01:54 +00:00
|
|
|
scale = ( l->intensity - dist ) * dot;
|
2007-11-04 03:34:51 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case emit_surface:
|
2012-03-17 20:01:54 +00:00
|
|
|
dot2 = -DotProduct( delta, l->normal );
|
|
|
|
if ( dot2 <= 0.001 ) {
|
|
|
|
goto skipadd; // behind light surface
|
|
|
|
}
|
|
|
|
scale = ( l->intensity / ( dist * dist ) ) * dot * dot2;
|
2007-11-04 03:34:51 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case emit_spotlight:
|
|
|
|
// linear falloff
|
2012-03-17 20:01:54 +00:00
|
|
|
dot2 = -DotProduct( delta, l->normal );
|
|
|
|
if ( dot2 <= l->stopdot ) {
|
|
|
|
goto skipadd; // outside light cone
|
|
|
|
}
|
|
|
|
scale = ( l->intensity - dist ) * dot;
|
2007-11-04 03:34:51 +00:00
|
|
|
break;
|
|
|
|
default:
|
2012-03-17 20:01:54 +00:00
|
|
|
Error( "Bad l->type" );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( TestLine_r( 0, pos, l->origin ) ) {
|
|
|
|
continue; // occluded
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( scale <= 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
|
|
|
|
|
|
|
// if this style doesn't have a table yet, allocate one
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !styletable[l->style] ) {
|
|
|
|
styletable[l->style] = malloc( mapsize );
|
|
|
|
memset( styletable[l->style], 0, mapsize );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
dest = styletable[l->style] + offset;
|
2007-11-04 03:34:51 +00:00
|
|
|
// add some light to it
|
2012-03-17 20:01:54 +00:00
|
|
|
VectorMA( dest, scale * lightscale, l->color, dest );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
skipadd: ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
=============
|
|
|
|
AddSampleToPatch
|
|
|
|
|
|
|
|
Take the sample's collected light and
|
|
|
|
add it back into the apropriate patch
|
|
|
|
for the radiosity pass.
|
|
|
|
|
|
|
|
The sample is added to all patches that might include
|
|
|
|
any part of it. They are counted and averaged, so it
|
|
|
|
doesn't generate extra light.
|
|
|
|
=============
|
|
|
|
*/
|
|
|
|
void AddSampleToPatch( vec3_t pos, vec3_t color, int facenum ){
|
|
|
|
patch_t *patch;
|
|
|
|
vec3_t mins, maxs;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if ( numbounce == 0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( color[0] + color[1] + color[2] < 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
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( patch = face_patches[facenum] ; patch ; patch = patch->next )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
// see if the point is in this patch (roughly)
|
2012-03-17 20:01:54 +00:00
|
|
|
WindingBounds( patch->winding, mins, maxs );
|
|
|
|
for ( i = 0 ; i < 3 ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( mins[i] > pos[i] + 16 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
goto nextpatch;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( maxs[i] < pos[i] - 16 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
goto nextpatch;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// add the sample to the patch
|
|
|
|
patch->samples++;
|
2012-03-17 20:01:54 +00:00
|
|
|
VectorAdd( patch->samplelight, color, patch->samplelight );
|
2007-11-04 03:34:51 +00:00
|
|
|
nextpatch:;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
=============
|
|
|
|
BuildFacelights
|
|
|
|
=============
|
|
|
|
*/
|
|
|
|
float sampleofs[5][2] =
|
2007-11-04 03:34:51 +00:00
|
|
|
{ {0,0}, {-0.25, -0.25}, {0.25, -0.25}, {0.25, 0.25}, {-0.25, 0.25} };
|
|
|
|
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void BuildFacelights( int facenum ){
|
|
|
|
dface_t *f;
|
|
|
|
lightinfo_t l[5];
|
|
|
|
float *styletable[MAX_LSTYLES];
|
|
|
|
int i, j;
|
|
|
|
float *spot;
|
|
|
|
patch_t *patch;
|
|
|
|
int numsamples;
|
|
|
|
int tablesize;
|
|
|
|
facelight_t *fl;
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
f = &dfaces[facenum];
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( texinfo[f->texinfo].flags & ( SURF_WARP | SURF_SKY ) ) {
|
|
|
|
return; // non-lit texture
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
memset( styletable,0, sizeof( styletable ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( extrasamples ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
numsamples = 5;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
else{
|
2007-11-04 03:34:51 +00:00
|
|
|
numsamples = 1;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
for ( i = 0 ; i < numsamples ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
memset( &l[i], 0, sizeof( l[i] ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
l[i].surfnum = facenum;
|
|
|
|
l[i].face = f;
|
2012-03-17 20:01:54 +00:00
|
|
|
VectorCopy( dplanes[f->planenum].normal, l[i].facenormal );
|
2007-11-04 03:34:51 +00:00
|
|
|
l[i].facedist = dplanes[f->planenum].dist;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( f->side ) {
|
|
|
|
VectorSubtract( vec3_origin, l[i].facenormal, l[i].facenormal );
|
2007-11-04 03:34:51 +00:00
|
|
|
l[i].facedist = -l[i].facedist;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the origin offset for rotating bmodels
|
2012-03-17 20:01:54 +00:00
|
|
|
VectorCopy( face_offset[facenum], l[i].modelorg );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
CalcFaceVectors( &l[i] );
|
|
|
|
CalcFaceExtents( &l[i] );
|
|
|
|
CalcPoints( &l[i], sampleofs[i][0], sampleofs[i][1] );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
tablesize = l[0].numsurfpt * sizeof( vec3_t );
|
|
|
|
styletable[0] = malloc( tablesize );
|
|
|
|
memset( styletable[0], 0, tablesize );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
fl = &facelight[facenum];
|
|
|
|
fl->numsamples = l[0].numsurfpt;
|
2012-03-17 20:01:54 +00:00
|
|
|
fl->origins = malloc( tablesize );
|
|
|
|
memcpy( fl->origins, l[0].surfpt, tablesize );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0 ; i < l[0].numsurfpt ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( j = 0 ; j < numsamples ; j++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
GatherSampleLight( l[j].surfpt[i], l[0].facenormal, styletable,
|
|
|
|
i * 3, tablesize, 1.0 / numsamples );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// contribute the sample to one or more patches
|
2012-03-17 20:01:54 +00:00
|
|
|
AddSampleToPatch( l[0].surfpt[i], styletable[0] + i * 3, facenum );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// average up the direct light on each patch for radiosity
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( patch = face_patches[facenum] ; patch ; patch = patch->next )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( patch->samples ) {
|
|
|
|
VectorScale( patch->samplelight, 1.0 / patch->samples, patch->samplelight );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// printf ("patch with no samples\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0 ; i < MAX_LSTYLES ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !styletable[i] ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( fl->numstyles == MAX_STYLES ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
break;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
fl->samples[fl->numstyles] = styletable[i];
|
|
|
|
fl->stylenums[fl->numstyles] = i;
|
|
|
|
fl->numstyles++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// the light from DIRECT_LIGHTS is sent out, but the
|
|
|
|
// texture itself should still be full bright
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( face_patches[facenum]->baselight[0] >= DIRECT_LIGHT ||
|
|
|
|
face_patches[facenum]->baselight[1] >= DIRECT_LIGHT ||
|
|
|
|
face_patches[facenum]->baselight[2] >= DIRECT_LIGHT
|
|
|
|
) {
|
2007-11-04 03:34:51 +00:00
|
|
|
spot = fl->samples[0];
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0 ; i < l[0].numsurfpt ; i++, spot += 3 )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
VectorAdd( spot, face_patches[facenum]->baselight, spot );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
=============
|
|
|
|
FinalLightFace
|
|
|
|
|
|
|
|
Add the indirect lighting on top of the direct
|
|
|
|
lighting and save into final map format
|
|
|
|
=============
|
|
|
|
*/
|
|
|
|
void FinalLightFace( int facenum ){
|
|
|
|
dface_t *f;
|
|
|
|
int i, j, k, st;
|
|
|
|
vec3_t lb;
|
|
|
|
patch_t *patch;
|
|
|
|
triangulation_t *trian;
|
|
|
|
facelight_t *fl;
|
|
|
|
float minlight;
|
|
|
|
float max, newmax;
|
|
|
|
byte *dest;
|
|
|
|
int pfacenum;
|
|
|
|
vec3_t facemins, facemaxs;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
f = &dfaces[facenum];
|
|
|
|
fl = &facelight[facenum];
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( texinfo[f->texinfo].flags & ( SURF_WARP | SURF_SKY ) ) {
|
|
|
|
return; // non-lit texture
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
ThreadLock();
|
2007-11-04 03:34:51 +00:00
|
|
|
f->lightofs = lightdatasize;
|
2012-03-17 20:01:54 +00:00
|
|
|
lightdatasize += fl->numstyles * ( fl->numsamples * 3 );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// add green sentinals between lightmaps
|
|
|
|
#if 0
|
2012-03-17 20:01:54 +00:00
|
|
|
lightdatasize += 64 * 3;
|
|
|
|
for ( i = 0 ; i < 64 ; i++ )
|
|
|
|
dlightdata[lightdatasize - ( i + 1 ) * 3 + 1] = 255;
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( lightdatasize > MAX_MAP_LIGHTING ) {
|
|
|
|
Error( "MAX_MAP_LIGHTING" );
|
|
|
|
}
|
|
|
|
ThreadUnlock();
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
f->styles[0] = 0;
|
|
|
|
f->styles[1] = f->styles[2] = f->styles[3] = 0xff;
|
|
|
|
|
|
|
|
//
|
|
|
|
// set up the triangulation
|
|
|
|
//
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( numbounce > 0 ) {
|
|
|
|
ClearBounds( facemins, facemaxs );
|
|
|
|
for ( i = 0 ; i < f->numedges ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
int ednum;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
ednum = dsurfedges[f->firstedge + i];
|
|
|
|
if ( ednum >= 0 ) {
|
|
|
|
AddPointToBounds( dvertexes[dedges[ednum].v[0]].point,
|
|
|
|
facemins, facemaxs );
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
AddPointToBounds( dvertexes[dedges[-ednum].v[1]].point,
|
|
|
|
facemins, facemaxs );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
trian = AllocTriangulation( &dplanes[f->planenum] );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// for all faces on the plane, add the nearby patches
|
|
|
|
// to the triangulation
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( pfacenum = planelinks[f->side][f->planenum]
|
|
|
|
; pfacenum ; pfacenum = facelinks[pfacenum] )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( patch = face_patches[pfacenum] ; patch ; patch = patch->next )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
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 ( facemins[i] - patch->origin[i] > subdiv * 2 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
break;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( patch->origin[i] - facemaxs[i] > subdiv * 2 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
break;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( i != 3 ) {
|
|
|
|
continue; // not needed for this face
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
AddPointToTriangulation( patch, trian );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0 ; i < trian->numpoints ; i++ )
|
|
|
|
memset( trian->edgematrix[i], 0, trian->numpoints * sizeof( trian->edgematrix[0][0] ) );
|
|
|
|
TriangulatePoints( trian );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
//
|
|
|
|
// sample the triangulation
|
|
|
|
//
|
|
|
|
|
|
|
|
// _minlight allows models that have faces that would not be
|
|
|
|
// illuminated to receive a mottled light pattern instead of
|
|
|
|
// black
|
2012-03-17 20:01:54 +00:00
|
|
|
minlight = FloatForKey( face_entity[facenum], "_minlight" ) * 128;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
dest = &dlightdata[f->lightofs];
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( fl->numstyles > MAXLIGHTMAPS ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
fl->numstyles = MAXLIGHTMAPS;
|
2012-03-17 20:01:54 +00:00
|
|
|
Sys_Printf( "face with too many lightstyles: (%f %f %f)\n",
|
|
|
|
face_patches[facenum]->origin[0],
|
|
|
|
face_patches[facenum]->origin[1],
|
|
|
|
face_patches[facenum]->origin[2]
|
|
|
|
);
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( st = 0 ; st < fl->numstyles ; st++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
f->styles[st] = fl->stylenums[st];
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( j = 0 ; j < fl->numsamples ; j++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
VectorCopy( ( fl->samples[st] + j * 3 ), lb );
|
|
|
|
if ( numbounce > 0 && st == 0 ) {
|
|
|
|
vec3_t add;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
SampleTriangulation( fl->origins + j * 3, trian, add );
|
|
|
|
VectorAdd( lb, add, lb );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
// add an ambient term if desired
|
2012-03-17 20:01:54 +00:00
|
|
|
lb[0] += ambient;
|
|
|
|
lb[1] += ambient;
|
|
|
|
lb[2] += ambient;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
VectorScale( lb, lightscale, lb );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// we need to clamp without allowing hue to change
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( k = 0 ; k < 3 ; k++ )
|
|
|
|
if ( lb[k] < 1 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
lb[k] = 1;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
max = lb[0];
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( lb[1] > max ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
max = lb[1];
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( lb[2] > max ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
max = lb[2];
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
newmax = max;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( newmax < 0 ) {
|
|
|
|
newmax = 0; // roundoff problems
|
|
|
|
}
|
|
|
|
if ( newmax < minlight ) {
|
|
|
|
newmax = minlight + ( rand() % 48 );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( newmax > maxlight ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
newmax = maxlight;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( k = 0 ; k < 3 ; k++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
*dest++ = lb[k] * newmax / max;
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( numbounce > 0 ) {
|
|
|
|
FreeTriangulation( trian );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|