Model_liquid.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 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 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 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 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 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. #include "../idlib/precompiled.h"
  21. #pragma hdrstop
  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->deformedSurface = 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->dominantTris = deformInfo->dominantTris;
  79. tri->numVerts = deformInfo->numOutputVerts;
  80. R_AllocStaticTriSurfVerts( tri, tri->numVerts );
  81. SIMDProcessor->Memcpy( tri->verts, verts.Ptr(), deformInfo->numSourceVerts * sizeof(tri->verts[0]) );
  82. // replicate the mirror seam vertexes
  83. base = deformInfo->numOutputVerts - deformInfo->numMirroredVerts;
  84. for ( i = 0 ; i < deformInfo->numMirroredVerts ; i++ ) {
  85. tri->verts[base + i] = tri->verts[deformInfo->mirroredVerts[i]];
  86. }
  87. R_BoundTriSurf( tri );
  88. // If a surface is going to be have a lighting interaction generated, it will also have to call
  89. // R_DeriveTangents() to get normals, tangents, and face planes. If it only
  90. // needs shadows generated, it will only have to generate face planes. If it only
  91. // has ambient drawing, or is culled, no additional work will be necessary
  92. if ( !r_useDeferredTangents.GetBool() ) {
  93. // set face planes, vertex normals, tangents
  94. R_DeriveTangents( tri );
  95. }
  96. surf.geometry = tri;
  97. surf.shader = shader;
  98. return surf;
  99. }
  100. /*
  101. ====================
  102. idRenderModelLiquid::WaterDrop
  103. ====================
  104. */
  105. void idRenderModelLiquid::WaterDrop( int x, int y, float *page ) {
  106. int cx, cy;
  107. int left,top,right,bottom;
  108. int square;
  109. int radsquare = drop_radius * drop_radius;
  110. float invlength = 1.0f / ( float )radsquare;
  111. float dist;
  112. if ( x < 0 ) {
  113. x = 1 + drop_radius + random.RandomInt( verts_x - 2 * drop_radius - 1 );
  114. }
  115. if ( y < 0 ) {
  116. y = 1 + drop_radius + random.RandomInt( verts_y - 2 * drop_radius - 1 );
  117. }
  118. left=-drop_radius; right = drop_radius;
  119. top=-drop_radius; bottom = drop_radius;
  120. // Perform edge clipping...
  121. if ( x - drop_radius < 1 ) {
  122. left -= (x-drop_radius-1);
  123. }
  124. if ( y - drop_radius < 1 ) {
  125. top -= (y-drop_radius-1);
  126. }
  127. if ( x + drop_radius > verts_x - 1 ) {
  128. right -= (x+drop_radius-verts_x+1);
  129. }
  130. if ( y + drop_radius > verts_y - 1 ) {
  131. bottom-= (y+drop_radius-verts_y+1);
  132. }
  133. for ( cy = top; cy < bottom; cy++ ) {
  134. for ( cx = left; cx < right; cx++ ) {
  135. square = cy*cy + cx*cx;
  136. if ( square < radsquare ) {
  137. dist = idMath::Sqrt( (float)square * invlength );
  138. page[verts_x*(cy+y) + cx+x] += idMath::Cos16( dist * idMath::PI * 0.5f ) * drop_height;
  139. }
  140. }
  141. }
  142. }
  143. /*
  144. ====================
  145. idRenderModelLiquid::IntersectBounds
  146. ====================
  147. */
  148. void idRenderModelLiquid::IntersectBounds( const idBounds &bounds, float displacement ) {
  149. int cx, cy;
  150. int left,top,right,bottom;
  151. float up, down;
  152. float *pos;
  153. left = ( int )( bounds[ 0 ].x / scale_x );
  154. right = ( int )( bounds[ 1 ].x / scale_x );
  155. top = ( int )( bounds[ 0 ].y / scale_y );
  156. bottom = ( int )( bounds[ 1 ].y / scale_y );
  157. down = bounds[ 0 ].z;
  158. up = bounds[ 1 ].z;
  159. if ( ( right < 1 ) || ( left >= verts_x ) || ( bottom < 1 ) || ( top >= verts_x ) ) {
  160. return;
  161. }
  162. // Perform edge clipping...
  163. if ( left < 1 ) {
  164. left = 1;
  165. }
  166. if ( right >= verts_x ) {
  167. right = verts_x - 1;
  168. }
  169. if ( top < 1 ) {
  170. top = 1;
  171. }
  172. if ( bottom >= verts_y ) {
  173. bottom = verts_y - 1;
  174. }
  175. for ( cy = top; cy < bottom; cy++ ) {
  176. for ( cx = left; cx < right; cx++ ) {
  177. pos = &page1[ verts_x * cy + cx ];
  178. if ( *pos > down ) {//&& ( *pos < up ) ) {
  179. *pos = down;
  180. }
  181. }
  182. }
  183. }
  184. /*
  185. ====================
  186. idRenderModelLiquid::Update
  187. ====================
  188. */
  189. void idRenderModelLiquid::Update( void ) {
  190. int x, y;
  191. float *p2;
  192. float *p1;
  193. float value;
  194. time += update_tics;
  195. idSwap( page1, page2 );
  196. if ( time > nextDropTime ) {
  197. WaterDrop( -1, -1, page2 );
  198. nextDropTime = time + drop_delay;
  199. } else if ( time < nextDropTime - drop_delay ) {
  200. nextDropTime = time + drop_delay;
  201. }
  202. p1 = page1;
  203. p2 = page2;
  204. switch( liquid_type ) {
  205. case 0 :
  206. for ( y = 1; y < verts_y - 1; y++ ) {
  207. p2 += verts_x;
  208. p1 += verts_x;
  209. for ( x = 1; x < verts_x - 1; x++ ) {
  210. value =
  211. ( p2[ x + verts_x ] +
  212. p2[ x - verts_x ] +
  213. p2[ x + 1 ] +
  214. p2[ x - 1 ] +
  215. p2[ x - verts_x - 1 ] +
  216. p2[ x - verts_x + 1 ] +
  217. p2[ x + verts_x - 1 ] +
  218. p2[ x + verts_x + 1 ] +
  219. p2[ x ] ) * ( 2.0f / 9.0f ) -
  220. p1[ x ];
  221. p1[ x ] = value * density;
  222. }
  223. }
  224. break;
  225. case 1 :
  226. for ( y = 1; y < verts_y - 1; y++ ) {
  227. p2 += verts_x;
  228. p1 += verts_x;
  229. for ( x = 1; x < verts_x - 1; x++ ) {
  230. value =
  231. ( p2[ x + verts_x ] +
  232. p2[ x - verts_x ] +
  233. p2[ x + 1 ] +
  234. p2[ x - 1 ] +
  235. p2[ x - verts_x - 1 ] +
  236. p2[ x - verts_x + 1 ] +
  237. p2[ x + verts_x - 1 ] +
  238. p2[ x + verts_x + 1 ] ) * 0.25f -
  239. p1[ x ];
  240. p1[ x ] = value * density;
  241. }
  242. }
  243. break;
  244. case 2 :
  245. for ( y = 1; y < verts_y - 1; y++ ) {
  246. p2 += verts_x;
  247. p1 += verts_x;
  248. for ( x = 1; x < verts_x - 1; x++ ) {
  249. value =
  250. ( p2[ x + verts_x ] +
  251. p2[ x - verts_x ] +
  252. p2[ x + 1 ] +
  253. p2[ x - 1 ] +
  254. p2[ x - verts_x - 1 ] +
  255. p2[ x - verts_x + 1 ] +
  256. p2[ x + verts_x - 1 ] +
  257. p2[ x + verts_x + 1 ] +
  258. p2[ x ] ) * ( 1.0f / 9.0f );
  259. p1[ x ] = value * density;
  260. }
  261. }
  262. break;
  263. }
  264. }
  265. /*
  266. ====================
  267. idRenderModelLiquid::Reset
  268. ====================
  269. */
  270. void idRenderModelLiquid::Reset() {
  271. int i, x, y;
  272. if ( pages.Num() < 2 * verts_x * verts_y ) {
  273. return;
  274. }
  275. nextDropTime = 0;
  276. time = 0;
  277. random.SetSeed( seed );
  278. page1 = pages.Ptr();
  279. page2 = page1 + verts_x * verts_y;
  280. for ( i = 0, y = 0; y < verts_y; y++ ) {
  281. for ( x = 0; x < verts_x; x++, i++ ) {
  282. page1[ i ] = 0.0f;
  283. page2[ i ] = 0.0f;
  284. verts[ i ].xyz.z = 0.0f;
  285. }
  286. }
  287. }
  288. /*
  289. ====================
  290. idRenderModelLiquid::InitFromFile
  291. ====================
  292. */
  293. void idRenderModelLiquid::InitFromFile( const char *fileName ) {
  294. int i, x, y;
  295. idToken token;
  296. idParser parser( LEXFL_ALLOWPATHNAMES | LEXFL_NOSTRINGESCAPECHARS );
  297. idList<int> tris;
  298. float size_x, size_y;
  299. float rate;
  300. name = fileName;
  301. if ( !parser.LoadFile( fileName ) ) {
  302. MakeDefaultModel();
  303. return;
  304. }
  305. size_x = scale_x * verts_x;
  306. size_y = scale_y * verts_y;
  307. while( parser.ReadToken( &token ) ) {
  308. if ( !token.Icmp( "seed" ) ) {
  309. seed = parser.ParseInt();
  310. } else if ( !token.Icmp( "size_x" ) ) {
  311. size_x = parser.ParseFloat();
  312. } else if ( !token.Icmp( "size_y" ) ) {
  313. size_y = parser.ParseFloat();
  314. } else if ( !token.Icmp( "verts_x" ) ) {
  315. verts_x = parser.ParseFloat();
  316. if ( verts_x < 2 ) {
  317. parser.Warning( "Invalid # of verts. Using default model." );
  318. MakeDefaultModel();
  319. return;
  320. }
  321. } else if ( !token.Icmp( "verts_y" ) ) {
  322. verts_y = parser.ParseFloat();
  323. if ( verts_y < 2 ) {
  324. parser.Warning( "Invalid # of verts. Using default model." );
  325. MakeDefaultModel();
  326. return;
  327. }
  328. } else if ( !token.Icmp( "liquid_type" ) ) {
  329. liquid_type = parser.ParseInt() - 1;
  330. if ( ( liquid_type < 0 ) || ( liquid_type >= LIQUID_MAX_TYPES ) ) {
  331. parser.Warning( "Invalid liquid_type. Using default model." );
  332. MakeDefaultModel();
  333. return;
  334. }
  335. } else if ( !token.Icmp( "density" ) ) {
  336. density = parser.ParseFloat();
  337. } else if ( !token.Icmp( "drop_height" ) ) {
  338. drop_height = parser.ParseFloat();
  339. } else if ( !token.Icmp( "drop_radius" ) ) {
  340. drop_radius = parser.ParseInt();
  341. } else if ( !token.Icmp( "drop_delay" ) ) {
  342. drop_delay = SEC2MS( parser.ParseFloat() );
  343. } else if ( !token.Icmp( "shader" ) ) {
  344. parser.ReadToken( &token );
  345. shader = declManager->FindMaterial( token );
  346. } else if ( !token.Icmp( "seed" ) ) {
  347. seed = parser.ParseInt();
  348. } else if ( !token.Icmp( "update_rate" ) ) {
  349. rate = parser.ParseFloat();
  350. if ( ( rate <= 0.0f ) || ( rate > 60.0f ) ) {
  351. parser.Warning( "Invalid update_rate. Must be between 0 and 60. Using default model." );
  352. MakeDefaultModel();
  353. return;
  354. }
  355. update_tics = 1000 / rate;
  356. } else {
  357. parser.Warning( "Unknown parameter '%s'. Using default model.", token.c_str() );
  358. MakeDefaultModel();
  359. return;
  360. }
  361. }
  362. scale_x = size_x / ( verts_x - 1 );
  363. scale_y = size_y / ( verts_y - 1 );
  364. pages.SetNum( 2 * verts_x * verts_y );
  365. page1 = pages.Ptr();
  366. page2 = page1 + verts_x * verts_y;
  367. verts.SetNum( verts_x * verts_y );
  368. for ( i = 0, y = 0; y < verts_y; y++ ) {
  369. for ( x = 0; x < verts_x; x++, i++ ) {
  370. page1[ i ] = 0.0f;
  371. page2[ i ] = 0.0f;
  372. verts[ i ].Clear();
  373. verts[ i ].xyz.Set( x * scale_x, y * scale_y, 0.0f );
  374. verts[ i ].st.Set( (float) x / (float)( verts_x - 1 ), (float) -y / (float)( verts_y - 1 ) );
  375. }
  376. }
  377. tris.SetNum( ( verts_x - 1 ) * ( verts_y - 1 ) * 6 );
  378. for( i = 0, y = 0; y < verts_y - 1; y++ ) {
  379. for( x = 1; x < verts_x; x++, i += 6 ) {
  380. tris[ i + 0 ] = y * verts_x + x;
  381. tris[ i + 1 ] = y * verts_x + x - 1;
  382. tris[ i + 2 ] = ( y + 1 ) * verts_x + x - 1;
  383. tris[ i + 3 ] = ( y + 1 ) * verts_x + x - 1;
  384. tris[ i + 4 ] = ( y + 1 ) * verts_x + x;
  385. tris[ i + 5 ] = y * verts_x + x;
  386. }
  387. }
  388. // build the information that will be common to all animations of this mesh:
  389. // sil edge connectivity and normal / tangent generation information
  390. deformInfo = R_BuildDeformInfo( verts.Num(), verts.Ptr(), tris.Num(), tris.Ptr(), true );
  391. bounds.Clear();
  392. bounds.AddPoint( idVec3( 0.0f, 0.0f, drop_height * -10.0f ) );
  393. bounds.AddPoint( idVec3( ( verts_x - 1 ) * scale_x, ( verts_y - 1 ) * scale_y, drop_height * 10.0f ) );
  394. // set the timestamp for reloadmodels
  395. fileSystem->ReadFile( name, NULL, &timeStamp );
  396. Reset();
  397. }
  398. /*
  399. ====================
  400. idRenderModelLiquid::InstantiateDynamicModel
  401. ====================
  402. */
  403. idRenderModel *idRenderModelLiquid::InstantiateDynamicModel( const struct renderEntity_s *ent, const struct viewDef_s *view, idRenderModel *cachedModel ) {
  404. idRenderModelStatic *staticModel;
  405. int frames;
  406. int t;
  407. float lerp;
  408. if ( cachedModel ) {
  409. delete cachedModel;
  410. cachedModel = NULL;
  411. }
  412. if ( !deformInfo ) {
  413. return NULL;
  414. }
  415. if ( !view ) {
  416. t = 0;
  417. } else {
  418. t = view->renderView.time;
  419. }
  420. // update the liquid model
  421. frames = ( t - time ) / update_tics;
  422. if ( frames > LIQUID_MAX_SKIP_FRAMES ) {
  423. // don't let time accumalate when skipping frames
  424. time += update_tics * ( frames - LIQUID_MAX_SKIP_FRAMES );
  425. frames = LIQUID_MAX_SKIP_FRAMES;
  426. }
  427. while( frames > 0 ) {
  428. Update();
  429. frames--;
  430. }
  431. // create the surface
  432. lerp = ( float )( t - time ) / ( float )update_tics;
  433. modelSurface_t surf = GenerateSurface( lerp );
  434. staticModel = new idRenderModelStatic;
  435. staticModel->AddSurface( surf );
  436. staticModel->bounds = surf.geometry->bounds;
  437. return staticModel;
  438. }
  439. /*
  440. ====================
  441. idRenderModelLiquid::IsDynamicModel
  442. ====================
  443. */
  444. dynamicModel_t idRenderModelLiquid::IsDynamicModel() const {
  445. return DM_CONTINUOUS;
  446. }
  447. /*
  448. ====================
  449. idRenderModelLiquid::Bounds
  450. ====================
  451. */
  452. idBounds idRenderModelLiquid::Bounds(const struct renderEntity_s *ent) const {
  453. // FIXME: need to do this better
  454. return bounds;
  455. }