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

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