terrain.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256
  1. /*
  2. ===========================================================================
  3. Copyright (C) 1999-2005 Id Software, Inc.
  4. This file is part of Quake III Arena source code.
  5. Quake III Arena source code is free software; you can redistribute it
  6. and/or modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of the License,
  8. or (at your option) any later version.
  9. Quake III Arena source code is distributed in the hope that it will be
  10. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Foobar; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. ===========================================================================
  17. */
  18. #include "qbsp.h"
  19. #include <assert.h>
  20. #define SURF_WIDTH 2048
  21. #define SURF_HEIGHT 2048
  22. #define GROW_VERTS 512
  23. #define GROW_INDICES 512
  24. #define GROW_SURFACES 128
  25. #define VectorSet(v, x, y, z) v[0] = x;v[1] = y;v[2] = z;
  26. void QuakeTextureVecs( plane_t *plane, vec_t shift[2], vec_t rotate, vec_t scale[2], vec_t mappingVecs[2][4] );
  27. typedef struct {
  28. shaderInfo_t *shader;
  29. int x, y;
  30. int maxVerts;
  31. int numVerts;
  32. drawVert_t *verts;
  33. int maxIndexes;
  34. int numIndexes;
  35. int *indexes;
  36. } terrainSurf_t;
  37. static terrainSurf_t *surfaces = NULL;
  38. static terrainSurf_t *lastSurface = NULL;
  39. static int numsurfaces = 0;
  40. static int maxsurfaces = 0;
  41. /*
  42. ================
  43. ShaderForLayer
  44. ================
  45. */
  46. shaderInfo_t *ShaderForLayer( int minlayer, int maxlayer, const char *shadername ) {
  47. char shader[ MAX_QPATH ];
  48. if ( minlayer == maxlayer ) {
  49. sprintf( shader, "textures/%s_%d", shadername, maxlayer );
  50. } else {
  51. sprintf( shader, "textures/%s_%dto%d", shadername, minlayer, maxlayer );
  52. }
  53. return ShaderInfoForShader( shader );
  54. }
  55. /*
  56. ================
  57. CompareVert
  58. ================
  59. */
  60. qboolean CompareVert( drawVert_t *v1, drawVert_t *v2, qboolean checkst ) {
  61. int i;
  62. for( i = 0; i < 3; i++ ) {
  63. if ( floor( v1->xyz[ i ] + 0.1 ) != floor( v2->xyz[ i ] + 0.1 ) ) {
  64. return qfalse;
  65. }
  66. if ( checkst && ( ( v1->st[ 0 ] != v2->st[ 0 ] ) || ( v1->st[ 1 ] != v2->st[ 1 ] ) ) ) {
  67. return qfalse;
  68. }
  69. }
  70. return qtrue;
  71. }
  72. /*
  73. ================
  74. LoadAlphaMap
  75. ================
  76. */
  77. byte *LoadAlphaMap( int *num_layers, int *alphawidth, int *alphaheight ) {
  78. int *alphamap32;
  79. byte *alphamap;
  80. const char *alphamapname;
  81. char ext[ 128 ];
  82. int width;
  83. int height;
  84. int layers;
  85. int size;
  86. int i;
  87. assert( alphawidth );
  88. assert( alphaheight );
  89. assert( num_layers );
  90. layers = atoi( ValueForKey( mapent, "layers" ) );
  91. if ( layers < 1 ) {
  92. Error ("SetTerrainTextures: invalid value for 'layers' (%d)", layers );
  93. }
  94. alphamapname = ValueForKey( mapent, "alphamap" );
  95. if ( !alphamapname[ 0 ] ) {
  96. Error ("LoadAlphaMap: No alphamap specified on terrain" );
  97. }
  98. ExtractFileExtension( alphamapname, ext);
  99. if ( !Q_stricmp( ext, "tga" ) ) {
  100. Load32BitImage( ExpandGamePath( alphamapname ), &alphamap32, &width, &height );
  101. size = width * height;
  102. alphamap = malloc( size );
  103. for( i = 0; i < size; i++ ) {
  104. alphamap[ i ] = ( ( alphamap32[ i ] & 0xff ) * layers ) / 256;
  105. if ( alphamap[ i ] >= layers ) {
  106. alphamap[ i ] = layers - 1;
  107. }
  108. }
  109. } else {
  110. Load256Image( ExpandGamePath( alphamapname ), &alphamap, NULL, &width, &height );
  111. size = width * height;
  112. for( i = 0; i < size; i++ ) {
  113. if ( alphamap[ i ] >= layers ) {
  114. alphamap[ i ] = layers - 1;
  115. }
  116. }
  117. }
  118. if ( ( width < 2 ) || ( height < 2 ) ) {
  119. Error ("LoadAlphaMap: alphamap width/height must be at least 2x2." );
  120. }
  121. *num_layers = layers;
  122. *alphawidth = width;
  123. *alphaheight = height;
  124. return alphamap;
  125. }
  126. /*
  127. ================
  128. CalcTerrainSize
  129. ================
  130. */
  131. void CalcTerrainSize( vec3_t mins, vec3_t maxs, vec3_t size ) {
  132. bspbrush_t *brush;
  133. int i;
  134. const char *key;
  135. // calculate the size of the terrain
  136. ClearBounds( mins, maxs );
  137. for( brush = mapent->brushes; brush != NULL; brush = brush->next ) {
  138. AddPointToBounds( brush->mins, mins, maxs );
  139. AddPointToBounds( brush->maxs, mins, maxs );
  140. }
  141. key = ValueForKey( mapent, "min" );
  142. if ( key[ 0 ] ) {
  143. GetVectorForKey( mapent, "min", mins );
  144. }
  145. key = ValueForKey( mapent, "max" );
  146. if ( key[ 0 ] ) {
  147. GetVectorForKey( mapent, "max", maxs );
  148. }
  149. for( i = 0; i < 3; i++ ) {
  150. mins[ i ] = floor( mins[ i ] + 0.1 );
  151. maxs[ i ] = floor( maxs[ i ] + 0.1 );
  152. }
  153. VectorSubtract( maxs, mins, size );
  154. if ( ( size[ 0 ] <= 0 ) || ( size[ 1 ] <= 0 ) ) {
  155. Error ("CalcTerrainSize: Invalid terrain size: %fx%f", size[ 0 ], size[ 1 ] );
  156. }
  157. }
  158. /*
  159. ==================
  160. IsTriangleDegenerate
  161. Returns qtrue if all three points are collinear or backwards
  162. ===================
  163. */
  164. #define COLINEAR_AREA 10
  165. static qboolean IsTriangleDegenerate( drawVert_t *points, int a, int b, int c ) {
  166. vec3_t v1, v2, v3;
  167. float d;
  168. VectorSubtract( points[b].xyz, points[a].xyz, v1 );
  169. VectorSubtract( points[c].xyz, points[a].xyz, v2 );
  170. CrossProduct( v1, v2, v3 );
  171. d = VectorLength( v3 );
  172. // assume all very small or backwards triangles will cause problems
  173. if ( d < COLINEAR_AREA ) {
  174. return qtrue;
  175. }
  176. return qfalse;
  177. }
  178. /*
  179. ===============
  180. SideAsTriFan
  181. The surface can't be represented as a single tristrip without
  182. leaving a degenerate triangle (and therefore a crack), so add
  183. a point in the middle and create (points-1) triangles in fan order
  184. ===============
  185. */
  186. static void SideAsTriFan( terrainSurf_t *surf, int *index, int num ) {
  187. int i;
  188. int colorSum[4];
  189. drawVert_t *mid, *v;
  190. // make sure we have enough space for a new vert
  191. if ( surf->numVerts >= surf->maxVerts ) {
  192. surf->maxVerts += GROW_VERTS;
  193. surf->verts = realloc( surf->verts, surf->maxVerts * sizeof( *surf->verts ) );
  194. }
  195. // create a new point in the center of the face
  196. mid = &surf->verts[ surf->numVerts ];
  197. surf->numVerts++;
  198. colorSum[0] = colorSum[1] = colorSum[2] = colorSum[3] = 0;
  199. for (i = 0 ; i < num; i++ ) {
  200. v = &surf->verts[ index[ i ] ];
  201. VectorAdd( mid->xyz, v->xyz, mid->xyz );
  202. mid->st[0] += v->st[0];
  203. mid->st[1] += v->st[1];
  204. mid->lightmap[0] += v->lightmap[0];
  205. mid->lightmap[1] += v->lightmap[1];
  206. colorSum[0] += v->color[0];
  207. colorSum[1] += v->color[1];
  208. colorSum[2] += v->color[2];
  209. colorSum[3] += v->color[3];
  210. }
  211. mid->xyz[0] /= num;
  212. mid->xyz[1] /= num;
  213. mid->xyz[2] /= num;
  214. mid->st[0] /= num;
  215. mid->st[1] /= num;
  216. mid->lightmap[0] /= num;
  217. mid->lightmap[1] /= num;
  218. mid->color[0] = colorSum[0] / num;
  219. mid->color[1] = colorSum[1] / num;
  220. mid->color[2] = colorSum[2] / num;
  221. mid->color[3] = colorSum[3] / num;
  222. // fill in indices in trifan order
  223. if ( surf->numIndexes + num * 3 > surf->maxIndexes ) {
  224. surf->maxIndexes = surf->numIndexes + num * 3;
  225. surf->indexes = realloc( surf->indexes, surf->maxIndexes * sizeof( *surf->indexes ) );
  226. }
  227. for ( i = 0 ; i < num; i++ ) {
  228. surf->indexes[ surf->numIndexes++ ] = surf->numVerts - 1;
  229. surf->indexes[ surf->numIndexes++ ] = index[ i ];
  230. surf->indexes[ surf->numIndexes++ ] = index[ (i+1) % ( surf->numVerts - 1 ) ];
  231. }
  232. }
  233. /*
  234. ================
  235. SideAsTristrip
  236. Try to create indices that make (points-2) triangles in tristrip order
  237. ================
  238. */
  239. #define MAX_INDICES 1024
  240. static void SideAsTristrip( terrainSurf_t *surf, int *index, int num ) {
  241. int i;
  242. int rotate;
  243. int numIndices;
  244. int ni;
  245. int a, b, c;
  246. int indices[ MAX_INDICES ];
  247. // determine the triangle strip order
  248. numIndices = ( num - 2 ) * 3;
  249. if ( numIndices > MAX_INDICES ) {
  250. Error( "MAX_INDICES exceeded for surface" );
  251. }
  252. // try all possible orderings of the points looking
  253. // for a strip order that isn't degenerate
  254. for ( rotate = 0 ; rotate < num; rotate++ ) {
  255. for ( ni = 0, i = 0 ; i < num - 2 - i ; i++ ) {
  256. a = index[ ( num - 1 - i + rotate ) % num ];
  257. b = index[ ( i + rotate ) % num ];
  258. c = index[ ( num - 2 - i + rotate ) % num ];
  259. if ( IsTriangleDegenerate( surf->verts, a, b, c ) ) {
  260. break;
  261. }
  262. indices[ni++] = a;
  263. indices[ni++] = b;
  264. indices[ni++] = c;
  265. if ( i + 1 != num - 1 - i ) {
  266. a = index[ ( num - 2 - i + rotate ) % num ];
  267. b = index[ ( i + rotate ) % num ];
  268. c = index[ ( i + 1 + rotate ) % num ];
  269. if ( IsTriangleDegenerate( surf->verts, a, b, c ) ) {
  270. break;
  271. }
  272. indices[ni++] = a;
  273. indices[ni++] = b;
  274. indices[ni++] = c;
  275. }
  276. }
  277. if ( ni == numIndices ) {
  278. break; // got it done without degenerate triangles
  279. }
  280. }
  281. // if any triangle in the strip is degenerate,
  282. // render from a centered fan point instead
  283. if ( ni < numIndices ) {
  284. SideAsTriFan( surf, index, num );
  285. return;
  286. }
  287. // a normal tristrip
  288. if ( surf->numIndexes + ni > surf->maxIndexes ) {
  289. surf->maxIndexes = surf->numIndexes + ni;
  290. surf->indexes = realloc( surf->indexes, surf->maxIndexes * sizeof( *surf->indexes ) );
  291. }
  292. memcpy( surf->indexes + surf->numIndexes, indices, ni * sizeof( *surf->indexes ) );
  293. surf->numIndexes += ni;
  294. }
  295. /*
  296. ================
  297. CreateTerrainSurface
  298. ================
  299. */
  300. void CreateTerrainSurface( terrainSurf_t *surf, shaderInfo_t *shader ) {
  301. int i, j, k;
  302. drawVert_t *out;
  303. drawVert_t *in;
  304. mapDrawSurface_t *newsurf;
  305. newsurf = AllocDrawSurf();
  306. newsurf->miscModel = qtrue;
  307. newsurf->shaderInfo = shader;
  308. newsurf->lightmapNum = -1;
  309. newsurf->fogNum = -1;
  310. newsurf->numIndexes = surf->numIndexes;
  311. newsurf->numVerts = surf->numVerts;
  312. // copy the indices
  313. newsurf->indexes = malloc( surf->numIndexes * sizeof( *newsurf->indexes ) );
  314. memcpy( newsurf->indexes, surf->indexes, surf->numIndexes * sizeof( *newsurf->indexes ) );
  315. // allocate the vertices
  316. newsurf->verts = malloc( surf->numVerts * sizeof( *newsurf->verts ) );
  317. memset( newsurf->verts, 0, surf->numVerts * sizeof( *newsurf->verts ) );
  318. // calculate the surface verts
  319. out = newsurf->verts;
  320. for( i = 0; i < newsurf->numVerts; i++, out++ ) {
  321. VectorCopy( surf->verts[ i ].xyz, out->xyz );
  322. // set the texture coordinates
  323. out->st[ 0 ] = surf->verts[ i ].st[ 0 ];
  324. out->st[ 1 ] = surf->verts[ i ].st[ 1 ];
  325. // the colors will be set by the lighting pass
  326. out->color[0] = 255;
  327. out->color[1] = 255;
  328. out->color[2] = 255;
  329. out->color[3] = surf->verts[ i ].color[ 3 ];
  330. // calculate the vertex normal
  331. VectorClear( out->normal );
  332. for( j = 0; j < numsurfaces; j++ ) {
  333. in = surfaces[ j ].verts;
  334. for( k = 0; k < surfaces[ j ].numVerts; k++, in++ ) {
  335. if ( CompareVert( out, in, qfalse ) ) {
  336. VectorAdd( out->normal, in->normal, out->normal );
  337. }
  338. }
  339. }
  340. VectorNormalize( out->normal, out->normal );
  341. }
  342. }
  343. /*
  344. ================
  345. EmitTerrainVerts
  346. ================
  347. */
  348. void EmitTerrainVerts( side_t *side, terrainSurf_t *surf, int maxlayer, int alpha[ MAX_POINTS_ON_WINDING ], qboolean projecttexture ) {
  349. int i;
  350. int j;
  351. drawVert_t *vert;
  352. int *indices;
  353. int numindices;
  354. int maxindices;
  355. int xyplane;
  356. vec3_t xynorm = { 0, 0, 1 };
  357. vec_t shift[ 2 ] = { 0, 0 };
  358. vec_t scale[ 2 ] = { 0.5, 0.5 };
  359. float vecs[ 2 ][ 4 ];
  360. static int numtimes = 0;
  361. numtimes++;
  362. if ( !surf->verts ) {
  363. surf->numVerts = 0;
  364. surf->maxVerts = GROW_VERTS;
  365. surf->verts = malloc( surf->maxVerts * sizeof( *surf->verts ) );
  366. surf->numIndexes = 0;
  367. surf->maxIndexes = GROW_INDICES;
  368. surf->indexes = malloc( surf->maxIndexes * sizeof( *surf->indexes ) );
  369. }
  370. // calculate the texture coordinate vectors
  371. xyplane = FindFloatPlane( xynorm, 0 );
  372. QuakeTextureVecs( &mapplanes[ xyplane ], shift, 0, scale, vecs );
  373. // emit the vertexes
  374. numindices = 0;
  375. maxindices = surf->maxIndexes;
  376. indices = malloc ( maxindices * sizeof( *indices ) );
  377. for ( i = 0; i < side->winding->numpoints; i++ ) {
  378. vert = &surf->verts[ surf->numVerts ];
  379. // set the final alpha value--0 for texture 1, 255 for texture 2
  380. if ( alpha[ i ] < maxlayer ) {
  381. vert->color[3] = 0;
  382. } else {
  383. vert->color[3] = 255;
  384. }
  385. vert->xyz[ 0 ] = floor( side->winding->p[ i ][ 0 ] + 0.1f );
  386. vert->xyz[ 1 ] = floor( side->winding->p[ i ][ 1 ] + 0.1f );
  387. vert->xyz[ 2 ] = floor( side->winding->p[ i ][ 2 ] + 0.1f );
  388. // set the texture coordinates
  389. if ( projecttexture ) {
  390. vert->st[0] = ( vecs[0][3] + DotProduct( vecs[ 0 ], vert->xyz ) ) / surf->shader->width;
  391. vert->st[1] = ( vecs[1][3] + DotProduct( vecs[ 1 ], vert->xyz ) ) / surf->shader->height;
  392. } else {
  393. vert->st[0] = ( side->vecs[0][3] + DotProduct( side->vecs[ 0 ], vert->xyz ) ) / surf->shader->width;
  394. vert->st[1] = ( side->vecs[1][3] + DotProduct( side->vecs[ 1 ], vert->xyz ) ) / surf->shader->height;
  395. }
  396. VectorCopy( mapplanes[ side->planenum ].normal, vert->normal );
  397. for( j = 0; j < surf->numVerts; j++ ) {
  398. if ( CompareVert( vert, &surf->verts[ j ], qtrue ) ) {
  399. break;
  400. }
  401. }
  402. if ( numindices >= maxindices ) {
  403. maxindices += GROW_INDICES;
  404. indices = realloc( indices, maxindices * sizeof( *indices ) );
  405. }
  406. if ( j != surf->numVerts ) {
  407. indices[ numindices++ ] = j;
  408. } else {
  409. indices[ numindices++ ] = surf->numVerts;
  410. surf->numVerts++;
  411. if ( surf->numVerts >= surf->maxVerts ) {
  412. surf->maxVerts += GROW_VERTS;
  413. surf->verts = realloc( surf->verts, surf->maxVerts * sizeof( *surf->verts ) );
  414. }
  415. }
  416. }
  417. SideAsTristrip( surf, indices, numindices );
  418. free( indices );
  419. }
  420. /*
  421. ================
  422. SurfaceForShader
  423. ================
  424. */
  425. terrainSurf_t *SurfaceForShader( shaderInfo_t *shader, int x, int y ) {
  426. int i;
  427. if ( lastSurface && ( lastSurface->shader == shader ) && ( lastSurface->x == x ) && ( lastSurface->y == y ) ) {
  428. return lastSurface;
  429. }
  430. lastSurface = surfaces;
  431. for( i = 0; i < numsurfaces; i++, lastSurface++ ) {
  432. if ( ( lastSurface->shader == shader ) && ( lastSurface->x == x ) && ( lastSurface->y == y ) ) {
  433. return lastSurface;
  434. }
  435. }
  436. if ( numsurfaces >= maxsurfaces ) {
  437. maxsurfaces += GROW_SURFACES;
  438. surfaces = realloc( surfaces, maxsurfaces * sizeof( *surfaces ) );
  439. memset( surfaces + numsurfaces + 1, 0, ( maxsurfaces - numsurfaces - 1 ) * sizeof( *surfaces ) );
  440. }
  441. lastSurface= &surfaces[ numsurfaces++ ];
  442. lastSurface->shader = shader;
  443. lastSurface->x = x;
  444. lastSurface->y = y;
  445. return lastSurface;
  446. }
  447. /*
  448. ================
  449. SetTerrainTextures
  450. ================
  451. */
  452. void SetTerrainTextures( void ) {
  453. int i;
  454. int x, y;
  455. int layer;
  456. int minlayer, maxlayer;
  457. float s, t;
  458. float min_s, min_t;
  459. int alpha[ MAX_POINTS_ON_WINDING ];
  460. shaderInfo_t *si, *terrainShader;
  461. bspbrush_t *brush;
  462. side_t *side;
  463. const char *shadername;
  464. vec3_t mins, maxs;
  465. vec3_t size;
  466. int surfwidth, surfheight, surfsize;
  467. terrainSurf_t *surf;
  468. byte *alphamap;
  469. int alphawidth, alphaheight;
  470. int num_layers;
  471. extern qboolean onlyents;
  472. if ( onlyents ) {
  473. return;
  474. }
  475. shadername = ValueForKey( mapent, "shader" );
  476. if ( !shadername[ 0 ] ) {
  477. Error ("SetTerrainTextures: shader not specified" );
  478. }
  479. alphamap = LoadAlphaMap( &num_layers, &alphawidth, &alphaheight );
  480. mapent->firstDrawSurf = numMapDrawSurfs;
  481. // calculate the size of the terrain
  482. CalcTerrainSize( mins, maxs, size );
  483. surfwidth = ( size[ 0 ] + SURF_WIDTH - 1 ) / SURF_WIDTH;
  484. surfheight = ( size[ 1 ] + SURF_HEIGHT - 1 ) / SURF_HEIGHT;
  485. surfsize = surfwidth * surfheight;
  486. lastSurface = NULL;
  487. numsurfaces = 0;
  488. maxsurfaces = 0;
  489. for( i = num_layers; i > 0; i-- ) {
  490. maxsurfaces += i * surfsize;
  491. }
  492. surfaces = malloc( maxsurfaces * sizeof( *surfaces ) );
  493. memset( surfaces, 0, maxsurfaces * sizeof( *surfaces ) );
  494. terrainShader = ShaderInfoForShader( "textures/common/terrain" );
  495. for( brush = mapent->brushes; brush != NULL; brush = brush->next ) {
  496. // only create surfaces for sides marked as terrain
  497. for( side = brush->sides; side < &brush->sides[ brush->numsides ]; side++ ) {
  498. if ( !side->shaderInfo ) {
  499. continue;
  500. }
  501. if ( ( ( side->surfaceFlags | side->shaderInfo->surfaceFlags ) & SURF_NODRAW ) && !strstr( side->shaderInfo->shader, "terrain" ) ) {
  502. continue;
  503. }
  504. minlayer = num_layers;
  505. maxlayer = 0;
  506. // project each point of the winding onto the alphamap to determine which
  507. // textures to blend
  508. min_s = 1.0;
  509. min_t = 1.0;
  510. for( i = 0; i < side->winding->numpoints; i++ ) {
  511. s = floor( side->winding->p[ i ][ 0 ] + 0.1f - mins[ 0 ] ) / size[ 0 ];
  512. t = floor( side->winding->p[ i ][ 1 ] + 0.1f - mins[ 0 ] ) / size[ 1 ];
  513. if ( s < 0 ) {
  514. s = 0;
  515. }
  516. if ( t < 0 ) {
  517. t = 0;
  518. }
  519. if ( s >= 1.0 ) {
  520. s = 1.0;
  521. }
  522. if ( t >= 1.0 ) {
  523. t = 1.0;
  524. }
  525. if ( s < min_s ) {
  526. min_s = s;
  527. }
  528. if ( t < min_t ) {
  529. min_t = t;
  530. }
  531. x = ( alphawidth - 1 ) * s;
  532. y = ( alphaheight - 1 ) * t;
  533. layer = alphamap[ x + y * alphawidth ];
  534. if ( layer < minlayer ) {
  535. minlayer = layer;
  536. }
  537. if ( layer > maxlayer ) {
  538. maxlayer = layer;
  539. }
  540. alpha[ i ] = layer;
  541. }
  542. x = min_s * surfwidth;
  543. if ( x >= surfwidth ) {
  544. x = surfwidth - 1;
  545. }
  546. y = min_t * surfheight;
  547. if ( y >= surfheight ) {
  548. y = surfheight - 1;
  549. }
  550. if ( strstr( side->shaderInfo->shader, "terrain" ) ) {
  551. si = ShaderForLayer( minlayer, maxlayer, shadername );
  552. if ( showseams ) {
  553. for( i = 0; i < side->winding->numpoints; i++ ) {
  554. if ( ( alpha[ i ] != minlayer ) && ( alpha[ i ] != maxlayer ) ) {
  555. si = ShaderInfoForShader( "textures/common/white" );
  556. break;
  557. }
  558. }
  559. }
  560. surf = SurfaceForShader( si, x, y );
  561. EmitTerrainVerts( side, surf, maxlayer, alpha, qtrue );
  562. } else {
  563. si = side->shaderInfo;
  564. side->shaderInfo = terrainShader;
  565. surf = SurfaceForShader( si, x, y );
  566. EmitTerrainVerts( side, surf, maxlayer, alpha, qfalse );
  567. }
  568. }
  569. }
  570. // create the final surfaces
  571. for( surf = surfaces, i = 0; i < numsurfaces; i++, surf++ ) {
  572. if ( surf->numVerts ) {
  573. CreateTerrainSurface( surf, surf->shader );
  574. }
  575. }
  576. //
  577. // clean up any allocated memory
  578. //
  579. for( surf = surfaces, i = 0; i < numsurfaces; i++, surf++ ) {
  580. if ( surf->verts ) {
  581. free( surf->verts );
  582. free( surf->indexes );
  583. }
  584. }
  585. free( alphamap );
  586. free( surfaces );
  587. surfaces = NULL;
  588. lastSurface = NULL;
  589. numsurfaces = 0;
  590. maxsurfaces = 0;
  591. }
  592. /*****************************************************************************
  593. New terrain code
  594. ******************************************************************************/
  595. typedef struct terrainFace_s {
  596. shaderInfo_t *shaderInfo;
  597. //texdef_t texdef;
  598. float vecs[ 2 ][ 4 ]; // texture coordinate mapping
  599. } terrainFace_t;
  600. typedef struct terrainVert_s {
  601. vec3_t xyz;
  602. terrainFace_t tri;
  603. } terrainVert_t;
  604. typedef struct terrainMesh_s {
  605. float scale_x;
  606. float scale_y;
  607. vec3_t origin;
  608. int width, height;
  609. terrainVert_t *map;
  610. } terrainMesh_t;
  611. terrainVert_t *Terrain_GetVert( terrainMesh_t *pm, int x, int y ) {
  612. return &pm->map[ x + y * pm->width ];
  613. }
  614. void Terrain_GetTriangles( terrainMesh_t *pm, int x, int y, terrainVert_t **verts ) {
  615. if ( ( x + y ) & 1 ) {
  616. // first tri
  617. verts[ 0 ] = Terrain_GetVert( pm, x, y );
  618. verts[ 1 ] = Terrain_GetVert( pm, x, y + 1 );
  619. verts[ 2 ] = Terrain_GetVert( pm, x + 1, y + 1 );
  620. // second tri
  621. verts[ 3 ] = verts[ 2 ];
  622. verts[ 4 ] = Terrain_GetVert( pm, x + 1, y );
  623. verts[ 5 ] = verts[ 0 ];
  624. } else {
  625. // first tri
  626. verts[ 0 ] = Terrain_GetVert( pm, x, y );
  627. verts[ 1 ] = Terrain_GetVert( pm, x, y + 1 );
  628. verts[ 2 ] = Terrain_GetVert( pm, x + 1, y );
  629. // second tri
  630. verts[ 3 ] = verts[ 2 ];
  631. verts[ 4 ] = verts[ 1 ];
  632. verts[ 5 ] = Terrain_GetVert( pm, x + 1, y + 1 );
  633. }
  634. }
  635. /*
  636. ================
  637. EmitTerrainVerts2
  638. ================
  639. */
  640. void EmitTerrainVerts2( terrainSurf_t *surf, terrainVert_t **verts, int alpha[ 3 ] ) {
  641. int i;
  642. int j;
  643. drawVert_t *vert;
  644. int *indices;
  645. int numindices;
  646. int maxindices;
  647. int xyplane;
  648. vec3_t xynorm = { 0, 0, 1 };
  649. vec_t shift[ 2 ] = { 0, 0 };
  650. vec_t scale[ 2 ] = { 0.5, 0.5 };
  651. float vecs[ 2 ][ 4 ];
  652. vec4_t plane;
  653. if ( !surf->verts ) {
  654. surf->numVerts = 0;
  655. surf->maxVerts = GROW_VERTS;
  656. surf->verts = malloc( surf->maxVerts * sizeof( *surf->verts ) );
  657. surf->numIndexes = 0;
  658. surf->maxIndexes = GROW_INDICES;
  659. surf->indexes = malloc( surf->maxIndexes * sizeof( *surf->indexes ) );
  660. }
  661. // calculate the texture coordinate vectors
  662. xyplane = FindFloatPlane( xynorm, 0 );
  663. QuakeTextureVecs( &mapplanes[ xyplane ], shift, 0, scale, vecs );
  664. // emit the vertexes
  665. numindices = 0;
  666. maxindices = surf->maxIndexes;
  667. assert( maxindices >= 0 );
  668. indices = malloc ( maxindices * sizeof( *indices ) );
  669. PlaneFromPoints( plane, verts[ 0 ]->xyz, verts[ 1 ]->xyz, verts[ 2 ]->xyz );
  670. for ( i = 0; i < 3; i++ ) {
  671. vert = &surf->verts[ surf->numVerts ];
  672. if ( alpha[ i ] ) {
  673. vert->color[3] = 255;
  674. } else {
  675. vert->color[3] = 0;
  676. }
  677. vert->xyz[ 0 ] = floor( verts[ i ]->xyz[ 0 ] + 0.1f );
  678. vert->xyz[ 1 ] = floor( verts[ i ]->xyz[ 1 ] + 0.1f );
  679. vert->xyz[ 2 ] = floor( verts[ i ]->xyz[ 2 ] + 0.1f );
  680. // set the texture coordinates
  681. vert->st[0] = ( vecs[0][3] + DotProduct( vecs[ 0 ], vert->xyz ) ) / surf->shader->width;
  682. vert->st[1] = ( vecs[1][3] + DotProduct( vecs[ 1 ], vert->xyz ) ) / surf->shader->height;
  683. VectorCopy( plane, vert->normal );
  684. for( j = 0; j < surf->numVerts; j++ ) {
  685. if ( CompareVert( vert, &surf->verts[ j ], qtrue ) ) {
  686. break;
  687. }
  688. }
  689. if ( numindices >= maxindices ) {
  690. maxindices += GROW_INDICES;
  691. indices = realloc( indices, maxindices * sizeof( *indices ) );
  692. }
  693. if ( j != surf->numVerts ) {
  694. indices[ numindices++ ] = j;
  695. } else {
  696. indices[ numindices++ ] = surf->numVerts;
  697. surf->numVerts++;
  698. if ( surf->numVerts >= surf->maxVerts ) {
  699. surf->maxVerts += GROW_VERTS;
  700. surf->verts = realloc( surf->verts, surf->maxVerts * sizeof( *surf->verts ) );
  701. }
  702. }
  703. }
  704. SideAsTristrip( surf, indices, numindices );
  705. free( indices );
  706. }
  707. int MapPlaneFromPoints( vec3_t p0, vec3_t p1, vec3_t p2 );
  708. void QuakeTextureVecs( plane_t *plane, vec_t shift[2], vec_t rotate, vec_t scale[2], vec_t mappingVecs[2][4] );
  709. qboolean RemoveDuplicateBrushPlanes( bspbrush_t *b );
  710. void SetBrushContents( bspbrush_t *b );
  711. void AddBrushSide( vec3_t v1, vec3_t v2, vec3_t v3, shaderInfo_t *terrainShader ) {
  712. side_t *side;
  713. int planenum;
  714. side = &buildBrush->sides[ buildBrush->numsides ];
  715. memset( side, 0, sizeof( *side ) );
  716. buildBrush->numsides++;
  717. side->shaderInfo = terrainShader;
  718. // find the plane number
  719. planenum = MapPlaneFromPoints( v1, v2, v3 );
  720. side->planenum = planenum;
  721. }
  722. void MakeBrushFromTriangle( vec3_t v1, vec3_t v2, vec3_t v3, shaderInfo_t *terrainShader ) {
  723. bspbrush_t *b;
  724. vec3_t d1;
  725. vec3_t d2;
  726. vec3_t d3;
  727. VectorSet( d1, v1[ 0 ], v1[ 1 ], MIN_WORLD_COORD + 10 ); //FIXME
  728. VectorSet( d2, v2[ 0 ], v2[ 1 ], MIN_WORLD_COORD + 10 );
  729. VectorSet( d3, v3[ 0 ], v3[ 1 ], MIN_WORLD_COORD + 10 );
  730. buildBrush->numsides = 0;
  731. buildBrush->detail = qfalse;
  732. AddBrushSide( v1, v2, v3, terrainShader );
  733. AddBrushSide( v1, d1, v2, terrainShader );
  734. AddBrushSide( v2, d2, v3, terrainShader );
  735. AddBrushSide( v3, d3, v1, terrainShader );
  736. AddBrushSide( d3, d2, d1, terrainShader );
  737. buildBrush->portalareas[0] = -1;
  738. buildBrush->portalareas[1] = -1;
  739. buildBrush->entitynum = num_entities-1;
  740. buildBrush->brushnum = entitySourceBrushes;
  741. // if there are mirrored planes, the entire brush is invalid
  742. if ( !RemoveDuplicateBrushPlanes( buildBrush ) ) {
  743. return;
  744. }
  745. // get the content for the entire brush
  746. SetBrushContents( buildBrush );
  747. buildBrush->contents |= CONTENTS_DETAIL;
  748. b = FinishBrush();
  749. if ( !b ) {
  750. return;
  751. }
  752. }
  753. void MakeTerrainIntoBrushes( terrainMesh_t *tm ) {
  754. int index[ 6 ];
  755. int y;
  756. int x;
  757. terrainVert_t *verts;
  758. shaderInfo_t *terrainShader;
  759. terrainShader = ShaderInfoForShader( "textures/common/terrain" );
  760. verts = tm->map;
  761. for( y = 0; y < tm->height - 1; y++ ) {
  762. for( x = 0; x < tm->width - 1; x++ ) {
  763. if ( ( x + y ) & 1 ) {
  764. // first tri
  765. index[ 0 ] = x + y * tm->width;
  766. index[ 1 ] = x + ( y + 1 ) * tm->width;
  767. index[ 2 ] = ( x + 1 ) + ( y + 1 ) * tm->width;
  768. index[ 3 ] = ( x + 1 ) + ( y + 1 ) * tm->width;
  769. index[ 4 ] = ( x + 1 ) + y * tm->width;
  770. index[ 5 ] = x + y * tm->width;
  771. } else {
  772. // first tri
  773. index[ 0 ] = x + y * tm->width;
  774. index[ 1 ] = x + ( y + 1 ) * tm->width;
  775. index[ 2 ] = ( x + 1 ) + y * tm->width;
  776. index[ 3 ] = ( x + 1 ) + y * tm->width;
  777. index[ 4 ] = x + ( y + 1 ) * tm->width;
  778. index[ 5 ] = ( x + 1 ) + ( y + 1 ) * tm->width;
  779. }
  780. MakeBrushFromTriangle( verts[ index[ 0 ] ].xyz, verts[ index[ 1 ] ].xyz, verts[ index[ 2 ] ].xyz, terrainShader );
  781. MakeBrushFromTriangle( verts[ index[ 3 ] ].xyz, verts[ index[ 4 ] ].xyz, verts[ index[ 5 ] ].xyz, terrainShader );
  782. }
  783. }
  784. }
  785. void Terrain_ParseFace( terrainFace_t *face ) {
  786. shaderInfo_t *si;
  787. vec_t shift[ 2 ];
  788. vec_t rotate;
  789. vec_t scale[ 2 ];
  790. char name[ MAX_QPATH ];
  791. char shader[ MAX_QPATH ];
  792. plane_t p;
  793. // read the texturedef
  794. GetToken( qfalse );
  795. strcpy( name, token );
  796. GetToken( qfalse );
  797. shift[ 0 ] = atof(token);
  798. GetToken( qfalse );
  799. shift[ 1 ] = atof( token );
  800. GetToken( qfalse );
  801. rotate = atof( token );
  802. GetToken( qfalse );
  803. scale[ 0 ] = atof( token );
  804. GetToken( qfalse );
  805. scale[ 1 ] = atof( token );
  806. // find default flags and values
  807. sprintf( shader, "textures/%s", name );
  808. si = ShaderInfoForShader( shader );
  809. face->shaderInfo = si;
  810. //face->texdef = si->texdef;
  811. // skip over old contents
  812. GetToken( qfalse );
  813. // skip over old flags
  814. GetToken( qfalse );
  815. // skip over old value
  816. GetToken( qfalse );
  817. //Surface_Parse( &face->texdef );
  818. //Surface_BuildTexdef( &face->texdef );
  819. // make a fake horizontal plane
  820. VectorSet( p.normal, 0, 0, 1 );
  821. p.dist = 0;
  822. p.type = PlaneTypeForNormal( p.normal );
  823. QuakeTextureVecs( &p, shift, rotate, scale, face->vecs );
  824. }
  825. #define MAX_TERRAIN_TEXTURES 128
  826. static int numtextures = 0;;
  827. static shaderInfo_t *textures[ MAX_TERRAIN_TEXTURES ];
  828. void Terrain_AddTexture( shaderInfo_t *texture ) {
  829. int i;
  830. if ( !texture ) {
  831. return;
  832. }
  833. for( i = 0; i < numtextures; i++ ) {
  834. if ( textures[ i ] == texture ) {
  835. return;
  836. }
  837. }
  838. if ( numtextures >= MAX_TERRAIN_TEXTURES ) {
  839. Error( "Too many textures on terrain" );
  840. return;
  841. }
  842. textures[ numtextures++ ] = texture;
  843. }
  844. int LayerForShader( shaderInfo_t *shader ) {
  845. int i;
  846. int l;
  847. l = strlen( shader->shader );
  848. for( i = l - 1; i >= 0; i-- ) {
  849. if ( shader->shader[ i ] == '_' ) {
  850. return atoi( &shader->shader[ i + 1 ] );
  851. break;
  852. }
  853. }
  854. return 0;
  855. }
  856. /*
  857. =================
  858. ParseTerrain
  859. Creates a mapDrawSurface_t from the terrain text
  860. =================
  861. */
  862. void ParseTerrain( void ) {
  863. int i, j;
  864. int x, y;
  865. int x1, y1;
  866. terrainMesh_t t;
  867. int index;
  868. terrainVert_t *verts[ 6 ];
  869. int num_layers;
  870. int layer, minlayer, maxlayer;
  871. int alpha[ 6 ];
  872. shaderInfo_t *si, *terrainShader;
  873. int surfwidth, surfheight, surfsize;
  874. terrainSurf_t *surf;
  875. char shadername[ MAX_QPATH ];
  876. mapent->firstDrawSurf = numMapDrawSurfs;
  877. memset( &t, 0, sizeof( t ) );
  878. MatchToken( "{" );
  879. // get width
  880. GetToken( qtrue );
  881. t.width = atoi( token );
  882. // get height
  883. GetToken( qfalse );
  884. t.height = atoi( token );
  885. // get scale_x
  886. GetToken( qfalse );
  887. t.scale_x = atof( token );
  888. // get scale_y
  889. GetToken( qfalse );
  890. t.scale_y = atof( token );
  891. // get origin
  892. GetToken( qtrue );
  893. t.origin[ 0 ] = atof( token );
  894. GetToken( qfalse );
  895. t.origin[ 1 ] = atof( token );
  896. GetToken( qfalse );
  897. t.origin[ 2 ] = atof( token );
  898. t.map = malloc( t.width * t.height * sizeof( t.map[ 0 ] ) );
  899. if ( t.width <= 0 || t.height <= 0 ) {
  900. Error( "ParseTerrain: bad size" );
  901. }
  902. numtextures = 0;
  903. index = 0;
  904. for ( i = 0; i < t.height; i++ ) {
  905. for( j = 0; j < t.width; j++, index++ ) {
  906. // get height
  907. GetToken( qtrue );
  908. t.map[ index ].xyz[ 0 ] = t.origin[ 0 ] + t.scale_x * ( float )j;
  909. t.map[ index ].xyz[ 1 ] = t.origin[ 1 ] + t.scale_y * ( float )i;
  910. t.map[ index ].xyz[ 2 ] = t.origin[ 2 ] + atof( token );
  911. Terrain_ParseFace( &t.map[ index ].tri );
  912. Terrain_AddTexture( t.map[ index ].tri.shaderInfo );
  913. }
  914. }
  915. MatchToken( "}" );
  916. MatchToken( "}" );
  917. MakeTerrainIntoBrushes( &t );
  918. surfwidth = ( ( t.scale_x * t.width ) + SURF_WIDTH - 1 ) / SURF_WIDTH;
  919. surfheight = ( ( t.scale_y * t.height ) + SURF_HEIGHT - 1 ) / SURF_HEIGHT;
  920. surfsize = surfwidth * surfheight;
  921. //FIXME
  922. num_layers = 0;
  923. for( i = 0; i < numtextures; i++ ) {
  924. layer = LayerForShader( textures[ i ] ) + 1;
  925. if ( layer > num_layers ) {
  926. num_layers = layer;
  927. }
  928. }
  929. num_layers = 4;
  930. memset( alpha, 0, sizeof( alpha ) );
  931. lastSurface = NULL;
  932. numsurfaces = 0;
  933. maxsurfaces = 0;
  934. for( i = num_layers; i > 0; i-- ) {
  935. maxsurfaces += i * surfsize;
  936. }
  937. surfaces = malloc( maxsurfaces * sizeof( *surfaces ) );
  938. memset( surfaces, 0, maxsurfaces * sizeof( *surfaces ) );
  939. terrainShader = ShaderInfoForShader( "textures/common/terrain" );
  940. // get the shadername
  941. if ( Q_strncasecmp( textures[ 0 ]->shader, "textures/", 9 ) == 0 ) {
  942. strcpy( shadername, &textures[ 0 ]->shader[ 9 ] );
  943. } else {
  944. strcpy( shadername, textures[ 0 ]->shader );
  945. }
  946. j = strlen( shadername );
  947. for( i = j - 1; i >= 0; i-- ) {
  948. if ( shadername[ i ] == '_' ) {
  949. shadername[ i ] = 0;
  950. break;
  951. }
  952. }
  953. for( y = 0; y < t.height - 1; y++ ) {
  954. for( x = 0; x < t.width - 1; x++ ) {
  955. Terrain_GetTriangles( &t, x, y, verts );
  956. x1 = ( ( float )x / ( float )( t.width - 1 ) ) * surfwidth;
  957. if ( x1 >= surfwidth ) {
  958. x1 = surfwidth - 1;
  959. }
  960. y1 = ( ( float )y / ( float )( t.height - 1 ) ) * surfheight;
  961. if ( y1 >= surfheight ) {
  962. y1 = surfheight - 1;
  963. }
  964. maxlayer = minlayer = LayerForShader( verts[ 0 ]->tri.shaderInfo );
  965. for( i = 0; i < 3; i++ ) {
  966. layer = LayerForShader( verts[ i ]->tri.shaderInfo );
  967. if ( layer < minlayer ) {
  968. minlayer = layer;
  969. }
  970. if ( layer > maxlayer ) {
  971. maxlayer = layer;
  972. }
  973. }
  974. for( i = 0; i < 3; i++ ) {
  975. layer = LayerForShader( verts[ i ]->tri.shaderInfo );
  976. if ( layer > minlayer ) {
  977. alpha[ i ] = 1.0f;
  978. } else {
  979. alpha[ i ] = 0.0f;
  980. }
  981. }
  982. si = ShaderForLayer( minlayer, maxlayer, shadername );
  983. surf = SurfaceForShader( si, x1, y1 );
  984. EmitTerrainVerts2( surf, &verts[ 0 ], &alpha[ 0 ] );
  985. // second triangle
  986. maxlayer = minlayer = LayerForShader( verts[ 3 ]->tri.shaderInfo );
  987. for( i = 3; i < 6; i++ ) {
  988. layer = LayerForShader( verts[ i ]->tri.shaderInfo );
  989. if ( layer < minlayer ) {
  990. minlayer = layer;
  991. }
  992. if ( layer > maxlayer ) {
  993. maxlayer = layer;
  994. }
  995. }
  996. for( i = 3; i < 6; i++ ) {
  997. layer = LayerForShader( verts[ i ]->tri.shaderInfo );
  998. if ( layer > minlayer ) {
  999. alpha[ i ] = 1.0f;
  1000. } else {
  1001. alpha[ i ] = 0.0f;
  1002. }
  1003. }
  1004. si = ShaderForLayer( minlayer, maxlayer, shadername );
  1005. surf = SurfaceForShader( si, x1, y1 );
  1006. EmitTerrainVerts2( surf, &verts[ 3 ], &alpha[ 3 ] );
  1007. }
  1008. }
  1009. // create the final surfaces
  1010. for( surf = surfaces, i = 0; i < numsurfaces; i++, surf++ ) {
  1011. if ( surf->numVerts ) {
  1012. CreateTerrainSurface( surf, surf->shader );
  1013. }
  1014. }
  1015. //
  1016. // clean up any allocated memory
  1017. //
  1018. for( surf = surfaces, i = 0; i < numsurfaces; i++, surf++ ) {
  1019. if ( surf->verts ) {
  1020. free( surf->verts );
  1021. free( surf->indexes );
  1022. }
  1023. }
  1024. free( surfaces );
  1025. surfaces = NULL;
  1026. lastSurface = NULL;
  1027. numsurfaces = 0;
  1028. maxsurfaces = 0;
  1029. free( t.map );
  1030. }