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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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, glm::vec2(newMax) / newMax.w);
  166. }
  167. }
  168. }
  169. }
  170. void Render::screenShot(const char* filenameBase) {
  171. orAssert(filenameBase != nullptr);
  172. int w = Window::getSize().x;
  173. int h = Window::getSize().y;
  174. int sz = w * h;
  175. unsigned char* image = new unsigned char[sz * 3];
  176. gl::glReadPixels(0, 0, w, h, gl::GL_RGB, gl::GL_UNSIGNED_BYTE, image);
  177. unsigned char* buffer = new unsigned char[sz * 3];
  178. for (int x = 0; x < w; x++) {
  179. for (int y = 0; y < h; y++) {
  180. buffer[3 * (x + (y * w))] = image[3 * (x + ((h - y - 1) * w))];
  181. buffer[(3 * (x + (y * w))) + 1] = image[(3 * (x + ((h - y - 1) * w))) + 1];
  182. buffer[(3 * (x + (y * w))) + 2] = image[(3 * (x + ((h - y - 1) * w))) + 2];
  183. }
  184. }
  185. // Don't overwrite files
  186. static int count = 0;
  187. bool done = false;
  188. std::string f;
  189. while (!done) {
  190. std::ostringstream filename;
  191. filename << filenameBase << "-" << count++ << ".png";
  192. f = filename.str();
  193. std::ifstream fs(f);
  194. done = !fs.good();
  195. fs.close();
  196. }
  197. if (!stbi_write_png(f.c_str(), w, h, 3, buffer, 0)) {
  198. Log::get(LOG_ERROR) << "Error saving image \"" << f << "\"!" << Log::endl;
  199. }
  200. delete [] image;
  201. delete [] buffer;
  202. }
  203. static const int modeStringCount = 4;
  204. static const char* modeStrings[modeStringCount] = {
  205. "Splash", "Texture", "Wireframe", "Solid"
  206. };
  207. void Render::displayUI() {
  208. if (ImGui::CollapsingHeader("Render Settings##render")) {
  209. int item = 0;
  210. if (mode == RenderMode::Texture)
  211. item = 1;
  212. else if (mode == RenderMode::Wireframe)
  213. item = 2;
  214. else if (mode == RenderMode::Solid)
  215. item = 3;
  216. if (ImGui::Combo("Render Mode##render", &item, modeStrings, modeStringCount)) {
  217. if (item == 0)
  218. mode = RenderMode::LoadScreen;
  219. else if (item == 1)
  220. mode = RenderMode::Texture;
  221. else if (item == 2)
  222. mode = RenderMode::Wireframe;
  223. else if (item == 3)
  224. mode = RenderMode::Solid;
  225. }
  226. ImGui::Separator();
  227. ImGui::Text("Camera:");
  228. bool updateViewFrustum = Camera::getUpdateViewFrustum();
  229. if (ImGui::Checkbox("Update Frustum##camera", &updateViewFrustum)) {
  230. Camera::setUpdateViewFrustum(updateViewFrustum);
  231. }
  232. ImGui::SameLine();
  233. ImGui::Checkbox("Show Frustum##camera", &displayViewFrustum);
  234. ImGui::SameLine();
  235. bool showOverlay = Camera::getShowOverlay();
  236. if (ImGui::Checkbox("Overlay##camera", &showOverlay)) {
  237. Camera::setShowOverlay(showOverlay);
  238. }
  239. ImGui::SameLine();
  240. bool keepInRoom = Camera::getKeepInRoom();
  241. if (ImGui::Checkbox("Keep in Room##camera", &keepInRoom)) {
  242. Camera::setKeepInRoom(keepInRoom);
  243. }
  244. glm::vec3 camPos = Camera::getPosition();
  245. if (ImGui::SliderFloat3("Position##camera", &camPos.x, -100000, 100000)) {
  246. Camera::setPosition(camPos);
  247. }
  248. ImGui::Separator();
  249. ImGui::Text("Bounding Boxes:");
  250. bool showBoundingBox = Room::getShowBoundingBox();
  251. if (ImGui::Checkbox("Rooms##bbox", &showBoundingBox)) {
  252. Room::setShowBoundingBox(showBoundingBox);
  253. }
  254. ImGui::SameLine();
  255. bool showBoundingBox2 = StaticMesh::getShowBoundingBox();
  256. if (ImGui::Checkbox("StaticMeshes##bbox", &showBoundingBox2)) {
  257. StaticMesh::setShowBoundingBox(showBoundingBox2);
  258. }
  259. ImGui::SameLine();
  260. bool showBoundingBox3 = Portal::getShowBoundingBox();
  261. if (ImGui::Checkbox("Portals##bbox", &showBoundingBox3)) {
  262. Portal::setShowBoundingBox(showBoundingBox3);
  263. }
  264. ImGui::SameLine();
  265. ImGui::Checkbox("VisChecks##bbox", &displayVisibilityCheck);
  266. ImGui::Separator();
  267. ImGui::Text("Renderable Objects:");
  268. ImGui::Text("Room: ");
  269. ImGui::SameLine();
  270. bool showRoomGeometry = Room::getShowRoomGeometry();
  271. if (ImGui::Checkbox("Geometry##renderroom", &showRoomGeometry)) {
  272. Room::setShowRoomGeometry(showRoomGeometry);
  273. }
  274. ImGui::SameLine();
  275. bool showRoomModels = Room::getShowRoomModels();
  276. if (ImGui::Checkbox("Models##renderroom", &showRoomModels)) {
  277. Room::setShowRoomModels(showRoomModels);
  278. }
  279. ImGui::SameLine();
  280. bool showRoomSprites = Room::getShowRoomSprites();
  281. if (ImGui::Checkbox("Sprites##renderroom", &showRoomSprites)) {
  282. Room::setShowRoomSprites(showRoomSprites);
  283. }
  284. ImGui::Text("Entity: ");
  285. ImGui::SameLine();
  286. bool showEntitySprites = Entity::getShowEntitySprites();
  287. if (ImGui::Checkbox("Sprites##renderentity", &showEntitySprites)) {
  288. Entity::setShowEntitySprites(showEntitySprites);
  289. }
  290. ImGui::SameLine();
  291. bool showEntityMeshes = Entity::getShowEntityMeshes();
  292. if (ImGui::Checkbox("Meshes##renderentity", &showEntityMeshes)) {
  293. Entity::setShowEntityMeshes(showEntityMeshes);
  294. }
  295. ImGui::SameLine();
  296. bool showEntityModels = Entity::getShowEntityModels();
  297. if (ImGui::Checkbox("Models##renderentity", &showEntityModels)) {
  298. Entity::setShowEntityModels(showEntityModels);
  299. }
  300. ImGui::Separator();
  301. if (ImGui::Button("New Splash##render")) {
  302. TextureManager::initializeSplash();
  303. }
  304. ImGui::SameLine();
  305. if (ImGui::Button("New Menu##render")) {
  306. Menu::initialize();
  307. }
  308. }
  309. }