Open Source Tomb Raider Engine
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

Render.cpp 6.7KB

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