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.

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