BrittleFracture.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381
  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 "Game_local.h"
  23. CLASS_DECLARATION( idEntity, idBrittleFracture )
  24. EVENT( EV_Activate, idBrittleFracture::Event_Activate )
  25. EVENT( EV_Touch, idBrittleFracture::Event_Touch )
  26. END_CLASS
  27. const int SHARD_ALIVE_TIME = 5000;
  28. const int SHARD_FADE_START = 2000;
  29. static const char *brittleFracture_SnapshotName = "_BrittleFracture_Snapshot_";
  30. /*
  31. ================
  32. idBrittleFracture::idBrittleFracture
  33. ================
  34. */
  35. idBrittleFracture::idBrittleFracture( void ) {
  36. material = NULL;
  37. decalMaterial = NULL;
  38. decalSize = 0.0f;
  39. maxShardArea = 0.0f;
  40. maxShatterRadius = 0.0f;
  41. minShatterRadius = 0.0f;
  42. linearVelocityScale = 0.0f;
  43. angularVelocityScale = 0.0f;
  44. shardMass = 0.0f;
  45. density = 0.0f;
  46. friction = 0.0f;
  47. bouncyness = 0.0f;
  48. fxFracture.Clear();
  49. bounds.Clear();
  50. disableFracture = false;
  51. lastRenderEntityUpdate = -1;
  52. changed = false;
  53. fl.networkSync = true;
  54. #ifdef _D3XP
  55. isXraySurface = false;
  56. #endif
  57. }
  58. /*
  59. ================
  60. idBrittleFracture::~idBrittleFracture
  61. ================
  62. */
  63. idBrittleFracture::~idBrittleFracture( void ) {
  64. int i;
  65. for ( i = 0; i < shards.Num(); i++ ) {
  66. shards[i]->decals.DeleteContents( true );
  67. delete shards[i];
  68. }
  69. // make sure the render entity is freed before the model is freed
  70. FreeModelDef();
  71. renderModelManager->FreeModel( renderEntity.hModel );
  72. }
  73. /*
  74. ================
  75. idBrittleFracture::Save
  76. ================
  77. */
  78. void idBrittleFracture::Save( idSaveGame *savefile ) const {
  79. int i, j;
  80. savefile->WriteInt( health );
  81. entityFlags_s flags = fl;
  82. LittleBitField( &flags, sizeof( flags ) );
  83. savefile->Write( &flags, sizeof( flags ) );
  84. // setttings
  85. savefile->WriteMaterial( material );
  86. savefile->WriteMaterial( decalMaterial );
  87. savefile->WriteFloat( decalSize );
  88. savefile->WriteFloat( maxShardArea );
  89. savefile->WriteFloat( maxShatterRadius );
  90. savefile->WriteFloat( minShatterRadius );
  91. savefile->WriteFloat( linearVelocityScale );
  92. savefile->WriteFloat( angularVelocityScale );
  93. savefile->WriteFloat( shardMass );
  94. savefile->WriteFloat( density );
  95. savefile->WriteFloat( friction );
  96. savefile->WriteFloat( bouncyness );
  97. savefile->WriteString( fxFracture );
  98. // state
  99. savefile->WriteBounds( bounds );
  100. savefile->WriteBool( disableFracture );
  101. savefile->WriteInt( lastRenderEntityUpdate );
  102. savefile->WriteBool( changed );
  103. savefile->WriteStaticObject( physicsObj );
  104. savefile->WriteInt( shards.Num() );
  105. for ( i = 0; i < shards.Num(); i++ ) {
  106. savefile->WriteWinding( shards[i]->winding );
  107. savefile->WriteInt( shards[i]->decals.Num() );
  108. for ( j = 0; j < shards[i]->decals.Num(); j++ ) {
  109. savefile->WriteWinding( *shards[i]->decals[j] );
  110. }
  111. savefile->WriteInt( shards[i]->neighbours.Num() );
  112. for ( j = 0; j < shards[i]->neighbours.Num(); j++ ) {
  113. int index = shards.FindIndex(shards[i]->neighbours[j]);
  114. assert(index != -1);
  115. savefile->WriteInt( index );
  116. }
  117. savefile->WriteInt( shards[i]->edgeHasNeighbour.Num() );
  118. for ( j = 0; j < shards[i]->edgeHasNeighbour.Num(); j++ ) {
  119. savefile->WriteBool( shards[i]->edgeHasNeighbour[j] );
  120. }
  121. savefile->WriteInt( shards[i]->droppedTime );
  122. savefile->WriteInt( shards[i]->islandNum );
  123. savefile->WriteBool( shards[i]->atEdge );
  124. savefile->WriteStaticObject( shards[i]->physicsObj );
  125. }
  126. #ifdef _D3XP
  127. savefile->WriteBool( isXraySurface );
  128. #endif
  129. }
  130. /*
  131. ================
  132. idBrittleFracture::Restore
  133. ================
  134. */
  135. void idBrittleFracture::Restore( idRestoreGame *savefile ) {
  136. int i, j , num;
  137. renderEntity.hModel = renderModelManager->AllocModel();
  138. renderEntity.hModel->InitEmpty( brittleFracture_SnapshotName );
  139. renderEntity.callback = idBrittleFracture::ModelCallback;
  140. renderEntity.noShadow = true;
  141. renderEntity.noSelfShadow = true;
  142. renderEntity.noDynamicInteractions = false;
  143. savefile->ReadInt( health );
  144. savefile->Read( &fl, sizeof( fl ) );
  145. LittleBitField( &fl, sizeof( fl ) );
  146. // setttings
  147. savefile->ReadMaterial( material );
  148. savefile->ReadMaterial( decalMaterial );
  149. savefile->ReadFloat( decalSize );
  150. savefile->ReadFloat( maxShardArea );
  151. savefile->ReadFloat( maxShatterRadius );
  152. savefile->ReadFloat( minShatterRadius );
  153. savefile->ReadFloat( linearVelocityScale );
  154. savefile->ReadFloat( angularVelocityScale );
  155. savefile->ReadFloat( shardMass );
  156. savefile->ReadFloat( density );
  157. savefile->ReadFloat( friction );
  158. savefile->ReadFloat( bouncyness );
  159. savefile->ReadString( fxFracture );
  160. // state
  161. savefile->ReadBounds(bounds);
  162. savefile->ReadBool( disableFracture );
  163. savefile->ReadInt( lastRenderEntityUpdate );
  164. savefile->ReadBool( changed );
  165. savefile->ReadStaticObject( physicsObj );
  166. RestorePhysics( &physicsObj );
  167. savefile->ReadInt( num );
  168. shards.SetNum( num );
  169. for ( i = 0; i < num; i++ ) {
  170. shards[i] = new shard_t;
  171. }
  172. for ( i = 0; i < num; i++ ) {
  173. savefile->ReadWinding( shards[i]->winding );
  174. savefile->ReadInt( j );
  175. shards[i]->decals.SetNum( j );
  176. for ( j = 0; j < shards[i]->decals.Num(); j++ ) {
  177. shards[i]->decals[j] = new idFixedWinding;
  178. savefile->ReadWinding( *shards[i]->decals[j] );
  179. }
  180. savefile->ReadInt( j );
  181. shards[i]->neighbours.SetNum( j );
  182. for ( j = 0; j < shards[i]->neighbours.Num(); j++ ) {
  183. int index;
  184. savefile->ReadInt( index );
  185. assert(index != -1);
  186. shards[i]->neighbours[j] = shards[index];
  187. }
  188. savefile->ReadInt( j );
  189. shards[i]->edgeHasNeighbour.SetNum( j );
  190. for ( j = 0; j < shards[i]->edgeHasNeighbour.Num(); j++ ) {
  191. savefile->ReadBool( shards[i]->edgeHasNeighbour[j] );
  192. }
  193. savefile->ReadInt( shards[i]->droppedTime );
  194. savefile->ReadInt( shards[i]->islandNum );
  195. savefile->ReadBool( shards[i]->atEdge );
  196. savefile->ReadStaticObject( shards[i]->physicsObj );
  197. if ( shards[i]->droppedTime < 0 ) {
  198. shards[i]->clipModel = physicsObj.GetClipModel( i );
  199. } else {
  200. shards[i]->clipModel = shards[i]->physicsObj.GetClipModel();
  201. }
  202. }
  203. #ifdef _D3XP
  204. savefile->ReadBool( isXraySurface );
  205. #endif
  206. }
  207. /*
  208. ================
  209. idBrittleFracture::Spawn
  210. ================
  211. */
  212. void idBrittleFracture::Spawn( void ) {
  213. // get shard properties
  214. decalMaterial = declManager->FindMaterial( spawnArgs.GetString( "mtr_decal" ) );
  215. decalSize = spawnArgs.GetFloat( "decalSize", "40" );
  216. maxShardArea = spawnArgs.GetFloat( "maxShardArea", "200" );
  217. maxShardArea = idMath::ClampFloat( 100, 10000, maxShardArea );
  218. maxShatterRadius = spawnArgs.GetFloat( "maxShatterRadius", "40" );
  219. minShatterRadius = spawnArgs.GetFloat( "minShatterRadius", "10" );
  220. linearVelocityScale = spawnArgs.GetFloat( "linearVelocityScale", "0.1" );
  221. angularVelocityScale = spawnArgs.GetFloat( "angularVelocityScale", "40" );
  222. fxFracture = spawnArgs.GetString( "fx" );
  223. // get rigid body properties
  224. shardMass = spawnArgs.GetFloat( "shardMass", "20" );
  225. shardMass = idMath::ClampFloat( 0.001f, 1000.0f, shardMass );
  226. spawnArgs.GetFloat( "density", "0.1", density );
  227. density = idMath::ClampFloat( 0.001f, 1000.0f, density );
  228. spawnArgs.GetFloat( "friction", "0.4", friction );
  229. friction = idMath::ClampFloat( 0.0f, 1.0f, friction );
  230. spawnArgs.GetFloat( "bouncyness", "0.01", bouncyness );
  231. bouncyness = idMath::ClampFloat( 0.0f, 1.0f, bouncyness );
  232. disableFracture = spawnArgs.GetBool( "disableFracture", "0" );
  233. health = spawnArgs.GetInt( "health", "40" );
  234. fl.takedamage = true;
  235. // FIXME: set "bleed" so idProjectile calls AddDamageEffect
  236. spawnArgs.SetBool( "bleed", 1 );
  237. #ifdef _D3XP
  238. // check for xray surface
  239. if ( 1 ) {
  240. const idRenderModel *model = renderEntity.hModel;
  241. isXraySurface = false;
  242. for ( int i = 0; i < model->NumSurfaces(); i++ ) {
  243. const modelSurface_t *surf = model->Surface( i );
  244. if ( idStr( surf->shader->GetName() ) == "textures/smf/window_scratch" ) {
  245. isXraySurface = true;
  246. break;
  247. }
  248. }
  249. }
  250. #endif
  251. CreateFractures( renderEntity.hModel );
  252. FindNeighbours();
  253. renderEntity.hModel = renderModelManager->AllocModel();
  254. renderEntity.hModel->InitEmpty( brittleFracture_SnapshotName );
  255. renderEntity.callback = idBrittleFracture::ModelCallback;
  256. renderEntity.noShadow = true;
  257. renderEntity.noSelfShadow = true;
  258. renderEntity.noDynamicInteractions = false;
  259. }
  260. /*
  261. ================
  262. idBrittleFracture::AddShard
  263. ================
  264. */
  265. void idBrittleFracture::AddShard( idClipModel *clipModel, idFixedWinding &w ) {
  266. shard_t *shard = new shard_t;
  267. shard->clipModel = clipModel;
  268. shard->droppedTime = -1;
  269. shard->winding = w;
  270. shard->decals.Clear();
  271. shard->edgeHasNeighbour.AssureSize( w.GetNumPoints(), false );
  272. shard->neighbours.Clear();
  273. shard->atEdge = false;
  274. shards.Append( shard );
  275. }
  276. /*
  277. ================
  278. idBrittleFracture::RemoveShard
  279. ================
  280. */
  281. void idBrittleFracture::RemoveShard( int index ) {
  282. int i;
  283. delete shards[index];
  284. shards.RemoveIndex( index );
  285. physicsObj.RemoveIndex( index );
  286. for ( i = index; i < shards.Num(); i++ ) {
  287. shards[i]->clipModel->SetId( i );
  288. }
  289. }
  290. /*
  291. ================
  292. idBrittleFracture::UpdateRenderEntity
  293. ================
  294. */
  295. bool idBrittleFracture::UpdateRenderEntity( renderEntity_s *renderEntity, const renderView_t *renderView ) const {
  296. int i, j, k, n, msec, numTris, numDecalTris;
  297. float fade;
  298. dword packedColor;
  299. srfTriangles_t *tris, *decalTris;
  300. modelSurface_t surface;
  301. idDrawVert *v;
  302. idPlane plane;
  303. idMat3 tangents;
  304. // this may be triggered by a model trace or other non-view related source,
  305. // to which we should look like an empty model
  306. if ( !renderView ) {
  307. return false;
  308. }
  309. // don't regenerate it if it is current
  310. if ( lastRenderEntityUpdate == gameLocal.time || !changed ) {
  311. return false;
  312. }
  313. lastRenderEntityUpdate = gameLocal.time;
  314. changed = false;
  315. numTris = 0;
  316. numDecalTris = 0;
  317. for ( i = 0; i < shards.Num(); i++ ) {
  318. n = shards[i]->winding.GetNumPoints();
  319. if ( n > 2 ) {
  320. numTris += n - 2;
  321. }
  322. for ( k = 0; k < shards[i]->decals.Num(); k++ ) {
  323. n = shards[i]->decals[k]->GetNumPoints();
  324. if ( n > 2 ) {
  325. numDecalTris += n - 2;
  326. }
  327. }
  328. }
  329. // FIXME: re-use model surfaces
  330. renderEntity->hModel->InitEmpty( brittleFracture_SnapshotName );
  331. // allocate triangle surfaces for the fractures and decals
  332. tris = renderEntity->hModel->AllocSurfaceTriangles( numTris * 3, material->ShouldCreateBackSides() ? numTris * 6 : numTris * 3 );
  333. decalTris = renderEntity->hModel->AllocSurfaceTriangles( numDecalTris * 3, decalMaterial->ShouldCreateBackSides() ? numDecalTris * 6 : numDecalTris * 3 );
  334. for ( i = 0; i < shards.Num(); i++ ) {
  335. const idVec3 &origin = shards[i]->clipModel->GetOrigin();
  336. const idMat3 &axis = shards[i]->clipModel->GetAxis();
  337. fade = 1.0f;
  338. if ( shards[i]->droppedTime >= 0 ) {
  339. msec = gameLocal.time - shards[i]->droppedTime - SHARD_FADE_START;
  340. if ( msec > 0 ) {
  341. fade = 1.0f - (float) msec / ( SHARD_ALIVE_TIME - SHARD_FADE_START );
  342. }
  343. }
  344. packedColor = PackColor( idVec4( renderEntity->shaderParms[ SHADERPARM_RED ] * fade,
  345. renderEntity->shaderParms[ SHADERPARM_GREEN ] * fade,
  346. renderEntity->shaderParms[ SHADERPARM_BLUE ] * fade,
  347. fade ) );
  348. const idWinding &winding = shards[i]->winding;
  349. winding.GetPlane( plane );
  350. tangents = ( plane.Normal() * axis ).ToMat3();
  351. for ( j = 2; j < winding.GetNumPoints(); j++ ) {
  352. v = &tris->verts[tris->numVerts++];
  353. v->Clear();
  354. v->xyz = origin + winding[0].ToVec3() * axis;
  355. v->st[0] = winding[0].s;
  356. v->st[1] = winding[0].t;
  357. v->normal = tangents[0];
  358. v->tangents[0] = tangents[1];
  359. v->tangents[1] = tangents[2];
  360. v->SetColor( packedColor );
  361. v = &tris->verts[tris->numVerts++];
  362. v->Clear();
  363. v->xyz = origin + winding[j-1].ToVec3() * axis;
  364. v->st[0] = winding[j-1].s;
  365. v->st[1] = winding[j-1].t;
  366. v->normal = tangents[0];
  367. v->tangents[0] = tangents[1];
  368. v->tangents[1] = tangents[2];
  369. v->SetColor( packedColor );
  370. v = &tris->verts[tris->numVerts++];
  371. v->Clear();
  372. v->xyz = origin + winding[j].ToVec3() * axis;
  373. v->st[0] = winding[j].s;
  374. v->st[1] = winding[j].t;
  375. v->normal = tangents[0];
  376. v->tangents[0] = tangents[1];
  377. v->tangents[1] = tangents[2];
  378. v->SetColor( packedColor );
  379. tris->indexes[tris->numIndexes++] = tris->numVerts - 3;
  380. tris->indexes[tris->numIndexes++] = tris->numVerts - 2;
  381. tris->indexes[tris->numIndexes++] = tris->numVerts - 1;
  382. if ( material->ShouldCreateBackSides() ) {
  383. tris->indexes[tris->numIndexes++] = tris->numVerts - 2;
  384. tris->indexes[tris->numIndexes++] = tris->numVerts - 3;
  385. tris->indexes[tris->numIndexes++] = tris->numVerts - 1;
  386. }
  387. }
  388. for ( k = 0; k < shards[i]->decals.Num(); k++ ) {
  389. const idWinding &decalWinding = *shards[i]->decals[k];
  390. for ( j = 2; j < decalWinding.GetNumPoints(); j++ ) {
  391. v = &decalTris->verts[decalTris->numVerts++];
  392. v->Clear();
  393. v->xyz = origin + decalWinding[0].ToVec3() * axis;
  394. v->st[0] = decalWinding[0].s;
  395. v->st[1] = decalWinding[0].t;
  396. v->normal = tangents[0];
  397. v->tangents[0] = tangents[1];
  398. v->tangents[1] = tangents[2];
  399. v->SetColor( packedColor );
  400. v = &decalTris->verts[decalTris->numVerts++];
  401. v->Clear();
  402. v->xyz = origin + decalWinding[j-1].ToVec3() * axis;
  403. v->st[0] = decalWinding[j-1].s;
  404. v->st[1] = decalWinding[j-1].t;
  405. v->normal = tangents[0];
  406. v->tangents[0] = tangents[1];
  407. v->tangents[1] = tangents[2];
  408. v->SetColor( packedColor );
  409. v = &decalTris->verts[decalTris->numVerts++];
  410. v->Clear();
  411. v->xyz = origin + decalWinding[j].ToVec3() * axis;
  412. v->st[0] = decalWinding[j].s;
  413. v->st[1] = decalWinding[j].t;
  414. v->normal = tangents[0];
  415. v->tangents[0] = tangents[1];
  416. v->tangents[1] = tangents[2];
  417. v->SetColor( packedColor );
  418. decalTris->indexes[decalTris->numIndexes++] = decalTris->numVerts - 3;
  419. decalTris->indexes[decalTris->numIndexes++] = decalTris->numVerts - 2;
  420. decalTris->indexes[decalTris->numIndexes++] = decalTris->numVerts - 1;
  421. if ( decalMaterial->ShouldCreateBackSides() ) {
  422. decalTris->indexes[decalTris->numIndexes++] = decalTris->numVerts - 2;
  423. decalTris->indexes[decalTris->numIndexes++] = decalTris->numVerts - 3;
  424. decalTris->indexes[decalTris->numIndexes++] = decalTris->numVerts - 1;
  425. }
  426. }
  427. }
  428. }
  429. tris->tangentsCalculated = true;
  430. decalTris->tangentsCalculated = true;
  431. SIMDProcessor->MinMax( tris->bounds[0], tris->bounds[1], tris->verts, tris->numVerts );
  432. SIMDProcessor->MinMax( decalTris->bounds[0], decalTris->bounds[1], decalTris->verts, decalTris->numVerts );
  433. memset( &surface, 0, sizeof( surface ) );
  434. surface.shader = material;
  435. surface.id = 0;
  436. surface.geometry = tris;
  437. renderEntity->hModel->AddSurface( surface );
  438. memset( &surface, 0, sizeof( surface ) );
  439. surface.shader = decalMaterial;
  440. surface.id = 1;
  441. surface.geometry = decalTris;
  442. renderEntity->hModel->AddSurface( surface );
  443. return true;
  444. }
  445. /*
  446. ================
  447. idBrittleFracture::ModelCallback
  448. ================
  449. */
  450. bool idBrittleFracture::ModelCallback( renderEntity_s *renderEntity, const renderView_t *renderView ) {
  451. const idBrittleFracture *ent;
  452. ent = static_cast<idBrittleFracture *>(gameLocal.entities[ renderEntity->entityNum ]);
  453. if ( !ent ) {
  454. gameLocal.Error( "idBrittleFracture::ModelCallback: callback with NULL game entity" );
  455. }
  456. return ent->UpdateRenderEntity( renderEntity, renderView );
  457. }
  458. /*
  459. ================
  460. idBrittleFracture::Present
  461. ================
  462. */
  463. void idBrittleFracture::Present() {
  464. // don't present to the renderer if the entity hasn't changed
  465. if ( !( thinkFlags & TH_UPDATEVISUALS ) ) {
  466. return;
  467. }
  468. BecomeInactive( TH_UPDATEVISUALS );
  469. renderEntity.bounds = bounds;
  470. renderEntity.origin.Zero();
  471. renderEntity.axis.Identity();
  472. // force an update because the bounds/origin/axis may stay the same while the model changes
  473. renderEntity.forceUpdate = true;
  474. // add to refresh list
  475. if ( modelDefHandle == -1 ) {
  476. modelDefHandle = gameRenderWorld->AddEntityDef( &renderEntity );
  477. } else {
  478. gameRenderWorld->UpdateEntityDef( modelDefHandle, &renderEntity );
  479. }
  480. changed = true;
  481. }
  482. /*
  483. ================
  484. idBrittleFracture::Think
  485. ================
  486. */
  487. void idBrittleFracture::Think( void ) {
  488. int i, startTime, endTime, droppedTime;
  489. shard_t *shard;
  490. bool atRest = true, fading = false;
  491. // remove overdue shards
  492. for ( i = 0; i < shards.Num(); i++ ) {
  493. droppedTime = shards[i]->droppedTime;
  494. if ( droppedTime != -1 ) {
  495. if ( gameLocal.time - droppedTime > SHARD_ALIVE_TIME ) {
  496. RemoveShard( i );
  497. i--;
  498. }
  499. fading = true;
  500. }
  501. }
  502. // remove the entity when nothing is visible
  503. if ( !shards.Num() ) {
  504. PostEventMS( &EV_Remove, 0 );
  505. return;
  506. }
  507. if ( thinkFlags & TH_PHYSICS ) {
  508. startTime = gameLocal.previousTime;
  509. endTime = gameLocal.time;
  510. // run physics on shards
  511. for ( i = 0; i < shards.Num(); i++ ) {
  512. shard = shards[i];
  513. if ( shard->droppedTime == -1 ) {
  514. continue;
  515. }
  516. shard->physicsObj.Evaluate( endTime - startTime, endTime );
  517. if ( !shard->physicsObj.IsAtRest() ) {
  518. atRest = false;
  519. }
  520. }
  521. if ( atRest ) {
  522. BecomeInactive( TH_PHYSICS );
  523. } else {
  524. BecomeActive( TH_PHYSICS );
  525. }
  526. }
  527. if ( !atRest || bounds.IsCleared() ) {
  528. bounds.Clear();
  529. for ( i = 0; i < shards.Num(); i++ ) {
  530. bounds.AddBounds( shards[i]->clipModel->GetAbsBounds() );
  531. }
  532. }
  533. if ( fading ) {
  534. BecomeActive( TH_UPDATEVISUALS | TH_THINK );
  535. } else {
  536. BecomeInactive( TH_THINK );
  537. }
  538. RunPhysics();
  539. Present();
  540. }
  541. /*
  542. ================
  543. idBrittleFracture::ApplyImpulse
  544. ================
  545. */
  546. void idBrittleFracture::ApplyImpulse( idEntity *ent, int id, const idVec3 &point, const idVec3 &impulse ) {
  547. if ( id < 0 || id >= shards.Num() ) {
  548. return;
  549. }
  550. if ( shards[id]->droppedTime != -1 ) {
  551. shards[id]->physicsObj.ApplyImpulse( 0, point, impulse );
  552. } else if ( health <= 0 && !disableFracture ) {
  553. Shatter( point, impulse, gameLocal.time );
  554. }
  555. }
  556. /*
  557. ================
  558. idBrittleFracture::AddForce
  559. ================
  560. */
  561. void idBrittleFracture::AddForce( idEntity *ent, int id, const idVec3 &point, const idVec3 &force ) {
  562. if ( id < 0 || id >= shards.Num() ) {
  563. return;
  564. }
  565. if ( shards[id]->droppedTime != -1 ) {
  566. shards[id]->physicsObj.AddForce( 0, point, force );
  567. } else if ( health <= 0 && !disableFracture ) {
  568. Shatter( point, force, gameLocal.time );
  569. }
  570. }
  571. /*
  572. ================
  573. idBrittleFracture::ProjectDecal
  574. ================
  575. */
  576. void idBrittleFracture::ProjectDecal( const idVec3 &point, const idVec3 &dir, const int time, const char *damageDefName ) {
  577. int i, j, bits, clipBits;
  578. float a, c, s;
  579. idVec2 st[MAX_POINTS_ON_WINDING];
  580. idVec3 origin;
  581. idMat3 axis, axistemp;
  582. idPlane textureAxis[2];
  583. if ( gameLocal.isServer ) {
  584. idBitMsg msg;
  585. byte msgBuf[MAX_EVENT_PARAM_SIZE];
  586. msg.Init( msgBuf, sizeof( msgBuf ) );
  587. msg.BeginWriting();
  588. msg.WriteFloat( point[0] );
  589. msg.WriteFloat( point[1] );
  590. msg.WriteFloat( point[2] );
  591. msg.WriteFloat( dir[0] );
  592. msg.WriteFloat( dir[1] );
  593. msg.WriteFloat( dir[2] );
  594. ServerSendEvent( EVENT_PROJECT_DECAL, &msg, true, -1 );
  595. }
  596. if ( time >= gameLocal.time ) {
  597. // try to get the sound from the damage def
  598. const idDeclEntityDef *damageDef = NULL;
  599. const idSoundShader *sndShader = NULL;
  600. if ( damageDefName ) {
  601. damageDef = gameLocal.FindEntityDef( damageDefName, false );
  602. if ( damageDef ) {
  603. sndShader = declManager->FindSound( damageDef->dict.GetString( "snd_shatter", "" ) );
  604. }
  605. }
  606. if ( sndShader ) {
  607. StartSoundShader( sndShader, SND_CHANNEL_ANY, 0, false, NULL );
  608. } else {
  609. StartSound( "snd_bullethole", SND_CHANNEL_ANY, 0, false, NULL );
  610. }
  611. }
  612. a = gameLocal.random.RandomFloat() * idMath::TWO_PI;
  613. c = cos( a );
  614. s = -sin( a );
  615. axis[2] = -dir;
  616. axis[2].Normalize();
  617. axis[2].NormalVectors( axistemp[0], axistemp[1] );
  618. axis[0] = axistemp[ 0 ] * c + axistemp[ 1 ] * s;
  619. axis[1] = axistemp[ 0 ] * s + axistemp[ 1 ] * -c;
  620. textureAxis[0] = axis[0] * ( 1.0f / decalSize );
  621. textureAxis[0][3] = -( point * textureAxis[0].Normal() ) + 0.5f;
  622. textureAxis[1] = axis[1] * ( 1.0f / decalSize );
  623. textureAxis[1][3] = -( point * textureAxis[1].Normal() ) + 0.5f;
  624. for ( i = 0; i < shards.Num(); i++ ) {
  625. idFixedWinding &winding = shards[i]->winding;
  626. origin = shards[i]->clipModel->GetOrigin();
  627. axis = shards[i]->clipModel->GetAxis();
  628. float d0, d1;
  629. clipBits = -1;
  630. for ( j = 0; j < winding.GetNumPoints(); j++ ) {
  631. idVec3 p = origin + winding[j].ToVec3() * axis;
  632. st[j].x = d0 = textureAxis[0].Distance( p );
  633. st[j].y = d1 = textureAxis[1].Distance( p );
  634. bits = FLOATSIGNBITSET( d0 );
  635. d0 = 1.0f - d0;
  636. bits |= FLOATSIGNBITSET( d1 ) << 2;
  637. d1 = 1.0f - d1;
  638. bits |= FLOATSIGNBITSET( d0 ) << 1;
  639. bits |= FLOATSIGNBITSET( d1 ) << 3;
  640. clipBits &= bits;
  641. }
  642. if ( clipBits ) {
  643. continue;
  644. }
  645. idFixedWinding *decal = new idFixedWinding;
  646. shards[i]->decals.Append( decal );
  647. decal->SetNumPoints( winding.GetNumPoints() );
  648. for ( j = 0; j < winding.GetNumPoints(); j++ ) {
  649. (*decal)[j].ToVec3() = winding[j].ToVec3();
  650. (*decal)[j].s = st[j].x;
  651. (*decal)[j].t = st[j].y;
  652. }
  653. }
  654. BecomeActive( TH_UPDATEVISUALS );
  655. }
  656. /*
  657. ================
  658. idBrittleFracture::DropShard
  659. ================
  660. */
  661. void idBrittleFracture::DropShard( shard_t *shard, const idVec3 &point, const idVec3 &dir, const float impulse, const int time ) {
  662. int i, j, clipModelId;
  663. float dist, f;
  664. idVec3 dir2, origin;
  665. idMat3 axis;
  666. shard_t *neighbour;
  667. // don't display decals on dropped shards
  668. shard->decals.DeleteContents( true );
  669. // remove neighbour pointers of neighbours pointing to this shard
  670. for ( i = 0; i < shard->neighbours.Num(); i++ ) {
  671. neighbour = shard->neighbours[i];
  672. for ( j = 0; j < neighbour->neighbours.Num(); j++ ) {
  673. if ( neighbour->neighbours[j] == shard ) {
  674. neighbour->neighbours.RemoveIndex( j );
  675. break;
  676. }
  677. }
  678. }
  679. // remove neighbour pointers
  680. shard->neighbours.Clear();
  681. // remove the clip model from the static physics object
  682. clipModelId = shard->clipModel->GetId();
  683. physicsObj.SetClipModel( NULL, 1.0f, clipModelId, false );
  684. origin = shard->clipModel->GetOrigin();
  685. axis = shard->clipModel->GetAxis();
  686. // set the dropped time for fading
  687. shard->droppedTime = time;
  688. dir2 = origin - point;
  689. dist = dir2.Normalize();
  690. f = dist > maxShatterRadius ? 1.0f : idMath::Sqrt( dist - minShatterRadius ) * ( 1.0f / idMath::Sqrt( maxShatterRadius - minShatterRadius ) );
  691. // setup the physics
  692. shard->physicsObj.SetSelf( this );
  693. shard->physicsObj.SetClipModel( shard->clipModel, density );
  694. shard->physicsObj.SetMass( shardMass );
  695. shard->physicsObj.SetOrigin( origin );
  696. shard->physicsObj.SetAxis( axis );
  697. shard->physicsObj.SetBouncyness( bouncyness );
  698. shard->physicsObj.SetFriction( 0.6f, 0.6f, friction );
  699. shard->physicsObj.SetGravity( gameLocal.GetGravity() );
  700. shard->physicsObj.SetContents( CONTENTS_RENDERMODEL );
  701. shard->physicsObj.SetClipMask( MASK_SOLID | CONTENTS_MOVEABLECLIP );
  702. shard->physicsObj.ApplyImpulse( 0, origin, impulse * linearVelocityScale * dir );
  703. shard->physicsObj.SetAngularVelocity( dir.Cross( dir2 ) * ( f * angularVelocityScale ) );
  704. shard->clipModel->SetId( clipModelId );
  705. BecomeActive( TH_PHYSICS );
  706. }
  707. /*
  708. ================
  709. idBrittleFracture::Shatter
  710. ================
  711. */
  712. void idBrittleFracture::Shatter( const idVec3 &point, const idVec3 &impulse, const int time ) {
  713. int i;
  714. idVec3 dir;
  715. shard_t *shard;
  716. float m;
  717. if ( gameLocal.isServer ) {
  718. idBitMsg msg;
  719. byte msgBuf[MAX_EVENT_PARAM_SIZE];
  720. msg.Init( msgBuf, sizeof( msgBuf ) );
  721. msg.BeginWriting();
  722. msg.WriteFloat( point[0] );
  723. msg.WriteFloat( point[1] );
  724. msg.WriteFloat( point[2] );
  725. msg.WriteFloat( impulse[0] );
  726. msg.WriteFloat( impulse[1] );
  727. msg.WriteFloat( impulse[2] );
  728. ServerSendEvent( EVENT_SHATTER, &msg, true, -1 );
  729. }
  730. if ( time > ( gameLocal.time - SHARD_ALIVE_TIME ) ) {
  731. StartSound( "snd_shatter", SND_CHANNEL_ANY, 0, false, NULL );
  732. }
  733. if ( !IsBroken() ) {
  734. Break();
  735. }
  736. if ( fxFracture.Length() ) {
  737. idEntityFx::StartFx( fxFracture, &point, &GetPhysics()->GetAxis(), this, true );
  738. }
  739. dir = impulse;
  740. m = dir.Normalize();
  741. for ( i = 0; i < shards.Num(); i++ ) {
  742. shard = shards[i];
  743. if ( shard->droppedTime != -1 ) {
  744. continue;
  745. }
  746. if ( ( shard->clipModel->GetOrigin() - point ).LengthSqr() > Square( maxShatterRadius ) ) {
  747. continue;
  748. }
  749. DropShard( shard, point, dir, m, time );
  750. }
  751. DropFloatingIslands( point, impulse, time );
  752. }
  753. /*
  754. ================
  755. idBrittleFracture::DropFloatingIslands
  756. ================
  757. */
  758. void idBrittleFracture::DropFloatingIslands( const idVec3 &point, const idVec3 &impulse, const int time ) {
  759. int i, j, numIslands;
  760. int queueStart, queueEnd;
  761. shard_t *curShard, *nextShard, **queue;
  762. bool touchesEdge;
  763. idVec3 dir;
  764. dir = impulse;
  765. dir.Normalize();
  766. numIslands = 0;
  767. queue = (shard_t **) _alloca16( shards.Num() * sizeof(shard_t **) );
  768. for ( i = 0; i < shards.Num(); i++ ) {
  769. shards[i]->islandNum = 0;
  770. }
  771. for ( i = 0; i < shards.Num(); i++ ) {
  772. if ( shards[i]->droppedTime != -1 ) {
  773. continue;
  774. }
  775. if ( shards[i]->islandNum ) {
  776. continue;
  777. }
  778. queueStart = 0;
  779. queueEnd = 1;
  780. queue[0] = shards[i];
  781. shards[i]->islandNum = numIslands+1;
  782. touchesEdge = false;
  783. if ( shards[i]->atEdge ) {
  784. touchesEdge = true;
  785. }
  786. for ( curShard = queue[queueStart]; queueStart < queueEnd; curShard = queue[++queueStart] ) {
  787. for ( j = 0; j < curShard->neighbours.Num(); j++ ) {
  788. nextShard = curShard->neighbours[j];
  789. if ( nextShard->droppedTime != -1 ) {
  790. continue;
  791. }
  792. if ( nextShard->islandNum ) {
  793. continue;
  794. }
  795. queue[queueEnd++] = nextShard;
  796. nextShard->islandNum = numIslands+1;
  797. if ( nextShard->atEdge ) {
  798. touchesEdge = true;
  799. }
  800. }
  801. }
  802. numIslands++;
  803. // if the island is not connected to the world at any edges
  804. if ( !touchesEdge ) {
  805. for ( j = 0; j < queueEnd; j++ ) {
  806. DropShard( queue[j], point, dir, 0.0f, time );
  807. }
  808. }
  809. }
  810. }
  811. /*
  812. ================
  813. idBrittleFracture::Break
  814. ================
  815. */
  816. void idBrittleFracture::Break( void ) {
  817. fl.takedamage = false;
  818. physicsObj.SetContents( CONTENTS_RENDERMODEL | CONTENTS_TRIGGER );
  819. }
  820. /*
  821. ================
  822. idBrittleFracture::IsBroken
  823. ================
  824. */
  825. bool idBrittleFracture::IsBroken( void ) const {
  826. return ( fl.takedamage == false );
  827. }
  828. /*
  829. ================
  830. idBrittleFracture::Killed
  831. ================
  832. */
  833. void idBrittleFracture::Killed( idEntity *inflictor, idEntity *attacker, int damage, const idVec3 &dir, int location ) {
  834. if ( !disableFracture ) {
  835. ActivateTargets( this );
  836. Break();
  837. }
  838. }
  839. /*
  840. ================
  841. idBrittleFracture::AddDamageEffect
  842. ================
  843. */
  844. void idBrittleFracture::AddDamageEffect( const trace_t &collision, const idVec3 &velocity, const char *damageDefName ) {
  845. if ( !disableFracture ) {
  846. ProjectDecal( collision.c.point, collision.c.normal, gameLocal.time, damageDefName );
  847. }
  848. }
  849. /*
  850. ================
  851. idBrittleFracture::Fracture_r
  852. ================
  853. */
  854. void idBrittleFracture::Fracture_r( idFixedWinding &w ) {
  855. int i, j, bestPlane;
  856. float a, c, s, dist, bestDist;
  857. idVec3 origin;
  858. idPlane windingPlane, splitPlanes[2];
  859. idMat3 axis, axistemp;
  860. idFixedWinding back;
  861. idTraceModel trm;
  862. idClipModel *clipModel;
  863. while( 1 ) {
  864. origin = w.GetCenter();
  865. w.GetPlane( windingPlane );
  866. if ( w.GetArea() < maxShardArea ) {
  867. break;
  868. }
  869. // randomly create a split plane
  870. axis[2] = windingPlane.Normal();
  871. #ifdef _D3XP
  872. if ( isXraySurface ) {
  873. a = idMath::TWO_PI / 2.f;
  874. }
  875. else {
  876. a = gameLocal.random.RandomFloat() * idMath::TWO_PI;
  877. }
  878. #else
  879. a = gameLocal.random.RandomFloat() * idMath::TWO_PI;
  880. #endif
  881. c = cos( a );
  882. s = -sin( a );
  883. axis[2].NormalVectors( axistemp[0], axistemp[1] );
  884. axis[0] = axistemp[ 0 ] * c + axistemp[ 1 ] * s;
  885. axis[1] = axistemp[ 0 ] * s + axistemp[ 1 ] * -c;
  886. // get the best split plane
  887. bestDist = 0.0f;
  888. bestPlane = 0;
  889. for ( i = 0; i < 2; i++ ) {
  890. splitPlanes[i].SetNormal( axis[i] );
  891. splitPlanes[i].FitThroughPoint( origin );
  892. for ( j = 0; j < w.GetNumPoints(); j++ ) {
  893. dist = splitPlanes[i].Distance( w[j].ToVec3() );
  894. if ( dist > bestDist ) {
  895. bestDist = dist;
  896. bestPlane = i;
  897. }
  898. }
  899. }
  900. // split the winding
  901. if ( !w.Split( &back, splitPlanes[bestPlane] ) ) {
  902. break;
  903. }
  904. // recursively create shards for the back winding
  905. Fracture_r( back );
  906. }
  907. // translate the winding to it's center
  908. origin = w.GetCenter();
  909. for ( j = 0; j < w.GetNumPoints(); j++ ) {
  910. w[j].ToVec3() -= origin;
  911. }
  912. w.RemoveEqualPoints();
  913. trm.SetupPolygon( w );
  914. trm.Shrink( CM_CLIP_EPSILON );
  915. clipModel = new idClipModel( trm );
  916. physicsObj.SetClipModel( clipModel, 1.0f, shards.Num() );
  917. physicsObj.SetOrigin( GetPhysics()->GetOrigin() + origin, shards.Num() );
  918. physicsObj.SetAxis( GetPhysics()->GetAxis(), shards.Num() );
  919. AddShard( clipModel, w );
  920. }
  921. /*
  922. ================
  923. idBrittleFracture::CreateFractures
  924. ================
  925. */
  926. void idBrittleFracture::CreateFractures( const idRenderModel *renderModel ) {
  927. int i, j, k;
  928. const modelSurface_t *surf;
  929. const idDrawVert *v;
  930. idFixedWinding w;
  931. if ( !renderModel ) {
  932. return;
  933. }
  934. physicsObj.SetSelf( this );
  935. physicsObj.SetOrigin( GetPhysics()->GetOrigin(), 0 );
  936. physicsObj.SetAxis( GetPhysics()->GetAxis(), 0 );
  937. #ifdef _D3XP
  938. if ( isXraySurface ) {
  939. for ( i = 0; i < 1 /*renderModel->NumSurfaces()*/; i++ ) {
  940. surf = renderModel->Surface( i );
  941. material = surf->shader;
  942. w.Clear();
  943. int k = 0;
  944. v = &surf->geometry->verts[k];
  945. w.AddPoint( v->xyz );
  946. w[k].s = v->st[0];
  947. w[k].t = v->st[1];
  948. k = 1;
  949. v = &surf->geometry->verts[k];
  950. w.AddPoint( v->xyz );
  951. w[k].s = v->st[0];
  952. w[k].t = v->st[1];
  953. k = 3;
  954. v = &surf->geometry->verts[k];
  955. w.AddPoint( v->xyz );
  956. w[k].s = v->st[0];
  957. w[k].t = v->st[1];
  958. k = 2;
  959. v = &surf->geometry->verts[k];
  960. w.AddPoint( v->xyz );
  961. w[k].s = v->st[0];
  962. w[k].t = v->st[1];
  963. Fracture_r( w );
  964. }
  965. }
  966. else {
  967. for ( i = 0; i < 1 /*renderModel->NumSurfaces()*/; i++ ) {
  968. surf = renderModel->Surface( i );
  969. material = surf->shader;
  970. for ( j = 0; j < surf->geometry->numIndexes; j += 3 ) {
  971. w.Clear();
  972. for ( k = 0; k < 3; k++ ) {
  973. v = &surf->geometry->verts[ surf->geometry->indexes[ j + 2 - k ] ];
  974. w.AddPoint( v->xyz );
  975. w[k].s = v->st[0];
  976. w[k].t = v->st[1];
  977. }
  978. Fracture_r( w );
  979. }
  980. }
  981. }
  982. #else
  983. for ( i = 0; i < 1 /*renderModel->NumSurfaces()*/; i++ ) {
  984. surf = renderModel->Surface( i );
  985. material = surf->shader;
  986. for ( j = 0; j < surf->geometry->numIndexes; j += 3 ) {
  987. w.Clear();
  988. for ( k = 0; k < 3; k++ ) {
  989. v = &surf->geometry->verts[ surf->geometry->indexes[ j + 2 - k ] ];
  990. w.AddPoint( v->xyz );
  991. w[k].s = v->st[0];
  992. w[k].t = v->st[1];
  993. }
  994. Fracture_r( w );
  995. }
  996. }
  997. #endif
  998. physicsObj.SetContents( material->GetContentFlags() );
  999. SetPhysics( &physicsObj );
  1000. }
  1001. /*
  1002. ================
  1003. idBrittleFracture::FindNeighbours
  1004. ================
  1005. */
  1006. void idBrittleFracture::FindNeighbours( void ) {
  1007. int i, j, k, l;
  1008. idVec3 p1, p2, dir;
  1009. idMat3 axis;
  1010. idPlane plane[4];
  1011. for ( i = 0; i < shards.Num(); i++ ) {
  1012. shard_t *shard1 = shards[i];
  1013. const idWinding &w1 = shard1->winding;
  1014. const idVec3 &origin1 = shard1->clipModel->GetOrigin();
  1015. const idMat3 &axis1 = shard1->clipModel->GetAxis();
  1016. for ( k = 0; k < w1.GetNumPoints(); k++ ) {
  1017. p1 = origin1 + w1[k].ToVec3() * axis1;
  1018. p2 = origin1 + w1[(k+1)%w1.GetNumPoints()].ToVec3() * axis1;
  1019. dir = p2 - p1;
  1020. dir.Normalize();
  1021. axis = dir.ToMat3();
  1022. plane[0].SetNormal( dir );
  1023. plane[0].FitThroughPoint( p1 );
  1024. plane[1].SetNormal( -dir );
  1025. plane[1].FitThroughPoint( p2 );
  1026. plane[2].SetNormal( axis[1] );
  1027. plane[2].FitThroughPoint( p1 );
  1028. plane[3].SetNormal( axis[2] );
  1029. plane[3].FitThroughPoint( p1 );
  1030. for ( j = 0; j < shards.Num(); j++ ) {
  1031. if ( i == j ) {
  1032. continue;
  1033. }
  1034. shard_t *shard2 = shards[j];
  1035. for ( l = 0; l < shard1->neighbours.Num(); l++ ) {
  1036. if ( shard1->neighbours[l] == shard2 ) {
  1037. break;
  1038. }
  1039. }
  1040. if ( l < shard1->neighbours.Num() ) {
  1041. continue;
  1042. }
  1043. const idWinding &w2 = shard2->winding;
  1044. const idVec3 &origin2 = shard2->clipModel->GetOrigin();
  1045. const idMat3 &axis2 = shard2->clipModel->GetAxis();
  1046. for ( l = w2.GetNumPoints()-1; l >= 0; l-- ) {
  1047. p1 = origin2 + w2[l].ToVec3() * axis2;
  1048. p2 = origin2 + w2[(l-1+w2.GetNumPoints())%w2.GetNumPoints()].ToVec3() * axis2;
  1049. if ( plane[0].Side( p2, 0.1f ) == SIDE_FRONT && plane[1].Side( p1, 0.1f ) == SIDE_FRONT ) {
  1050. if ( plane[2].Side( p1, 0.1f ) == SIDE_ON && plane[3].Side( p1, 0.1f ) == SIDE_ON ) {
  1051. if ( plane[2].Side( p2, 0.1f ) == SIDE_ON && plane[3].Side( p2, 0.1f ) == SIDE_ON ) {
  1052. shard1->neighbours.Append( shard2 );
  1053. shard1->edgeHasNeighbour[k] = true;
  1054. shard2->neighbours.Append( shard1 );
  1055. shard2->edgeHasNeighbour[(l-1+w2.GetNumPoints())%w2.GetNumPoints()] = true;
  1056. break;
  1057. }
  1058. }
  1059. }
  1060. }
  1061. }
  1062. }
  1063. for ( k = 0; k < w1.GetNumPoints(); k++ ) {
  1064. if ( !shard1->edgeHasNeighbour[k] ) {
  1065. break;
  1066. }
  1067. }
  1068. if ( k < w1.GetNumPoints() ) {
  1069. shard1->atEdge = true;
  1070. } else {
  1071. shard1->atEdge = false;
  1072. }
  1073. }
  1074. }
  1075. /*
  1076. ================
  1077. idBrittleFracture::Event_Activate
  1078. ================
  1079. */
  1080. void idBrittleFracture::Event_Activate( idEntity *activator ) {
  1081. disableFracture = false;
  1082. if ( health <= 0 ) {
  1083. Break();
  1084. }
  1085. }
  1086. /*
  1087. ================
  1088. idBrittleFracture::Event_Touch
  1089. ================
  1090. */
  1091. void idBrittleFracture::Event_Touch( idEntity *other, trace_t *trace ) {
  1092. idVec3 point, impulse;
  1093. if ( !IsBroken() ) {
  1094. return;
  1095. }
  1096. if ( trace->c.id < 0 || trace->c.id >= shards.Num() ) {
  1097. return;
  1098. }
  1099. point = shards[trace->c.id]->clipModel->GetOrigin();
  1100. impulse = other->GetPhysics()->GetLinearVelocity() * other->GetPhysics()->GetMass();
  1101. Shatter( point, impulse, gameLocal.time );
  1102. }
  1103. /*
  1104. ================
  1105. idBrittleFracture::ClientPredictionThink
  1106. ================
  1107. */
  1108. void idBrittleFracture::ClientPredictionThink( void ) {
  1109. // only think forward because the state is not synced through snapshots
  1110. if ( !gameLocal.isNewFrame ) {
  1111. return;
  1112. }
  1113. Think();
  1114. }
  1115. /*
  1116. ================
  1117. idBrittleFracture::ClientReceiveEvent
  1118. ================
  1119. */
  1120. bool idBrittleFracture::ClientReceiveEvent( int event, int time, const idBitMsg &msg ) {
  1121. idVec3 point, dir;
  1122. switch( event ) {
  1123. case EVENT_PROJECT_DECAL: {
  1124. point[0] = msg.ReadFloat();
  1125. point[1] = msg.ReadFloat();
  1126. point[2] = msg.ReadFloat();
  1127. dir[0] = msg.ReadFloat();
  1128. dir[1] = msg.ReadFloat();
  1129. dir[2] = msg.ReadFloat();
  1130. ProjectDecal( point, dir, time, NULL );
  1131. return true;
  1132. }
  1133. case EVENT_SHATTER: {
  1134. point[0] = msg.ReadFloat();
  1135. point[1] = msg.ReadFloat();
  1136. point[2] = msg.ReadFloat();
  1137. dir[0] = msg.ReadFloat();
  1138. dir[1] = msg.ReadFloat();
  1139. dir[2] = msg.ReadFloat();
  1140. Shatter( point, dir, time );
  1141. return true;
  1142. }
  1143. default: {
  1144. return idEntity::ClientReceiveEvent( event, time, msg );
  1145. }
  1146. }
  1147. return false;
  1148. }