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.

LoaderTR2.cpp 46KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254
  1. /*!
  2. * \file src/loader/LoaderTR2.cpp
  3. * \brief TR2 level file loader
  4. *
  5. * \author xythobuz
  6. */
  7. #include <vector>
  8. #include "global.h"
  9. #include "Game.h"
  10. #include "Log.h"
  11. #include "Mesh.h"
  12. #include "Room.h"
  13. #include "SoundManager.h"
  14. #include "TextureManager.h"
  15. #include "World.h"
  16. #include "system/Sound.h"
  17. #include "utils/pixel.h"
  18. #include "loader/LoaderTR2.h"
  19. #include <glm/gtc/matrix_transform.hpp>
  20. int LoaderTR2::load(std::string f) {
  21. if (file.open(f) != 0) {
  22. return 1; // Could not open file
  23. }
  24. if (file.readU32() != 0x2D) {
  25. return 2; // Not a TR2 level?!
  26. }
  27. loadPalette();
  28. loadTextures();
  29. file.seek(file.tell() + 4); // Unused value?
  30. loadRooms();
  31. loadFloorData();
  32. loadMeshes();
  33. loadMoveables();
  34. loadStaticMeshes();
  35. loadTextiles();
  36. loadSprites();
  37. loadCameras();
  38. loadSoundSources();
  39. loadBoxesOverlapsZones();
  40. loadAnimatedTextures();
  41. loadItems();
  42. file.seek(file.tell() + 8192); // Skip Light map, only for 8bit coloring
  43. loadCinematicFrames();
  44. loadDemoData();
  45. loadSoundMap();
  46. loadSoundDetails();
  47. loadSampleIndices();
  48. loadExternalSoundFile(f);
  49. return 0;
  50. }
  51. // ---- Textures ----
  52. void LoaderTR2::loadPalette() {
  53. file.seek(file.tell() + 768); // Skip 8bit palette, 256 * 3 bytes
  54. // Read the 16bit palette, 256 * 4 bytes, RGBA, A unused
  55. for (int i = 0; i < 256; i++) {
  56. uint8_t r = file.readU8();
  57. uint8_t g = file.readU8();
  58. uint8_t b = file.readU8();
  59. uint8_t a = file.readU8();
  60. glm::vec4 c(r / 255.0f, g / 255.0f, b / 255.0f, 1.0f);
  61. TextureManager::setPalette(i, c);
  62. }
  63. }
  64. void LoaderTR2::loadTextures() {
  65. uint32_t numTextures = file.readU32();
  66. file.seek(file.tell() + (numTextures * 256 * 256)); // Skip 8bit textures
  67. // Read the 16bit textures, numTextures * 256 * 256 * 2 bytes
  68. for (unsigned int i = 0; i < numTextures; i++) {
  69. std::array<uint8_t, 256 * 256 * 2> arr;
  70. for (auto& x : arr) {
  71. x = file.readU8();
  72. }
  73. // Convert 16bit textures to 32bit textures
  74. unsigned char* img = argb16to32(&arr[0], 256, 256);
  75. int r = TextureManager::loadBufferSlot(img, 256, 256,
  76. ColorMode::ARGB, 32,
  77. TextureStorage::GAME, i);
  78. orAssertGreaterThanEqual(r, 0); //! \fixme properly handle error when texture could not be loaded!
  79. delete [] img;
  80. }
  81. if (numTextures > 0)
  82. Log::get(LOG_INFO) << "LoaderTR2: Found " << numTextures << " Textures!" << Log::endl;
  83. else
  84. Log::get(LOG_INFO) << "LoaderTR2: No Textures in this level?!" << Log::endl;
  85. }
  86. void LoaderTR2::loadTextiles() {
  87. uint32_t numObjectTextures = file.readU32();
  88. for (unsigned int o = 0; o < numObjectTextures; o++) {
  89. // 0 means that a texture is all-opaque, and that transparency
  90. // information is ignored.
  91. //
  92. // 1 means that transparency information is used. In 8-bit color,
  93. // index 0 is the transparent color, while in 16-bit color, the
  94. // top bit (0x8000) is the alpha channel (1 = opaque, 0 = transparent)
  95. //
  96. // 2 (TR3 only) means that the opacity (alpha) is equal to the intensity;
  97. // the brighter the color, the more opaque it is. The intensity is probably
  98. // calculated as the maximum of the individual color values.
  99. uint16_t attribute = file.readU16();
  100. // Index into the texture list
  101. uint16_t tile = file.readU16();
  102. TextureTile* t = new TextureTile(attribute, tile);
  103. // The four corner vertices of the texture
  104. // The Pixel values are the actual coordinates of the vertexs pixel
  105. // The Coordinate values depend on where the other vertices are in
  106. // the object texture. And if the object texture is used to specify
  107. // a triangle, then the fourth vertexs values will all be zero
  108. // Coordinate is 1 if Pixel is the low val, 255 if high val in object texture
  109. for (int i = 0; i < 4; i++) {
  110. uint8_t xCoordinate = file.readU8();
  111. uint8_t xPixel = file.readU8();
  112. uint8_t yCoordinate = file.readU8();
  113. uint8_t yPixel = file.readU8();
  114. orAssert((xCoordinate == 1) || (xCoordinate == 255) || (xCoordinate == 0));
  115. orAssert((yCoordinate == 1) || (yCoordinate == 255) || (yCoordinate == 0));
  116. t->add(TextureTileVertex(xCoordinate, xPixel, yCoordinate, yPixel));
  117. }
  118. TextureManager::addTile(t);
  119. }
  120. if (numObjectTextures > 0)
  121. Log::get(LOG_INFO) << "LoaderTR2: Found " << numObjectTextures << " Textiles!" << Log::endl;
  122. else
  123. Log::get(LOG_INFO) << "LoaderTR2: No Textiles in this level?!" << Log::endl;
  124. }
  125. void LoaderTR2::loadAnimatedTextures() {
  126. uint32_t numWords = file.readU32() - 1;
  127. uint16_t numAnimatedTextures = file.readU16();
  128. std::vector<uint16_t> animatedTextures;
  129. for (unsigned int a = 0; a < numWords; a++) {
  130. animatedTextures.push_back(file.readU16());
  131. }
  132. int pos = 0;
  133. for (unsigned int a = 0; a < numAnimatedTextures; a++) {
  134. int count = animatedTextures.at(pos) + 1;
  135. if ((pos + count) >= numWords) {
  136. Log::get(LOG_DEBUG) << "LoaderTR2: Invalid AnimatedTextures ("
  137. << pos + count << " >= " << numWords << ")!" << Log::endl;
  138. return;
  139. }
  140. for (int i = 0; i < count; i++) {
  141. TextureManager::addAnimatedTile(a, animatedTextures.at(pos + i + 1));
  142. }
  143. pos += count + 1;
  144. }
  145. if ((numAnimatedTextures > 0) || (numWords > 0))
  146. Log::get(LOG_INFO) << "LoaderTR2: Found " << numAnimatedTextures << " Animated Textures!" <<
  147. Log::endl;
  148. else
  149. Log::get(LOG_INFO) << "LoaderTR2: No Animated Textures in this level?!" << Log::endl;
  150. if (pos != numWords)
  151. Log::get(LOG_DEBUG) << "LoaderTR2: Extra bytes at end of AnimatedTextures?!" << Log::endl;
  152. }
  153. // ---- Rooms ----
  154. void LoaderTR2::loadRoomLights() {
  155. int16_t roomIntensity1 = file.read16();
  156. int16_t roomIntensity2 = file.read16();
  157. int16_t lightMode = file.read16();
  158. uint16_t numLights = file.readU16();
  159. for (unsigned int l = 0; l < numLights; l++) {
  160. // Position of light, in world coordinates
  161. int32_t x = file.read32();
  162. int32_t y = file.read32();
  163. int32_t z = file.read32();
  164. uint16_t intensity1 = file.readU16();
  165. uint16_t intensity2 = file.readU16(); // Almost always equal to intensity1
  166. uint32_t fade1 = file.readU32(); // Falloff value?
  167. uint32_t fade2 = file.readU32(); // Falloff value?
  168. // TODO store light somewhere
  169. }
  170. }
  171. void LoaderTR2::loadRoomStaticMeshes(std::vector<StaticModel*>& staticModels) {
  172. uint16_t numStaticMeshes = file.readU16();
  173. for (unsigned int s = 0; s < numStaticMeshes; s++) {
  174. // Absolute position in world coordinates
  175. int32_t x = file.read32();
  176. int32_t y = file.read32();
  177. int32_t z = file.read32();
  178. // High two bits (0xC000) indicate steps of
  179. // 90 degrees (eg. (rotation >> 14) * 90)
  180. uint16_t rotation = file.readU16();
  181. // Constant lighting, 0xFFFF means use mesh lighting
  182. //! \fixme Use static mesh lighting information
  183. uint16_t intensity1 = file.readU16();
  184. uint16_t intensity2 = file.readU16();
  185. // Which StaticMesh item to draw
  186. uint16_t objectID = file.readU16();
  187. staticModels.push_back(new StaticModel(glm::vec3(x, y, z),
  188. glm::radians((rotation >> 14) * 90.0f),
  189. objectID));
  190. }
  191. }
  192. void LoaderTR2::loadRoomDataEnd(int16_t& alternateRoom, unsigned int& roomFlags) {
  193. alternateRoom = file.read16();
  194. uint16_t flags = file.readU16();
  195. roomFlags = 0;
  196. if (flags & 0x0001) {
  197. roomFlags |= RoomFlagUnderWater;
  198. }
  199. }
  200. void LoaderTR2::loadRoomVertex(RoomVertexTR2& vert) {
  201. vert.x = file.read16();
  202. vert.y = file.read16();
  203. vert.z = file.read16();
  204. vert.light1 = file.read16();
  205. vert.attributes = file.readU16();
  206. vert.light2 = file.read16();
  207. }
  208. void LoaderTR2::loadRoomMesh(std::vector<IndexedRectangle>& rectangles,
  209. std::vector<IndexedRectangle>& triangles,
  210. uint16_t& numRectangles, uint16_t& numTriangles) {
  211. numRectangles = file.readU16();
  212. for (unsigned int r = 0; r < numRectangles; r++) {
  213. // Indices into the vertex list read just before
  214. uint16_t vertex1 = file.readU16();
  215. uint16_t vertex2 = file.readU16();
  216. uint16_t vertex3 = file.readU16();
  217. uint16_t vertex4 = file.readU16();
  218. // Index into the object-texture list
  219. uint16_t texture = file.readU16();
  220. rectangles.emplace_back(texture, vertex1, vertex2, vertex3, vertex4);
  221. }
  222. numTriangles = file.readU16();
  223. for (unsigned int t = 0; t < numTriangles; t++) {
  224. // Indices into the room vertex list
  225. uint16_t vertex1 = file.readU16();
  226. uint16_t vertex2 = file.readU16();
  227. uint16_t vertex3 = file.readU16();
  228. // Index into the object-texture list
  229. uint16_t texture = file.readU16();
  230. triangles.emplace_back(texture, vertex1, vertex2, vertex3);
  231. }
  232. }
  233. void LoaderTR2::loadRooms() {
  234. uint16_t numRooms = file.readU16();
  235. for (unsigned int i = 0; i < numRooms; i++) {
  236. // Room Header
  237. int32_t xOffset = file.read32();
  238. int32_t zOffset = file.read32();
  239. int32_t yBottom = file.read32(); // lowest point == largest y value
  240. int32_t yTop = file.read32(); // highest point == smallest y value
  241. glm::vec3 pos(xOffset, 0.0f, zOffset);
  242. // Number of data words (2 bytes) to follow
  243. uint32_t dataToFollow = file.readU32();
  244. glm::vec3 bbox[2] = {
  245. glm::vec3(0.0f, 0.0f, 0.0f),
  246. glm::vec3(0.0f, 0.0f, 0.0f)
  247. };
  248. uint16_t numVertices = file.readU16();
  249. std::vector<RoomVertexTR2> vertices;
  250. for (unsigned int v = 0; v < numVertices; v++) {
  251. RoomVertexTR2 vert;
  252. loadRoomVertex(vert);
  253. vertices.push_back(vert);
  254. // Fill bounding box
  255. if (v == 0) {
  256. for (int n = 0; n < 2; n++) {
  257. bbox[n].x = vert.x;
  258. bbox[n].y = vert.y;
  259. bbox[n].z = vert.z;
  260. }
  261. } else {
  262. if (vert.x < bbox[0].x)
  263. bbox[0].x = vert.x;
  264. if (vert.x > bbox[1].x)
  265. bbox[1].x = vert.x;
  266. if (vert.y < bbox[0].y)
  267. bbox[0].y = vert.y;
  268. if (vert.y > bbox[1].y)
  269. bbox[1].y = vert.y;
  270. if (vert.z < bbox[0].z)
  271. bbox[0].z = vert.z;
  272. if (vert.z > bbox[1].z)
  273. bbox[1].z = vert.z;
  274. }
  275. }
  276. bbox[0] += pos;
  277. bbox[1] += pos;
  278. std::vector<IndexedRectangle> rectangles;
  279. std::vector<IndexedRectangle> triangles;
  280. uint16_t numRectangles, numTriangles;
  281. loadRoomMesh(rectangles, triangles, numRectangles, numTriangles);
  282. uint16_t numSprites = file.readU16();
  283. std::vector<RoomSprite*> roomSprites;
  284. for (unsigned int s = 0; s < numSprites; s++) {
  285. uint16_t vertex = file.readU16(); // Index into vertex list
  286. uint16_t sprite = file.readU16(); // Index into sprite list
  287. auto& v = vertices.at(vertex);
  288. roomSprites.push_back(new RoomSprite(glm::vec3(v.x, v.y, v.z) + pos, sprite));
  289. }
  290. uint16_t numPortals = file.readU16();
  291. std::vector<Portal*> portals;
  292. for (unsigned int p = 0; p < numPortals; p++) {
  293. // Which room this portal leads to
  294. uint16_t adjoiningRoom = file.readU16();
  295. // Which way the portal faces
  296. // The normal points away from the adjacent room
  297. // To be seen through, it must point toward the viewpoint
  298. int16_t xNormal = file.read16();
  299. int16_t yNormal = file.read16();
  300. int16_t zNormal = file.read16();
  301. // The corners of this portal
  302. // The right-hand rule applies with respect to the normal
  303. int16_t xCorner1 = file.read16();
  304. int16_t yCorner1 = file.read16();
  305. int16_t zCorner1 = file.read16();
  306. int16_t xCorner2 = file.read16();
  307. int16_t yCorner2 = file.read16();
  308. int16_t zCorner2 = file.read16();
  309. int16_t xCorner3 = file.read16();
  310. int16_t yCorner3 = file.read16();
  311. int16_t zCorner3 = file.read16();
  312. int16_t xCorner4 = file.read16();
  313. int16_t yCorner4 = file.read16();
  314. int16_t zCorner4 = file.read16();
  315. portals.push_back(new Portal(adjoiningRoom,
  316. glm::vec3(xNormal, yNormal, zNormal),
  317. glm::vec3(xCorner1, yCorner1, zCorner1) + pos,
  318. glm::vec3(xCorner2, yCorner2, zCorner2) + pos,
  319. glm::vec3(xCorner3, yCorner3, zCorner3) + pos,
  320. glm::vec3(xCorner4, yCorner4, zCorner4) + pos));
  321. }
  322. uint16_t numZSectors = file.readU16();
  323. uint16_t numXSectors = file.readU16();
  324. for (unsigned int s = 0; s < (numZSectors * numXSectors); s++) {
  325. // Sectors are 1024*1024 world coordinates. Floor and Ceiling are
  326. // signed numbers of 256 units of height.
  327. // Floor/Ceiling value of 0x81 is used to indicate impenetrable
  328. // walls around the sector.
  329. // Floor values are used by the original engine to determine
  330. // what objects can be traversed and how. Relative steps of 1 (256)
  331. // can be walked up, 2..7 must be jumped up, larger than 7 is too high
  332. // If RoomAbove/Below is not none, the Ceiling/Floor is a collisional
  333. // portal to that room
  334. uint16_t indexFloorData = file.readU16();
  335. uint16_t indexBox = file.readU16(); // 0xFFFF if none
  336. uint8_t roomBelow = file.readU8(); // 0xFF if none
  337. int8_t floor = file.read8(); // Absolute height of floor (divided by 256)
  338. uint8_t roomAbove = file.readU8(); // 0xFF if none
  339. int8_t ceiling = file.read8(); // Absolute height of ceiling (/ 256)
  340. // In TR3 indexBox is more complicated. Only bits 4-14 are the 'real' index.
  341. // Bits 0-3 are most likely some kind of flag (footstep sound?).
  342. // There is a special value of the 'real' index, 2047 or 0x7FF.
  343. bool wall = false;
  344. if (((floor & 0xFF) == 0x81) || ((ceiling & 0xFF) == 0x81)) {
  345. wall = true;
  346. }
  347. //room->addSector(new Sector(floor * 256.0f, ceiling * 256.0f, wall));
  348. // TODO store sectors
  349. }
  350. loadRoomLights();
  351. std::vector<StaticModel*> staticModels;
  352. loadRoomStaticMeshes(staticModels);
  353. int16_t alternateRoom = -1;
  354. unsigned int roomFlags = 0;
  355. loadRoomDataEnd(alternateRoom, roomFlags);
  356. BoundingBox* boundingbox = new BoundingBox(bbox[0], bbox[1]);
  357. RoomMesh* mesh = new RoomMesh(vertices, rectangles, triangles);
  358. Room* room = new Room(pos, boundingbox, mesh, roomFlags, alternateRoom,
  359. numXSectors, numZSectors, i);
  360. for (auto p : portals)
  361. room->addPortal(p);
  362. for (auto m : staticModels)
  363. room->addModel(m);
  364. for (auto s : roomSprites)
  365. room->addSprite(s);
  366. World::addRoom(room);
  367. // Sanity check
  368. if ((numPortals == 0) && (numVertices == 0)
  369. && (numRectangles == 0) && (numTriangles == 0))
  370. Log::get(LOG_DEBUG) << "LoaderTR2: Room " << i << " seems invalid: " << numPortals << "p "
  371. << numRectangles << "r " << numTriangles << "t " << numVertices
  372. << "v" << Log::endl;
  373. }
  374. if (numRooms > 0)
  375. Log::get(LOG_INFO) << "LoaderTR2: Found " << numRooms << " Rooms!" << Log::endl;
  376. else
  377. Log::get(LOG_INFO) << "LoaderTR2: No Rooms in this Level?!" << Log::endl;
  378. }
  379. void LoaderTR2::loadFloorData() {
  380. uint32_t numFloorData = file.readU32();
  381. for (unsigned int f = 0; f < numFloorData; f++) {
  382. uint16_t unused = file.readU16();
  383. // TODO store floor data somewhere
  384. }
  385. if (numFloorData > 0)
  386. Log::get(LOG_INFO) << "LoaderTR2: Found " << numFloorData << " words FloorData, unimplemented!" <<
  387. Log::endl;
  388. else
  389. Log::get(LOG_INFO) << "LoaderTR2: No FloorData in this level?!" << Log::endl;
  390. }
  391. void LoaderTR2::loadSprites() {
  392. uint32_t numSpriteTextures = file.readU32();
  393. for (unsigned int s = 0; s < numSpriteTextures; s++) {
  394. uint16_t tile = file.readU16();
  395. uint8_t x = file.readU8();
  396. uint8_t y = file.readU8();
  397. uint16_t width = file.readU16(); // Actually (width * 256) + 255
  398. uint16_t height = file.readU16(); // Actually (height * 256) + 255
  399. // Required for what?
  400. int16_t leftSide = file.read16();
  401. int16_t topSide = file.read16();
  402. int16_t rightSide = file.read16();
  403. int16_t bottomSide = file.read16();
  404. Sprite* sp = new Sprite(tile, x, y, width, height);
  405. World::addSprite(sp);
  406. }
  407. uint32_t numSpriteSequences = file.readU32();
  408. for (unsigned int s = 0; s < numSpriteSequences; s++) {
  409. int32_t objectID = file.read32(); // Item identifier, matched in Items[]
  410. int16_t negativeLength = file.read16(); // Negative sprite count
  411. int16_t offset = file.read16(); // Where sequence starts in sprite texture list
  412. orAssertLessThan(negativeLength, 0);
  413. orAssertGreaterThanEqual(offset, 0);
  414. orAssertLessThanEqual(offset + (negativeLength * -1), numSpriteTextures);
  415. SpriteSequence* ss = new SpriteSequence(objectID, offset, (negativeLength * -1));
  416. World::addSpriteSequence(ss);
  417. }
  418. if ((numSpriteTextures > 0) || (numSpriteSequences > 0))
  419. Log::get(LOG_INFO) << "LoaderTR2: Found " << numSpriteTextures << " Sprites in " <<
  420. numSpriteSequences <<
  421. " Sequences!" << Log::endl;
  422. else
  423. Log::get(LOG_INFO) << "LoaderTR2: No Sprites in this level?!" << Log::endl;
  424. }
  425. // ---- Meshes ----
  426. int LoaderTR2::getPaletteIndex(uint16_t index) {
  427. return (index & 0xFF00) >> 8; // Use index into 16bit palette
  428. }
  429. void LoaderTR2::loadMeshes() {
  430. // Number of bitu16s of mesh data to follow
  431. // Read all the mesh data into a buffer, because
  432. // only afterward we can read the number of meshes
  433. // in this data block
  434. uint32_t numMeshData = file.readU32();
  435. std::vector<uint16_t> buffer;
  436. for (unsigned int i = 0; i < numMeshData; i++) {
  437. buffer.push_back(file.readU16());
  438. }
  439. uint32_t numMeshPointers = file.readU32();
  440. for (unsigned int i = 0; i < numMeshPointers; i++) {
  441. uint32_t meshPointer = file.readU32();
  442. if (numMeshData < (meshPointer / 2)) {
  443. Log::get(LOG_DEBUG) << "LoaderTR2: Invalid Mesh: "
  444. << (meshPointer / 2) << " > " << numMeshData << Log::endl;
  445. continue;
  446. }
  447. char* tmpPtr = reinterpret_cast<char*>(&buffer[meshPointer / 2]);
  448. BinaryMemory mem(tmpPtr, (numMeshData * 2) - meshPointer);
  449. int16_t mx = mem.read16();
  450. int16_t my = mem.read16();
  451. int16_t mz = mem.read16();
  452. int32_t collisionSize = mem.read32();
  453. // TODO store mesh collision info somewhere
  454. uint16_t numVertices = mem.readU16();
  455. std::vector<glm::vec3> vertices;
  456. for (int v = 0; v < numVertices; v++) {
  457. int16_t x = mem.read16();
  458. int16_t y = mem.read16();
  459. int16_t z = mem.read16();
  460. vertices.emplace_back(x, y, z);
  461. }
  462. int16_t numNormals = mem.read16();
  463. if (numNormals > 0) {
  464. // External vertex lighting is used, with the lighting calculated
  465. // from the rooms ambient and point-source lighting values. The
  466. // latter appears to use a simple Lambert law for directionality:
  467. // intensity is proportional to
  468. // max((normal direction).(direction to source), 0)
  469. for (int n = 0; n < numNormals; n++) {
  470. int16_t x = mem.read16();
  471. int16_t y = mem.read16();
  472. int16_t z = mem.read16();
  473. //mesh->addNormal(glm::vec3(x, y, z));
  474. }
  475. } else if (numNormals < 0) {
  476. // Internal vertex lighting is used,
  477. // using the data included with the mesh
  478. for (int l = 0; l < (numNormals * -1); l++) {
  479. int16_t light = mem.read16();
  480. // TODO store lights somewhere
  481. }
  482. }
  483. int16_t numTexturedRectangles = mem.read16();
  484. std::vector<IndexedRectangle> texturedRectangles;
  485. for (int r = 0; r < numTexturedRectangles; r++) {
  486. uint16_t vertex1 = mem.readU16();
  487. uint16_t vertex2 = mem.readU16();
  488. uint16_t vertex3 = mem.readU16();
  489. uint16_t vertex4 = mem.readU16();
  490. uint16_t texture = mem.readU16();
  491. texturedRectangles.emplace_back(texture, vertex1, vertex2, vertex3, vertex4);
  492. }
  493. int16_t numTexturedTriangles = mem.read16();
  494. std::vector<IndexedRectangle> texturedTriangles;
  495. for (int t = 0; t < numTexturedTriangles; t++) {
  496. uint16_t vertex1 = mem.readU16();
  497. uint16_t vertex2 = mem.readU16();
  498. uint16_t vertex3 = mem.readU16();
  499. uint16_t texture = mem.readU16();
  500. texturedTriangles.emplace_back(texture, vertex1, vertex2, vertex3);
  501. }
  502. int16_t numColoredRectangles = mem.read16();
  503. std::vector<IndexedColoredRectangle> coloredRectangles;
  504. for (int r = 0; r < numColoredRectangles; r++) {
  505. uint16_t vertex1 = mem.readU16();
  506. uint16_t vertex2 = mem.readU16();
  507. uint16_t vertex3 = mem.readU16();
  508. uint16_t vertex4 = mem.readU16();
  509. uint16_t texture = mem.readU16();
  510. int index = getPaletteIndex(texture);
  511. coloredRectangles.emplace_back(index, vertex1, vertex2, vertex3, vertex4);
  512. }
  513. int16_t numColoredTriangles = mem.read16();
  514. std::vector<IndexedColoredRectangle> coloredTriangles;
  515. for (int t = 0; t < numColoredTriangles; t++) {
  516. uint16_t vertex1 = mem.readU16();
  517. uint16_t vertex2 = mem.readU16();
  518. uint16_t vertex3 = mem.readU16();
  519. uint16_t texture = mem.readU16();
  520. int index = getPaletteIndex(texture);
  521. coloredTriangles.emplace_back(index, vertex1, vertex2, vertex3);
  522. }
  523. Mesh* mesh = new Mesh(vertices, texturedRectangles, texturedTriangles,
  524. coloredRectangles, coloredTriangles);
  525. World::addMesh(mesh);
  526. }
  527. if (numMeshPointers > 0)
  528. Log::get(LOG_INFO) << "LoaderTR2: Found " << numMeshPointers << " Meshes!" << Log::endl;
  529. else
  530. Log::get(LOG_INFO) << "LoaderTR2: No Meshes in this level?!" << Log::endl;
  531. }
  532. void LoaderTR2::loadStaticMeshes() {
  533. uint32_t numStaticMeshes = file.readU32();
  534. for (unsigned int s = 0; s < numStaticMeshes; s++) {
  535. uint32_t objectID = file.readU32(); // Matched in Items[]
  536. uint16_t mesh = file.readU16(); // Offset into MeshPointers[]
  537. // tr2_vertex BoundingBox[2][2];
  538. // First index is which one, second index is opposite corners
  539. int16_t x11 = file.read16();
  540. int16_t y11 = file.read16();
  541. int16_t z11 = file.read16();
  542. int16_t x12 = file.read16();
  543. int16_t y12 = file.read16();
  544. int16_t z12 = file.read16();
  545. int16_t x21 = file.read16();
  546. int16_t y21 = file.read16();
  547. int16_t z21 = file.read16();
  548. int16_t x22 = file.read16();
  549. int16_t y22 = file.read16();
  550. int16_t z22 = file.read16();
  551. // Meaning uncertain. Usually 2, and 3 for objects Lara can
  552. // travel through, like TR2s skeletons and underwater plants
  553. uint16_t flags = file.readU16();
  554. BoundingBox* bbox1 = new BoundingBox(glm::vec3(x11, y11, z11), glm::vec3(x12, y12, z12));
  555. BoundingBox* bbox2 = new BoundingBox(glm::vec3(x21, y21, z21), glm::vec3(x22, y22, z22));
  556. World::addStaticMesh(new StaticMesh(objectID, mesh, bbox1, bbox2));
  557. }
  558. if (numStaticMeshes > 0)
  559. Log::get(LOG_INFO) << "LoaderTR2: Found " << numStaticMeshes << " StaticMeshes!" << Log::endl;
  560. else
  561. Log::get(LOG_INFO) << "LoaderTR2: No StaticMeshes in this level?!" << Log::endl;
  562. }
  563. // ---- Moveables ----
  564. struct Animation_t {
  565. uint32_t frameOffset;
  566. uint8_t frameRate, frameSize;
  567. uint16_t stateID, frameStart, frameEnd, nextAnimation;
  568. uint16_t nextFrame, numStateChanges, stateChangeOffset;
  569. uint16_t numAnimCommands, animCommandOffset;
  570. Animation_t(uint32_t fo, uint8_t fr, uint8_t fs, uint16_t si,
  571. uint16_t fst, uint16_t fe, uint16_t na, uint16_t nf,
  572. uint16_t ns, uint16_t so, uint16_t nac, uint16_t ao)
  573. : frameOffset(fo), frameRate(fr), frameSize(fs),
  574. stateID(si), frameStart(fst), frameEnd(fe), nextAnimation(na),
  575. nextFrame(nf), numStateChanges(ns), stateChangeOffset(so),
  576. numAnimCommands(nac), animCommandOffset(ao) { }
  577. };
  578. struct StateChange_t {
  579. uint16_t stateID, numAnimDispatches, animDispatchOffset;
  580. StateChange_t(uint16_t s, uint16_t n, uint16_t a)
  581. : stateID(s), numAnimDispatches(n), animDispatchOffset(a) { }
  582. };
  583. struct AnimDispatch_t {
  584. int16_t low, high, nextAnimation, nextFrame;
  585. AnimDispatch_t(int16_t l, int16_t h, int16_t na, int16_t nf)
  586. : low(l), high(h), nextAnimation(na), nextFrame(nf) { }
  587. };
  588. void LoaderTR2::loadAngleSet(BoneFrame* bf, BinaryReader& frame, uint16_t numMeshes,
  589. uint16_t startingMesh, uint32_t meshTree,
  590. uint32_t numMeshTrees, std::vector<int32_t> meshTrees) {
  591. for (int i = 0; i < numMeshes; i++) {
  592. int mesh = startingMesh + i;
  593. glm::vec3 offset(0.0f, 0.0f, 0.0f);
  594. float rotation[3] = { 0.0f, 0.0f, 0.0f };
  595. char flag = (i == 0) ? 2 : 0;
  596. // Nonprimary tag - positioned relative to first tag
  597. if (i != 0) {
  598. char* tmp = reinterpret_cast<char*>(&meshTrees[0]) + meshTree; // TODO (meshTree * 4)?
  599. tmp += (i - 1) * 16; // TODO ?
  600. BinaryMemory tree(tmp, (numMeshTrees * 4) - meshTree - ((i - 1) * 16));
  601. flag = static_cast<char>(tree.readU32());
  602. offset.x = tree.read32();
  603. offset.y = tree.read32();
  604. offset.z = tree.read32();
  605. uint16_t a = frame.readU16();
  606. if (a & 0xC000) {
  607. // Single angle
  608. int index = 0;
  609. if ((a & 0x8000) && (a & 0x4000))
  610. index = 2;
  611. else if (a & 0x4000)
  612. index = 1;
  613. rotation[index] = (static_cast<float>(a & 0x03FF)) * 360.0f / 1024.0f;
  614. } else {
  615. // Three angles
  616. uint16_t b = frame.readU16();
  617. rotation[0] = (a & 0x3FF0) >> 4;
  618. rotation[1] = ((a & 0x000F) << 6) | ((b & 0xFC00) >> 10);
  619. rotation[2] = b & 0x03FF;
  620. for (int n = 0; n < 3; n++)
  621. rotation[n] = rotation[n] * 360.0f / 1024.0f;
  622. }
  623. }
  624. glm::vec3 rot(rotation[0], rotation[1], rotation[2]);
  625. BoneTag* bt = new BoneTag(mesh, offset, rot, flag);
  626. bf->add(bt);
  627. }
  628. }
  629. BoneFrame* LoaderTR2::loadFrame(BinaryReader& frame, uint16_t numMeshes,
  630. uint16_t startingMesh, uint32_t meshTree,
  631. uint32_t numMeshTrees, std::vector<int32_t> meshTrees) {
  632. int16_t bb1x = frame.read16();
  633. int16_t bb1y = frame.read16();
  634. int16_t bb1z = frame.read16();
  635. int16_t bb2x = frame.read16();
  636. int16_t bb2y = frame.read16();
  637. int16_t bb2z = frame.read16();
  638. glm::vec3 pos;
  639. pos.x = frame.read16();
  640. pos.y = frame.read16();
  641. pos.z = frame.read16();
  642. BoneFrame* bf = new BoneFrame(pos);
  643. loadAngleSet(bf, frame, numMeshes, startingMesh, meshTree, numMeshTrees, meshTrees);
  644. return bf;
  645. }
  646. void LoaderTR2::loadMoveables() {
  647. uint32_t numAnimations = file.readU32();
  648. std::vector<Animation_t> animations;
  649. for (unsigned int a = 0; a < numAnimations; a++) {
  650. // *Byte* Offset into Frames[] (so divide by 2!)
  651. uint32_t frameOffset = file.readU32();
  652. uint8_t frameRate = file.readU8(); // Engine ticks per frame
  653. // Number of bit16s in Frames[] used by this animation
  654. // Be careful when parsing frames using the FrameSize value
  655. // as the size of each frame, since an animations frame range
  656. // may extend into the next animations frame range, and that
  657. // may have a different FrameSize value.
  658. uint8_t frameSize = file.readU8();
  659. uint16_t stateID = file.readU16();
  660. file.seek(file.tell() + 8); // Skip 8 unknown bytes
  661. uint16_t frameStart = file.readU16(); // First frame in this animation
  662. uint16_t frameEnd = file.readU16(); // Last frame in this animation
  663. uint16_t nextAnimation = file.readU16();
  664. uint16_t nextFrame = file.readU16();
  665. uint16_t numStateChanges = file.readU16();
  666. uint16_t stateChangeOffset = file.readU16(); // Index into StateChanges[]
  667. uint16_t numAnimCommands = file.readU16(); // How many animation commands to use
  668. uint16_t animCommandOffset = file.readU16(); // Index into AnimCommand[]
  669. animations.emplace_back(frameOffset, frameRate, frameSize,
  670. stateID, frameStart, frameEnd, nextAnimation, nextFrame, numStateChanges,
  671. stateChangeOffset, numAnimCommands, animCommandOffset);
  672. }
  673. if (numAnimations > 0)
  674. Log::get(LOG_INFO) << "LoaderTR2: Found " << numAnimations << " Animations!" << Log::endl;
  675. else
  676. Log::get(LOG_INFO) << "LoaderTR2: No Animations in this level?!" << Log::endl;
  677. uint32_t numStateChanges = file.readU32();
  678. std::vector<StateChange_t> stateChanges;
  679. for (unsigned int s = 0; s < numStateChanges; s++) {
  680. uint16_t stateID = file.readU16();
  681. uint16_t numAnimDispatches = file.readU16(); // Number of ranges (always 1..5?)
  682. uint16_t animDispatchOffset = file.readU16(); // Index into AnimDispatches[]
  683. stateChanges.emplace_back(stateID, numAnimDispatches, animDispatchOffset);
  684. }
  685. if (numStateChanges > 0)
  686. Log::get(LOG_INFO) << "LoaderTR2: Found " << numStateChanges << " StateChanges!" << Log::endl;
  687. else
  688. Log::get(LOG_INFO) << "LoaderTR2: No StateChanges in this level?!" << Log::endl;
  689. uint32_t numAnimDispatches = file.readU32();
  690. std::vector<AnimDispatch_t> animDispatches;
  691. for (unsigned int a = 0; a < numAnimDispatches; a++) {
  692. int16_t low = file.read16(); // Lowest frame that uses this range
  693. int16_t high = file.read16(); // Highest frame (+1?) that uses this range
  694. int16_t nextAnimation = file.read16(); // Animation to go to
  695. int16_t nextFrame = file.read16(); // Frame offset to go to
  696. animDispatches.emplace_back(low, high, nextAnimation, nextFrame);
  697. }
  698. if (numAnimDispatches > 0)
  699. Log::get(LOG_INFO) << "LoaderTR2: Found " << numAnimDispatches << " AnimationDispatches!" <<
  700. Log::endl;
  701. else
  702. Log::get(LOG_INFO) << "LoaderTR2: No AnimationDispatches in this level?!" << Log::endl;
  703. uint32_t numAnimCommands = file.readU32();
  704. std::vector<int16_t> animCommands;
  705. for (unsigned int a = 0; a < numAnimCommands; a++) {
  706. // A list of Opcodes with zero or more operands each,
  707. // some referring to the whole animation (jump/grab points),
  708. // some to specific frames (sound, bubbles, ...).
  709. animCommands.push_back(file.read16());
  710. }
  711. if (numAnimCommands > 0)
  712. Log::get(LOG_INFO) << "LoaderTR2: Found " << numAnimCommands << " AnimationCommands!" << Log::endl;
  713. else
  714. Log::get(LOG_INFO) << "LoaderTR2: No AnimationCommands in this level?!" << Log::endl;
  715. // This is really one uint32_t flags, followed by
  716. // three int32_t x, y, z. However, we're given the number
  717. // of 32bits, as well as byte indices later, so we store
  718. // it as a single list of int32_t.
  719. uint32_t numMeshTrees = file.readU32();
  720. std::vector<int32_t> meshTrees;
  721. for (unsigned int m = 0; m < numMeshTrees; m++) {
  722. // 0x0002 - Put parent mesh on the mesh stack
  723. // 0x0001 - Pop mesh from stack, use as parent mesh
  724. // When both are not set, use previous mesh as parent mesh
  725. // When both are set, do 0x0001 first, then 0x0002, thereby
  726. // reading the stack but not changing it
  727. //uint32_t flags = file.readU32();
  728. // Offset of mesh origin from the parent mesh origin
  729. //int32_t x = file.read32();
  730. //int32_t y = file.read32();
  731. //int32_t z = file.read32();
  732. meshTrees.push_back(file.read32());
  733. }
  734. if (numMeshTrees > 0)
  735. Log::get(LOG_INFO) << "LoaderTR2: Found " << numMeshTrees << " MeshTrees!" << Log::endl;
  736. else
  737. Log::get(LOG_INFO) << "LoaderTR2: No MeshTrees in this level?!" << Log::endl;
  738. uint32_t numFrames = file.readU32();
  739. std::vector<uint16_t> frames;
  740. for (unsigned int f = 0; f < numFrames; f++) {
  741. // int16 bb1x, bb1y, bb1z
  742. // int16 bb2x, bb2y, bb2z
  743. // int16 offsetX, offsetY, offsetZ
  744. // What follows next is a list of angles with numMeshes (from Moveable) entries.
  745. // If the top bit (0x8000) of the first uint16 is set, a single X angle follows,
  746. // if the second bit (0x4000) is set, a Y angle follows, both are a Z angle.
  747. // If none is set, it's a three-axis rotation. The next 10 bits (0x3FF0) are
  748. // the X rotation, the next 10 (0x000F 0xFC00) are Y, the next (0x03FF) are
  749. // the Z rotation. The scaling is always 0x100->90deg.
  750. // Rotation order: Y, X, Z!
  751. frames.push_back(file.readU16());
  752. }
  753. if (numFrames > 0)
  754. Log::get(LOG_INFO) << "LoaderTR2: Found " << numFrames << " Frames!" << Log::endl;
  755. else
  756. Log::get(LOG_INFO) << "LoaderTR2: No Frames in this level?!" << Log::endl;
  757. uint32_t numMoveables = file.readU32();
  758. for (unsigned int m = 0; m < numMoveables; m++) {
  759. // Item identifier, matched in Items[]
  760. uint32_t objectID = file.readU32();
  761. uint16_t numMeshes = file.readU16();
  762. uint16_t startingMesh = file.readU16(); // Offset into MeshPointers[]
  763. uint32_t meshTree = file.readU32(); // Offset into MeshTree[]
  764. // *Byte* offset into Frames[] (divide by 2 for Frames[i])
  765. uint32_t frameOffset = file.readU32(); // Only needed if no animation
  766. // If animation index is 0xFFFF, the object is stationary or
  767. // animated by the engine (ponytail)
  768. uint16_t animation = file.readU16();
  769. /*
  770. if (animation == 0xFFFF) {
  771. */
  772. // Just add the frame indicated in frameOffset, nothing else
  773. char* tmp = reinterpret_cast<char*>(&frames[0]) + frameOffset;
  774. BinaryMemory frame(tmp, (numFrames * 2) - frameOffset);
  775. if (((numFrames * 2) - frameOffset) <= 0)
  776. continue; // TR1/LEVEL3A crashes without this?!
  777. BoneFrame* bf = loadFrame(frame, numMeshes, startingMesh, meshTree, numMeshTrees, meshTrees);
  778. AnimationFrame* af = new AnimationFrame(0);
  779. af->add(bf);
  780. SkeletalModel* sm = new SkeletalModel(objectID);
  781. sm->add(af);
  782. World::addSkeletalModel(sm);
  783. /*
  784. } else {
  785. // TODO Add the whole animation hierarchy
  786. auto& anim = animations.at(animation);
  787. char* tmp = reinterpret_cast<char*>(&frames[0]) + anim.frameOffset;
  788. BinaryMemory frame(tmp, (numFrames * 2) - anim.frameOffset);
  789. AnimationFrame* af = new AnimationFrame(0);
  790. for (int i = 0; i < ((anim.frameEnd - anim.frameStart) + 1); i++) {
  791. BoneFrame* bf = loadFrame(frame, numMeshes, startingMesh,
  792. meshTree, numMeshTrees, meshTrees);
  793. af->add(bf);
  794. }
  795. SkeletalModel* sm = new SkeletalModel(objectID);
  796. sm->add(af);
  797. World::addSkeletalModel(sm);
  798. }
  799. */
  800. }
  801. if (numMoveables > 0)
  802. Log::get(LOG_INFO) << "LoaderTR2: Found " << numMoveables << " Moveables!" << Log::endl;
  803. else
  804. Log::get(LOG_INFO) << "LoaderTR2: No Moveables in this level?!" << Log::endl;
  805. }
  806. void LoaderTR2::loadItems() {
  807. uint32_t numItems = file.readU32();
  808. for (unsigned int i = 0; i < numItems; i++) {
  809. int16_t objectID = file.read16();
  810. int16_t room = file.read16();
  811. // Item position in world coordinates
  812. int32_t x = file.read32();
  813. int32_t y = file.read32();
  814. int32_t z = file.read32();
  815. uint16_t angle = file.readU16(); // (0xC000 >> 14) * 90deg
  816. int16_t intensity1 = file.read16(); // Constant lighting; -1 means mesh lighting
  817. int16_t intensity2 = file.read16(); // Almost always like intensity1
  818. // 0x0100 - Initially visible
  819. // 0x3E00 - Activation mask, open, can be XORed with related FloorData list fields.
  820. uint16_t flags = file.readU16();
  821. glm::vec3 pos(
  822. static_cast<float>(x),
  823. static_cast<float>(y),
  824. static_cast<float>(z)
  825. );
  826. glm::vec3 rot(
  827. 0.0f,
  828. glm::radians(((angle >> 14) & 0x03) * 90.0f),
  829. 0.0f
  830. );
  831. Entity* e = new Entity(objectID, room, pos, rot);
  832. World::addEntity(e);
  833. if (objectID == 0) {
  834. Game::setLara(World::sizeEntity() - 1);
  835. }
  836. }
  837. if (numItems > 0)
  838. Log::get(LOG_INFO) << "LoaderTR2: Found " << numItems << " Items!" << Log::endl;
  839. else
  840. Log::get(LOG_INFO) << "LoaderTR2: No Items in this level?!" << Log::endl;
  841. }
  842. void LoaderTR2::loadBoxesOverlapsZones() {
  843. uint32_t numBoxes = file.readU32();
  844. for (unsigned int b = 0; b < numBoxes; b++) {
  845. // Sectors (* 1024 units)
  846. uint8_t zMin = file.readU8();
  847. uint8_t zMax = file.readU8();
  848. uint8_t xMin = file.readU8();
  849. uint8_t xMax = file.readU8();
  850. int16_t trueFloor = file.read16(); // Y value (no scaling)
  851. // Index into overlaps[]. The high bit is sometimes set
  852. // this occurs in front of swinging doors and the like
  853. uint16_t overlapIndex = file.readU16();
  854. // TODO store boxes somewhere
  855. }
  856. uint32_t numOverlaps = file.readU32();
  857. std::vector<std::vector<uint16_t>> overlaps;
  858. overlaps.emplace_back();
  859. unsigned int list = 0;
  860. for (unsigned int o = 0; o < numOverlaps; o++) {
  861. // Apparently used by NPCs to decide where to go next.
  862. // List of neighboring boxes for each box.
  863. // Each entry is a uint16, 0x8000 set marks end of list.
  864. uint16_t e = file.readU16();
  865. overlaps.at(list).push_back(e);
  866. if (e & 0x8000) {
  867. overlaps.emplace_back();
  868. list++;
  869. }
  870. }
  871. // TODO store overlaps somewhere
  872. for (unsigned int z = 0; z < numBoxes; z++) {
  873. // Normal room state
  874. int16_t ground1 = file.read16();
  875. int16_t ground2 = file.read16();
  876. int16_t ground3 = file.read16();
  877. int16_t ground4 = file.read16();
  878. int16_t fly = file.read16();
  879. // Alternate room state
  880. int16_t ground1alt = file.read16();
  881. int16_t ground2alt = file.read16();
  882. int16_t ground3alt = file.read16();
  883. int16_t ground4alt = file.read16();
  884. int16_t flyAlt = file.read16();
  885. // TODO store zones somewhere
  886. }
  887. if ((numBoxes > 0) || (numOverlaps > 0))
  888. Log::get(LOG_INFO) << "LoaderTR2: Found NPC NavigationHints (" << numBoxes
  889. << ", " << numOverlaps << ", " << list << "), unimplemented!" << Log::endl;
  890. else
  891. Log::get(LOG_INFO) << "LoaderTR2: No NPC NavigationHints in this level?!" << Log::endl;
  892. }
  893. // ---- Sound ----
  894. void LoaderTR2::loadSoundSources() {
  895. uint32_t numSoundSources = file.readU32();
  896. for (unsigned int s = 0; s < numSoundSources; s++) {
  897. // Absolute world coordinate positions of sound source
  898. int32_t x = file.read32();
  899. int32_t y = file.read32();
  900. int32_t z = file.read32();
  901. // Internal sound index
  902. uint16_t soundID = file.readU16();
  903. // Unknown, 0x40, 0x80 or 0xC0
  904. uint16_t flags = file.readU16();
  905. SoundManager::addSoundSource(glm::vec3(x, y, z), soundID, flags);
  906. }
  907. if (numSoundSources > 0)
  908. Log::get(LOG_INFO) << "LoaderTR2: Found " << numSoundSources << " SoundSources" << Log::endl;
  909. else
  910. Log::get(LOG_INFO) << "LoaderTR2: No SoundSources in this level?!" << Log::endl;
  911. }
  912. void LoaderTR2::loadSoundMap() {
  913. for (int i = 0; i < 370; i++) {
  914. SoundManager::addSoundMapEntry(file.read16());
  915. }
  916. }
  917. void LoaderTR2::loadSoundDetails() {
  918. uint32_t numSoundDetails = file.readU32();
  919. for (unsigned int s = 0; s < numSoundDetails; s++) {
  920. uint16_t sample = file.readU16(); // Index into SampleIndices[]
  921. uint16_t volume = file.readU16();
  922. // sound range? distance at which this sound can be heard?
  923. uint16_t unknown1 = file.readU16();
  924. // Bits 8-15: priority?
  925. // Bits 2-7: number of samples in this group
  926. // Bits 0-1: channel number?
  927. uint16_t unknown2 = file.readU16();
  928. SoundManager::addSoundDetail(sample, volume / 32767.0f);
  929. }
  930. if (numSoundDetails > 0)
  931. Log::get(LOG_INFO) << "LoaderTR2: Found " << numSoundDetails << " SoundDetails" << Log::endl;
  932. else
  933. Log::get(LOG_INFO) << "LoaderTR2: No SoundDetails in this level?!" << Log::endl;
  934. }
  935. void LoaderTR2::loadSampleIndices() {
  936. uint32_t numSampleIndices = file.readU32();
  937. for (unsigned int i = 0; i < numSampleIndices; i++) {
  938. SoundManager::addSampleIndex(file.readU32());
  939. }
  940. if (numSampleIndices > 0)
  941. Log::get(LOG_INFO) << "LoaderTR2: Found " << numSampleIndices << " SampleIndices" << Log::endl;
  942. else
  943. Log::get(LOG_INFO) << "LoaderTR2: No SampleIndices in this level?!" << Log::endl;
  944. }
  945. void LoaderTR2::loadExternalSoundFile(std::string f) {
  946. size_t dir = f.find_last_of("/\\");
  947. if (dir != std::string::npos) {
  948. f.replace(dir + 1, std::string::npos, "MAIN.SFX");
  949. } else {
  950. f = "MAIN.SFX";
  951. }
  952. BinaryFile sfx;
  953. if (sfx.open(f) != 0) {
  954. Log::get(LOG_INFO) << "LoaderTR2: Can't open \"" << f << "\"!" << Log::endl;
  955. return;
  956. }
  957. Log::get(LOG_INFO) << "LoaderTR2: Loading \"" << f << "\"" << Log::endl;
  958. int riffCount = loadSoundFiles(sfx);
  959. if (riffCount > 0)
  960. Log::get(LOG_INFO) << "LoaderTR2: Loaded " << riffCount << " SoundSamples" << Log::endl;
  961. else if (riffCount == 0)
  962. Log::get(LOG_INFO) << "LoaderTR2: No SoundSamples found!" << Log::endl;
  963. else
  964. Log::get(LOG_ERROR) << "LoaderTR2: Error loading SoundSamples!" << Log::endl;
  965. }
  966. int LoaderTR2::loadSoundFiles(BinaryReader& sfx, unsigned int count) {
  967. int riffCount = 0;
  968. while (!sfx.eof()) {
  969. if ((count > 0) && (riffCount >= count))
  970. break;
  971. char test[5];
  972. test[4] = '\0';
  973. for (int i = 0; i < 4; i++)
  974. test[i] = sfx.read8();
  975. if (std::string("RIFF") != std::string(test)) {
  976. Log::get(LOG_DEBUG) << "LoaderTR2: SoundSamples invalid! (" << riffCount
  977. << ", \"" << test << "\" != \"RIFF\")" << Log::endl;
  978. return -1;
  979. }
  980. // riffSize is (fileLength - 8)
  981. uint32_t riffSize = sfx.readU32();
  982. unsigned char* buff = new unsigned char[riffSize + 8];
  983. sfx.seek(sfx.tell() - 8);
  984. for (int i = 0; i < (riffSize + 8); i++)
  985. buff[i] = sfx.readU8();
  986. int ret = Sound::loadBuffer(buff, riffSize + 8);
  987. delete [] buff;
  988. orAssertGreaterThanEqual(ret, 0);
  989. riffCount++;
  990. }
  991. return riffCount;
  992. }
  993. // ---- Stuff ----
  994. void LoaderTR2::loadCameras() {
  995. uint32_t numCameras = file.readU32();
  996. for (unsigned int c = 0; c < numCameras; c++) {
  997. int32_t x = file.read32();
  998. int32_t y = file.read32();
  999. int32_t z = file.read32();
  1000. int16_t room = file.read16();
  1001. file.seek(file.tell() + 2); // Unknown, correlates to Boxes? Zones?
  1002. // TODO store cameras somewhere
  1003. }
  1004. if (numCameras > 0)
  1005. Log::get(LOG_INFO) << "LoaderTR2: Found " << numCameras << " Cameras, unimplemented!" << Log::endl;
  1006. }
  1007. void LoaderTR2::loadCinematicFrames() {
  1008. uint16_t numCinematicFrames = file.readU16();
  1009. for (unsigned int c = 0; c < numCinematicFrames; c++) {
  1010. int16_t rotY = file.read16(); // Y rotation, +-32767 = +-180deg
  1011. int16_t rotZ = file.read16(); // Z rotation, like rotY
  1012. int16_t rotZ2 = file.read16(); // Like rotZ?
  1013. int16_t posZ = file.read16(); // Camera pos relative to what?
  1014. int16_t posY = file.read16();
  1015. int16_t posX = file.read16();
  1016. int16_t unknown = file.read16(); // Changing this can cause runtime error
  1017. int16_t rotX = file.read16(); // X rotation, like rotY
  1018. // TODO store cinematic frames somewhere
  1019. }
  1020. if (numCinematicFrames > 0)
  1021. Log::get(LOG_INFO) << "LoaderTR2: Found " << numCinematicFrames
  1022. << " CinematicFrames, unimplemented!" << Log::endl;
  1023. }
  1024. void LoaderTR2::loadDemoData() {
  1025. uint16_t numDemoData = file.readU16();
  1026. for (unsigned int d = 0; d < numDemoData; d++)
  1027. file.readU8();
  1028. // TODO store demo data somewhere, find out meaning
  1029. if (numDemoData > 0)
  1030. Log::get(LOG_INFO) << "LoaderTR2: Found " << numDemoData << " bytes DemoData, unimplemented!" <<
  1031. Log::endl;
  1032. }