crater.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. //---------------------------------------------------------------------
  2. //
  3. // crater.cpp -- Crater/footprint manager for MechCommander 2
  4. //
  5. //---------------------------------------------------------------------------//
  6. // Copyright (C) Microsoft Corporation. All rights reserved. //
  7. //===========================================================================//
  8. //---------------------------------------------------------------------
  9. // Include Files
  10. #ifndef CRATER_H
  11. #include "crater.h"
  12. #endif
  13. #ifndef CAMERA_H
  14. #include "camera.h"
  15. #endif
  16. #ifndef TERRAIN_H
  17. #include "terrain.h"
  18. #endif
  19. #ifndef CIDENT_H
  20. #include "cident.h"
  21. #endif
  22. #ifndef PATHS_H
  23. #include "paths.h"
  24. #endif
  25. #ifndef CEVFX_H
  26. #include "cevfx.h"
  27. #endif
  28. #ifndef TXMMGR_H
  29. #include "txmmgr.h"
  30. #endif
  31. //---------------------------------------------------------------------
  32. // Static Globals
  33. CraterManagerPtr craterManager = NULL;
  34. extern bool useFog;
  35. extern bool drawOldWay;
  36. extern bool useNonWeaponEffects;
  37. #define MAX_CRATER_TEXTURES 2
  38. #define MAX_FOOTPRINTS 64
  39. //---------------------------------------------------------------------
  40. float craterUVTable [136] =
  41. {
  42. 0.000, 0.000, //Anubis
  43. 0.125, 0.000,
  44. 0.250, 0.000, //Atlas
  45. 0.375, 0.000,
  46. 0.500, 0.000, //BloodAsp
  47. 0.625, 0.000,
  48. 0.750, 0.000, //Bushwacker
  49. 0.875, 0.000,
  50. 0.000, 0.125, //Catapult
  51. 0.125, 0.125,
  52. 0.250, 0.125, //Cougar
  53. 0.375, 0.125,
  54. 0.500, 0.125, //Cyclops
  55. 0.625, 0.125,
  56. 0.750, 0.125, //Enfield
  57. 0.875, 0.125,
  58. 0.000, 0.250, //Flea (fireant)
  59. 0.125, 0.250,
  60. 0.250, 0.250, //highlander
  61. 0.375, 0.250,
  62. 0.500, 0.250, //hollander
  63. 0.625, 0.250,
  64. 0.750, 0.250, //hunchback
  65. 0.875, 0.250,
  66. 0.000, 0.375, //jaegermech
  67. 0.125, 0.375,
  68. 0.250, 0.375, //laohu
  69. 0.375, 0.375,
  70. 0.500, 0.375, //madcat
  71. 0.625, 0.375,
  72. 0.750, 0.375, //menshen
  73. 0.875, 0.375,
  74. 0.000, 0.500, //Raven
  75. 0.125, 0.500,
  76. 0.250, 0.500, //Ryoken
  77. 0.375, 0.500,
  78. 0.500, 0.500, //Shadowcat
  79. 0.625, 0.500,
  80. 0.750, 0.500, //Shayu
  81. 0.875, 0.500,
  82. 0.000, 0.625, //Shootist
  83. 0.125, 0.625,
  84. 0.250, 0.625, //Starslayer
  85. 0.375, 0.625,
  86. 0.500, 0.625, //thor
  87. 0.625, 0.625,
  88. 0.750, 0.625, //Uller
  89. 0.875, 0.625,
  90. 0.000, 0.750, //Urbanmech
  91. 0.125, 0.750,
  92. 0.250, 0.750, //Vulture
  93. 0.375, 0.750,
  94. 0.500, 0.750, //Werewolf (razorback)
  95. 0.625, 0.750,
  96. 0.750, 0.750, //Wolfhound
  97. 0.875, 0.750,
  98. 0.000, 0.875, //Zeus
  99. 0.125, 0.875,
  100. 0.0, 0.0, //CRATER_1,
  101. 0.50, 0.0, //CRATER_2,
  102. 0.0,0.50, //CRATER_3,
  103. 0.50,0.50 //CRATER_4,
  104. };
  105. //---------------------------------------------------------------------
  106. // class CraterManager
  107. long CraterManager::init (long numCraters, unsigned long craterTypeSize, char *craterFileName)
  108. {
  109. init();
  110. //-----------------------------------------------------
  111. // Allocate Heap to store crater Info
  112. maxCraters = numCraters;
  113. craterPosHeapSize = numCraters * (sizeof(CraterData) + allocatedBlockSize);
  114. //-----------------------------------------------------
  115. // create Heaps
  116. craterPosHeap = new HeapManager;
  117. gosASSERT(craterPosHeap != NULL);
  118. long result = craterPosHeap->createHeap(craterPosHeapSize);
  119. gosASSERT(result == NO_ERR);
  120. result = craterPosHeap->commitHeap(craterPosHeapSize);
  121. gosASSERT(result == NO_ERR);
  122. numCraterTextures = MAX_CRATER_TEXTURES;
  123. //---------------------------------------------------------
  124. // Setup Crater Texture handles
  125. craterTextureHandles = (DWORD *)malloc(sizeof(DWORD) * numCraterTextures);
  126. memset((MemoryPtr)craterTextureHandles,0xff,sizeof(DWORD) * numCraterTextures);
  127. craterTextureIndices = (DWORD *)malloc(sizeof(DWORD) * numCraterTextures);
  128. memset((MemoryPtr)craterTextureIndices,0xff,sizeof(DWORD) * numCraterTextures);
  129. //-----------------------------------------------------
  130. // Preload all of the craters for the mission.
  131. // This should just be one texture with all of the craters on it
  132. // and a generic set of UVs to mark each one.
  133. for (long i=0;i<numCraterTextures;i++)
  134. {
  135. char craterName[1024];
  136. sprintf(craterName,"defaults\\feet%04d",i);
  137. FullPathFileName craterPath;
  138. craterPath.init(texturePath,craterName,".tga");
  139. if (!i)
  140. craterTextureIndices[i] = mcTextureManager->loadTexture(craterPath, gos_Texture_Keyed, gosHint_DisableMipmap);
  141. else
  142. craterTextureIndices[i] = mcTextureManager->loadTexture(craterPath, gos_Texture_Alpha, gosHint_DisableMipmap);
  143. }
  144. //-----------------------------------------------------
  145. // Setup pointer and initial values
  146. craterList = (CraterDataPtr)craterPosHeap->getHeapPtr();
  147. memset(craterPosHeap->getHeapPtr(),-1,craterPosHeapSize);
  148. return(NO_ERR);
  149. }
  150. //---------------------------------------------------------------------
  151. void CraterManager::destroy (void)
  152. {
  153. //---------------------------------------------
  154. // Close file and whack it.
  155. if (craterFile)
  156. craterFile->close();
  157. delete craterFile;
  158. craterFile = NULL;
  159. //----------------------------------------------
  160. // Whack the Happy Crater Heaps
  161. delete craterShpHeap;
  162. craterShpHeap = NULL;
  163. delete craterPosHeap;
  164. craterPosHeap = NULL;
  165. craterPosHeapSize = craterShpHeapSize = 0;
  166. craterList = NULL;
  167. currentCrater = 0;
  168. free(craterTextureHandles);
  169. craterTextureHandles = NULL;
  170. free(craterTextureIndices);
  171. craterTextureIndices = NULL;
  172. }
  173. //---------------------------------------------------------------------
  174. long CraterManager::addCrater (long craterType, Stuff::Vector3D &position, float rotation)
  175. {
  176. if (!useNonWeaponEffects)
  177. return NO_ERR;
  178. //----------------------------------------------------------------
  179. bool drawMe = TRUE;
  180. if (drawMe)
  181. {
  182. craterList[currentCrater].craterShapeId = craterType;
  183. //-----------------------------------------------------------
  184. // Craters and foot prints are ON the terrain now.
  185. // Use Elevation Data to move them around.
  186. // Craters are always 32x32
  187. // Footprints and always 16x16
  188. // Rotation of 0 is south.
  189. float size = 16.0;
  190. if (craterList[currentCrater].craterShapeId > MAX_FOOTPRINTS)
  191. {
  192. size = 32.0;
  193. }
  194. craterList[currentCrater].position[0].x = -size;
  195. craterList[currentCrater].position[0].y = -size;
  196. craterList[currentCrater].position[0].z = 0.0f;
  197. craterList[currentCrater].position[1].x = size;
  198. craterList[currentCrater].position[1].y = -size;
  199. craterList[currentCrater].position[1].z = 0.0f;
  200. craterList[currentCrater].position[2].x = size;
  201. craterList[currentCrater].position[2].y = size;
  202. craterList[currentCrater].position[2].z = 0.0f;
  203. craterList[currentCrater].position[3].x = -size;
  204. craterList[currentCrater].position[3].y = size;
  205. craterList[currentCrater].position[3].z = 0.0f;
  206. float cRotation = rotation;
  207. OppRotate(craterList[currentCrater].position[0],cRotation);
  208. OppRotate(craterList[currentCrater].position[1],cRotation);
  209. OppRotate(craterList[currentCrater].position[2],cRotation);
  210. OppRotate(craterList[currentCrater].position[3],cRotation);
  211. craterList[currentCrater].position[0].Add(craterList[currentCrater].position[0],position);
  212. craterList[currentCrater].position[1].Add(craterList[currentCrater].position[1],position);
  213. craterList[currentCrater].position[2].Add(craterList[currentCrater].position[2],position);
  214. craterList[currentCrater].position[3].Add(craterList[currentCrater].position[3],position);
  215. craterList[currentCrater].position[0].z = land->getTerrainElevation(craterList[currentCrater].position[0]);
  216. craterList[currentCrater].position[1].z = land->getTerrainElevation(craterList[currentCrater].position[1]);
  217. craterList[currentCrater].position[2].z = land->getTerrainElevation(craterList[currentCrater].position[2]);
  218. craterList[currentCrater].position[3].z = land->getTerrainElevation(craterList[currentCrater].position[3]);
  219. currentCrater++;
  220. if (currentCrater == maxCraters)
  221. {
  222. currentCrater = 0;
  223. }
  224. }
  225. return(NO_ERR);
  226. }
  227. //---------------------------------------------------------------------
  228. long CraterManager::update (void)
  229. {
  230. if (!useNonWeaponEffects)
  231. return NO_ERR;
  232. //---------------------------------------------------------------
  233. // Run through all craters in list and do geometry during update
  234. CraterDataPtr currentCrater = craterList;
  235. for (long i=0;i<(long)maxCraters;i++,currentCrater++)
  236. {
  237. if (currentCrater->craterShapeId != -1)
  238. {
  239. if (currentCrater->craterShapeId > TURKINA_FOOTPRINT) //We are standard crater.
  240. {
  241. craterTextureHandles[0] = mcTextureManager->get_gosTextureHandle(craterTextureIndices[0]);
  242. mcTextureManager->addTriangle(craterTextureIndices[0],MC2_ISCRATERS | MC2_DRAWALPHA | MC2_ISTERRAIN);
  243. mcTextureManager->addTriangle(craterTextureIndices[0],MC2_ISCRATERS | MC2_DRAWALPHA | MC2_ISTERRAIN);
  244. }
  245. else //We are a footprint.
  246. {
  247. craterTextureHandles[1] = mcTextureManager->get_gosTextureHandle(craterTextureIndices[1]);
  248. mcTextureManager->addTriangle(craterTextureIndices[1],MC2_ISCRATERS | MC2_DRAWALPHA);
  249. mcTextureManager->addTriangle(craterTextureIndices[1],MC2_ISCRATERS | MC2_DRAWALPHA);
  250. }
  251. }
  252. }
  253. return (TRUE);
  254. }
  255. //---------------------------------------------------------------------
  256. void CraterManager::render (void)
  257. {
  258. if (!useNonWeaponEffects)
  259. return;
  260. //-----------------------------------------------------
  261. // Run through all craters in list and render anything
  262. // which not clipped or an invalid_crater
  263. CraterDataPtr currCrater = craterList;
  264. for (long i=0;i<(long)maxCraters;i++,currCrater++)
  265. {
  266. if (currCrater->craterShapeId != -1)
  267. {
  268. long handleOffset = 1;
  269. float uvAdd = 0.125;
  270. if (currCrater->craterShapeId >= MAX_FOOTPRINTS)
  271. {
  272. handleOffset = 0;
  273. uvAdd = 0.50;
  274. }
  275. bool onScreen1 = eye->projectZ(currCrater->position[0],currCrater->screenPos[0]);
  276. bool onScreen2 = eye->projectZ(currCrater->position[1],currCrater->screenPos[1]);
  277. bool onScreen3 = eye->projectZ(currCrater->position[2],currCrater->screenPos[2]);
  278. bool onScreen4 = eye->projectZ(currCrater->position[3],currCrater->screenPos[3]);
  279. //--------------------------------------------------
  280. // First, if we are using perspective, figure out
  281. // if object too far from camera. Far Clip Plane.
  282. float hazeFactor = 0.0f;
  283. if (eye->usePerspective)
  284. {
  285. Stuff::Point3D Distance;
  286. Stuff::Point3D objPosition;
  287. Stuff::Point3D eyePosition(eye->getCameraOrigin());
  288. objPosition.x = -currCrater->position[0].x;
  289. objPosition.y = currCrater->position[0].z;
  290. objPosition.z = currCrater->position[0].y;
  291. Distance.Subtract(objPosition,eyePosition);
  292. float eyeDistance = Distance.GetApproximateLength();
  293. if (eyeDistance > Camera::MaxClipDistance)
  294. {
  295. onScreen1 = false;
  296. onScreen2 = false;
  297. onScreen3 = false;
  298. onScreen4 = false;
  299. }
  300. else if (eyeDistance > Camera::MinHazeDistance)
  301. {
  302. hazeFactor = (eyeDistance - Camera::MinHazeDistance) * Camera::DistanceFactor;
  303. }
  304. else
  305. {
  306. hazeFactor = 0.0f;
  307. }
  308. }
  309. //----------------
  310. // Check clipping
  311. if (onScreen1 || onScreen2 || onScreen3 || onScreen4)
  312. {
  313. DWORD lightRGB = 0xffffffff;
  314. DWORD specR = 0, specB = 0, specG = 0;
  315. unsigned char lightr = 0xff,lightg = 0xff,lightb = 0xff;
  316. lightr = eye->ambientRed;
  317. lightg = eye->ambientGreen;
  318. lightb = eye->ambientBlue;
  319. lightRGB = lightb + (lightr<<16) + (lightg << 8) + (0xff << 24);
  320. if (Terrain::terrainTextures2)
  321. {
  322. if (TerrainQuad::rainLightLevel < 1.0f)
  323. {
  324. lightr = (float)lightr * TerrainQuad::rainLightLevel;
  325. lightb = (float)lightb * TerrainQuad::rainLightLevel;
  326. lightg = (float)lightg * TerrainQuad::rainLightLevel;
  327. }
  328. if (TerrainQuad::lighteningLevel > 0x0)
  329. {
  330. specR = specG = specB = TerrainQuad::lighteningLevel;
  331. }
  332. lightRGB = lightb + (lightr<<16) + (lightg << 8) + (0xff << 24);
  333. }
  334. DWORD fogRGB = (0xff<<24) + (specR<<16) + (specG<<8) + specB;
  335. if (useFog)
  336. {
  337. DWORD fogValue = 0xff;
  338. float fogStart = eye->fogStart;
  339. float fogFull = eye->fogFull;
  340. if (currCrater->position[0].z < fogStart)
  341. {
  342. float fogFactor = fogStart - currCrater->position[0].z;
  343. if (fogFactor < 0.0)
  344. fogRGB = (0xff<<24) + (specR<<16) + (specG<<8) + specB;
  345. else
  346. {
  347. fogFactor /= (fogStart - fogFull);
  348. if (fogFactor <= 1.0)
  349. {
  350. fogFactor *= fogFactor;
  351. fogFactor = 1.0 - fogFactor;
  352. fogFactor *= 256.0;
  353. }
  354. else
  355. {
  356. fogFactor = 256.0;
  357. }
  358. unsigned char fogResult = fogFactor;
  359. fogValue = fogFactor;
  360. fogRGB = (fogResult << 24) + (specR<<16) + (specG<<8) + specB;
  361. }
  362. }
  363. else
  364. {
  365. fogRGB = (0xff<<24) + (specR<<16) + (specG<<8) + specB;
  366. }
  367. if (hazeFactor != 0.0f)
  368. {
  369. float fogFactor = 1.0 - hazeFactor;
  370. DWORD distFog = float2long(fogFactor * 255.0f);
  371. if (distFog < fogValue)
  372. fogValue = distFog;
  373. fogRGB = (fogValue << 24) + (specR << 16) + (specG << 8) + (specB);
  374. }
  375. }
  376. if (drawOldWay)
  377. {
  378. //------------------------------------
  379. // Replace with Polygon Quad Elements
  380. gos_VERTEX gVertex[4];
  381. gVertex[0].x = currCrater->screenPos[0].x;
  382. gVertex[0].y = currCrater->screenPos[0].y;
  383. gVertex[0].z = currCrater->screenPos[0].z;
  384. gVertex[0].rhw = currCrater->screenPos[0].w;
  385. gVertex[0].u = craterUVTable[(currCrater->craterShapeId*2)];
  386. gVertex[0].v = craterUVTable[(currCrater->craterShapeId*2)+1];
  387. gVertex[0].argb = lightRGB;
  388. gVertex[0].frgb = fogRGB;
  389. gVertex[1].x = currCrater->screenPos[1].x;
  390. gVertex[1].y = currCrater->screenPos[1].y;
  391. gVertex[1].z = currCrater->screenPos[1].z;
  392. gVertex[1].rhw = currCrater->screenPos[1].w;
  393. gVertex[1].u = gVertex[0].u + uvAdd;
  394. gVertex[1].v = gVertex[0].v;
  395. gVertex[1].argb = lightRGB;
  396. gVertex[1].frgb = fogRGB;
  397. gVertex[2].x = currCrater->screenPos[2].x;
  398. gVertex[2].y = currCrater->screenPos[2].y;
  399. gVertex[2].z = currCrater->screenPos[2].z;
  400. gVertex[2].rhw = currCrater->screenPos[2].w;
  401. gVertex[2].u = gVertex[1].u;
  402. gVertex[2].v = gVertex[0].v + uvAdd;
  403. gVertex[2].argb = lightRGB;
  404. gVertex[2].frgb = fogRGB;
  405. gVertex[3].x = currCrater->screenPos[3].x;
  406. gVertex[3].y = currCrater->screenPos[3].y;
  407. gVertex[3].z = currCrater->screenPos[3].z;
  408. gVertex[3].rhw = currCrater->screenPos[3].w;
  409. gVertex[3].u = gVertex[0].u;
  410. gVertex[3].v = gVertex[2].v;
  411. gVertex[3].argb = lightRGB;
  412. gVertex[3].frgb = fogRGB;
  413. TexturedPolygonQuadElement element;
  414. element.init(gVertex,craterTextureHandles[handleOffset],false,false);
  415. //-----------------------------------------------------
  416. // FOG time. Set Render state to FOG on!
  417. if (useFog)
  418. {
  419. DWORD fogColor = eye->fogColor;
  420. gos_SetRenderState( gos_State_Fog, (int)&fogColor);
  421. }
  422. else
  423. {
  424. gos_SetRenderState( gos_State_Fog, 0);
  425. }
  426. element.draw();
  427. gos_SetRenderState( gos_State_Fog, 0); //ALWAYS SHUT FOG OFF WHEN DONE!
  428. }
  429. else
  430. {
  431. //------------------------------------
  432. gos_VERTEX gVertex[3];
  433. gos_VERTEX sVertex[3];
  434. gVertex[0].x = sVertex[0].x = currCrater->screenPos[0].x;
  435. gVertex[0].y = sVertex[0].y = currCrater->screenPos[0].y;
  436. gVertex[0].z = sVertex[0].z = currCrater->screenPos[0].z;
  437. gVertex[0].rhw = sVertex[0].rhw = currCrater->screenPos[0].w;
  438. gVertex[0].u = sVertex[0].u = craterUVTable[(currCrater->craterShapeId*2)];
  439. gVertex[0].v = sVertex[0].v = craterUVTable[(currCrater->craterShapeId*2)+1];
  440. gVertex[0].argb = sVertex[0].argb = lightRGB;
  441. gVertex[0].frgb = sVertex[0].frgb = fogRGB;
  442. gVertex[1].x = currCrater->screenPos[1].x;
  443. gVertex[1].y = currCrater->screenPos[1].y;
  444. gVertex[1].z = currCrater->screenPos[1].z;
  445. gVertex[1].rhw = currCrater->screenPos[1].w;
  446. gVertex[1].u = gVertex[0].u + uvAdd;
  447. gVertex[1].v = gVertex[0].v;
  448. gVertex[1].argb = lightRGB;
  449. gVertex[1].frgb = fogRGB;
  450. gVertex[2].x = sVertex[1].x = currCrater->screenPos[2].x;
  451. gVertex[2].y = sVertex[1].y = currCrater->screenPos[2].y;
  452. gVertex[2].z = sVertex[1].z = currCrater->screenPos[2].z;
  453. gVertex[2].rhw = sVertex[1].rhw = currCrater->screenPos[2].w;
  454. gVertex[2].u = sVertex[1].u = gVertex[1].u;
  455. gVertex[2].v = sVertex[1].v = gVertex[0].v + uvAdd;
  456. gVertex[2].argb = sVertex[1].argb = lightRGB;
  457. gVertex[2].frgb = sVertex[1].frgb = fogRGB;
  458. sVertex[2].x = currCrater->screenPos[3].x;
  459. sVertex[2].y = currCrater->screenPos[3].y;
  460. sVertex[2].z = currCrater->screenPos[3].z;
  461. sVertex[2].rhw = currCrater->screenPos[3].w;
  462. sVertex[2].u = gVertex[0].u;
  463. sVertex[2].v = gVertex[2].v;
  464. sVertex[2].argb = lightRGB;
  465. sVertex[2].frgb = fogRGB;
  466. if (currCrater->craterShapeId > TURKINA_FOOTPRINT) //We are standard crater.
  467. {
  468. mcTextureManager->addVertices(craterTextureIndices[handleOffset],gVertex,MC2_ISCRATERS | MC2_DRAWALPHA | MC2_ISTERRAIN);
  469. mcTextureManager->addVertices(craterTextureIndices[handleOffset],sVertex,MC2_ISCRATERS | MC2_DRAWALPHA | MC2_ISTERRAIN);
  470. }
  471. else //We are a footprint
  472. {
  473. mcTextureManager->addVertices(craterTextureIndices[handleOffset],gVertex,MC2_ISCRATERS | MC2_DRAWALPHA);
  474. mcTextureManager->addVertices(craterTextureIndices[handleOffset],sVertex,MC2_ISCRATERS | MC2_DRAWALPHA);
  475. }
  476. }
  477. }
  478. }
  479. }
  480. }
  481. //---------------------------------------------------------------------