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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*!
  2. * \file src/Render.cpp
  3. * \brief World Renderer
  4. *
  5. * \author Mongoose
  6. * \author xythobuz
  7. */
  8. #include <fstream>
  9. #include <sstream>
  10. #include "global.h"
  11. #include "BoundingBox.h"
  12. #include "Camera.h"
  13. #include "Log.h"
  14. #include "Menu.h"
  15. #include "StaticMesh.h"
  16. #include "World.h"
  17. #include "system/Shader.h"
  18. #include "system/Window.h"
  19. #include "Render.h"
  20. #include <glm/gtc/matrix_transform.hpp>
  21. #include <glbinding/gl/gl.h>
  22. #include "imgui/imgui.h"
  23. #include "stb/stb_image_write.h"
  24. RenderMode Render::mode = RenderMode::LoadScreen;
  25. std::vector<RoomRenderList> Render::roomList;
  26. bool Render::displayViewFrustum = false;
  27. bool Render::displayVisibilityCheck = false;
  28. void Render::display() {
  29. gl::glClear(gl::GL_COLOR_BUFFER_BIT | gl::GL_DEPTH_BUFFER_BIT);
  30. if (mode == RenderMode::LoadScreen) {
  31. ImGui::SetNextWindowPos(ImVec2(0.0f, 0.0f));
  32. ImGui::SetNextWindowSize(ImVec2(Window::getSize().x, Window::getSize().y));
  33. ImGui::Begin("SplashWindow", nullptr, ImGuiWindowFlags_NoTitleBar
  34. | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove
  35. | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse
  36. | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoSavedSettings);
  37. auto bm = TextureManager::getBufferManager(TEXTURE_SPLASH, TextureStorage::SYSTEM);
  38. ImGui::Image(bm, ImVec2(Window::getSize().x, Window::getSize().y));
  39. ImGui::End();
  40. return;
  41. }
  42. if (mode == RenderMode::Wireframe) {
  43. gl::glPolygonMode(gl::GL_FRONT_AND_BACK, gl::GL_LINE);
  44. } else {
  45. gl::glPolygonMode(gl::GL_FRONT_AND_BACK, gl::GL_FILL);
  46. }
  47. bool updated = Camera::update();
  48. glm::mat4 projection = Camera::getProjectionMatrix();
  49. glm::mat4 view = Camera::getViewMatrix();
  50. glm::mat4 VP = projection * view;
  51. if (updated || displayVisibilityCheck) {
  52. clearRoomList();
  53. buildRoomList(VP);
  54. }
  55. for (int r = roomList.size() - 1; r >= 0; r--) {
  56. auto& rl = roomList.at(r);
  57. //! \fixme TODO scissor to portal in screenspace to fix 4D rendering
  58. //gl::glEnable(gl::GL_SCISSOR_TEST);
  59. //gl::glScissor(rl.portalPos.x, rl.portalPos.y, rl.portalSize.x, rl.portalSize.y);
  60. //ImGui::Text("%.2f %.2f", rl.portalPos.x, rl.portalPos.y);
  61. //ImGui::Text("%.2f %.2f", rl.portalSize.x, rl.portalSize.y);
  62. //ImGui::Text("--");
  63. rl.room->display(VP);
  64. for (int i = 0; i < World::sizeEntity(); i++) {
  65. auto& e = World::getEntity(i);
  66. if (rl.room->getIndex() == e.getRoom()) {
  67. e.display(VP);
  68. }
  69. }
  70. //gl::glDisable(gl::GL_SCISSOR_TEST);
  71. }
  72. if (displayViewFrustum)
  73. Camera::displayFrustum(VP);
  74. BoundingBox::display();
  75. if (mode == RenderMode::Wireframe) {
  76. gl::glPolygonMode(gl::GL_FRONT_AND_BACK, gl::GL_FILL);
  77. }
  78. }
  79. void Render::buildRoomList(glm::mat4 VP, int room, glm::vec2 min, glm::vec2 max) {
  80. glm::vec2 halfSize = glm::vec2(Window::getSize()) / 2.0f;
  81. glm::vec2 pos = (min * halfSize) + halfSize;
  82. glm::vec2 size = (max * halfSize) + halfSize - pos;
  83. if (room < -1) {
  84. // Check if the camera currently is in a room...
  85. for (int i = 0; i < World::sizeRoom(); i++) {
  86. if (World::getRoom(i).getBoundingBox().inBox(Camera::getPosition())) {
  87. buildRoomList(VP, i);
  88. return;
  89. }
  90. }
  91. buildRoomList(VP, -1);
  92. } else if (room == -1) {
  93. // Check visibility for all rooms!
  94. for (int i = 0; i < World::sizeRoom(); i++) {
  95. if (Camera::boxInFrustum(World::getRoom(i).getBoundingBox())) {
  96. roomList.emplace_back(&World::getRoom(i), pos, size);
  97. }
  98. }
  99. } else {
  100. roomList.emplace_back(&World::getRoom(room), pos, size);
  101. if (displayVisibilityCheck) {
  102. // Display the visibility test for the portal to this room
  103. BoundingBox debugBox(glm::vec3(min, 0.0f), glm::vec3(max, 0.0f));
  104. debugBox.display(glm::mat4(1.0f), glm::vec3(1.0f, 1.0f, 1.0f), glm::vec3(0.0f, 1.0f, 0.0f));
  105. }
  106. // Check all portals leading from this room to somewhere else
  107. for (int i = 0; i < World::getRoom(room).sizePortals(); i++) {
  108. auto& portal = World::getRoom(room).getPortal(i);
  109. auto& room = World::getRoom(portal.getAdjoiningRoom());
  110. // Calculate the 2D screen-space bounding box of this portal
  111. glm::vec3 newMin, newMax;
  112. for (int c = 0; c < 4; c++) {
  113. glm::vec3 vert = portal.getVertex(c);
  114. glm::vec4 result = VP * glm::vec4(vert, 1.0f);
  115. vert = glm::vec3(result) / result.w;
  116. if (c == 0) {
  117. newMin = vert;
  118. newMax = vert;
  119. } else {
  120. if (vert.x < newMin.x)
  121. newMin.x = vert.x;
  122. if (vert.y < newMin.y)
  123. newMin.y = vert.y;
  124. if (vert.z < newMin.z)
  125. newMin.z = vert.z;
  126. if (vert.x > newMax.x)
  127. newMax.x = vert.x;
  128. if (vert.y > newMax.y)
  129. newMax.y = vert.y;
  130. if (vert.z > newMax.z)
  131. newMax.z = vert.z;
  132. }
  133. }
  134. //! \fixme Currently also checking behind player, because Z is always 1.0f?!
  135. //if ((newMin.z > 0.0f) || (newMin.z < -1.0f) || (newMax.z > 0.0f) || (newMax.z < -1.0f)) {
  136. // continue;
  137. //}
  138. // Check if the portal intersects the portal leading into this room
  139. if (!((min.x < newMax.x) && (max.x > newMin.x)
  140. && (min.y < newMax.y) && (max.y > newMin.y))) {
  141. continue;
  142. }
  143. // Check if the connected room is in our view frustum (could be visible)
  144. if (!Camera::boxInFrustum(room.getBoundingBox())) {
  145. continue;
  146. }
  147. // Check if this room is already in the list...
  148. bool found = false;
  149. for (int n = 0; n < roomList.size(); n++) {
  150. if (roomList.at(n).room == &room) {
  151. found = true;
  152. break;
  153. }
  154. }
  155. // ...only render it if it is not
  156. if (!found) {
  157. buildRoomList(VP, portal.getAdjoiningRoom(), glm::vec2(newMin), glm::vec2(newMax));
  158. }
  159. }
  160. }
  161. }
  162. void Render::screenShot(const char* filenameBase) {
  163. orAssert(filenameBase != nullptr);
  164. int w = Window::getSize().x;
  165. int h = Window::getSize().y;
  166. int sz = w * h;
  167. unsigned char* image = new unsigned char[sz * 3];
  168. gl::glReadPixels(0, 0, w, h, gl::GL_RGB, gl::GL_UNSIGNED_BYTE, image);
  169. unsigned char* buffer = new unsigned char[sz * 3];
  170. for (int x = 0; x < w; x++) {
  171. for (int y = 0; y < h; y++) {
  172. buffer[3 * (x + (y * w))] = image[3 * (x + ((h - y - 1) * w))];
  173. buffer[(3 * (x + (y * w))) + 1] = image[(3 * (x + ((h - y - 1) * w))) + 1];
  174. buffer[(3 * (x + (y * w))) + 2] = image[(3 * (x + ((h - y - 1) * w))) + 2];
  175. }
  176. }
  177. // Don't overwrite files
  178. static int count = 0;
  179. bool done = false;
  180. std::string f;
  181. while (!done) {
  182. std::ostringstream filename;
  183. filename << filenameBase << "-" << count++ << ".png";
  184. f = filename.str();
  185. std::ifstream fs(f);
  186. done = !fs.good();
  187. fs.close();
  188. }
  189. if (!stbi_write_png(f.c_str(), w, h, 3, buffer, 0)) {
  190. Log::get(LOG_ERROR) << "Error saving image \"" << f << "\"!" << Log::endl;
  191. }
  192. delete [] image;
  193. delete [] buffer;
  194. }
  195. static const int modeStringCount = 4;
  196. static const char* modeStrings[modeStringCount] = {
  197. "Splash", "Texture", "Wireframe", "Solid"
  198. };
  199. void Render::displayUI() {
  200. if (ImGui::CollapsingHeader("Render Settings##render")) {
  201. int item = 0;
  202. if (mode == RenderMode::Texture)
  203. item = 1;
  204. else if (mode == RenderMode::Wireframe)
  205. item = 2;
  206. else if (mode == RenderMode::Solid)
  207. item = 3;
  208. if (ImGui::Combo("Render Mode##render", &item, modeStrings, modeStringCount)) {
  209. if (item == 0)
  210. mode = RenderMode::LoadScreen;
  211. else if (item == 1)
  212. mode = RenderMode::Texture;
  213. else if (item == 2)
  214. mode = RenderMode::Wireframe;
  215. else if (item == 3)
  216. mode = RenderMode::Solid;
  217. }
  218. ImGui::Separator();
  219. ImGui::Text("Camera:");
  220. bool updateViewFrustum = Camera::getUpdateViewFrustum();
  221. if (ImGui::Checkbox("Update Frustum##camera", &updateViewFrustum)) {
  222. Camera::setUpdateViewFrustum(updateViewFrustum);
  223. }
  224. ImGui::SameLine();
  225. ImGui::Checkbox("Show Frustum##camera", &displayViewFrustum);
  226. ImGui::SameLine();
  227. bool showOverlay = Camera::getShowOverlay();
  228. if (ImGui::Checkbox("Overlay##camera", &showOverlay)) {
  229. Camera::setShowOverlay(showOverlay);
  230. }
  231. ImGui::SameLine();
  232. bool keepInRoom = Camera::getKeepInRoom();
  233. if (ImGui::Checkbox("Keep in Room##camera", &keepInRoom)) {
  234. Camera::setKeepInRoom(keepInRoom);
  235. }
  236. glm::vec3 camPos = Camera::getPosition();
  237. if (ImGui::SliderFloat3("Position##camera", &camPos.x, -100000, 100000)) {
  238. Camera::setPosition(camPos);
  239. }
  240. ImGui::Separator();
  241. ImGui::Text("Bounding Boxes:");
  242. bool showBoundingBox = Room::getShowBoundingBox();
  243. if (ImGui::Checkbox("Rooms##bbox", &showBoundingBox)) {
  244. Room::setShowBoundingBox(showBoundingBox);
  245. }
  246. ImGui::SameLine();
  247. bool showBoundingBox2 = StaticMesh::getShowBoundingBox();
  248. if (ImGui::Checkbox("StaticMeshes##bbox", &showBoundingBox2)) {
  249. StaticMesh::setShowBoundingBox(showBoundingBox2);
  250. }
  251. ImGui::SameLine();
  252. bool showBoundingBox3 = Portal::getShowBoundingBox();
  253. if (ImGui::Checkbox("Portals##bbox", &showBoundingBox3)) {
  254. Portal::setShowBoundingBox(showBoundingBox3);
  255. }
  256. ImGui::SameLine();
  257. ImGui::Checkbox("VisChecks##bbox", &displayVisibilityCheck);
  258. ImGui::Separator();
  259. ImGui::Text("Renderable Objects:");
  260. ImGui::Text("Room: ");
  261. ImGui::SameLine();
  262. bool showRoomGeometry = Room::getShowRoomGeometry();
  263. if (ImGui::Checkbox("Geometry##renderroom", &showRoomGeometry)) {
  264. Room::setShowRoomGeometry(showRoomGeometry);
  265. }
  266. ImGui::SameLine();
  267. bool showRoomModels = Room::getShowRoomModels();
  268. if (ImGui::Checkbox("Models##renderroom", &showRoomModels)) {
  269. Room::setShowRoomModels(showRoomModels);
  270. }
  271. ImGui::SameLine();
  272. bool showRoomSprites = Room::getShowRoomSprites();
  273. if (ImGui::Checkbox("Sprites##renderroom", &showRoomSprites)) {
  274. Room::setShowRoomSprites(showRoomSprites);
  275. }
  276. ImGui::Text("Entity: ");
  277. ImGui::SameLine();
  278. bool showEntitySprites = Entity::getShowEntitySprites();
  279. if (ImGui::Checkbox("Sprites##renderentity", &showEntitySprites)) {
  280. Entity::setShowEntitySprites(showEntitySprites);
  281. }
  282. ImGui::SameLine();
  283. bool showEntityMeshes = Entity::getShowEntityMeshes();
  284. if (ImGui::Checkbox("Meshes##renderentity", &showEntityMeshes)) {
  285. Entity::setShowEntityMeshes(showEntityMeshes);
  286. }
  287. ImGui::SameLine();
  288. bool showEntityModels = Entity::getShowEntityModels();
  289. if (ImGui::Checkbox("Models##renderentity", &showEntityModels)) {
  290. Entity::setShowEntityModels(showEntityModels);
  291. }
  292. ImGui::Separator();
  293. if (ImGui::Button("New Splash##render")) {
  294. TextureManager::initializeSplash();
  295. }
  296. ImGui::SameLine();
  297. if (ImGui::Button("New Menu##render")) {
  298. Menu::initialize();
  299. }
  300. }
  301. }