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.

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