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.

Render.cpp 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. /*!
  2. * \file src/Render.cpp
  3. * \brief OpenRaider Renderer class
  4. *
  5. * \author Mongoose
  6. * \author xythobuz
  7. */
  8. #include <algorithm>
  9. #include <sstream>
  10. #include <stdlib.h>
  11. #include <math.h>
  12. #include <string.h>
  13. #include "global.h"
  14. #include "Camera.h"
  15. #include "Game.h"
  16. #include "Render.h"
  17. #include "utils/strings.h"
  18. #include "utils/tga.h"
  19. #include "Window.h"
  20. #include "World.h"
  21. Render::Render() {
  22. mSkyMesh = -1;
  23. mSkyMeshRotation = false;
  24. mMode = Render::modeDisabled;
  25. mLock = 0;
  26. mFlags = (fRoomAlpha | fEntityModels | fRenderPonytail);
  27. debugTexture = -1;
  28. debugTextile = -1;
  29. debugTextureStorage = TextureManager::TextureStorage::GAME;
  30. debugX = 0.0f;
  31. debugY = 0.0f;
  32. debugW = 256.0f;
  33. debugH = 256.0f;
  34. }
  35. Render::~Render() {
  36. ClearWorld();
  37. }
  38. void Render::ClearWorld() {
  39. mRoomRenderList.clear();
  40. }
  41. void Render::screenShot(const char* filenameBase) {
  42. int sz = getWindow().getWidth() * getWindow().getHeight();
  43. unsigned char* image = new unsigned char[sz * 3];
  44. static int count = 0;
  45. bool done = false;
  46. assert(filenameBase != nullptr);
  47. // Don't overwrite files
  48. std::ostringstream filename;
  49. while (!done) {
  50. filename << filenameBase << "-" << count++ << ".tga";
  51. FILE* f = fopen(filename.str().c_str(), "rb");
  52. if (f) {
  53. fclose(f);
  54. } else {
  55. done = true;
  56. }
  57. }
  58. glReadPixels(0, 0, getWindow().getWidth(), getWindow().getHeight(), GL_BGR_EXT, GL_UNSIGNED_BYTE,
  59. image);
  60. tgaSave(filename.str().c_str(), image, getWindow().getWidth(), getWindow().getHeight(), 0);
  61. delete [] image;
  62. }
  63. unsigned int Render::getFlags() {
  64. return mFlags;
  65. }
  66. void Render::lightRoom(Room& room) {
  67. for (unsigned int i = 0; i < room.sizeLights(); ++i) {
  68. Light& light = room.getLight(i);
  69. float pos[4], color[4];
  70. float dir[3];
  71. light.getPos(pos);
  72. light.getColor(color);
  73. light.getDir(dir);
  74. glEnable(GL_LIGHT0 + i);
  75. switch (light.getType()) {
  76. case Light::typeSpot:
  77. glLightf(GL_LIGHT0 + i, GL_SPOT_CUTOFF, light.getCutoff());
  78. glLightfv(GL_LIGHT0 + i, GL_POSITION, pos);
  79. glLightfv(GL_LIGHT0 + i, GL_SPOT_DIRECTION, dir);
  80. glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, color);
  81. break;
  82. case Light::typePoint:
  83. case Light::typeDirectional:
  84. glLightf(GL_LIGHT0 + i, GL_CONSTANT_ATTENUATION, 1.0); // 1.0
  85. // GL_QUADRATIC_ATTENUATION
  86. // GL_LINEAR_ATTENUATION
  87. glLightf(GL_LIGHT0 + i, GL_LINEAR_ATTENUATION, light.getAtt());
  88. glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, color); // GL_DIFFUSE
  89. glLightfv(GL_LIGHT0 + i, GL_POSITION, pos);
  90. break;
  91. }
  92. }
  93. }
  94. void Render::clearFlags(unsigned int flags) {
  95. mFlags &= ~flags;
  96. if (flags & Render::fFog) {
  97. if (glIsEnabled(GL_FOG)) {
  98. glDisable(GL_FOG);
  99. }
  100. }
  101. if (flags & Render::fGL_Lights) {
  102. glDisable(GL_LIGHTING);
  103. }
  104. }
  105. void Render::setFlags(unsigned int flags) {
  106. mFlags |= flags;
  107. if (flags & Render::fFog) {
  108. glEnable(GL_FOG);
  109. glFogf(GL_FOG_MODE, GL_EXP2);
  110. glFogf(GL_FOG_DENSITY, 0.00008f);
  111. glFogf(GL_FOG_START, 30000.0f);
  112. glFogf(GL_FOG_END, 50000.0f);
  113. float color[4];
  114. color[0] = BLACK[0] * 256.0f;
  115. color[1] = BLACK[1] * 256.0f;
  116. color[2] = BLACK[2] * 256.0f;
  117. color[3] = BLACK[3] * 256.0f;
  118. glFogfv(GL_FOG_COLOR, color);
  119. }
  120. if (flags & Render::fGL_Lights) {
  121. float color[4];
  122. color[0] = WHITE[0] * 256.0f;
  123. color[1] = WHITE[1] * 256.0f;
  124. color[2] = WHITE[2] * 256.0f;
  125. color[3] = WHITE[3] * 256.0f;
  126. glEnable(GL_LIGHTING);
  127. glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 0);
  128. glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, color);
  129. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, color);
  130. color[0] = GREY[0] * 256.0f;
  131. color[1] = GREY[1] * 256.0f;
  132. color[2] = GREY[2] * 256.0f;
  133. color[3] = GREY[3] * 256.0f;
  134. glLightModelfv(GL_LIGHT_MODEL_AMBIENT, color);
  135. }
  136. }
  137. int Render::getMode() {
  138. return mMode;
  139. }
  140. void Render::setMode(int n) {
  141. mMode = n;
  142. switch (mMode) {
  143. case Render::modeDisabled:
  144. break;
  145. case Render::modeSolid:
  146. case Render::modeWireframe:
  147. glClearColor(PURPLE[0] / 256.0f, PURPLE[1] / 256.0f,
  148. PURPLE[2] / 256.0f, PURPLE[3] / 256.0f);
  149. glDisable(GL_TEXTURE_2D);
  150. break;
  151. default:
  152. if (mMode == Render::modeLoadScreen) {
  153. glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  154. } else {
  155. glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  156. }
  157. glClearColor(BLACK[0] / 256.0f, BLACK[1] / 256.0f,
  158. BLACK[2] / 256.0f, BLACK[3] / 256.0f);
  159. glEnable(GL_TEXTURE_2D);
  160. }
  161. }
  162. void Render::display() {
  163. switch (mMode) {
  164. case Render::modeDisabled:
  165. return;
  166. case Render::modeLoadScreen:
  167. drawLoadScreen();
  168. return;
  169. default:
  170. break;
  171. }
  172. if (mMode == Render::modeWireframe)
  173. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  174. else
  175. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  176. float camOffsetH = 0.0f;
  177. switch (getGame().getLara().getMoveType()) {
  178. case Entity::MoveTypeFly:
  179. case Entity::MoveTypeNoClipping:
  180. case Entity::MoveTypeSwim:
  181. //camOffsetH = 64.0f;
  182. camOffsetH = 512.0f;
  183. break;
  184. case Entity::MoveTypeWalk:
  185. case Entity::MoveTypeWalkNoSwim:
  186. camOffsetH = 512.0f;
  187. break;
  188. }
  189. float curPos[3], camPos[3], atPos[3];
  190. curPos[0] = getGame().getLara().getPos(0);
  191. curPos[1] = getGame().getLara().getPos(1);
  192. curPos[2] = getGame().getLara().getPos(2);
  193. float yaw = getGame().getLara().getAngle(1);
  194. // Mongoose 2002.08.24, New 3rd person camera hack
  195. camPos[0] = curPos[0] - (1024.0f * sinf(yaw));
  196. camPos[1] = curPos[1] - camOffsetH; // UP is lower val
  197. camPos[2] = curPos[2] - (1024.0f * cosf(yaw));
  198. long index = getGame().getLara().getRoom();
  199. long sector = getWorld().getRoom(index).getSector(camPos[0], camPos[2]);
  200. // Handle camera out of world
  201. if ((sector < 0) ||
  202. ((unsigned int)sector >= getWorld().getRoom(index).sizeSectors()) ||
  203. getWorld().getRoom(index).isWall(sector)) {
  204. camPos[0] = curPos[0] + (64.0f * sinf(yaw));
  205. camPos[1] -= 64.0f;
  206. camPos[2] = curPos[2] + (64.0f * cosf(yaw));
  207. }
  208. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  209. glLoadIdentity();
  210. // Setup view in OpenGL with camera
  211. getCamera().setPosition(camPos);
  212. getCamera().update();
  213. getCamera().getTarget(atPos);
  214. // Mongoose 2002.08.13, Quick fix to render OpenRaider upside down
  215. Window::lookAt(camPos[0], camPos[1], camPos[2], atPos[0], atPos[1], atPos[2], 0.0f, -1.0f, 0.0f);
  216. // Update view volume for vising
  217. updateViewVolume();
  218. // Render world
  219. glColor3ubv(GREY); // was WHITE
  220. drawSkyMesh(96.0);
  221. // Figure out how much of the map to render
  222. newRoomRenderList(index);
  223. // Room solid pass, need to do depth sorting to avoid 2 pass render
  224. for (unsigned int i = 0; i < mRoomRenderList.size(); i++) {
  225. Room& room = *mRoomRenderList[i];
  226. if (mFlags & Render::fGL_Lights)
  227. lightRoom(room);
  228. room.display(false);
  229. }
  230. // Draw all visible enities
  231. if (mFlags & Render::fEntityModels) {
  232. std::vector<Entity*> entityRenderList;
  233. for (unsigned int i = 0; i < getWorld().sizeEntity(); i++) {
  234. Entity& e = getWorld().getEntity(i);
  235. // Don't show Lara to the player
  236. if (&e == &getGame().getLara())
  237. continue;
  238. // Mongoose 2002.08.15, Nothing to draw, skip
  239. // Mongoose 2002.12.24, Some entities have no animation =p
  240. if (e.getModel().size() == 0)
  241. continue;
  242. // Is it in view volume? ( Hack to use sphere )
  243. if (!isVisible(e.getPos(0), e.getPos(1), e.getPos(2), 512.0f))
  244. continue;
  245. //! \fixme Is it in a room we're rendering?
  246. //if (mRoomRenderList[e->room] == 0x0)
  247. //{
  248. // continue;
  249. //}
  250. entityRenderList.push_back(&e);
  251. }
  252. // Draw objects not tied to rooms
  253. glPushMatrix();
  254. // Draw lara or other player model ( move to entity rendering method )
  255. getGame().getLara().display();
  256. // Draw sprites after player to handle alpha
  257. for (unsigned int i = 0; i < getWorld().sizeSprite(); i++) {
  258. SpriteSequence& sprite = getWorld().getSprite(i);
  259. for (unsigned int j = 0; j < sprite.size(); j++)
  260. sprite.get(j).display();
  261. }
  262. glPopMatrix();
  263. // Depth sort entityRenderList and display each entity
  264. std::sort(entityRenderList.begin(), entityRenderList.end(), Entity::compare);
  265. for (unsigned int i = 0; i < entityRenderList.size(); i++) {
  266. entityRenderList[i]->display();
  267. }
  268. }
  269. // Room alpha pass
  270. // Skip room alpha pass for modes w/o texture
  271. if (!(mMode == Render::modeSolid || mMode == Render::modeWireframe)) {
  272. for (unsigned int i = 0; i < mRoomRenderList.size(); i++)
  273. mRoomRenderList[i]->display(true);
  274. if (debugTexture >= 0) {
  275. getWindow().glEnter2D();
  276. drawTexture(debugX, debugY, debugW, debugH, debugTexture, debugTextureStorage);
  277. getWindow().glExit2D();
  278. } else if (debugTextile >= 0) {
  279. getWindow().glEnter2D();
  280. drawTextile(debugX, debugY, debugW, debugH, debugTextile);
  281. getWindow().glExit2D();
  282. }
  283. }
  284. if (mMode == Render::modeWireframe)
  285. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  286. glFlush();
  287. }
  288. void Render::drawLoadScreen() {
  289. getWindow().glEnter2D();
  290. drawTexture(0.0f, 0.0f, getWindow().getWidth(), getWindow().getHeight(),
  291. TEXTURE_SPLASH, TextureManager::TextureStorage::SYSTEM);
  292. if (debugTexture >= 0)
  293. drawTexture(debugX, debugY, debugW, debugH, debugTexture, debugTextureStorage);
  294. else if (debugTextile >= 0)
  295. drawTextile(debugX, debugY, debugW, debugH, debugTextile);
  296. getWindow().glExit2D();
  297. glFlush();
  298. }
  299. void Render::newRoomRenderList(int index) {
  300. assert(index >= 0);
  301. static int currentRoomId = -1;
  302. // Update room render list if needed
  303. if (currentRoomId != index) {
  304. buildRoomRenderList(getWorld().getRoom(index));
  305. }
  306. currentRoomId = index;
  307. }
  308. void Render::buildRoomRenderList(Room& room) {
  309. // Must be visible
  310. //! \fixme Add depth sorting here - remove multipass
  311. if (!isVisible(room.getBoundingBox()))
  312. return;
  313. // Must not already be cached
  314. for (unsigned int i = 0; i < mRoomRenderList.size(); i++) {
  315. Room* room2 = mRoomRenderList[i];
  316. if (room2 == &room)
  317. return;
  318. }
  319. /* Add current room to list */
  320. mRoomRenderList.push_back(&room);
  321. // Try to add adj rooms and their adj rooms, skip this room
  322. for (unsigned int i = 1; i < room.sizeAdjacentRooms(); i++) {
  323. if (room.getAdjacentRoom(i) < 0)
  324. continue;
  325. Room& room2 = getWorld().getRoom(room.getAdjacentRoom(i));
  326. //! \fixme Add portal visibility check here
  327. if (&room2 != &room)
  328. buildRoomRenderList(room2);
  329. }
  330. }
  331. void Render::drawSkyMesh(float scale) {
  332. //skeletal_model_t *model = getWorld().getModel(mSkyMesh);
  333. //if (!model)
  334. // return;
  335. glDisable(GL_DEPTH_TEST);
  336. glPushMatrix();
  337. if (mSkyMeshRotation)
  338. glRotated(90.0, 1, 0, 0);
  339. glTranslated(0.0, 1000.0, 0.0);
  340. glScaled(scale, scale, scale);
  341. //drawModel(model);
  342. //drawModelMesh(getWorld().getMesh(mSkyMesh), );
  343. glPopMatrix();
  344. glEnable(GL_DEPTH_TEST);
  345. }
  346. void Render::setSkyMesh(int index, bool rot) {
  347. mSkyMesh = index;
  348. mSkyMeshRotation = rot;
  349. }
  350. void Render::updateViewVolume() {
  351. float proj[16], mdl[16];
  352. glGetFloatv(GL_PROJECTION_MATRIX, proj);
  353. glGetFloatv(GL_MODELVIEW_MATRIX, mdl);
  354. mViewVolume.updateFrame(proj, mdl);
  355. }
  356. bool Render::isVisible(BoundingBox& box) {
  357. float bbox[2][3];
  358. box.getBoundingBox(bbox);
  359. // For debugging purposes
  360. if (mMode == Render::modeWireframe)
  361. box.display(true, PINK, RED);
  362. return mViewVolume.isBboxInFrustum(bbox[0], bbox[1]);
  363. }
  364. bool Render::isVisible(float x, float y, float z) {
  365. // For debugging purposes
  366. if (mMode == Render::modeWireframe) {
  367. glPointSize(5.0);
  368. glColor3ubv(PINK);
  369. glBegin(GL_POINTS);
  370. glVertex3f(x, y, z);
  371. glEnd();
  372. }
  373. return (mViewVolume.isPointInFrustum(x, y, z));
  374. }
  375. bool Render::isVisible(float x, float y, float z, float radius) {
  376. // For debugging purposes
  377. if (mMode == Render::modeWireframe) {
  378. glPointSize(5.0);
  379. glColor3ubv(PINK);
  380. glBegin(GL_POINTS);
  381. glVertex3f(x, y, z);
  382. glEnd();
  383. }
  384. return (mViewVolume.isSphereInFrustum(x, y, z, radius));
  385. }
  386. float Render::getDistToSphereFromNear(float x, float y, float z, float radius) {
  387. return mViewVolume.getDistToSphereFromNear(x, y, z, radius);
  388. }
  389. void Render::debugDisplayTexture(int texture, TextureManager::TextureStorage s,
  390. float x, float y, float w, float h) {
  391. debugTexture = texture;
  392. debugTextureStorage = s;
  393. debugX = x;
  394. debugY = y;
  395. debugW = w;
  396. debugH = h;
  397. debugTextile = -1;
  398. }
  399. void Render::drawTexture(float x, float y, float w, float h,
  400. unsigned int texture, TextureManager::TextureStorage s) {
  401. float z = 0.0f;
  402. glColor3ubv(WHITE);
  403. if (mFlags & Render::fGL_Lights)
  404. glDisable(GL_LIGHTING);
  405. getTextureManager().bindTextureId(texture, s);
  406. glBegin(GL_TRIANGLE_STRIP);
  407. glTexCoord2f(1.0, 1.0);
  408. glVertex3f(x + w, y + h, z);
  409. glTexCoord2f(0.0, 1.0);
  410. glVertex3f(x, y + h, z);
  411. glTexCoord2f(1.0, 0.0);
  412. glVertex3f(x + w, y, z);
  413. glTexCoord2f(0.0, 0.0);
  414. glVertex3f(x, y, z);
  415. glEnd();
  416. if (mFlags & Render::fGL_Lights)
  417. glEnable(GL_LIGHTING);
  418. }
  419. void Render::debugDisplayTextile(int texture, float x, float y, float w, float h) {
  420. debugTextile = texture;
  421. debugX = x;
  422. debugY = y;
  423. debugW = w;
  424. debugH = h;
  425. debugTexture = -1;
  426. }
  427. void Render::drawTextile(float x, float y, float w, float h, unsigned int textile) {
  428. float z = 0.0f;
  429. glColor3ubv(WHITE);
  430. if (mFlags & Render::fGL_Lights)
  431. glDisable(GL_LIGHTING);
  432. getTextureManager().getTile(textile).display(x, y, w, h, z);
  433. if (mFlags & Render::fGL_Lights)
  434. glEnable(GL_LIGHTING);
  435. }