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 63KB

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