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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 "global.h"
  11. #include "Camera.h"
  12. #include "Game.h"
  13. #include "Log.h"
  14. #include "UI.h"
  15. #include "World.h"
  16. #include "system/Shader.h"
  17. #include "system/Window.h"
  18. #include "utils/strings.h"
  19. #include "Render.h"
  20. #include <glm/gtc/matrix_transform.hpp>
  21. #include "imgui/imgui.h"
  22. #include "stb/stb_image_write.h"
  23. RenderMode Render::mode = RenderMode::LoadScreen;
  24. std::vector<Room*> Render::roomList;
  25. bool Render::displayViewFrustum = false;
  26. RenderMode Render::getMode() {
  27. return mode;
  28. }
  29. void Render::setMode(RenderMode m) {
  30. mode = m;
  31. switch (mode) {
  32. case RenderMode::Solid:
  33. case RenderMode::Wireframe:
  34. //glClearColor(PURPLE[0] / 256.0f, PURPLE[1] / 256.0f,
  35. // PURPLE[2] / 256.0f, PURPLE[3] / 256.0f);
  36. break;
  37. default:
  38. //glClearColor(BLACK[0] / 256.0f, BLACK[1] / 256.0f,
  39. // BLACK[2] / 256.0f, BLACK[3] / 256.0f);
  40. break;
  41. }
  42. }
  43. void Render::display() {
  44. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  45. if (mode == RenderMode::LoadScreen) {
  46. glm::vec4 color(1.0f, 1.0f, 1.0f, 1.0f);
  47. drawTexture(0.0f, 0.0f, Window::getSize().x, Window::getSize().y,
  48. color, TEXTURE_SPLASH, TextureStorage::SYSTEM);
  49. return;
  50. }
  51. if (mode == RenderMode::Wireframe) {
  52. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  53. } else {
  54. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  55. }
  56. if (Camera::update()) {
  57. clearRoomList();
  58. buildRoomList(-2); // TODO cache room
  59. }
  60. glm::mat4 projection = Camera::getProjectionMatrix();
  61. glm::mat4 view = Camera::getViewMatrix();
  62. glm::mat4 VP = projection * view;
  63. for (auto r : roomList) {
  64. r->display(VP);
  65. }
  66. if (displayViewFrustum)
  67. Camera::displayFrustum(VP);
  68. if (mode == RenderMode::Wireframe) {
  69. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  70. }
  71. }
  72. void Render::clearRoomList() {
  73. roomList.clear();
  74. }
  75. void Render::buildRoomList(int room) {
  76. if (room < -1) {
  77. // Check if the camera currently is in a room...
  78. for (int i = 0; i < getWorld().sizeRoom(); i++) {
  79. if (getWorld().getRoom(i).getBoundingBox().inBox(Camera::getPosition())) {
  80. buildRoomList(i);
  81. return;
  82. }
  83. }
  84. buildRoomList(-1);
  85. } else if (room == -1) {
  86. // Check visibility for all rooms!
  87. for (int i = 0; i < getWorld().sizeRoom(); i++) {
  88. if (Camera::boxInFrustum(getWorld().getRoom(i).getBoundingBox())) {
  89. roomList.push_back(&getWorld().getRoom(i));
  90. }
  91. }
  92. } else {
  93. // Check visibility of room and connected rooms, recursively?
  94. if (Camera::boxInFrustum(getWorld().getRoom(room).getBoundingBox())) {
  95. roomList.push_back(&getWorld().getRoom(room));
  96. for (int i = 0; i < getWorld().getRoom(room).sizePortals(); i++) {
  97. int r = getWorld().getRoom(room).getPortal(i).getAdjoiningRoom();
  98. bool found = false;
  99. for (int n = 0; n < roomList.size(); n++) {
  100. if (roomList.at(n) == &getWorld().getRoom(r)) {
  101. found = true;
  102. break;
  103. }
  104. }
  105. if (!found)
  106. buildRoomList(r);
  107. }
  108. }
  109. }
  110. }
  111. void Render::screenShot(const char* filenameBase) {
  112. assert(filenameBase != nullptr);
  113. int w = Window::getSize().x;
  114. int h = Window::getSize().y;
  115. int sz = w * h;
  116. unsigned char* image = new unsigned char[sz * 3];
  117. glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, image);
  118. unsigned char* buffer = new unsigned char[sz * 3];
  119. for (int x = 0; x < w; x++) {
  120. for (int y = 0; y < h; y++) {
  121. buffer[3 * (x + (y * w))] = image[3 * (x + ((h - y - 1) * w))];
  122. buffer[(3 * (x + (y * w))) + 1] = image[(3 * (x + ((h - y - 1) * w))) + 1];
  123. buffer[(3 * (x + (y * w))) + 2] = image[(3 * (x + ((h - y - 1) * w))) + 2];
  124. }
  125. }
  126. // Don't overwrite files
  127. static int count = 0;
  128. bool done = false;
  129. std::string f;
  130. while (!done) {
  131. std::ostringstream filename;
  132. filename << filenameBase << "-" << count++ << ".png";
  133. f = filename.str();
  134. std::ifstream fs(f);
  135. done = !fs.good();
  136. fs.close();
  137. }
  138. if (!stbi_write_png(f.c_str(), w, h, 3, buffer, 0)) {
  139. getLog() << "Error saving image \"" << f << "\"!" << Log::endl;
  140. }
  141. delete [] image;
  142. delete [] buffer;
  143. }
  144. void Render::drawTexture(float x, float y, float w, float h, glm::vec4 color,
  145. unsigned int texture, TextureStorage s) {
  146. std::vector<glm::vec2> vertices;
  147. std::vector<glm::vec2> uvs;
  148. vertices.push_back(glm::vec2(x, y + h));
  149. vertices.push_back(glm::vec2(x, y));
  150. vertices.push_back(glm::vec2(x + w, y + h));
  151. vertices.push_back(glm::vec2(x + w, y));
  152. vertices.push_back(glm::vec2(x + w, y + h));
  153. vertices.push_back(glm::vec2(x, y));
  154. uvs.push_back(glm::vec2(0.0f, 1.0f));
  155. uvs.push_back(glm::vec2(0.0f, 0.0f));
  156. uvs.push_back(glm::vec2(1.0f, 1.0f));
  157. uvs.push_back(glm::vec2(1.0f, 0.0f));
  158. uvs.push_back(glm::vec2(1.0f, 1.0f));
  159. uvs.push_back(glm::vec2(0.0f, 0.0f));
  160. static ShaderBuffer vert, uv;
  161. vert.bufferData(vertices);
  162. uv.bufferData(uvs);
  163. Shader::drawGL(vert, uv, color, texture, s);
  164. }
  165. static const int modeStringCount = 4;
  166. static const char* modeStrings[modeStringCount] = {
  167. "Splash", "Texture", "Wireframe", "Solid"
  168. };
  169. void Render::displayUI() {
  170. if (ImGui::CollapsingHeader("Render Settings##render")) {
  171. int item = 0;
  172. if (mode == RenderMode::Texture)
  173. item = 1;
  174. else if (mode == RenderMode::Wireframe)
  175. item = 2;
  176. else if (mode == RenderMode::Solid)
  177. item = 3;
  178. if (ImGui::Combo("Mode", &item, modeStrings, modeStringCount)) {
  179. if (item == 0)
  180. mode = RenderMode::LoadScreen;
  181. else if (item == 1)
  182. mode = RenderMode::Texture;
  183. else if (item == 2)
  184. mode = RenderMode::Wireframe;
  185. else if (item == 3)
  186. mode = RenderMode::Solid;
  187. }
  188. bool updateViewFrustum = Camera::getUpdateViewFrustum();
  189. if (ImGui::Checkbox("Update Frustum##render", &updateViewFrustum)) {
  190. Camera::setUpdateViewFrustum(updateViewFrustum);
  191. }
  192. ImGui::SameLine();
  193. ImGui::Checkbox("Show Frustum##render", &displayViewFrustum);
  194. }
  195. }