Open Source Tomb Raider Engine
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Render.cpp 7.1KB

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