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

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