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

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