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

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