Open Source Tomb Raider Engine
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Render.cpp 11KB

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