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

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