Model_liquid.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #pragma hdrstop
  21. #include "../idlib/precompiled.h"
  22. #include "tr_local.h"
  23. #include "Model_local.h"
  24. #define LIQUID_MAX_SKIP_FRAMES 5
  25. #define LIQUID_MAX_TYPES 3
  26. /*
  27. ====================
  28. idRenderModelLiquid::idRenderModelLiquid
  29. ====================
  30. */
  31. idRenderModelLiquid::idRenderModelLiquid() {
  32. verts_x = 32;
  33. verts_y = 32;
  34. scale_x = 256.0f;
  35. scale_y = 256.0f;
  36. liquid_type = 0;
  37. density = 0.97f;
  38. drop_height = 4;
  39. drop_radius = 4;
  40. drop_delay = 1000;
  41. shader = declManager->FindMaterial( NULL );
  42. update_tics = 33; // ~30 hz
  43. time = 0;
  44. seed = 0;
  45. random.SetSeed( 0 );
  46. }
  47. /*
  48. ====================
  49. idRenderModelLiquid::GenerateSurface
  50. ====================
  51. */
  52. modelSurface_t idRenderModelLiquid::GenerateSurface( float lerp ) {
  53. srfTriangles_t *tri;
  54. int i, base;
  55. idDrawVert *vert;
  56. modelSurface_t surf;
  57. float inv_lerp;
  58. inv_lerp = 1.0f - lerp;
  59. vert = verts.Ptr();
  60. for( i = 0; i < verts.Num(); i++, vert++ ) {
  61. vert->xyz.z = page1[ i ] * lerp + page2[ i ] * inv_lerp;
  62. }
  63. tr.pc.c_deformedSurfaces++;
  64. tr.pc.c_deformedVerts += deformInfo->numOutputVerts;
  65. tr.pc.c_deformedIndexes += deformInfo->numIndexes;
  66. tri = R_AllocStaticTriSurf();
  67. // note that some of the data is references, and should not be freed
  68. tri->referencedIndexes = true;
  69. tri->numIndexes = deformInfo->numIndexes;
  70. tri->indexes = deformInfo->indexes;
  71. tri->silIndexes = deformInfo->silIndexes;
  72. tri->numMirroredVerts = deformInfo->numMirroredVerts;
  73. tri->mirroredVerts = deformInfo->mirroredVerts;
  74. tri->numDupVerts = deformInfo->numDupVerts;
  75. tri->dupVerts = deformInfo->dupVerts;
  76. tri->numSilEdges = deformInfo->numSilEdges;
  77. tri->silEdges = deformInfo->silEdges;
  78. tri->numVerts = deformInfo->numOutputVerts;
  79. R_AllocStaticTriSurfVerts( tri, tri->numVerts );
  80. SIMDProcessor->Memcpy( tri->verts, verts.Ptr(), deformInfo->numSourceVerts * sizeof(tri->verts[0]) );
  81. // replicate the mirror seam vertexes
  82. base = deformInfo->numOutputVerts - deformInfo->numMirroredVerts;
  83. for ( i = 0 ; i < deformInfo->numMirroredVerts ; i++ ) {
  84. tri->verts[base + i] = tri->verts[deformInfo->mirroredVerts[i]];
  85. }
  86. R_BoundTriSurf( tri );
  87. surf.geometry = tri;
  88. surf.shader = shader;
  89. return surf;
  90. }
  91. /*
  92. ====================
  93. idRenderModelLiquid::WaterDrop
  94. ====================
  95. */
  96. void idRenderModelLiquid::WaterDrop( int x, int y, float *page ) {
  97. int cx, cy;
  98. int left,top,right,bottom;
  99. int square;
  100. int radsquare = drop_radius * drop_radius;
  101. float invlength = 1.0f / ( float )radsquare;
  102. float dist;
  103. if ( x < 0 ) {
  104. x = 1 + drop_radius + random.RandomInt( verts_x - 2 * drop_radius - 1 );
  105. }
  106. if ( y < 0 ) {
  107. y = 1 + drop_radius + random.RandomInt( verts_y - 2 * drop_radius - 1 );
  108. }
  109. left=-drop_radius; right = drop_radius;
  110. top=-drop_radius; bottom = drop_radius;
  111. // Perform edge clipping...
  112. if ( x - drop_radius < 1 ) {
  113. left -= (x-drop_radius-1);
  114. }
  115. if ( y - drop_radius < 1 ) {
  116. top -= (y-drop_radius-1);
  117. }
  118. if ( x + drop_radius > verts_x - 1 ) {
  119. right -= (x+drop_radius-verts_x+1);
  120. }
  121. if ( y + drop_radius > verts_y - 1 ) {
  122. bottom-= (y+drop_radius-verts_y+1);
  123. }
  124. for ( cy = top; cy < bottom; cy++ ) {
  125. for ( cx = left; cx < right; cx++ ) {
  126. square = cy*cy + cx*cx;
  127. if ( square < radsquare ) {
  128. dist = idMath::Sqrt( (float)square * invlength );
  129. page[verts_x*(cy+y) + cx+x] += idMath::Cos16( dist * idMath::PI * 0.5f ) * drop_height;
  130. }
  131. }
  132. }
  133. }
  134. /*
  135. ====================
  136. idRenderModelLiquid::IntersectBounds
  137. ====================
  138. */
  139. void idRenderModelLiquid::IntersectBounds( const idBounds &bounds, float displacement ) {
  140. int cx, cy;
  141. int left,top,right,bottom;
  142. float up, down;
  143. float *pos;
  144. left = ( int )( bounds[ 0 ].x / scale_x );
  145. right = ( int )( bounds[ 1 ].x / scale_x );
  146. top = ( int )( bounds[ 0 ].y / scale_y );
  147. bottom = ( int )( bounds[ 1 ].y / scale_y );
  148. down = bounds[ 0 ].z;
  149. up = bounds[ 1 ].z;
  150. if ( ( right < 1 ) || ( left >= verts_x ) || ( bottom < 1 ) || ( top >= verts_x ) ) {
  151. return;
  152. }
  153. // Perform edge clipping...
  154. if ( left < 1 ) {
  155. left = 1;
  156. }
  157. if ( right >= verts_x ) {
  158. right = verts_x - 1;
  159. }
  160. if ( top < 1 ) {
  161. top = 1;
  162. }
  163. if ( bottom >= verts_y ) {
  164. bottom = verts_y - 1;
  165. }
  166. for ( cy = top; cy < bottom; cy++ ) {
  167. for ( cx = left; cx < right; cx++ ) {
  168. pos = &page1[ verts_x * cy + cx ];
  169. if ( *pos > down ) {//&& ( *pos < up ) ) {
  170. *pos = down;
  171. }
  172. }
  173. }
  174. }
  175. /*
  176. ====================
  177. idRenderModelLiquid::Update
  178. ====================
  179. */
  180. void idRenderModelLiquid::Update() {
  181. int x, y;
  182. float *p2;
  183. float *p1;
  184. float value;
  185. time += update_tics;
  186. SwapValues( page1, page2 );
  187. if ( time > nextDropTime ) {
  188. WaterDrop( -1, -1, page2 );
  189. nextDropTime = time + drop_delay;
  190. } else if ( time < nextDropTime - drop_delay ) {
  191. nextDropTime = time + drop_delay;
  192. }
  193. p1 = page1;
  194. p2 = page2;
  195. switch( liquid_type ) {
  196. case 0 :
  197. for ( y = 1; y < verts_y - 1; y++ ) {
  198. p2 += verts_x;
  199. p1 += verts_x;
  200. for ( x = 1; x < verts_x - 1; x++ ) {
  201. value =
  202. ( p2[ x + verts_x ] +
  203. p2[ x - verts_x ] +
  204. p2[ x + 1 ] +
  205. p2[ x - 1 ] +
  206. p2[ x - verts_x - 1 ] +
  207. p2[ x - verts_x + 1 ] +
  208. p2[ x + verts_x - 1 ] +
  209. p2[ x + verts_x + 1 ] +
  210. p2[ x ] ) * ( 2.0f / 9.0f ) -
  211. p1[ x ];
  212. p1[ x ] = value * density;
  213. }
  214. }
  215. break;
  216. case 1 :
  217. for ( y = 1; y < verts_y - 1; y++ ) {
  218. p2 += verts_x;
  219. p1 += verts_x;
  220. for ( x = 1; x < verts_x - 1; x++ ) {
  221. value =
  222. ( p2[ x + verts_x ] +
  223. p2[ x - verts_x ] +
  224. p2[ x + 1 ] +
  225. p2[ x - 1 ] +
  226. p2[ x - verts_x - 1 ] +
  227. p2[ x - verts_x + 1 ] +
  228. p2[ x + verts_x - 1 ] +
  229. p2[ x + verts_x + 1 ] ) * 0.25f -
  230. p1[ x ];
  231. p1[ x ] = value * density;
  232. }
  233. }
  234. break;
  235. case 2 :
  236. for ( y = 1; y < verts_y - 1; y++ ) {
  237. p2 += verts_x;
  238. p1 += verts_x;
  239. for ( x = 1; x < verts_x - 1; x++ ) {
  240. value =
  241. ( p2[ x + verts_x ] +
  242. p2[ x - verts_x ] +
  243. p2[ x + 1 ] +
  244. p2[ x - 1 ] +
  245. p2[ x - verts_x - 1 ] +
  246. p2[ x - verts_x + 1 ] +
  247. p2[ x + verts_x - 1 ] +
  248. p2[ x + verts_x + 1 ] +
  249. p2[ x ] ) * ( 1.0f / 9.0f );
  250. p1[ x ] = value * density;
  251. }
  252. }
  253. break;
  254. }
  255. }
  256. /*
  257. ====================
  258. idRenderModelLiquid::Reset
  259. ====================
  260. */
  261. void idRenderModelLiquid::Reset() {
  262. int i, x, y;
  263. if ( pages.Num() < 2 * verts_x * verts_y ) {
  264. return;
  265. }
  266. nextDropTime = 0;
  267. time = 0;
  268. random.SetSeed( seed );
  269. page1 = pages.Ptr();
  270. page2 = page1 + verts_x * verts_y;
  271. for ( i = 0, y = 0; y < verts_y; y++ ) {
  272. for ( x = 0; x < verts_x; x++, i++ ) {
  273. page1[ i ] = 0.0f;
  274. page2[ i ] = 0.0f;
  275. verts[ i ].xyz.z = 0.0f;
  276. }
  277. }
  278. }
  279. /*
  280. ====================
  281. idRenderModelLiquid::InitFromFile
  282. ====================
  283. */
  284. void idRenderModelLiquid::InitFromFile( const char *fileName ) {
  285. int i, x, y;
  286. idToken token;
  287. idParser parser( LEXFL_ALLOWPATHNAMES | LEXFL_NOSTRINGESCAPECHARS );
  288. idList<int> tris;
  289. float size_x, size_y;
  290. float rate;
  291. name = fileName;
  292. if ( !parser.LoadFile( fileName ) ) {
  293. MakeDefaultModel();
  294. return;
  295. }
  296. size_x = scale_x * verts_x;
  297. size_y = scale_y * verts_y;
  298. while( parser.ReadToken( &token ) ) {
  299. if ( !token.Icmp( "seed" ) ) {
  300. seed = parser.ParseInt();
  301. } else if ( !token.Icmp( "size_x" ) ) {
  302. size_x = parser.ParseFloat();
  303. } else if ( !token.Icmp( "size_y" ) ) {
  304. size_y = parser.ParseFloat();
  305. } else if ( !token.Icmp( "verts_x" ) ) {
  306. verts_x = parser.ParseFloat();
  307. if ( verts_x < 2 ) {
  308. parser.Warning( "Invalid # of verts. Using default model." );
  309. MakeDefaultModel();
  310. return;
  311. }
  312. } else if ( !token.Icmp( "verts_y" ) ) {
  313. verts_y = parser.ParseFloat();
  314. if ( verts_y < 2 ) {
  315. parser.Warning( "Invalid # of verts. Using default model." );
  316. MakeDefaultModel();
  317. return;
  318. }
  319. } else if ( !token.Icmp( "liquid_type" ) ) {
  320. liquid_type = parser.ParseInt() - 1;
  321. if ( ( liquid_type < 0 ) || ( liquid_type >= LIQUID_MAX_TYPES ) ) {
  322. parser.Warning( "Invalid liquid_type. Using default model." );
  323. MakeDefaultModel();
  324. return;
  325. }
  326. } else if ( !token.Icmp( "density" ) ) {
  327. density = parser.ParseFloat();
  328. } else if ( !token.Icmp( "drop_height" ) ) {
  329. drop_height = parser.ParseFloat();
  330. } else if ( !token.Icmp( "drop_radius" ) ) {
  331. drop_radius = parser.ParseInt();
  332. } else if ( !token.Icmp( "drop_delay" ) ) {
  333. drop_delay = SEC2MS( parser.ParseFloat() );
  334. } else if ( !token.Icmp( "shader" ) ) {
  335. parser.ReadToken( &token );
  336. shader = declManager->FindMaterial( token );
  337. } else if ( !token.Icmp( "update_rate" ) ) {
  338. rate = parser.ParseFloat();
  339. if ( ( rate <= 0.0f ) || ( rate > 60.0f ) ) {
  340. parser.Warning( "Invalid update_rate. Must be between 0 and 60. Using default model." );
  341. MakeDefaultModel();
  342. return;
  343. }
  344. update_tics = 1000 / rate;
  345. } else {
  346. parser.Warning( "Unknown parameter '%s'. Using default model.", token.c_str() );
  347. MakeDefaultModel();
  348. return;
  349. }
  350. }
  351. scale_x = size_x / ( verts_x - 1 );
  352. scale_y = size_y / ( verts_y - 1 );
  353. pages.SetNum( 2 * verts_x * verts_y );
  354. page1 = pages.Ptr();
  355. page2 = page1 + verts_x * verts_y;
  356. verts.SetNum( verts_x * verts_y );
  357. for ( i = 0, y = 0; y < verts_y; y++ ) {
  358. for ( x = 0; x < verts_x; x++, i++ ) {
  359. page1[ i ] = 0.0f;
  360. page2[ i ] = 0.0f;
  361. verts[ i ].Clear();
  362. verts[ i ].xyz.Set( x * scale_x, y * scale_y, 0.0f );
  363. verts[ i ].SetTexCoord( (float) x / (float)( verts_x - 1 ), (float) -y / (float)( verts_y - 1 ) );
  364. }
  365. }
  366. tris.SetNum( ( verts_x - 1 ) * ( verts_y - 1 ) * 6 );
  367. for( i = 0, y = 0; y < verts_y - 1; y++ ) {
  368. for( x = 1; x < verts_x; x++, i += 6 ) {
  369. tris[ i + 0 ] = y * verts_x + x;
  370. tris[ i + 1 ] = y * verts_x + x - 1;
  371. tris[ i + 2 ] = ( y + 1 ) * verts_x + x - 1;
  372. tris[ i + 3 ] = ( y + 1 ) * verts_x + x - 1;
  373. tris[ i + 4 ] = ( y + 1 ) * verts_x + x;
  374. tris[ i + 5 ] = y * verts_x + x;
  375. }
  376. }
  377. // build the information that will be common to all animations of this mesh:
  378. // sil edge connectivity and normal / tangent generation information
  379. deformInfo = R_BuildDeformInfo( verts.Num(), verts.Ptr(), tris.Num(), tris.Ptr(), true );
  380. bounds.Clear();
  381. bounds.AddPoint( idVec3( 0.0f, 0.0f, drop_height * -10.0f ) );
  382. bounds.AddPoint( idVec3( ( verts_x - 1 ) * scale_x, ( verts_y - 1 ) * scale_y, drop_height * 10.0f ) );
  383. // set the timestamp for reloadmodels
  384. fileSystem->ReadFile( name, NULL, &timeStamp );
  385. Reset();
  386. }
  387. /*
  388. ====================
  389. idRenderModelLiquid::InstantiateDynamicModel
  390. ====================
  391. */
  392. idRenderModel *idRenderModelLiquid::InstantiateDynamicModel( const struct renderEntity_s *ent, const viewDef_t *view, idRenderModel *cachedModel ) {
  393. idRenderModelStatic *staticModel;
  394. int frames;
  395. int t;
  396. float lerp;
  397. if ( cachedModel ) {
  398. delete cachedModel;
  399. cachedModel = NULL;
  400. }
  401. if ( !deformInfo ) {
  402. return NULL;
  403. }
  404. if ( !view ) {
  405. t = 0;
  406. } else {
  407. t = view->renderView.time[0];
  408. }
  409. // update the liquid model
  410. frames = ( t - time ) / update_tics;
  411. if ( frames > LIQUID_MAX_SKIP_FRAMES ) {
  412. // don't let time accumalate when skipping frames
  413. time += update_tics * ( frames - LIQUID_MAX_SKIP_FRAMES );
  414. frames = LIQUID_MAX_SKIP_FRAMES;
  415. }
  416. while( frames > 0 ) {
  417. Update();
  418. frames--;
  419. }
  420. // create the surface
  421. lerp = ( float )( t - time ) / ( float )update_tics;
  422. modelSurface_t surf = GenerateSurface( lerp );
  423. staticModel = new (TAG_MODEL) idRenderModelStatic;
  424. staticModel->AddSurface( surf );
  425. staticModel->bounds = surf.geometry->bounds;
  426. return staticModel;
  427. }
  428. /*
  429. ====================
  430. idRenderModelLiquid::IsDynamicModel
  431. ====================
  432. */
  433. dynamicModel_t idRenderModelLiquid::IsDynamicModel() const {
  434. return DM_CONTINUOUS;
  435. }
  436. /*
  437. ====================
  438. idRenderModelLiquid::Bounds
  439. ====================
  440. */
  441. idBounds idRenderModelLiquid::Bounds(const struct renderEntity_s *ent) const {
  442. // FIXME: need to do this better
  443. return bounds;
  444. }