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

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