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

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