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

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