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

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