2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 ----------------------------------------------------------------------------------
23 Foliage code for Wolfenstein: Enemy Territory by ydnar@splashdamage.com
25 ------------------------------------------------------------------------------- */
30 #define SURFACE_FOLIAGE_C
39 #define MAX_FOLIAGE_INSTANCES 8192
41 static int numFoliageInstances;
42 static foliageInstance_t foliageInstances[ MAX_FOLIAGE_INSTANCES ];
47 SubdivideFoliageTriangle_r()
48 recursively subdivides a triangle until the triangle is smaller than
49 the desired density, then pseudo-randomly sets a point
52 static void SubdivideFoliageTriangle_r( mapDrawSurface_t *ds, foliage_t *foliage, bspDrawVert_t **tri )
54 bspDrawVert_t mid, *tri2[ 3 ];
59 if( numFoliageInstances >= MAX_FOLIAGE_INSTANCES )
68 if( !PlaneFromPoints( plane, tri[ 0 ]->xyz, tri[ 1 ]->xyz, tri[ 2 ]->xyz ) )
71 /* if normal is too far off vertical, then don't place an instance */
72 if( plane[ 2 ] < 0.5f )
79 float *a, *b, dx, dy, dz, dist, maxDist;
80 foliageInstance_t *fi;
84 fi = &foliageInstances[ numFoliageInstances ];
86 /* find the longest edge and split it */
89 VectorClear( fi->xyz );
90 VectorClear( fi->normal );
91 for( i = 0; i < 3; i++ )
95 b = tri[ (i + 1) % 3 ]->xyz;
100 dz = a[ 2 ] - b[ 2 ];
101 dist = (dx * dx) + (dy * dy) + (dz * dz);
110 /* add to centroid */
111 VectorAdd( fi->xyz, tri[ i ]->xyz, fi->xyz );
112 VectorAdd( fi->normal, tri[ i ]->normal, fi->normal );
115 /* is the triangle small enough? */
116 if( maxDist <= (foliage->density * foliage->density) )
118 float alpha, odds, r;
121 /* get average alpha */
122 if( foliage->inverseAlpha == 2 )
126 alpha = ((float) tri[ 0 ]->color[ 0 ][ 3 ] + (float) tri[ 1 ]->color[ 0 ][ 3 ] + (float) tri[ 2 ]->color[ 0 ][ 3 ]) / 765.0f;
127 if( foliage->inverseAlpha == 1 )
128 alpha = 1.0f - alpha;
134 odds = foliage->odds * alpha;
140 VectorScale( fi->xyz, 0.33333333f, fi->xyz );
141 if( VectorNormalize( fi->normal, fi->normal ) == 0.0f )
144 /* add to count and return */
145 numFoliageInstances++;
150 /* split the longest edge and map it */
151 LerpDrawVert( tri[ max ], tri[ (max + 1) % 3 ], &mid );
153 /* recurse to first triangle */
154 VectorCopy( tri, tri2 );
156 SubdivideFoliageTriangle_r( ds, foliage, tri2 );
158 /* recurse to second triangle */
159 VectorCopy( tri, tri2 );
160 tri2[ (max + 1) % 3 ] = ∣
161 SubdivideFoliageTriangle_r( ds, foliage, tri2 );
168 generates a foliage file for a bsp
171 void Foliage( mapDrawSurface_t *src )
173 int i, j, k, x, y, pw[ 5 ], r, oldNumMapDrawSurfs;
174 mapDrawSurface_t *ds;
177 mesh_t srcMesh, *subdivided, *mesh;
178 bspDrawVert_t *verts, *dv[ 3 ], *fi;
184 si = src->shaderInfo;
185 if( si == NULL || si->foliage == NULL )
188 /* do every foliage */
189 for( foliage = si->foliage; foliage != NULL; foliage = foliage->next )
192 numFoliageInstances = 0;
194 /* map the surface onto the lightmap origin/cluster/normal buffers */
198 case SURFACE_FORCED_META:
199 case SURFACE_TRIANGLES:
203 /* map the triangles */
204 for( i = 0; i < src->numIndexes; i += 3 )
206 dv[ 0 ] = &verts[ src->indexes[ i ] ];
207 dv[ 1 ] = &verts[ src->indexes[ i + 1 ] ];
208 dv[ 2 ] = &verts[ src->indexes[ i + 2 ] ];
209 SubdivideFoliageTriangle_r( src, foliage, dv );
214 /* make a mesh from the drawsurf */
215 srcMesh.width = src->patchWidth;
216 srcMesh.height = src->patchHeight;
217 srcMesh.verts = src->verts;
218 subdivided = SubdivideMesh( srcMesh, 8, 512 );
220 /* fit it to the curve and remove colinear verts on rows/columns */
221 PutMeshOnCurve( *subdivided );
222 mesh = RemoveLinearMeshColumnsRows( subdivided );
223 FreeMesh( subdivided );
228 /* map the mesh quads */
229 for( y = 0; y < (mesh->height - 1); y++ )
231 for( x = 0; x < (mesh->width - 1); x++ )
234 pw[ 0 ] = x + (y * mesh->width);
235 pw[ 1 ] = x + ((y + 1) * mesh->width);
236 pw[ 2 ] = x + 1 + ((y + 1) * mesh->width);
237 pw[ 3 ] = x + 1 + (y * mesh->width);
238 pw[ 4 ] = x + (y * mesh->width); /* same as pw[ 0 ] */
243 /* get drawverts and map first triangle */
244 dv[ 0 ] = &verts[ pw[ r + 0 ] ];
245 dv[ 1 ] = &verts[ pw[ r + 1 ] ];
246 dv[ 2 ] = &verts[ pw[ r + 2 ] ];
247 SubdivideFoliageTriangle_r( src, foliage, dv );
249 /* get drawverts and map second triangle */
250 dv[ 0 ] = &verts[ pw[ r + 0 ] ];
251 dv[ 1 ] = &verts[ pw[ r + 2 ] ];
252 dv[ 2 ] = &verts[ pw[ r + 3 ] ];
253 SubdivideFoliageTriangle_r( src, foliage, dv );
266 if( numFoliageInstances < 1 )
269 /* remember surface count */
270 oldNumMapDrawSurfs = numMapDrawSurfs;
272 /* set transform matrix */
273 VectorSet( scale, foliage->scale, foliage->scale, foliage->scale );
274 m4x4_scale_for_vec3( transform, scale );
276 /* add the model to the bsp */
277 InsertModel( foliage->model, 0, transform, NULL, NULL, src->entityNum, src->castShadows, src->recvShadows, 0, src->lightmapScale );
279 /* walk each new surface */
280 for( i = oldNumMapDrawSurfs; i < numMapDrawSurfs; i++ )
283 ds = &mapDrawSurfs[ i ];
286 ds->type = SURFACE_FOLIAGE;
287 ds->numFoliageInstances = numFoliageInstances;
290 ds->patchWidth = ds->numFoliageInstances;
291 ds->patchHeight = ds->numVerts;
293 /* set fog to be same as source surface */
294 ds->fogNum = src->fogNum;
296 /* add a drawvert for every instance */
297 verts = safe_malloc( (ds->numVerts + ds->numFoliageInstances) * sizeof( *verts ) );
298 memset( verts, 0, (ds->numVerts + ds->numFoliageInstances) * sizeof( *verts ) );
299 memcpy( verts, ds->verts, ds->numVerts * sizeof( *verts ) );
304 for( j = 0; j < ds->numFoliageInstances; j++ )
306 /* get vert (foliage instance) */
307 fi = &ds->verts[ ds->numVerts + j ];
309 /* copy xyz and normal */
310 VectorCopy( foliageInstances[ j ].xyz, fi->xyz );
311 VectorCopy( foliageInstances[ j ].normal, fi->normal );
313 /* ydnar: set color */
314 for( k = 0; k < MAX_LIGHTMAPS; k++ )
316 fi->color[ k ][ 0 ] = 255;
317 fi->color[ k ][ 1 ] = 255;
318 fi->color[ k ][ 2 ] = 255;
319 fi->color[ k ][ 3 ] = 255;
324 ds->numVerts += ds->numFoliageInstances;