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

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