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
|
|
|
Foliage code for Wolfenstein: Enemy Territory by ydnar@splashdamage.com
|
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 SURFACE_FOLIAGE_C
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* dependencies */
|
|
|
|
#include "q3map2.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
#define MAX_FOLIAGE_INSTANCES 8192
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
static int numFoliageInstances;
|
|
|
|
static foliageInstance_t foliageInstances[ MAX_FOLIAGE_INSTANCES ];
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
SubdivideFoliageTriangle_r()
|
|
|
|
recursively subdivides a triangle until the triangle is smaller than
|
|
|
|
the desired density, then pseudo-randomly sets a point
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void SubdivideFoliageTriangle_r( mapDrawSurface_t *ds, foliage_t *foliage, bspDrawVert_t **tri ){
|
|
|
|
bspDrawVert_t mid, *tri2[ 3 ];
|
|
|
|
int max;
|
|
|
|
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* limit test */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( numFoliageInstances >= MAX_FOLIAGE_INSTANCES ) {
|
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
|
|
|
/* plane test */
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
vec4_t plane;
|
|
|
|
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* make a plane */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !PlaneFromPoints( plane, tri[ 0 ]->xyz, tri[ 1 ]->xyz, tri[ 2 ]->xyz ) ) {
|
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
|
|
|
/* if normal is too far off vertical, then don't place an instance */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( plane[ 2 ] < 0.5f ) {
|
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
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* subdivide calc */
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
int i;
|
|
|
|
float *a, *b, dx, dy, dz, dist, maxDist;
|
|
|
|
foliageInstance_t *fi;
|
|
|
|
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* get instance */
|
|
|
|
fi = &foliageInstances[ numFoliageInstances ];
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* find the longest edge and split it */
|
|
|
|
max = -1;
|
|
|
|
maxDist = 0.0f;
|
|
|
|
VectorClear( fi->xyz );
|
|
|
|
VectorClear( fi->normal );
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < 3; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
/* get verts */
|
|
|
|
a = tri[ i ]->xyz;
|
2012-03-17 20:01:54 +00:00
|
|
|
b = tri[ ( i + 1 ) % 3 ]->xyz;
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* get dists */
|
|
|
|
dx = a[ 0 ] - b[ 0 ];
|
|
|
|
dy = a[ 1 ] - b[ 1 ];
|
|
|
|
dz = a[ 2 ] - b[ 2 ];
|
2012-03-17 20:01:54 +00:00
|
|
|
dist = ( dx * dx ) + ( dy * dy ) + ( dz * dz );
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* longer? */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( dist > maxDist ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
maxDist = dist;
|
|
|
|
max = i;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* add to centroid */
|
|
|
|
VectorAdd( fi->xyz, tri[ i ]->xyz, fi->xyz );
|
|
|
|
VectorAdd( fi->normal, tri[ i ]->normal, fi->normal );
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* is the triangle small enough? */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( maxDist <= ( foliage->density * foliage->density ) ) {
|
|
|
|
float alpha, odds, r;
|
|
|
|
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* get average alpha */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( foliage->inverseAlpha == 2 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
alpha = 1.0f;
|
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
|
|
|
alpha = ( (float) tri[ 0 ]->color[ 0 ][ 3 ] + (float) tri[ 1 ]->color[ 0 ][ 3 ] + (float) tri[ 2 ]->color[ 0 ][ 3 ] ) / 765.0f;
|
|
|
|
if ( foliage->inverseAlpha == 1 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
alpha = 1.0f - alpha;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( alpha < 0.75f ) {
|
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
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* roll the dice */
|
|
|
|
odds = foliage->odds * alpha;
|
|
|
|
r = Random();
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( r > odds ) {
|
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
|
|
|
/* scale centroid */
|
|
|
|
VectorScale( fi->xyz, 0.33333333f, fi->xyz );
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( VectorNormalize( fi->normal, fi->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
|
|
|
/* add to count and return */
|
|
|
|
numFoliageInstances++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* split the longest edge and map it */
|
2012-03-17 20:01:54 +00:00
|
|
|
LerpDrawVert( tri[ max ], tri[ ( max + 1 ) % 3 ], &mid );
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* recurse to first triangle */
|
|
|
|
VectorCopy( tri, tri2 );
|
|
|
|
tri2[ max ] = ∣
|
|
|
|
SubdivideFoliageTriangle_r( ds, foliage, tri2 );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* recurse to second triangle */
|
|
|
|
VectorCopy( tri, tri2 );
|
2012-03-17 20:01:54 +00:00
|
|
|
tri2[ ( max + 1 ) % 3 ] = ∣
|
2007-11-04 03:34:51 +00:00
|
|
|
SubdivideFoliageTriangle_r( ds, foliage, tri2 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
GenFoliage()
|
|
|
|
generates a foliage file for a bsp
|
|
|
|
*/
|
|
|
|
|
|
|
|
void Foliage( mapDrawSurface_t *src ){
|
|
|
|
int i, j, k, x, y, pw[ 5 ], r, oldNumMapDrawSurfs;
|
|
|
|
mapDrawSurface_t *ds;
|
|
|
|
shaderInfo_t *si;
|
|
|
|
foliage_t *foliage;
|
|
|
|
mesh_t srcMesh, *subdivided, *mesh;
|
|
|
|
bspDrawVert_t *verts, *dv[ 3 ], *fi;
|
|
|
|
vec3_t scale;
|
|
|
|
m4x4_t transform;
|
|
|
|
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* get shader */
|
|
|
|
si = src->shaderInfo;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( si == NULL || si->foliage == NULL ) {
|
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
|
|
|
/* do every foliage */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( foliage = si->foliage; foliage != NULL; foliage = foliage->next )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
/* zero out */
|
|
|
|
numFoliageInstances = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* map the surface onto the lightmap origin/cluster/normal buffers */
|
2012-03-17 20:01:54 +00:00
|
|
|
switch ( src->type )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
case SURFACE_META:
|
|
|
|
case SURFACE_FORCED_META:
|
|
|
|
case SURFACE_TRIANGLES:
|
|
|
|
/* get verts */
|
|
|
|
verts = src->verts;
|
|
|
|
|
|
|
|
/* map the triangles */
|
|
|
|
for ( i = 0; i < src->numIndexes; i += 3 )
|
|
|
|
{
|
|
|
|
dv[ 0 ] = &verts[ src->indexes[ i ] ];
|
|
|
|
dv[ 1 ] = &verts[ src->indexes[ i + 1 ] ];
|
|
|
|
dv[ 2 ] = &verts[ src->indexes[ i + 2 ] ];
|
|
|
|
SubdivideFoliageTriangle_r( src, foliage, dv );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SURFACE_PATCH:
|
|
|
|
/* make a mesh from the drawsurf */
|
|
|
|
srcMesh.width = src->patchWidth;
|
|
|
|
srcMesh.height = src->patchHeight;
|
|
|
|
srcMesh.verts = src->verts;
|
|
|
|
subdivided = SubdivideMesh( srcMesh, 8, 512 );
|
|
|
|
|
|
|
|
/* fit it to the curve and remove colinear verts on rows/columns */
|
|
|
|
PutMeshOnCurve( *subdivided );
|
|
|
|
mesh = RemoveLinearMeshColumnsRows( subdivided );
|
|
|
|
FreeMesh( subdivided );
|
|
|
|
|
|
|
|
/* get verts */
|
|
|
|
verts = mesh->verts;
|
|
|
|
|
|
|
|
/* map the mesh quads */
|
|
|
|
for ( y = 0; y < ( mesh->height - 1 ); y++ )
|
|
|
|
{
|
|
|
|
for ( x = 0; x < ( mesh->width - 1 ); x++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
/* set indexes */
|
|
|
|
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 ] */
|
|
|
|
|
|
|
|
/* set radix */
|
|
|
|
r = ( x + y ) & 1;
|
|
|
|
|
|
|
|
/* get drawverts and map first triangle */
|
|
|
|
dv[ 0 ] = &verts[ pw[ r + 0 ] ];
|
|
|
|
dv[ 1 ] = &verts[ pw[ r + 1 ] ];
|
|
|
|
dv[ 2 ] = &verts[ pw[ r + 2 ] ];
|
|
|
|
SubdivideFoliageTriangle_r( src, foliage, dv );
|
|
|
|
|
|
|
|
/* get drawverts and map second triangle */
|
|
|
|
dv[ 0 ] = &verts[ pw[ r + 0 ] ];
|
|
|
|
dv[ 1 ] = &verts[ pw[ r + 2 ] ];
|
|
|
|
dv[ 2 ] = &verts[ pw[ r + 3 ] ];
|
2007-11-04 03:34:51 +00:00
|
|
|
SubdivideFoliageTriangle_r( src, foliage, dv );
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* free the mesh */
|
|
|
|
FreeMesh( mesh );
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* any origins? */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( numFoliageInstances < 1 ) {
|
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
|
|
|
/* remember surface count */
|
|
|
|
oldNumMapDrawSurfs = numMapDrawSurfs;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* set transform matrix */
|
|
|
|
VectorSet( scale, foliage->scale, foliage->scale, foliage->scale );
|
|
|
|
m4x4_scale_for_vec3( transform, scale );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* add the model to the bsp */
|
|
|
|
InsertModel( foliage->model, 0, transform, NULL, NULL, src->entityNum, src->castShadows, src->recvShadows, 0, src->lightmapScale );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* walk each new surface */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = oldNumMapDrawSurfs; i < numMapDrawSurfs; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
/* get surface */
|
|
|
|
ds = &mapDrawSurfs[ i ];
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* set up */
|
|
|
|
ds->type = SURFACE_FOLIAGE;
|
|
|
|
ds->numFoliageInstances = numFoliageInstances;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* a wee hack */
|
|
|
|
ds->patchWidth = ds->numFoliageInstances;
|
|
|
|
ds->patchHeight = ds->numVerts;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* set fog to be same as source surface */
|
|
|
|
ds->fogNum = src->fogNum;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* add a drawvert for every instance */
|
2012-03-17 20:01:54 +00:00
|
|
|
verts = safe_malloc( ( ds->numVerts + ds->numFoliageInstances ) * sizeof( *verts ) );
|
|
|
|
memset( verts, 0, ( ds->numVerts + ds->numFoliageInstances ) * sizeof( *verts ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
memcpy( verts, ds->verts, ds->numVerts * sizeof( *verts ) );
|
|
|
|
free( ds->verts );
|
|
|
|
ds->verts = verts;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* copy the verts */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( j = 0; j < ds->numFoliageInstances; j++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
/* get vert (foliage instance) */
|
|
|
|
fi = &ds->verts[ ds->numVerts + j ];
|
|
|
|
|
|
|
|
/* copy xyz and normal */
|
|
|
|
VectorCopy( foliageInstances[ j ].xyz, fi->xyz );
|
|
|
|
VectorCopy( foliageInstances[ j ].normal, fi->normal );
|
|
|
|
|
|
|
|
/* ydnar: set color */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( k = 0; k < MAX_LIGHTMAPS; k++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
fi->color[ k ][ 0 ] = 255;
|
|
|
|
fi->color[ k ][ 1 ] = 255;
|
|
|
|
fi->color[ k ][ 2 ] = 255;
|
|
|
|
fi->color[ k ][ 3 ] = 255;
|
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* increment */
|
|
|
|
ds->numVerts += ds->numFoliageInstances;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|