1 /* -------------------------------------------------------------------------------
3 Copyright (C) 1999-2007 id Software, Inc. and contributors.
4 For a list of contributors, see the accompanying CONTRIBUTORS file.
6 This file is part of GtkRadiant.
8 GtkRadiant is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 GtkRadiant is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GtkRadiant; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 ----------------------------------------------------------------------------------
24 Foliage code for Wolfenstein: Enemy Territory by ydnar@splashdamage.com
26 ------------------------------------------------------------------------------- */
31 #define SURFACE_FOLIAGE_C
40 #define MAX_FOLIAGE_INSTANCES 8192
42 static int numFoliageInstances;
43 static foliageInstance_t foliageInstances[ MAX_FOLIAGE_INSTANCES ];
48 SubdivideFoliageTriangle_r()
49 recursively subdivides a triangle until the triangle is smaller than
50 the desired density, then pseudo-randomly sets a point
53 static void SubdivideFoliageTriangle_r( mapDrawSurface_t *ds, foliage_t *foliage, bspDrawVert_t **tri )
55 bspDrawVert_t mid, *tri2[ 3 ];
60 if( numFoliageInstances >= MAX_FOLIAGE_INSTANCES )
69 if( !PlaneFromPoints( plane, tri[ 0 ]->xyz, tri[ 1 ]->xyz, tri[ 2 ]->xyz ) )
72 /* if normal is too far off vertical, then don't place an instance */
73 if( plane[ 2 ] < 0.5f )
80 float *a, *b, dx, dy, dz, dist, maxDist;
81 foliageInstance_t *fi;
85 fi = &foliageInstances[ numFoliageInstances ];
87 /* find the longest edge and split it */
90 VectorClear( fi->xyz );
91 VectorClear( fi->normal );
92 for( i = 0; i < 3; i++ )
96 b = tri[ (i + 1) % 3 ]->xyz;
100 dy = a[ 1 ] - b[ 1 ];
101 dz = a[ 2 ] - b[ 2 ];
102 dist = (dx * dx) + (dy * dy) + (dz * dz);
111 /* add to centroid */
112 VectorAdd( fi->xyz, tri[ i ]->xyz, fi->xyz );
113 VectorAdd( fi->normal, tri[ i ]->normal, fi->normal );
116 /* is the triangle small enough? */
117 if( maxDist <= (foliage->density * foliage->density) )
119 float alpha, odds, r;
122 /* get average alpha */
123 if( foliage->inverseAlpha == 2 )
127 alpha = ((float) tri[ 0 ]->color[ 0 ][ 3 ] + (float) tri[ 1 ]->color[ 0 ][ 3 ] + (float) tri[ 2 ]->color[ 0 ][ 3 ]) / 765.0f;
128 if( foliage->inverseAlpha == 1 )
129 alpha = 1.0f - alpha;
135 odds = foliage->odds * alpha;
141 VectorScale( fi->xyz, 0.33333333f, fi->xyz );
142 if( VectorNormalize( fi->normal, fi->normal ) == 0.0f )
145 /* add to count and return */
146 numFoliageInstances++;
151 /* split the longest edge and map it */
152 LerpDrawVert( tri[ max ], tri[ (max + 1) % 3 ], &mid );
154 /* recurse to first triangle */
155 VectorCopy( tri, tri2 );
157 SubdivideFoliageTriangle_r( ds, foliage, tri2 );
159 /* recurse to second triangle */
160 VectorCopy( tri, tri2 );
161 tri2[ (max + 1) % 3 ] = ∣
162 SubdivideFoliageTriangle_r( ds, foliage, tri2 );
169 generates a foliage file for a bsp
172 void Foliage( mapDrawSurface_t *src )
174 int i, j, k, x, y, pw[ 5 ], r, oldNumMapDrawSurfs;
175 mapDrawSurface_t *ds;
178 mesh_t srcMesh, *subdivided, *mesh;
179 bspDrawVert_t *verts, *dv[ 3 ], *fi;
185 si = src->shaderInfo;
186 if( si == NULL || si->foliage == NULL )
189 /* do every foliage */
190 for( foliage = si->foliage; foliage != NULL; foliage = foliage->next )
193 numFoliageInstances = 0;
195 /* map the surface onto the lightmap origin/cluster/normal buffers */
199 case SURFACE_FORCED_META:
200 case SURFACE_TRIANGLES:
204 /* map the triangles */
205 for( i = 0; i < src->numIndexes; i += 3 )
207 dv[ 0 ] = &verts[ src->indexes[ i ] ];
208 dv[ 1 ] = &verts[ src->indexes[ i + 1 ] ];
209 dv[ 2 ] = &verts[ src->indexes[ i + 2 ] ];
210 SubdivideFoliageTriangle_r( src, foliage, dv );
215 /* make a mesh from the drawsurf */
216 srcMesh.width = src->patchWidth;
217 srcMesh.height = src->patchHeight;
218 srcMesh.verts = src->verts;
219 subdivided = SubdivideMesh( srcMesh, 8, 512 );
221 /* fit it to the curve and remove colinear verts on rows/columns */
222 PutMeshOnCurve( *subdivided );
223 mesh = RemoveLinearMeshColumnsRows( subdivided );
224 FreeMesh( subdivided );
229 /* map the mesh quads */
230 for( y = 0; y < (mesh->height - 1); y++ )
232 for( x = 0; x < (mesh->width - 1); x++ )
235 pw[ 0 ] = x + (y * mesh->width);
236 pw[ 1 ] = x + ((y + 1) * mesh->width);
237 pw[ 2 ] = x + 1 + ((y + 1) * mesh->width);
238 pw[ 3 ] = x + 1 + (y * mesh->width);
239 pw[ 4 ] = x + (y * mesh->width); /* same as pw[ 0 ] */
244 /* get drawverts and map first triangle */
245 dv[ 0 ] = &verts[ pw[ r + 0 ] ];
246 dv[ 1 ] = &verts[ pw[ r + 1 ] ];
247 dv[ 2 ] = &verts[ pw[ r + 2 ] ];
248 SubdivideFoliageTriangle_r( src, foliage, dv );
250 /* get drawverts and map second triangle */
251 dv[ 0 ] = &verts[ pw[ r + 0 ] ];
252 dv[ 1 ] = &verts[ pw[ r + 2 ] ];
253 dv[ 2 ] = &verts[ pw[ r + 3 ] ];
254 SubdivideFoliageTriangle_r( src, foliage, dv );
267 if( numFoliageInstances < 1 )
270 /* remember surface count */
271 oldNumMapDrawSurfs = numMapDrawSurfs;
273 /* set transform matrix */
274 VectorSet( scale, foliage->scale, foliage->scale, foliage->scale );
275 m4x4_scale_for_vec3( transform, scale );
277 /* add the model to the bsp */
278 InsertModel( foliage->model, 0, 0, transform, NULL, NULL, src->entityNum, src->castShadows, src->recvShadows, 0, src->lightmapScale, 0, 0 );
280 /* walk each new surface */
281 for( i = oldNumMapDrawSurfs; i < numMapDrawSurfs; i++ )
284 ds = &mapDrawSurfs[ i ];
287 ds->type = SURFACE_FOLIAGE;
288 ds->numFoliageInstances = numFoliageInstances;
291 ds->patchWidth = ds->numFoliageInstances;
292 ds->patchHeight = ds->numVerts;
294 /* set fog to be same as source surface */
295 ds->fogNum = src->fogNum;
297 /* add a drawvert for every instance */
298 verts = safe_malloc( (ds->numVerts + ds->numFoliageInstances) * sizeof( *verts ) );
299 memset( verts, 0, (ds->numVerts + ds->numFoliageInstances) * sizeof( *verts ) );
300 memcpy( verts, ds->verts, ds->numVerts * sizeof( *verts ) );
305 for( j = 0; j < ds->numFoliageInstances; j++ )
307 /* get vert (foliage instance) */
308 fi = &ds->verts[ ds->numVerts + j ];
310 /* copy xyz and normal */
311 VectorCopy( foliageInstances[ j ].xyz, fi->xyz );
312 VectorCopy( foliageInstances[ j ].normal, fi->normal );
314 /* ydnar: set color */
315 for( k = 0; k < MAX_LIGHTMAPS; k++ )
317 fi->color[ k ][ 0 ] = 255;
318 fi->color[ k ][ 1 ] = 255;
319 fi->color[ k ][ 2 ] = 255;
320 fi->color[ k ][ 3 ] = 255;
325 ds->numVerts += ds->numFoliageInstances;