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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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& r = 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 port = portal.getVertex(c);
  114. glm::vec4 result = VP * glm::vec4(port, 1.0f);
  115. glm::vec3 vert = glm::vec3(result) / result.w;
  116. ImGui::Text("%.2f %.2f %.2f", port.x, port.y, port.z);
  117. ImGui::Text("%.2f %.2f %.2f %.2f", result.x, result.y, result.z, result.w);
  118. ImGui::Text("%.2f %.2f %.2f", vert.x, vert.y, vert.z);
  119. ImGui::Text("----");
  120. if (c == 0) {
  121. newMin = vert;
  122. newMax = vert;
  123. } else {
  124. if (vert.x < newMin.x)
  125. newMin.x = vert.x;
  126. if (vert.y < newMin.y)
  127. newMin.y = vert.y;
  128. if (vert.z < newMin.z)
  129. newMin.z = vert.z;
  130. if (vert.x > newMax.x)
  131. newMax.x = vert.x;
  132. if (vert.y > newMax.y)
  133. newMax.y = vert.y;
  134. if (vert.z > newMax.z)
  135. newMax.z = vert.z;
  136. }
  137. }
  138. //ImGui::Text("%.2f %.2f %.2f", newMin.x, newMin.y, newMin.z);
  139. //ImGui::Text("%.2f %.2f %.2f", newMax.x, newMax.y, newMax.z);
  140. //ImGui::Text("----");
  141. //! \fixme Currently also checking behind player, because Z is always 1.0f?!
  142. //if ((newMin.z > 0.0f) || (newMin.z < -1.0f) || (newMax.z > 0.0f) || (newMax.z < -1.0f)) {
  143. // continue;
  144. //}
  145. //! \fixme Need to check portal normal, only render if it points in our direction
  146. //if (!normalFacingUs) {
  147. // continue;
  148. //}
  149. // Check if the portal intersects the portal leading into this room
  150. if (!((min.x < newMax.x) && (max.x > newMin.x)
  151. && (min.y < newMax.y) && (max.y > newMin.y))) {
  152. continue;
  153. }
  154. // Check if the connected room is in our view frustum (could be visible)
  155. if (!Camera::boxInFrustum(r.getBoundingBox())) {
  156. //continue;
  157. }
  158. // Check if this room is already in the list...
  159. bool found = false;
  160. for (int n = 0; n < roomList.size(); n++) {
  161. if (roomList.at(n).room == &r) {
  162. found = true;
  163. break;
  164. }
  165. }
  166. // ...only render it if it is not
  167. if (!found) {
  168. buildRoomList(VP, portal.getAdjoiningRoom(), glm::vec2(newMin), glm::vec2(newMax));
  169. }
  170. }
  171. }
  172. }
  173. void Render::screenShot(const char* filenameBase) {
  174. orAssert(filenameBase != nullptr);
  175. int w = Window::getSize().x;
  176. int h = Window::getSize().y;
  177. int sz = w * h;
  178. unsigned char* image = new unsigned char[sz * 3];
  179. gl::glReadPixels(0, 0, w, h, gl::GL_RGB, gl::GL_UNSIGNED_BYTE, image);
  180. unsigned char* buffer = new unsigned char[sz * 3];
  181. for (int x = 0; x < w; x++) {
  182. for (int y = 0; y < h; y++) {
  183. buffer[3 * (x + (y * w))] = image[3 * (x + ((h - y - 1) * w))];
  184. buffer[(3 * (x + (y * w))) + 1] = image[(3 * (x + ((h - y - 1) * w))) + 1];
  185. buffer[(3 * (x + (y * w))) + 2] = image[(3 * (x + ((h - y - 1) * w))) + 2];
  186. }
  187. }
  188. // Don't overwrite files
  189. static int count = 0;
  190. bool done = false;
  191. std::string f;
  192. while (!done) {
  193. std::ostringstream filename;
  194. filename << filenameBase << "-" << count++ << ".png";
  195. f = filename.str();
  196. std::ifstream fs(f);
  197. done = !fs.good();
  198. fs.close();
  199. }
  200. if (!stbi_write_png(f.c_str(), w, h, 3, buffer, 0)) {
  201. Log::get(LOG_ERROR) << "Error saving image \"" << f << "\"!" << Log::endl;
  202. }
  203. delete [] image;
  204. delete [] buffer;
  205. }
  206. static const int modeStringCount = 4;
  207. static const char* modeStrings[modeStringCount] = {
  208. "Splash", "Texture", "Wireframe", "Solid"
  209. };
  210. void Render::displayUI() {
  211. if (ImGui::CollapsingHeader("Render Settings##render")) {
  212. int item = 0;
  213. if (mode == RenderMode::Texture)
  214. item = 1;
  215. else if (mode == RenderMode::Wireframe)
  216. item = 2;
  217. else if (mode == RenderMode::Solid)
  218. item = 3;
  219. if (ImGui::Combo("Render Mode##render", &item, modeStrings, modeStringCount)) {
  220. if (item == 0)
  221. mode = RenderMode::LoadScreen;
  222. else if (item == 1)
  223. mode = RenderMode::Texture;
  224. else if (item == 2)
  225. mode = RenderMode::Wireframe;
  226. else if (item == 3)
  227. mode = RenderMode::Solid;
  228. }
  229. ImGui::Separator();
  230. ImGui::Text("Camera:");
  231. bool updateViewFrustum = Camera::getUpdateViewFrustum();
  232. if (ImGui::Checkbox("Update Frustum##camera", &updateViewFrustum)) {
  233. Camera::setUpdateViewFrustum(updateViewFrustum);
  234. }
  235. ImGui::SameLine();
  236. ImGui::Checkbox("Show Frustum##camera", &displayViewFrustum);
  237. ImGui::SameLine();
  238. bool showOverlay = Camera::getShowOverlay();
  239. if (ImGui::Checkbox("Overlay##camera", &showOverlay)) {
  240. Camera::setShowOverlay(showOverlay);
  241. }
  242. ImGui::SameLine();
  243. bool keepInRoom = Camera::getKeepInRoom();
  244. if (ImGui::Checkbox("Keep in Room##camera", &keepInRoom)) {
  245. Camera::setKeepInRoom(keepInRoom);
  246. }
  247. glm::vec3 camPos = Camera::getPosition();
  248. if (ImGui::SliderFloat3("Position##camera", &camPos.x, -100000, 100000)) {
  249. Camera::setPosition(camPos);
  250. }
  251. ImGui::Separator();
  252. ImGui::Text("Bounding Boxes:");
  253. bool showBoundingBox = Room::getShowBoundingBox();
  254. if (ImGui::Checkbox("Rooms##bbox", &showBoundingBox)) {
  255. Room::setShowBoundingBox(showBoundingBox);
  256. }
  257. ImGui::SameLine();
  258. bool showBoundingBox2 = StaticMesh::getShowBoundingBox();
  259. if (ImGui::Checkbox("StaticMeshes##bbox", &showBoundingBox2)) {
  260. StaticMesh::setShowBoundingBox(showBoundingBox2);
  261. }
  262. ImGui::SameLine();
  263. bool showBoundingBox3 = Portal::getShowBoundingBox();
  264. if (ImGui::Checkbox("Portals##bbox", &showBoundingBox3)) {
  265. Portal::setShowBoundingBox(showBoundingBox3);
  266. }
  267. ImGui::SameLine();
  268. ImGui::Checkbox("VisChecks##bbox", &displayVisibilityCheck);
  269. ImGui::Separator();
  270. ImGui::Text("Renderable Objects:");
  271. ImGui::Text("Room: ");
  272. ImGui::SameLine();
  273. bool showRoomGeometry = Room::getShowRoomGeometry();
  274. if (ImGui::Checkbox("Geometry##renderroom", &showRoomGeometry)) {
  275. Room::setShowRoomGeometry(showRoomGeometry);
  276. }
  277. ImGui::SameLine();
  278. bool showRoomModels = Room::getShowRoomModels();
  279. if (ImGui::Checkbox("Models##renderroom", &showRoomModels)) {
  280. Room::setShowRoomModels(showRoomModels);
  281. }
  282. ImGui::SameLine();
  283. bool showRoomSprites = Room::getShowRoomSprites();
  284. if (ImGui::Checkbox("Sprites##renderroom", &showRoomSprites)) {
  285. Room::setShowRoomSprites(showRoomSprites);
  286. }
  287. ImGui::Text("Entity: ");
  288. ImGui::SameLine();
  289. bool showEntitySprites = Entity::getShowEntitySprites();
  290. if (ImGui::Checkbox("Sprites##renderentity", &showEntitySprites)) {
  291. Entity::setShowEntitySprites(showEntitySprites);
  292. }
  293. ImGui::SameLine();
  294. bool showEntityMeshes = Entity::getShowEntityMeshes();
  295. if (ImGui::Checkbox("Meshes##renderentity", &showEntityMeshes)) {
  296. Entity::setShowEntityMeshes(showEntityMeshes);
  297. }
  298. ImGui::SameLine();
  299. bool showEntityModels = Entity::getShowEntityModels();
  300. if (ImGui::Checkbox("Models##renderentity", &showEntityModels)) {
  301. Entity::setShowEntityModels(showEntityModels);
  302. }
  303. ImGui::Separator();
  304. if (ImGui::Button("New Splash##render")) {
  305. TextureManager::initializeSplash();
  306. }
  307. ImGui::SameLine();
  308. if (ImGui::Button("New Menu##render")) {
  309. Menu::initialize();
  310. }
  311. }
  312. }