Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Game.cpp 54KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720
  1. /*!
  2. * \file src/Game.cpp
  3. * \brief Game abstraction
  4. *
  5. * \author xythobuz
  6. */
  7. #ifdef __APPLE__
  8. #include <OpenGL/gl.h>
  9. #else
  10. #include <GL/gl.h>
  11. #endif
  12. #include <algorithm>
  13. #include <map>
  14. #include <cstdlib>
  15. #include "main.h"
  16. #include "Console.h"
  17. #include "Game.h"
  18. #include "utils/strings.h"
  19. #include "games/TombRaider1.h"
  20. // Old Code compatibility
  21. #define TexelScale 256.0f
  22. #ifndef EXPERIMENTAL_UNFIFIED_ROOM_GEOMETERY
  23. #define TextureLimit 24
  24. #endif
  25. #ifdef EXPERIMENTAL
  26. std::vector<unsigned int> gColorTextureHACK;
  27. #endif
  28. #ifdef MULTITEXTURE
  29. std::map<int, int> gMapTex2Bump;
  30. #endif
  31. Game::Game() {
  32. mLoaded = false;
  33. mName = NULL;
  34. mLara = NULL;
  35. mTextureStart = 0;
  36. mTextureLevelOffset = 0;
  37. mTextureOffset = 0;
  38. }
  39. Game::~Game() {
  40. destroy();
  41. }
  42. int Game::initialize() {
  43. // Enable Renderer
  44. getRender().initTextures(getOpenRaider().mDataDir, &mTextureStart, &mTextureLevelOffset);
  45. getRender().setMode(Render::modeLoadScreen);
  46. // Enable World Hopping
  47. getWorld().setFlag(World::fEnableHopping);
  48. return 0;
  49. }
  50. void Game::destroy() {
  51. if (mName)
  52. delete [] mName;
  53. mLoaded = false;
  54. getRender().setMode(Render::modeDisabled);
  55. getWorld().destroy();
  56. getRender().ClearWorld();
  57. getSound().clear(); // Remove all previously loaded sounds
  58. }
  59. bool Game::isLoaded() {
  60. return mLoaded;
  61. }
  62. int Game::loadLevel(const char *level) {
  63. if (mLoaded)
  64. destroy();
  65. mName = bufferString("%s", level);
  66. // Load the level pak into TombRaider
  67. getConsole().print("Loading %s", mName);
  68. int error = mTombRaider.Load(mName);
  69. if (error != 0) {
  70. return error;
  71. }
  72. // If required, load the external sound effect file MAIN.SFX into TombRaider
  73. if ((mTombRaider.getEngine() == TR_VERSION_2) || (mTombRaider.getEngine() == TR_VERSION_3)) {
  74. char *tmp = bufferString("%sMAIN.SFX", level); // Ensure theres enough space
  75. size_t length = strlen(tmp);
  76. size_t dir = 0;
  77. for (int i = length - 1; i >= 0; i--) {
  78. if ((tmp[i] == '/') || (tmp[i] == '\\')) {
  79. dir = i + 1; // Find where the filename (bla.tr2) starts
  80. break;
  81. }
  82. }
  83. strcpy(tmp + dir, "MAIN.SFX"); // overwrite the name itself with MAIN.SFX
  84. tmp[dir + 8] = '\0';
  85. error = mTombRaider.loadSFX(tmp);
  86. if (error != 0) {
  87. getConsole().print("Could not load %s", tmp);
  88. }
  89. delete [] tmp;
  90. }
  91. // Process data
  92. processTextures();
  93. processRooms();
  94. processModels();
  95. processSprites();
  96. processMoveables();
  97. processPakSounds();
  98. // Free pak file
  99. mTombRaider.reset();
  100. // Check if the level contains Lara
  101. if (mLara == NULL) {
  102. getConsole().print("Can't find Lara entity in level pak!");
  103. return -1;
  104. }
  105. mLoaded = true;
  106. getRender().setMode(Render::modeVertexLight);
  107. return 0;
  108. }
  109. void Game::handleAction(ActionEvents action, bool isFinished) {
  110. if (mLoaded) {
  111. if (action == forwardAction) {
  112. getWorld().moveEntity(mLara, 'f');
  113. } else if (action == backwardAction) {
  114. getWorld().moveEntity(mLara, 'b');
  115. } else if (action == leftAction) {
  116. getWorld().moveEntity(mLara, 'l');
  117. } else if (action == rightAction) {
  118. getWorld().moveEntity(mLara, 'r');
  119. }
  120. }
  121. }
  122. void Game::handleMouseMotion(int xrel, int yrel) {
  123. if (mLoaded) {
  124. // Move Camera on X Axis
  125. if (xrel > 0)
  126. while (xrel-- > 0)
  127. getCamera().command(CAMERA_ROTATE_RIGHT);
  128. else if (xrel < 0)
  129. while (xrel++ < 0)
  130. getCamera().command(CAMERA_ROTATE_LEFT);
  131. // Move Camera on Y Axis
  132. if (yrel > 0)
  133. while (yrel-- > 0)
  134. getCamera().command(CAMERA_ROTATE_UP);
  135. else if (yrel < 0)
  136. while (yrel++ < 0)
  137. getCamera().command(CAMERA_ROTATE_DOWN);
  138. // Fix Laras rotation
  139. mLara->angles[1] = getCamera().getRadianYaw();
  140. mLara->angles[2] = getCamera().getRadianPitch();
  141. }
  142. }
  143. void Game::processPakSounds()
  144. {
  145. unsigned char *riff;
  146. unsigned int riffSz;
  147. //tr2_sound_source_t *sound;
  148. //tr2_sound_details_t *detail;
  149. //float pos[3];
  150. unsigned int i;
  151. int id;
  152. /* detail
  153. short sample;
  154. short volume;
  155. short sound_range;
  156. short flags; // bits 8-15: priority?, 2-7: number of sound samples
  157. // in this group, bits 0-1: channel number
  158. */
  159. printf("Processing pak sound files: ");
  160. for (i = 0; i < mTombRaider.getSoundSamplesCount(); ++i)
  161. {
  162. mTombRaider.getSoundSample(i, &riffSz, &riff);
  163. getSound().addWave(riff, riffSz, &id, Sound::SoundFlagsNone);
  164. //if (((i + 1) == TR_SOUND_F_PISTOL) && (id > 0))
  165. //{
  166. //m_testSFX = id;
  167. //}
  168. delete [] riff;
  169. // sound[i].sound_id; // internal sound index
  170. // sound[i].flags; // 0x40, 0x80, or 0xc0
  171. //pos[0] = sound[i].x;
  172. //pos[1] = sound[i].y;
  173. //pos[2] = sound[i].z;
  174. //getSound().SourceAt(id, pos);
  175. //printf(".");
  176. //fflush(stdout);
  177. }
  178. printf("Done! Found %u files.\n", mTombRaider.getSoundSamplesCount());
  179. }
  180. void Game::processTextures()
  181. {
  182. unsigned char *image;
  183. unsigned char *bumpmap;
  184. int i;
  185. printf("Processing TR textures: ");
  186. //if ( mTombRaider.getNumBumpMaps())
  187. // gBumpMapStart = mTombRaider.NumTextures();
  188. for (i = 0; i < mTombRaider.NumTextures(); ++i)
  189. {
  190. mTombRaider.Texture(i, &image, &bumpmap);
  191. // Overwrite any previous level textures on load
  192. getRender().loadTexture(image, 256, 256, mTextureLevelOffset + i);
  193. #ifdef MULTITEXTURE
  194. gMapTex2Bump[mTextureLevelOffset + i] = -1;
  195. #endif
  196. if (bumpmap)
  197. {
  198. #ifdef MULTITEXTURE
  199. gMapTex2Bump[mTextureLevelOffset + i] = mTextureLevelOffset + i +
  200. mTombRaider.NumTextures();
  201. #endif
  202. getRender().loadTexture(bumpmap, 256, 256, mTextureLevelOffset + i +
  203. mTombRaider.NumTextures());
  204. }
  205. if (image)
  206. delete [] image;
  207. if (bumpmap)
  208. delete [] bumpmap;
  209. //printf(".");
  210. //fflush(stdout);
  211. }
  212. mTextureOffset = mTextureLevelOffset + mTombRaider.NumTextures();
  213. printf("Done! Found %d textures.\n", mTombRaider.NumTextures());
  214. }
  215. void Game::processSprites()
  216. {
  217. int i, j, k, l, x, y, s_index, width, height;
  218. float scale, width2, height2;
  219. tr2_sprite_texture_t *sprite;
  220. tr2_sprite_texture_t *sprite_textures;
  221. tr2_sprite_sequence_t *sprite_sequence;
  222. sprite_seq_t *r_mesh;
  223. tr2_item_t *item;
  224. item = mTombRaider.Item();
  225. sprite_textures = mTombRaider.Sprite();
  226. sprite_sequence = mTombRaider.SpriteSequence();
  227. scale = 4.0;
  228. printf("Processing sprites: ");
  229. for (i = 0; i < mTombRaider.NumItems() - 1; ++i)
  230. {
  231. // It's a mesh, skip it
  232. if (mTombRaider.Engine() == TR_VERSION_1 && item[i].intensity1 == -1)
  233. continue;
  234. k = item[i].object_id;
  235. // Search the SpriteSequence list
  236. // (if we didn't already decide that it's a mesh)
  237. for (j = 0; j < (int)mTombRaider.NumSpriteSequences(); ++j)
  238. {
  239. if (sprite_sequence[j].object_id == k)
  240. {
  241. k = item[i].object_id;
  242. s_index = sprite_sequence[j].offset;
  243. r_mesh = new sprite_seq_t;
  244. getWorld().addSprite(r_mesh);
  245. r_mesh->num_sprites = -sprite_sequence[j].negative_length;
  246. r_mesh->sprite = new sprite_t[r_mesh->num_sprites];
  247. for (l = 0; l < r_mesh->num_sprites; ++l)
  248. {
  249. sprite = &sprite_textures[s_index];
  250. width = sprite->width >> 8;
  251. height = sprite->height >> 8;
  252. x = sprite->x;
  253. y = sprite->y;
  254. width2 = width * scale;
  255. height2 = height * scale;
  256. // For vising use
  257. r_mesh->sprite[l].pos[0] = item[i].x;
  258. r_mesh->sprite[l].pos[1] = item[i].y;
  259. r_mesh->sprite[l].pos[2] = item[i].z;
  260. r_mesh->sprite[l].texture = sprite->tile + mTextureStart;
  261. r_mesh->sprite[l].radius = width2 / 2.0f;
  262. r_mesh->sprite[l].vertex[0].pos[0] = -width2 / 2.0f;
  263. r_mesh->sprite[l].vertex[1].pos[0] = -width2 / 2.0f;
  264. r_mesh->sprite[l].vertex[2].pos[0] = width2 / 2.0f;
  265. r_mesh->sprite[l].vertex[3].pos[0] = width2 / 2.0f;
  266. r_mesh->sprite[l].vertex[0].pos[1] = 0;
  267. r_mesh->sprite[l].vertex[1].pos[1] = -height2;
  268. r_mesh->sprite[l].vertex[2].pos[1] = -height2;
  269. r_mesh->sprite[l].vertex[3].pos[1] = 0;
  270. r_mesh->sprite[l].vertex[0].pos[2] = 0;
  271. r_mesh->sprite[l].vertex[1].pos[2] = 0;
  272. r_mesh->sprite[l].vertex[2].pos[2] = 0;
  273. r_mesh->sprite[l].vertex[3].pos[2] = 0;
  274. r_mesh->sprite[l].texel[3].st[0] = (vec_t)(x+width)/TexelScale;
  275. r_mesh->sprite[l].texel[3].st[1] = (vec_t)(y+height)/TexelScale;
  276. r_mesh->sprite[l].texel[2].st[0] = (vec_t)(x+width)/TexelScale;
  277. r_mesh->sprite[l].texel[2].st[1] = (vec_t)(y)/TexelScale;
  278. r_mesh->sprite[l].texel[1].st[0] = (vec_t)(x) /TexelScale;
  279. r_mesh->sprite[l].texel[1].st[1] = (vec_t)(y) /TexelScale;
  280. r_mesh->sprite[l].texel[0].st[0] = (vec_t)(x) / TexelScale;
  281. r_mesh->sprite[l].texel[0].st[1] = (vec_t)(y+height)/TexelScale;
  282. //printf(".");
  283. //fflush(stdout);
  284. }
  285. }
  286. }
  287. }
  288. printf("Done! Found %d sprites.\n", mTombRaider.NumSpriteSequences());
  289. }
  290. void Game::processMoveables()
  291. {
  292. std::vector<unsigned int> cache;
  293. std::vector<skeletal_model_t *> cache2;
  294. tr2_mesh_t *mesh = NULL;
  295. tr2_moveable_t *moveable = NULL;
  296. tr2_meshtree_t *meshtree = NULL;
  297. tr2_item_t *item = NULL;
  298. tr2_animation_t *animation = NULL;
  299. unsigned short *frame = NULL;
  300. tr2_sprite_sequence_t *sprite_sequence = NULL;
  301. tr2_object_texture_t *object_texture = NULL;
  302. int i, j, object_id;
  303. int ent = 0;
  304. unsigned int statCount = 0;
  305. frame = mTombRaider.Frame();
  306. moveable = mTombRaider.Moveable();
  307. meshtree = mTombRaider.MeshTree();
  308. mesh = mTombRaider.Mesh();
  309. object_texture = mTombRaider.ObjectTextures();
  310. item = mTombRaider.Item();
  311. animation = mTombRaider.Animation();
  312. sprite_sequence = mTombRaider.SpriteSequence();
  313. printf("Processing skeletal models: ");
  314. for (i = 0; i < mTombRaider.NumItems(); ++i)
  315. {
  316. object_id = item[i].object_id;
  317. // It may not be a moveable, test for sprite
  318. if (!(mTombRaider.Engine() == TR_VERSION_1 && item[i].intensity1 == -1))
  319. {
  320. for (j = 0; j < (int)mTombRaider.NumSpriteSequences(); ++j)
  321. {
  322. if (sprite_sequence[j].object_id == object_id)
  323. break;
  324. }
  325. // It's not a moveable, skip sprite
  326. if (j != (int)mTombRaider.NumSpriteSequences())
  327. {
  328. //printf("s");
  329. //fflush(stdout);
  330. continue;
  331. }
  332. }
  333. for (j = 0; j < (int)mTombRaider.NumMoveables(); ++j)
  334. {
  335. if ((int)moveable[j].object_id == object_id)
  336. break;
  337. }
  338. // It's not a moveable or even a sprite?, skip unknown
  339. if (j == (int)mTombRaider.NumMoveables())
  340. {
  341. //printf("?"); // what the wolf?
  342. //fflush(stdout);
  343. continue;
  344. }
  345. processMoveable(j, i, &ent, cache2, cache, object_id);
  346. statCount++;
  347. }
  348. // Get models that aren't items
  349. for (i = 0; i < mTombRaider.NumMoveables(); ++i)
  350. {
  351. switch ((int)moveable[i].object_id)
  352. {
  353. case 30:
  354. case 2: // Which tr needs this as model again?
  355. processMoveable(i, i, &ent, cache2, cache,
  356. (int)moveable[i].object_id);
  357. break;
  358. default:
  359. switch (mTombRaider.Engine())
  360. {
  361. case TR_VERSION_1:
  362. switch ((int)moveable[i].object_id)
  363. {
  364. case TombRaider1::LaraMutant:
  365. processMoveable(i, i, &ent, cache2, cache,
  366. (int)moveable[i].object_id);
  367. break;
  368. }
  369. break;
  370. case TR_VERSION_4:
  371. switch ((int)moveable[i].object_id)
  372. {
  373. case TR4_PISTOLS_ANIM:
  374. case TR4_UZI_ANIM:
  375. case TR4_SHOTGUN_ANIM:
  376. case TR4_CROSSBOW_ANIM:
  377. case TR4_GRENADE_GUN_ANIM:
  378. case TR4_SIXSHOOTER_ANIM:
  379. processMoveable(i, i, &ent, cache2, cache,
  380. (int)moveable[i].object_id);
  381. break;
  382. }
  383. break;
  384. case TR_VERSION_2:
  385. case TR_VERSION_3:
  386. case TR_VERSION_5:
  387. case TR_VERSION_UNKNOWN:
  388. break;
  389. }
  390. }
  391. }
  392. printf("Done! Found %d models.\n", mTombRaider.NumMoveables() + statCount);
  393. }
  394. void Game::processMoveable(int index, int i, int *ent,
  395. std::vector<skeletal_model_t *> &cache2,
  396. std::vector<unsigned int> &cache, int object_id)
  397. {
  398. skeletal_model_t *r_model = NULL;
  399. skeletal_model_t *c_model = NULL;
  400. animation_frame_t *animation_frame = NULL;
  401. tr2_mesh_t *mesh = NULL;
  402. tr2_moveable_t *moveable = NULL;
  403. tr2_meshtree_t *meshtree = NULL;
  404. tr2_item_t *item = NULL;
  405. tr2_animation_t *animation = NULL;
  406. tr2_meshtree_t *mesh_tree = NULL;
  407. bone_frame_t *bone = NULL;
  408. bone_tag_t *tag = NULL;
  409. entity_t *thing = NULL;
  410. SkeletalModel *sModel = 0x0;
  411. unsigned short *frame;
  412. int j, k, a, frame_step;
  413. unsigned int l, frame_offset, frame_count, f;
  414. float pos[3];
  415. float yaw;
  416. bool lara = false;
  417. int skyMesh;
  418. skyMesh = mTombRaider.getSkyModelId();
  419. frame = mTombRaider.Frame();
  420. moveable = mTombRaider.Moveable();
  421. meshtree = mTombRaider.MeshTree();
  422. mesh = mTombRaider.Mesh();
  423. item = mTombRaider.Item();
  424. animation = mTombRaider.Animation();
  425. pos[0] = item[i].x;
  426. pos[1] = item[i].y;
  427. pos[2] = item[i].z;
  428. yaw = ((item[i].angle >> 14) & 0x03);
  429. yaw *= 90;
  430. thing = new entity_t;
  431. thing->id = (*ent)++;
  432. thing->type = 0x00;
  433. thing->pos[0] = item[i].x;
  434. thing->pos[1] = item[i].y;
  435. thing->pos[2] = item[i].z;
  436. thing->angles[1] = yaw;
  437. thing->objectId = moveable[index].object_id;
  438. thing->moving = false;
  439. thing->animate = false;
  440. sModel = new SkeletalModel();
  441. getRender().addSkeletalModel(sModel);
  442. thing->tmpHook = sModel; // temp hack to keep a running version during refactoring
  443. if (mTombRaider.Engine() == TR_VERSION_1)
  444. {
  445. switch (thing->objectId)
  446. {
  447. case TombRaider1::Wolf:
  448. thing->state = TombRaider1::WolfState_Lying;
  449. //thing->animate = true;
  450. sModel->setAnimation(3);
  451. sModel->setFrame(0);
  452. break;
  453. }
  454. }
  455. //! \fixme Check here and see if we already have one for object_id later
  456. // if (getWorld().isCachedSkeletalModel(moveable[index].object_id))
  457. // {
  458. // thing->modelId = getRender().add(sModel);
  459. // return;
  460. // }
  461. r_model = new skeletal_model_t;
  462. r_model->id = moveable[index].object_id;
  463. // Gather more info if this is lara
  464. if (moveable[index].object_id == 0)
  465. {
  466. lara = true;
  467. thing->type = 0x02;
  468. mLara = thing; // Mongoose 2002.03.22, Cheap hack for now
  469. mLara->master = 0x0;
  470. switch (mTombRaider.Engine())
  471. {
  472. case TR_VERSION_3:
  473. mLara->modelId = i;
  474. sModel->setAnimation(TR_ANIAMTION_RUN);
  475. sModel->setIdleAnimation(TR_ANIAMTION_STAND);
  476. r_model->tr4Overlay = false;
  477. break;
  478. case TR_VERSION_4:
  479. mLara->modelId = i;
  480. sModel->setAnimation(TR_ANIAMTION_RUN);
  481. sModel->setIdleAnimation(TR_ANIAMTION_STAND);
  482. // Only TR4 lara has 2 layer bone tags/meshes per bone frame
  483. r_model->tr4Overlay = true;
  484. break;
  485. case TR_VERSION_1:
  486. case TR_VERSION_2:
  487. case TR_VERSION_5:
  488. case TR_VERSION_UNKNOWN:
  489. mLara->modelId = index;
  490. sModel->setAnimation(TR_ANIAMTION_RUN);
  491. sModel->setIdleAnimation(TR_ANIAMTION_STAND);
  492. r_model->tr4Overlay = false;
  493. break;
  494. }
  495. r_model->ponytailId = 0;
  496. }
  497. else
  498. {
  499. lara = false;
  500. r_model->ponytailId = -1;
  501. }
  502. // Animation
  503. a = moveable[index].animation;
  504. frame_offset = animation[a].frame_offset / 2;
  505. frame_step = animation[a].frame_size;
  506. int frame_cycle = 0;
  507. if (a >= (int)mTombRaider.NumAnimations())
  508. {
  509. a = mTombRaider.NumFrames() - frame_offset;
  510. }
  511. else
  512. {
  513. a = (animation[a].frame_offset / 2) - frame_offset;
  514. }
  515. if (frame_step != 0) // prevent divide-by-zero errors
  516. a /= frame_step;
  517. if (a != 0) // prevent divide-by-zero errors
  518. frame_offset += frame_step * (frame_cycle % a);
  519. if (a < 0)
  520. {
  521. //continue;
  522. return;
  523. }
  524. //! \fixme Might be better UID for each model, but this seems to work well
  525. j = object_id;
  526. // We only want one copy of the skeletal model in memory
  527. unsigned int foundIndex;
  528. bool found = false;
  529. for (foundIndex = 0; foundIndex < cache.size(); foundIndex++) {
  530. if ((int)cache[foundIndex] == j) {
  531. found = true;
  532. break;
  533. }
  534. }
  535. if (!found)
  536. {
  537. sModel->model = r_model;
  538. getWorld().addEntity(thing);
  539. k = getWorld().addModel(r_model);
  540. cache.push_back(j);
  541. cache2.push_back(r_model);
  542. switch (mTombRaider.Engine())
  543. {
  544. case TR_VERSION_4:
  545. if (mLara && moveable[index].object_id == 30)
  546. {
  547. r_model->ponytailId = k;
  548. r_model->ponytailMeshId = moveable[index].starting_mesh;
  549. r_model->ponytailNumMeshes = ((moveable[index].num_meshes > 0) ?
  550. moveable[index].num_meshes : 0);
  551. r_model->ponytailAngle = -90.0f;
  552. r_model->ponytail[0] = -3;
  553. r_model->ponytail[1] = -22;
  554. r_model->ponytail[2] = -20;
  555. r_model->ponyOff = 40;
  556. r_model->ponyOff2 = 32;
  557. r_model->pigtails = false;
  558. // Try to guess pigtails by looking for certian num verts in head
  559. if (mesh[moveable[0].starting_mesh].num_vertices > 80)
  560. {
  561. r_model->pigtails = true;
  562. r_model->ponyOff -= 20;
  563. r_model->ponytail[1] -= 32;
  564. }
  565. getRender().setFlags(Render::fRenderPonytail);
  566. getConsole().print("Found known ponytail");
  567. }
  568. break; // ?
  569. case TR_VERSION_1:
  570. case TR_VERSION_2:
  571. case TR_VERSION_3:
  572. case TR_VERSION_5:
  573. case TR_VERSION_UNKNOWN:
  574. if (mLara && moveable[index].object_id == 2)
  575. {
  576. r_model->ponytailId = k;
  577. r_model->ponytailMeshId = moveable[index].starting_mesh;
  578. r_model->ponytailNumMeshes = ((moveable[index].num_meshes > 0) ?
  579. moveable[index].num_meshes : 0);
  580. r_model->ponytailAngle = -90.0f;
  581. r_model->ponytail[0] = 0;
  582. r_model->ponytail[1] = -20;
  583. r_model->ponytail[2] = -20;
  584. r_model->ponyOff = 40;
  585. r_model->ponyOff2 = 0;
  586. getRender().setFlags(Render::fRenderPonytail);
  587. getConsole().print("Found ponytail?");
  588. }
  589. break;
  590. }
  591. }
  592. else
  593. {
  594. // Already cached
  595. delete r_model;
  596. c_model = cache2[foundIndex];
  597. sModel->model = c_model;
  598. getWorld().addEntity(thing);
  599. getWorld().addModel(c_model);
  600. printf("c");
  601. return;
  602. }
  603. int aloop = mTombRaider.getNumAnimsForMoveable(index);
  604. #ifdef DEBUG_MOVEABLE
  605. printf("\nanimation = %i, num_animations = %i\n",
  606. moveable[index].animation, aloop);
  607. printf("\nitem[%i].flags = %i\nentity[%i]\n",
  608. i, item[i].flags, thing->id);
  609. #endif
  610. //a = moveable[index].animation;
  611. //frame_offset = animation[a].frame_offset / 2;
  612. //frame_step = animation[a].frame_size;
  613. for (; a < aloop; ++a,
  614. frame_offset = animation[a].frame_offset / 2,
  615. frame_step = animation[a].frame_size)
  616. {
  617. animation_frame = new animation_frame_t;
  618. r_model->animation.push_back(animation_frame);
  619. frame_count = (animation[a].frame_end - animation[a].frame_start) + 1;
  620. animation_frame->rate = animation[a].frame_rate;
  621. #ifdef DEBUG_MOVEABLE
  622. printf("animation[%i] state and unknowns = %i, %i, %i, %i, %i\n",
  623. a, animation[a].state_id, animation[a].unknown1,
  624. animation[a].unknown2, animation[a].unknown3,
  625. animation[a].unknown4);
  626. printf("animation[%i].frame_rate = %i\n",
  627. a, animation[a].frame_rate);
  628. printf("animation[%i].next_animation = %i\n",
  629. a, animation[a].next_animation);
  630. printf("animation[%i].frame_offset = %u\n",
  631. a, animation[a].frame_offset);
  632. printf("animation[%i].anim_command = %i\n",
  633. a, animation[a].anim_command);
  634. printf("animation[%i].num_anim_commands = %i\n",
  635. a, animation[a].num_anim_commands);
  636. printf("animation[%i].state_change_offset = %i\n",
  637. a, animation[a].state_change_offset);
  638. printf(" frame_offset = %u\n",
  639. frame_offset);
  640. #endif
  641. // Get all the frames for aniamtion
  642. for (f = 0; f < frame_count; ++f, frame_offset += frame_step)
  643. {
  644. // HACK: Lara's ObjectID is 315, but her meshes start at 0, so make a
  645. // quick substitution (so she doesn't appear as a bunch of thighs)
  646. if (index == 0 && mTombRaider.Engine() == TR_VERSION_3)
  647. {
  648. for (j = 0; j < (int)mTombRaider.NumMoveables() && !index; ++j)
  649. {
  650. if (moveable[j].object_id == 315)
  651. index = j;
  652. }
  653. }
  654. // Fix Lara in TR4
  655. if (index == 0 && mTombRaider.Engine() == TR_VERSION_4)
  656. {
  657. for (j = 0; j < (int)mTombRaider.NumMoveables() && !index; ++j)
  658. {
  659. // Body is ItemID 8, joints are ItemID 9
  660. // (TR4 demo: body is ItemID 10, joints are ItemID 11)
  661. if (moveable[j].object_id == 8)
  662. index = j;
  663. }
  664. }
  665. else if (moveable[index].object_id == 8 &&
  666. mTombRaider.Engine() == TR_VERSION_4)
  667. {
  668. // KLUDGE to do "skinning"
  669. index = 0;
  670. for (j = 0; j < (int)mTombRaider.NumMoveables() && !index; ++j)
  671. {
  672. // Body is ItemID 8, joints are ItemID 9
  673. // (TR4 demo: body is ItemID 10, joints are ItemID 11)
  674. if (moveable[j].object_id == 9)
  675. index = j;
  676. }
  677. }
  678. #ifdef DEBUG_MOVEABLE
  679. printf("animation[%i].boneframe[%u] = offset %u, step %i\n",
  680. a, f, frame_offset, frame_step);
  681. #endif
  682. // Mongoose 2002.08.15, Was
  683. // if (frame_offset + 8 > _tombraider.NumFrames())
  684. if (frame_offset > mTombRaider.NumFrames())
  685. {
  686. getConsole().print("WARNING: Bad animation frame %i > %i",
  687. frame_offset, mTombRaider.NumFrames());
  688. // Mongoose 2002.08.15, Attempt to skip more likely bad animation data
  689. getConsole().print("WARNING: Handling bad animation data...");
  690. return; //continue;
  691. }
  692. // Generate bone frames and tags per frame ////////////
  693. bone = new bone_frame_t;
  694. animation_frame->frame.push_back(bone);
  695. // Init translate for bone frame
  696. bone->pos[0] = (short)frame[frame_offset + 6];
  697. bone->pos[1] = (short)frame[frame_offset + 7];
  698. bone->pos[2] = (short)frame[frame_offset + 8];
  699. bone->yaw = yaw;
  700. //printf("%f %f %f\n", bone->pos[0], bone->pos[1], bone->pos[2]);
  701. l = 9; // First angle offset in this Frame
  702. // Run through the tag and calculate the rotation and offset
  703. for (j = 0; j < (int)moveable[index].num_meshes; ++j)
  704. {
  705. tag = new bone_tag_t;
  706. bone->tag.push_back(tag);
  707. tag->off[0] = 0.0;
  708. tag->off[1] = 0.0;
  709. tag->off[2] = 0.0;
  710. tag->flag = 0x00;
  711. tag->rot[0] = 0.0;
  712. tag->rot[1] = 0.0;
  713. tag->rot[2] = 0.0;
  714. tag->mesh = moveable[index].starting_mesh + j;
  715. // Setup offsets to produce skeletion
  716. if (j == 0)
  717. {
  718. // Since we use bone's offset, these aren't used
  719. tag->off[0] = 0.0;
  720. tag->off[1] = 0.0;
  721. tag->off[2] = 0.0;
  722. // Always push tag[0], this isn't really used either
  723. tag->flag = 0x02;
  724. }
  725. else // Nonprimary tag - position relative to first tag
  726. {
  727. int *tree;
  728. // Hack: moveable[index].mesh_tree is a byte offset
  729. // into mesh_tree[], so we have to convert to index
  730. tree = (int *)(void *)meshtree;
  731. mesh_tree = (tr2_meshtree_t *)&tree[moveable[index].mesh_tree
  732. + ((j - 1) * 4)];
  733. tag->off[0] = mesh_tree->x;
  734. tag->off[1] = mesh_tree->y;
  735. tag->off[2] = mesh_tree->z;
  736. tag->flag = (char)mesh_tree->flags;
  737. }
  738. // Setup tag rotations
  739. mTombRaider.computeRotationAngles(&frame, &frame_offset, &l,
  740. tag->rot, tag->rot+1, tag->rot+2);
  741. }
  742. }
  743. }
  744. if (i == skyMesh)
  745. {
  746. getRender().setSkyMesh(i, //moveable[i].starting_mesh,
  747. (mTombRaider.Engine() == TR_VERSION_2));
  748. }
  749. //printf(".");
  750. //fflush(stdout);
  751. }
  752. bool compareFaceTextureId(const void *voidA, const void *voidB)
  753. {
  754. texture_tri_t *a = (texture_tri_t *)voidA, *b = (texture_tri_t *)voidB;
  755. if (!a || !b)
  756. return false; // error really
  757. return (a->texture < b->texture);
  758. }
  759. #ifdef EXPERIMENTAL
  760. void Game::setupTextureColor(texture_tri_t *r_tri, float *colorf)
  761. {
  762. unsigned char color[4];
  763. unsigned int colorI;
  764. color[0] = (unsigned char)(colorf[0]*255.0f);
  765. color[1] = (unsigned char)(colorf[1]*255.0f);
  766. color[2] = (unsigned char)(colorf[2]*255.0f);
  767. color[3] = (unsigned char)(colorf[3]*255.0f);
  768. ((unsigned char *)(&colorI))[3] = color[0];
  769. ((unsigned char *)(&colorI))[2] = color[1];
  770. ((unsigned char *)(&colorI))[1] = color[2];
  771. ((unsigned char *)(&colorI))[0] = color[3];
  772. bool found = false;
  773. unsigned int foundIndex = 0;
  774. for (foundIndex = 0; foundIndex < gColorTextureHACK.size(); foundIndex++) {
  775. if (gColorTextureHACK[foundIndex] == colorI) {
  776. found = true;
  777. break;
  778. }
  779. }
  780. if (!found)
  781. {
  782. gColorTextureHACK.push_back(colorI);
  783. r_tri->texture = mTextureOffset + gColorTextureHACK.size();
  784. getRender().loadTexture(Texture::generateColorTexture(color, 32, 32),
  785. 32, 32,
  786. r_tri->texture);
  787. #ifdef DEBUG_COLOR_TEXTURE_GEN
  788. printf("Color 0x%02x%02x%02x%02x | 0x%08xto texture[%u]?\n",
  789. color[0], color[1], color[2], color[3], colorI,
  790. gColorTextureHACK.size());
  791. #endif
  792. }
  793. else
  794. {
  795. //printf("Color already loaded %i -> 0x%08x\n",
  796. // gColorTextureHACK.getCurrentIndex(),
  797. // gColorTextureHACK.current());
  798. r_tri->texture = mTextureOffset + foundIndex;
  799. }
  800. //r_tri->texture = white; // White texture
  801. }
  802. #endif
  803. void Game::processModels()
  804. {
  805. printf("Processing meshes: ");
  806. for (int index = 0; index < mTombRaider.getMeshCount(); index++) {
  807. int i, j, count, texture;
  808. int vertexIndices[6];
  809. float st[12];
  810. float color[4];
  811. unsigned short transparency;
  812. texture_tri_t *r_tri;
  813. // Assert common sense
  814. if (index < 0 || !mTombRaider.isMeshValid(index))
  815. {
  816. //! \fixme allow sparse lists with matching ids instead?
  817. getWorld().addMesh(NULL); // Filler, to make meshes array ids align
  818. //printf("x");
  819. //fflush(stdout);
  820. return;
  821. }
  822. #ifndef EXPERIMENTAL
  823. // WHITE texture id
  824. int white = 0;
  825. #endif
  826. model_mesh_t *mesh = new model_mesh_t;
  827. // Mongoose 2002.08.30, Testing support for 'shootable' models ( traceable )
  828. mTombRaider.getMeshCollisionInfo(index, mesh->center, &mesh->radius);
  829. //! \fixme Arrays don't work either =)
  830. // Mesh geometery, colors, etc
  831. mTombRaider.getMeshVertexArrays(index,
  832. &mesh->vertexCount, &mesh->vertices,
  833. &mesh->normalCount, &mesh->normals,
  834. &mesh->colorCount, &mesh->colors);
  835. // Textured Triangles
  836. count = mTombRaider.getMeshTexturedTriangleCount(index);
  837. mesh->texturedTriangles.reserve(count); // little faster
  838. for (i = 0; i < count; ++i)
  839. {
  840. r_tri = new texture_tri_t;
  841. mTombRaider.getMeshTexturedTriangle(index, i,
  842. r_tri->index,
  843. r_tri->st,
  844. &r_tri->texture,
  845. &r_tri->transparency);
  846. r_tri->texture += mTextureStart;
  847. // Add to face vector
  848. mesh->texturedTriangles.push_back(r_tri);
  849. }
  850. // Coloured Triangles
  851. count = mTombRaider.getMeshColoredTriangleCount(index);
  852. mesh->coloredTriangles.reserve(count); // little faster
  853. for (i = 0; i < count; i++)
  854. {
  855. r_tri = new texture_tri_t;
  856. mTombRaider.getMeshColoredTriangle(index, i,
  857. r_tri->index,
  858. color);
  859. r_tri->st[0] = color[0];
  860. r_tri->st[1] = color[1];
  861. r_tri->st[2] = color[2];
  862. r_tri->st[3] = color[3];
  863. r_tri->st[4] = 1.0;
  864. r_tri->st[5] = 1.0;
  865. #ifdef EXPERIMENTAL
  866. setupTextureColor(r_tri, color);
  867. #else
  868. r_tri->texture = white; // White texture
  869. #endif
  870. r_tri->transparency = 0;
  871. // Add to face vector
  872. mesh->coloredTriangles.push_back(r_tri);
  873. }
  874. // Textured Rectangles
  875. count = mTombRaider.getMeshTexturedRectangleCount(index);
  876. mesh->texturedRectangles.reserve(count*2); // little faster
  877. for (i = 0; i < count; ++i)
  878. {
  879. mTombRaider.getMeshTexturedRectangle(index, i,
  880. vertexIndices,
  881. st,
  882. &texture,
  883. &transparency);
  884. r_tri = new texture_tri_t;
  885. for (j = 0; j < 3; ++j)
  886. r_tri->index[j] = vertexIndices[j];
  887. for (j = 0; j < 6; ++j)
  888. r_tri->st[j] = st[j];
  889. r_tri->texture = texture + mTextureStart;
  890. r_tri->transparency = transparency;
  891. // Add to face vector
  892. mesh->texturedRectangles.push_back(r_tri);
  893. r_tri = new texture_tri_t;
  894. for (j = 3; j < 6; ++j)
  895. r_tri->index[j-3] = vertexIndices[j];
  896. for (j = 6; j < 12; ++j)
  897. r_tri->st[j-6] = st[j];
  898. r_tri->texture = texture + mTextureStart;
  899. r_tri->transparency = transparency;
  900. // Add to face vector
  901. mesh->texturedRectangles.push_back(r_tri);
  902. }
  903. // Coloured Rectangles
  904. count = mTombRaider.getMeshColoredRectangleCount(index);
  905. mesh->coloredRectangles.reserve(count*2); // little faster
  906. for (i = 0; i < count; ++i)
  907. {
  908. mTombRaider.getMeshColoredRectangle(index, i,
  909. vertexIndices,
  910. color);
  911. r_tri = new texture_tri_t;
  912. for (j = 0; j < 3; ++j)
  913. r_tri->index[j] = vertexIndices[j];
  914. //for (j = 0; j < 6; ++j)
  915. // r_tri->st[j] = st[j];
  916. r_tri->st[0] = color[0];
  917. r_tri->st[1] = color[1];
  918. r_tri->st[2] = color[2];
  919. r_tri->st[3] = color[3];
  920. r_tri->st[4] = 1.0;
  921. r_tri->st[5] = 1.0;
  922. #ifdef EXPERIMENTAL
  923. //for (j = 6; j < 12; ++j)
  924. // r_tri->st[j-6] = st[j];
  925. setupTextureColor(r_tri, color);
  926. #else
  927. r_tri->texture = white; // White texture
  928. #endif
  929. r_tri->transparency = 0;
  930. // Add to face vector
  931. mesh->coloredRectangles.push_back(r_tri);
  932. r_tri = new texture_tri_t;
  933. for (j = 3; j < 6; ++j)
  934. r_tri->index[j-3] = vertexIndices[j];
  935. //for (j = 6; j < 12; ++j)
  936. // r_tri->st[j-6] = st[j];
  937. r_tri->st[0] = color[0];
  938. r_tri->st[1] = color[1];
  939. r_tri->st[2] = color[2];
  940. r_tri->st[3] = color[3];
  941. r_tri->st[4] = 1.0;
  942. r_tri->st[5] = 1.0;
  943. #ifdef EXPERIMENTAL
  944. setupTextureColor(r_tri, color);
  945. #else
  946. r_tri->texture = white; // White texture
  947. #endif
  948. r_tri->transparency = 0;
  949. // Add to face vector
  950. mesh->coloredRectangles.push_back(r_tri);
  951. }
  952. // Sort faces by texture
  953. std::sort(mesh->texturedTriangles.begin(), mesh->texturedTriangles.end(), compareFaceTextureId);
  954. std::sort(mesh->coloredTriangles.begin(), mesh->coloredTriangles.end(), compareFaceTextureId);
  955. std::sort(mesh->texturedRectangles.begin(), mesh->texturedRectangles.end(), compareFaceTextureId);
  956. std::sort(mesh->coloredRectangles.begin(), mesh->coloredRectangles.end(), compareFaceTextureId);
  957. getWorld().addMesh(mesh);
  958. //printf(".");
  959. //fflush(stdout);
  960. }
  961. printf("Done! Found %d meshes.\n", mTombRaider.getMeshCount());
  962. }
  963. void Game::processRooms()
  964. {
  965. printf("Processing rooms: ");
  966. for (int index = 0; index < mTombRaider.NumRooms(); index++) {
  967. unsigned int i, j, count;
  968. room_mesh_t *r_mesh = NULL;
  969. RenderRoom *rRoom = NULL;
  970. Matrix transform;
  971. if (!mTombRaider.isRoomValid(index))
  972. {
  973. getConsole().print("WARNING: Handling invalid vertex array in room");
  974. getWorld().addRoom(0x0);
  975. getRender().addRoom(0x0);
  976. //printf("x");
  977. //fflush(stdout);
  978. return;
  979. }
  980. rRoom = new RenderRoom();
  981. r_mesh = new room_mesh_t;
  982. r_mesh->id = index;
  983. mTombRaider.getRoomInfo(index, &r_mesh->flags, r_mesh->pos,
  984. r_mesh->bbox_min, r_mesh->bbox_max);
  985. // Adjust positioning for OR world coord translation
  986. r_mesh->bbox_min[0] += r_mesh->pos[0];
  987. r_mesh->bbox_max[0] += r_mesh->pos[0];
  988. r_mesh->bbox_min[2] += r_mesh->pos[2];
  989. r_mesh->bbox_max[2] += r_mesh->pos[2];
  990. // Mongoose 2002.04.03, Setup 3d transform
  991. transform.setIdentity();
  992. transform.translate(r_mesh->pos);
  993. // Setup portals
  994. float portalVertices[12];
  995. count = mTombRaider.getRoomPortalCount(index);
  996. //! \fixme OR wrongly uses a cached adj room list for rendering vis
  997. r_mesh->adjacentRooms.reserve(count + 1);
  998. // Current room is always first
  999. r_mesh->adjacentRooms.push_back(index);
  1000. for (i = 0; i < count; ++i)
  1001. {
  1002. portal_t *portal = new portal_t;
  1003. mTombRaider.getRoomPortal(index, i,
  1004. &portal->adjoining_room, portal->normal,
  1005. portalVertices);
  1006. for (j = 0; j < 4; ++j)
  1007. {
  1008. portal->vertices[j][0] = portalVertices[j*3];
  1009. portal->vertices[j][1] = portalVertices[j*3+1];
  1010. portal->vertices[j][2] = portalVertices[j*3+2];
  1011. // Relative coors in vis portals
  1012. transform.multiply3v(portal->vertices[j], portal->vertices[j]);
  1013. }
  1014. r_mesh->adjacentRooms.push_back(portal->adjoining_room);
  1015. r_mesh->portals.push_back(portal);
  1016. }
  1017. // Physics/gameplay use /////////////////////////////
  1018. //! \fixme Use more of sector structure, boxes, and floordata
  1019. // List of sectors in this room
  1020. unsigned int sectorFlags;
  1021. int floorDataIndex, boxIndex, roomBelow, roomAbove;
  1022. count = mTombRaider.getRoomSectorCount(index, &r_mesh->numZSectors,
  1023. &r_mesh->numXSectors);
  1024. r_mesh->sectors.reserve(count);
  1025. for (i = 0; i < count; ++i)
  1026. {
  1027. sector_t *sector = new sector_t;
  1028. mTombRaider.getRoomSector(index, i, &sectorFlags,
  1029. &sector->ceiling, &sector->floor,
  1030. &floorDataIndex, &boxIndex, &roomBelow,
  1031. &roomAbove);
  1032. if (sectorFlags & tombraiderSector_wall)
  1033. {
  1034. sector->wall = true;
  1035. }
  1036. else
  1037. {
  1038. sector->wall = false;
  1039. }
  1040. r_mesh->sectors.push_back(sector);
  1041. }
  1042. // Setup collision boxes ( Should use sectors, but this is a test )
  1043. count = mTombRaider.getRoomBoxCount(index);
  1044. r_mesh->boxes.reserve(count);
  1045. //! fixme Only to be done only on room[0]? I don't think so...
  1046. for (i = 0; !index && i < count; ++i)
  1047. {
  1048. box_t *box = new box_t;
  1049. mTombRaider.getRoomBox(index, i,
  1050. box->a.pos, box->b.pos, box->c.pos, box->d.pos);
  1051. r_mesh->boxes.push_back(box);
  1052. }
  1053. // Setup room lights /////////////////////////////////////
  1054. unsigned int lightFlags, lightType;
  1055. count = mTombRaider.getRoomLightCount(index);
  1056. rRoom->lights.reserve(count);
  1057. for (i = 0; i < count; ++i)
  1058. {
  1059. Light *light = new Light();
  1060. mTombRaider.getRoomLight(index, i,
  1061. light->mPos, light->mColor, light->mDir,
  1062. &light->mAtt, &light->mCutoff,
  1063. &lightType, &lightFlags);
  1064. switch (lightType)
  1065. {
  1066. case tombraiderLight_typeDirectional:
  1067. light->mType = Light::typeDirectional;
  1068. break;
  1069. case tombraiderLight_typeSpot:
  1070. light->mType = Light::typeSpot;
  1071. break;
  1072. case tombraiderLight_typePoint:
  1073. default:
  1074. light->mType = Light::typePoint;
  1075. }
  1076. rRoom->lights.push_back(light);
  1077. }
  1078. // Room geometery //////////////////////////////////
  1079. //#define EXPERIMENTAL_UNFIFIED_ROOM_GEOMETERY
  1080. #ifdef EXPERIMENTAL_UNFIFIED_ROOM_GEOMETERY
  1081. unsigned int vertexCount, normalCount, colorCount, triCount;
  1082. vec_t *vertexArray;
  1083. vec_t *normalArray;
  1084. vec_t *colorArray;
  1085. unsigned int *indices, *flags;
  1086. float *texCoords;
  1087. int *textures;
  1088. mTombRaider.getRoomVertexArrays(index,
  1089. &vertexCount, &vertexArray,
  1090. &normalCount, &normalArray,
  1091. &colorCount, &colorArray);
  1092. rRoom->mesh.bufferVertexArray(vertexCount, (vec_t *)vertexArray);
  1093. rRoom->mesh.bufferNormalArray(normalCount, (vec_t *)normalArray);
  1094. rRoom->mesh.bufferColorArray(vertexCount, (vec_t *)colorArray);
  1095. mTombRaider.getRoomTriangles(index, mTextureStart,
  1096. &triCount, &indices, &texCoords, &textures,
  1097. &flags);
  1098. rRoom->mesh.bufferTriangles(triCount, indices, texCoords, textures, flags);
  1099. #else
  1100. float rgba[4];
  1101. float xyz[3];
  1102. count = mTombRaider.getRoomVertexCount(index);
  1103. rRoom->mesh.allocateVertices(count);
  1104. rRoom->mesh.allocateNormals(0); // count
  1105. rRoom->mesh.allocateColors(count);
  1106. for (i = 0; i < count; ++i)
  1107. {
  1108. mTombRaider.getRoomVertex(index, i, xyz, rgba);
  1109. rRoom->mesh.setVertex(i, xyz[0], xyz[1], xyz[2]);
  1110. rRoom->mesh.setColor(i, rgba);
  1111. }
  1112. // Mongoose 2002.06.09, Setup allocation of meshes and polygons
  1113. // Counters ( Textured polygon lists are allocated per texture)
  1114. // ( Textures are mapped to these meshes )
  1115. int triangle_counter[TextureLimit];
  1116. int triangle_counter_alpha[TextureLimit];
  1117. int rectangle_counter[TextureLimit];
  1118. int rectangle_counter_alpha[TextureLimit];
  1119. int tris_mesh_map[TextureLimit];
  1120. int rect_mesh_map[TextureLimit];
  1121. for (i = 0; i < TextureLimit; ++i)
  1122. {
  1123. triangle_counter[i] = 0;
  1124. triangle_counter_alpha[i] = 0;
  1125. rectangle_counter[i] = 0;
  1126. rectangle_counter_alpha[i] = 0;
  1127. tris_mesh_map[i] = -1;
  1128. rect_mesh_map[i] = -1;
  1129. }
  1130. unsigned int numTris = 0;
  1131. unsigned int numQuads = 0;
  1132. int texture;
  1133. unsigned int r, t, q, v, flags;
  1134. unsigned int indices[4];
  1135. float texCoords[8];
  1136. count = mTombRaider.getRoomTriangleCount(index);
  1137. // Mongoose 2002.08.15, Presort by alpha and texture and setup mapping
  1138. for (t = 0; t < count; ++t)
  1139. {
  1140. mTombRaider.getRoomTriangle(index, t,
  1141. indices, texCoords, &texture, &flags);
  1142. texture += mTextureStart;
  1143. if (texture > (int)TextureLimit)
  1144. {
  1145. getConsole().print("Handling bad room[%i].tris[%i].texture = %i",
  1146. index, t, texture);
  1147. texture = TextureLimit - 1;
  1148. }
  1149. // Counters set up polygon allocation
  1150. if (flags & tombraiderFace_Alpha ||
  1151. flags & tombraiderFace_PartialAlpha)
  1152. {
  1153. triangle_counter_alpha[texture] += 1;
  1154. }
  1155. else
  1156. {
  1157. triangle_counter[texture] += 1;
  1158. }
  1159. // Counter sets up texture id to mesh id mapping
  1160. if (tris_mesh_map[texture] == -1)
  1161. {
  1162. tris_mesh_map[texture] = ++numTris;
  1163. }
  1164. }
  1165. count = mTombRaider.getRoomRectangleCount(index);
  1166. for (r = 0; r < count; ++r)
  1167. {
  1168. mTombRaider.getRoomRectangle(index, r,
  1169. indices, texCoords, &texture, &flags);
  1170. texture += mTextureStart;
  1171. if (texture > (int)TextureLimit)
  1172. {
  1173. getConsole().print("Handling bad room[%i].quad[%i].texture = %i",
  1174. index, r, texture);
  1175. texture = TextureLimit - 1;
  1176. }
  1177. if (flags & tombraiderFace_Alpha ||
  1178. flags & tombraiderFace_PartialAlpha)
  1179. {
  1180. rectangle_counter_alpha[texture] += 1;
  1181. }
  1182. else
  1183. {
  1184. rectangle_counter[texture] += 1;
  1185. }
  1186. if (rect_mesh_map[texture] == -1)
  1187. {
  1188. rect_mesh_map[texture] = ++numQuads;
  1189. }
  1190. }
  1191. // Allocate indexed polygon meshes
  1192. rRoom->mesh.allocateTriangles(numTris);
  1193. rRoom->mesh.allocateRectangles(numQuads);
  1194. for (i = 0, j = 0; i < TextureLimit; ++i)
  1195. {
  1196. if (tris_mesh_map[i] > 0)
  1197. {
  1198. j = tris_mesh_map[i] - 1;
  1199. t = triangle_counter[i];
  1200. rRoom->mesh.mTris[j].texture = i;
  1201. #ifdef MULTITEXTURE
  1202. rRoom->mesh.mTris[j].bumpmap = gMapTex2Bump[i];
  1203. #endif
  1204. rRoom->mesh.mTris[j].cnum_triangles = 0;
  1205. rRoom->mesh.mTris[j].num_triangles = 0;
  1206. rRoom->mesh.mTris[j].cnum_alpha_triangles = 0;
  1207. rRoom->mesh.mTris[j].num_alpha_triangles = 0;
  1208. rRoom->mesh.mTris[j].triangles = 0x0;
  1209. rRoom->mesh.mTris[j].alpha_triangles = 0x0;
  1210. rRoom->mesh.mTris[j].texcoors = 0x0;
  1211. rRoom->mesh.mTris[j].texcoors2 = 0x0;
  1212. if (t > 0)
  1213. {
  1214. rRoom->mesh.mTris[j].num_triangles = t;
  1215. rRoom->mesh.mTris[j].triangles = new unsigned int[t*3];
  1216. rRoom->mesh.mTris[j].num_texcoors = t * 3;
  1217. rRoom->mesh.mTris[j].texcoors = new vec2_t[t * 3];
  1218. }
  1219. t = triangle_counter_alpha[i];
  1220. if (t > 0)
  1221. {
  1222. rRoom->mesh.mTris[j].num_alpha_triangles = t;
  1223. rRoom->mesh.mTris[j].alpha_triangles = new unsigned int[t*3];
  1224. rRoom->mesh.mTris[j].num_texcoors2 = t * 3;
  1225. rRoom->mesh.mTris[j].texcoors2 = new vec2_t[t * 3];
  1226. }
  1227. }
  1228. ///////////////////////////////////////////
  1229. if (rect_mesh_map[i] > 0)
  1230. {
  1231. j = rect_mesh_map[i] - 1;
  1232. r = rectangle_counter[i];
  1233. rRoom->mesh.mQuads[j].texture = i;
  1234. #ifdef MULTITEXTURE
  1235. rRoom->mesh.mQuads[j].bumpmap = gMapTex2Bump[i];
  1236. #endif
  1237. rRoom->mesh.mQuads[j].cnum_quads = 0;
  1238. rRoom->mesh.mQuads[j].num_quads = 0;
  1239. rRoom->mesh.mQuads[j].cnum_alpha_quads = 0;
  1240. rRoom->mesh.mQuads[j].num_alpha_quads = 0;
  1241. rRoom->mesh.mQuads[j].quads = 0x0;
  1242. rRoom->mesh.mQuads[j].alpha_quads = 0x0;
  1243. rRoom->mesh.mQuads[j].texcoors = 0x0;
  1244. rRoom->mesh.mQuads[j].texcoors2 = 0x0;
  1245. if (r > 0)
  1246. {
  1247. rRoom->mesh.mQuads[j].num_quads = r;
  1248. rRoom->mesh.mQuads[j].quads = new unsigned int[r*4];
  1249. rRoom->mesh.mQuads[j].num_texcoors = r * 4;
  1250. rRoom->mesh.mQuads[j].texcoors = new vec2_t[r * 4];
  1251. }
  1252. r = rectangle_counter_alpha[i];
  1253. if (r > 0)
  1254. {
  1255. rRoom->mesh.mQuads[j].num_alpha_quads = r;
  1256. rRoom->mesh.mQuads[j].alpha_quads = new unsigned int[r*4];
  1257. rRoom->mesh.mQuads[j].num_texcoors2 = r * 4;
  1258. rRoom->mesh.mQuads[j].texcoors2 = new vec2_t[r * 4];
  1259. }
  1260. }
  1261. }
  1262. // Generate textured triangles
  1263. count = mTombRaider.getRoomTriangleCount(index);
  1264. for (t = 0; t < count; ++t)
  1265. {
  1266. mTombRaider.getRoomTriangle(index, t,
  1267. indices, texCoords, &texture, &flags);
  1268. // Adjust texture id using mTextureStart to map into
  1269. // correct textures
  1270. texture += mTextureStart;
  1271. j = tris_mesh_map[texture] - 1;
  1272. // Setup per vertex
  1273. for (i = 0; i < 3; ++i)
  1274. {
  1275. // Get vertex index {(0, a), (1, b), (2, c)}
  1276. v = indices[i];
  1277. if ((flags & tombraiderFace_Alpha ||
  1278. flags & tombraiderFace_PartialAlpha) &&
  1279. rRoom->mesh.mTris[j].num_alpha_triangles > 0)
  1280. {
  1281. q = rRoom->mesh.mTris[j].cnum_alpha_triangles*3+i;
  1282. rRoom->mesh.mTris[j].alpha_triangles[q] = v;
  1283. rRoom->mesh.mTris[j].texcoors2[q][0] = texCoords[i*2];
  1284. rRoom->mesh.mTris[j].texcoors2[q][1] = texCoords[i*2+1];
  1285. }
  1286. else if (rRoom->mesh.mTris[j].num_triangles > 0)
  1287. {
  1288. q = rRoom->mesh.mTris[j].cnum_triangles*3+i;
  1289. rRoom->mesh.mTris[j].triangles[q] = v;
  1290. rRoom->mesh.mTris[j].texcoors[q][0] = texCoords[i*2];
  1291. rRoom->mesh.mTris[j].texcoors[q][1] = texCoords[i*2+1];
  1292. }
  1293. // Partial alpha hack
  1294. if (flags & tombraiderFace_PartialAlpha)
  1295. {
  1296. //rRoom->mesh.colors[v].rgba[3] = 0.45;
  1297. }
  1298. }
  1299. if (flags & tombraiderFace_Alpha ||
  1300. flags & tombraiderFace_PartialAlpha)
  1301. {
  1302. rRoom->mesh.mTris[j].cnum_alpha_triangles++;
  1303. }
  1304. else
  1305. {
  1306. rRoom->mesh.mTris[j].cnum_triangles++;
  1307. }
  1308. }
  1309. // Generate textured quads
  1310. count = mTombRaider.getRoomRectangleCount(index);
  1311. for (r = 0; r < count; ++r)
  1312. {
  1313. mTombRaider.getRoomRectangle(index, r,
  1314. indices, texCoords, &texture, &flags);
  1315. // Adjust texture id using mTextureStart to map into
  1316. // correct textures
  1317. texture += mTextureStart;
  1318. if (texture > (int)TextureLimit)
  1319. {
  1320. texture = TextureLimit - 1;
  1321. }
  1322. j = rect_mesh_map[texture] - 1;
  1323. if (rRoom->mesh.mQuads[j].num_quads <= 0 &&
  1324. rRoom->mesh.mQuads[j].num_alpha_quads <= 0)
  1325. continue;
  1326. // Setup per vertex
  1327. for (i = 0; i < 4; ++i)
  1328. {
  1329. // Get vertex index {(0, a), (1, b), (2, c), (3, d)}
  1330. v = indices[i];
  1331. if ((flags & tombraiderFace_Alpha ||
  1332. flags & tombraiderFace_PartialAlpha) &&
  1333. rRoom->mesh.mQuads[j].num_alpha_quads > 0)
  1334. {
  1335. q = rRoom->mesh.mQuads[j].cnum_alpha_quads*4+i;
  1336. rRoom->mesh.mQuads[j].alpha_quads[q] = v;
  1337. rRoom->mesh.mQuads[j].texcoors2[q][0] = texCoords[i*2];
  1338. rRoom->mesh.mQuads[j].texcoors2[q][1] = texCoords[i*2+1];
  1339. }
  1340. else if (rRoom->mesh.mQuads[j].num_quads > 0)
  1341. {
  1342. q = rRoom->mesh.mQuads[j].cnum_quads*4+i;
  1343. rRoom->mesh.mQuads[j].quads[q] = v;
  1344. rRoom->mesh.mQuads[j].texcoors[q][0] = texCoords[i*2];
  1345. rRoom->mesh.mQuads[j].texcoors[q][1] = texCoords[i*2+1];
  1346. }
  1347. // Partial alpha hack
  1348. if (flags & tombraiderFace_PartialAlpha)
  1349. {
  1350. //rRoom->mesh.colors[v].rgba[3] = 0.45;
  1351. }
  1352. }
  1353. if (flags & tombraiderFace_Alpha ||
  1354. flags & tombraiderFace_PartialAlpha)
  1355. {
  1356. rRoom->mesh.mQuads[j].cnum_alpha_quads++;
  1357. }
  1358. else
  1359. {
  1360. rRoom->mesh.mQuads[j].cnum_quads++;
  1361. }
  1362. }
  1363. #endif
  1364. // Room models
  1365. count = mTombRaider.getRoomModelCount(index);
  1366. r_mesh->models.reserve(count);
  1367. for (i = 0; i < count; ++i)
  1368. {
  1369. static_model_t *model = new static_model_t;
  1370. mTombRaider.getRoomModel(index, i,
  1371. &model->index, model->pos, &model->yaw);
  1372. r_mesh->models.push_back(model);
  1373. }
  1374. // Room sprites
  1375. float spriteVertices[12];
  1376. float spriteTexCoords[8];
  1377. count = mTombRaider.getRoomSpriteCount(index);
  1378. r_mesh->sprites.reserve(count);
  1379. for (i = 0; i < count; ++i)
  1380. {
  1381. sprite_t *sprite = new sprite_t;
  1382. mTombRaider.getRoomSprite(index, i,
  1383. 10.0f, &sprite->texture, sprite->pos,
  1384. spriteVertices, spriteTexCoords);
  1385. sprite->texture += mTextureStart; // OpenRaider preloads some textures
  1386. sprite->vertex[0].pos[0] = spriteVertices[0];
  1387. sprite->vertex[0].pos[1] = spriteVertices[1];
  1388. sprite->vertex[0].pos[2] = spriteVertices[2];
  1389. sprite->vertex[1].pos[0] = spriteVertices[3];
  1390. sprite->vertex[1].pos[1] = spriteVertices[4];
  1391. sprite->vertex[1].pos[2] = spriteVertices[5];
  1392. sprite->vertex[2].pos[0] = spriteVertices[6];
  1393. sprite->vertex[2].pos[1] = spriteVertices[7];
  1394. sprite->vertex[2].pos[2] = spriteVertices[8];
  1395. sprite->vertex[3].pos[0] = spriteVertices[9];
  1396. sprite->vertex[3].pos[1] = spriteVertices[10];
  1397. sprite->vertex[3].pos[2] = spriteVertices[11];
  1398. sprite->texel[0].st[0] = spriteTexCoords[0];
  1399. sprite->texel[0].st[1] = spriteTexCoords[1];
  1400. sprite->texel[1].st[0] = spriteTexCoords[2];
  1401. sprite->texel[1].st[1] = spriteTexCoords[3];
  1402. sprite->texel[2].st[0] = spriteTexCoords[4];
  1403. sprite->texel[2].st[1] = spriteTexCoords[5];
  1404. sprite->texel[3].st[0] = spriteTexCoords[6];
  1405. sprite->texel[3].st[1] = spriteTexCoords[7];
  1406. r_mesh->sprites.push_back(sprite);
  1407. }
  1408. getWorld().addRoom(r_mesh);
  1409. rRoom->room = r_mesh;
  1410. getRender().addRoom(rRoom);
  1411. //printf(".");
  1412. //fflush(stdout);
  1413. }
  1414. printf("Done! Found %d rooms.\n", mTombRaider.NumRooms());
  1415. }