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.0KB

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