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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 "BoundingSphere.h"
  13. #include "Camera.h"
  14. #include "Log.h"
  15. #include "Menu.h"
  16. #include "StaticMesh.h"
  17. #include "World.h"
  18. #include "system/Shader.h"
  19. #include "system/Window.h"
  20. #include "Render.h"
  21. #include <glm/gtc/matrix_transform.hpp>
  22. #include <glbinding/gl/gl.h>
  23. #include "imgui/imgui.h"
  24. #include "stb/stb_image_write.h"
  25. RenderMode Render::mode = RenderMode::LoadScreen;
  26. std::vector<RoomRenderList> Render::roomList;
  27. bool Render::displayViewFrustum = false;
  28. bool Render::displayVisibilityCheck = false;
  29. void Render::display() {
  30. gl::glClear(gl::GL_COLOR_BUFFER_BIT | gl::GL_DEPTH_BUFFER_BIT);
  31. if (mode == RenderMode::LoadScreen) {
  32. ImGui::SetNextWindowPos(ImVec2(0.0f, 0.0f));
  33. ImGui::SetNextWindowSize(ImVec2(Window::getSize().x, Window::getSize().y));
  34. ImGui::Begin("SplashWindow", nullptr, ImGuiWindowFlags_NoTitleBar
  35. | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove
  36. | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse
  37. | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoSavedSettings);
  38. auto bm = TextureManager::getBufferManager(TEXTURE_SPLASH, TextureStorage::SYSTEM);
  39. ImGui::Image(bm, ImVec2(Window::getSize().x, Window::getSize().y));
  40. ImGui::End();
  41. return;
  42. }
  43. if (mode == RenderMode::Wireframe) {
  44. gl::glPolygonMode(gl::GL_FRONT_AND_BACK, gl::GL_LINE);
  45. } else {
  46. gl::glPolygonMode(gl::GL_FRONT_AND_BACK, gl::GL_FILL);
  47. }
  48. bool updated = Camera::update();
  49. glm::mat4 projection = Camera::getProjectionMatrix();
  50. glm::mat4 view = Camera::getViewMatrix();
  51. glm::mat4 VP = projection * view;
  52. if (updated || displayVisibilityCheck) {
  53. clearRoomList();
  54. buildRoomList(VP);
  55. }
  56. for (int r = roomList.size() - 1; r >= 0; r--) {
  57. auto& rl = roomList.at(r);
  58. //! \fixme TODO scissor to portal in screenspace to fix 4D rendering
  59. //gl::glEnable(gl::GL_SCISSOR_TEST);
  60. //gl::glScissor(rl.portalPos.x, rl.portalPos.y, rl.portalSize.x, rl.portalSize.y);
  61. //ImGui::Text("%.2f %.2f", rl.portalPos.x, rl.portalPos.y);
  62. //ImGui::Text("%.2f %.2f", rl.portalSize.x, rl.portalSize.y);
  63. //ImGui::Text("--");
  64. rl.room->display(VP);
  65. for (int i = 0; i < World::sizeEntity(); i++) {
  66. auto& e = World::getEntity(i);
  67. if (rl.room->getIndex() == e.getRoom()) {
  68. e.display(VP);
  69. }
  70. }
  71. //gl::glDisable(gl::GL_SCISSOR_TEST);
  72. }
  73. if (displayViewFrustum)
  74. Camera::displayFrustum(VP);
  75. BoundingBox::display();
  76. BoundingSphere::display();
  77. if (mode == RenderMode::Wireframe) {
  78. gl::glPolygonMode(gl::GL_FRONT_AND_BACK, gl::GL_FILL);
  79. }
  80. }
  81. void Render::buildRoomList(glm::mat4 VP, int room, glm::vec2 min, glm::vec2 max) {
  82. glm::vec2 halfSize = glm::vec2(Window::getSize()) / 2.0f;
  83. glm::vec2 pos = (min * halfSize) + halfSize;
  84. glm::vec2 size = (max * halfSize) + halfSize - pos;
  85. if (room < -1) {
  86. // Check if the camera currently is in a room...
  87. for (int i = 0; i < World::sizeRoom(); i++) {
  88. if (World::getRoom(i).getBoundingBox().inBox(Camera::getPosition())) {
  89. buildRoomList(VP, i);
  90. return;
  91. }
  92. }
  93. buildRoomList(VP, -1);
  94. } else if (room == -1) {
  95. // Check visibility for all rooms!
  96. for (int i = 0; i < World::sizeRoom(); i++) {
  97. if (Camera::boxInFrustum(World::getRoom(i).getBoundingBox())) {
  98. roomList.emplace_back(&World::getRoom(i), pos, size);
  99. }
  100. }
  101. } else {
  102. roomList.emplace_back(&World::getRoom(room), pos, size);
  103. if (displayVisibilityCheck) {
  104. // Display the visibility test for the portal to this room
  105. BoundingBox debugBox(glm::vec3(min, 0.0f), glm::vec3(max, 0.0f));
  106. debugBox.display(glm::mat4(1.0f), glm::vec3(1.0f, 1.0f, 1.0f), glm::vec3(0.0f, 1.0f, 0.0f));
  107. }
  108. // Check all portals leading from this room to somewhere else
  109. for (int i = 0; i < World::getRoom(room).sizePortals(); i++) {
  110. auto& portal = World::getRoom(room).getPortal(i);
  111. auto& r = World::getRoom(portal.getAdjoiningRoom());
  112. // Calculate the 2D screen-space bounding box of this portal
  113. glm::vec4 newMin, newMax;
  114. for (int c = 0; c < 4; c++) {
  115. glm::vec3 port = portal.getVertex(c);
  116. glm::vec4 vert = VP * glm::vec4(port, 1.0f);
  117. if (c == 0) {
  118. newMin = vert;
  119. newMax = vert;
  120. } else {
  121. if (vert.x < newMin.x)
  122. newMin.x = vert.x;
  123. if (vert.y < newMin.y)
  124. newMin.y = vert.y;
  125. if (vert.z < newMin.z)
  126. newMin.z = vert.z;
  127. if (vert.w < newMin.w)
  128. newMin.w = vert.w;
  129. if (vert.x > newMax.x)
  130. newMax.x = vert.x;
  131. if (vert.y > newMax.y)
  132. newMax.y = vert.y;
  133. if (vert.z > newMax.z)
  134. newMax.z = vert.z;
  135. if (vert.w > newMax.w)
  136. newMax.w = vert.w;
  137. }
  138. }
  139. // Check if the portal lies behind the player
  140. if (!((newMin.z <= newMin.w) && (newMin.z >= -newMin.w)
  141. && (newMax.z <= newMax.w) && (newMax.z >= -newMax.w))) {
  142. continue;
  143. }
  144. //! \fixme Need to check portal normal, only render if it points in our direction
  145. //if (!normalFacingUs) {
  146. // continue;
  147. //}
  148. // Check if the portal intersects the portal leading into this room
  149. if (!((min.x < (newMax.x / newMax.w)) && (max.x > (newMin.x / newMin.w))
  150. && (min.y < (newMax.y / newMax.w)) && (max.y > (newMin.y / newMin.w)))) {
  151. continue;
  152. }
  153. // Check if the connected room is in our view frustum (could be visible)
  154. if (!Camera::boxInFrustum(r.getBoundingBox())) {
  155. //continue;
  156. }
  157. // Check if this room is already in the list...
  158. bool found = false;
  159. for (int n = 0; n < roomList.size(); n++) {
  160. if (roomList.at(n).room == &r) {
  161. found = true;
  162. break;
  163. }
  164. }
  165. // ...only render it if it is not
  166. if (!found) {
  167. buildRoomList(VP, portal.getAdjoiningRoom(), glm::vec2(newMin) / newMin.w,
  168. glm::vec2(newMax) / newMax.w);
  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 keepInRoom = Camera::getKeepInRoom();
  239. if (ImGui::Checkbox("Keep in Room##camera", &keepInRoom)) {
  240. Camera::setKeepInRoom(keepInRoom);
  241. }
  242. glm::vec3 camPos = Camera::getPosition();
  243. if (ImGui::SliderFloat3("Position##camera", &camPos.x, -100000, 100000)) {
  244. Camera::setPosition(camPos);
  245. }
  246. ImGui::Separator();
  247. ImGui::Text("Bounding Boxes:");
  248. bool showBoundingBox = Room::getShowBoundingBox();
  249. if (ImGui::Checkbox("Rooms##bbox", &showBoundingBox)) {
  250. Room::setShowBoundingBox(showBoundingBox);
  251. }
  252. ImGui::SameLine();
  253. bool showBoundingBox2 = StaticMesh::getShowBoundingBox();
  254. if (ImGui::Checkbox("StaticMeshes##bbox", &showBoundingBox2)) {
  255. StaticMesh::setShowBoundingBox(showBoundingBox2);
  256. }
  257. ImGui::SameLine();
  258. bool showBoundingBox3 = Portal::getShowBoundingBox();
  259. if (ImGui::Checkbox("Portals##bbox", &showBoundingBox3)) {
  260. Portal::setShowBoundingBox(showBoundingBox3);
  261. }
  262. ImGui::SameLine();
  263. ImGui::Checkbox("VisChecks##bbox", &displayVisibilityCheck);
  264. ImGui::Separator();
  265. ImGui::Text("Renderable Objects:");
  266. ImGui::Text("Room: ");
  267. ImGui::SameLine();
  268. bool showRoomGeometry = Room::getShowRoomGeometry();
  269. if (ImGui::Checkbox("Geometry##renderroom", &showRoomGeometry)) {
  270. Room::setShowRoomGeometry(showRoomGeometry);
  271. }
  272. ImGui::SameLine();
  273. bool showRoomModels = Room::getShowRoomModels();
  274. if (ImGui::Checkbox("Models##renderroom", &showRoomModels)) {
  275. Room::setShowRoomModels(showRoomModels);
  276. }
  277. ImGui::SameLine();
  278. bool showRoomSprites = Room::getShowRoomSprites();
  279. if (ImGui::Checkbox("Sprites##renderroom", &showRoomSprites)) {
  280. Room::setShowRoomSprites(showRoomSprites);
  281. }
  282. ImGui::Text("Entity: ");
  283. ImGui::SameLine();
  284. bool showEntitySprites = Entity::getShowEntitySprites();
  285. if (ImGui::Checkbox("Sprites##renderentity", &showEntitySprites)) {
  286. Entity::setShowEntitySprites(showEntitySprites);
  287. }
  288. ImGui::SameLine();
  289. bool showEntityMeshes = Entity::getShowEntityMeshes();
  290. if (ImGui::Checkbox("Meshes##renderentity", &showEntityMeshes)) {
  291. Entity::setShowEntityMeshes(showEntityMeshes);
  292. }
  293. ImGui::SameLine();
  294. bool showEntityModels = Entity::getShowEntityModels();
  295. if (ImGui::Checkbox("Models##renderentity", &showEntityModels)) {
  296. Entity::setShowEntityModels(showEntityModels);
  297. }
  298. ImGui::Separator();
  299. if (ImGui::Button("New Splash##render")) {
  300. TextureManager::initializeSplash();
  301. }
  302. ImGui::SameLine();
  303. if (ImGui::Button("New Menu##render")) {
  304. Menu::initialize();
  305. }
  306. }
  307. }