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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. void Render::display() {
  27. gl::glClear(gl::GL_COLOR_BUFFER_BIT | gl::GL_DEPTH_BUFFER_BIT);
  28. if (mode == RenderMode::LoadScreen) {
  29. glm::vec4 color(1.0f, 1.0f, 1.0f, 1.0f);
  30. drawTexture(0.0f, 0.0f, Window::getSize().x, Window::getSize().y,
  31. color, TEXTURE_SPLASH, TextureStorage::SYSTEM);
  32. return;
  33. }
  34. if (mode == RenderMode::Wireframe) {
  35. gl::glPolygonMode(gl::GL_FRONT_AND_BACK, gl::GL_LINE);
  36. } else {
  37. gl::glPolygonMode(gl::GL_FRONT_AND_BACK, gl::GL_FILL);
  38. }
  39. if (Camera::update()) {
  40. int r = Camera::getRoom();
  41. clearRoomList();
  42. if (r < 0) {
  43. buildRoomList();
  44. } else {
  45. buildRoomList(r);
  46. }
  47. }
  48. glm::mat4 projection = Camera::getProjectionMatrix();
  49. glm::mat4 view = Camera::getViewMatrix();
  50. glm::mat4 VP = projection * view;
  51. for (int r = roomList.size() - 1; r >= 0; r--) {
  52. roomList.at(r)->display(VP);
  53. for (int i = 0; i < getWorld().sizeEntity(); i++) {
  54. auto& e = getWorld().getEntity(i);
  55. if (roomList.at(r)->getIndex() == e.getRoom()) {
  56. e.display(VP);
  57. }
  58. }
  59. }
  60. if (displayViewFrustum)
  61. Camera::displayFrustum(VP);
  62. if (mode == RenderMode::Wireframe) {
  63. gl::glPolygonMode(gl::GL_FRONT_AND_BACK, gl::GL_FILL);
  64. }
  65. }
  66. void Render::buildRoomList(int room, int budget) {
  67. if (room < -1) {
  68. // Check if the camera currently is in a room...
  69. for (int i = 0; i < getWorld().sizeRoom(); i++) {
  70. if (getWorld().getRoom(i).getBoundingBox().inBox(Camera::getPosition())) {
  71. buildRoomList(i, budget);
  72. return;
  73. }
  74. }
  75. buildRoomList(-1, budget);
  76. } else if (room == -1) {
  77. // Check visibility for all rooms!
  78. for (int i = 0; i < getWorld().sizeRoom(); i++) {
  79. if (Camera::boxInFrustum(getWorld().getRoom(i).getBoundingBox())) {
  80. roomList.push_back(&getWorld().getRoom(i));
  81. }
  82. }
  83. } else {
  84. // Check visibility of room and connected rooms, recursively
  85. if (Camera::boxInFrustum(getWorld().getRoom(room).getBoundingBox())) {
  86. roomList.push_back(&getWorld().getRoom(room));
  87. for (int i = 0; i < getWorld().getRoom(room).sizePortals(); i++) {
  88. int r = getWorld().getRoom(room).getPortal(i).getAdjoiningRoom();
  89. bool found = false;
  90. for (int n = 0; n < roomList.size(); n++) {
  91. if (roomList.at(n) == &getWorld().getRoom(r)) {
  92. found = true;
  93. break;
  94. }
  95. }
  96. if (!found) {
  97. if (budget > 0) {
  98. buildRoomList(r, --budget);
  99. }
  100. }
  101. }
  102. }
  103. }
  104. }
  105. void Render::screenShot(const char* filenameBase) {
  106. orAssert(filenameBase != nullptr);
  107. int w = Window::getSize().x;
  108. int h = Window::getSize().y;
  109. int sz = w * h;
  110. unsigned char* image = new unsigned char[sz * 3];
  111. gl::glReadPixels(0, 0, w, h, gl::GL_RGB, gl::GL_UNSIGNED_BYTE, image);
  112. unsigned char* buffer = new unsigned char[sz * 3];
  113. for (int x = 0; x < w; x++) {
  114. for (int y = 0; y < h; y++) {
  115. buffer[3 * (x + (y * w))] = image[3 * (x + ((h - y - 1) * w))];
  116. buffer[(3 * (x + (y * w))) + 1] = image[(3 * (x + ((h - y - 1) * w))) + 1];
  117. buffer[(3 * (x + (y * w))) + 2] = image[(3 * (x + ((h - y - 1) * w))) + 2];
  118. }
  119. }
  120. // Don't overwrite files
  121. static int count = 0;
  122. bool done = false;
  123. std::string f;
  124. while (!done) {
  125. std::ostringstream filename;
  126. filename << filenameBase << "-" << count++ << ".png";
  127. f = filename.str();
  128. std::ifstream fs(f);
  129. done = !fs.good();
  130. fs.close();
  131. }
  132. if (!stbi_write_png(f.c_str(), w, h, 3, buffer, 0)) {
  133. Log::get(LOG_ERROR) << "Error saving image \"" << f << "\"!" << Log::endl;
  134. }
  135. delete [] image;
  136. delete [] buffer;
  137. }
  138. void Render::drawTexture(float x, float y, float w, float h, glm::vec4 color,
  139. unsigned int texture, TextureStorage s) {
  140. std::vector<glm::vec2> vertices;
  141. std::vector<glm::vec2> uvs;
  142. vertices.push_back(glm::vec2(x, y + h));
  143. vertices.push_back(glm::vec2(x + w, y + h));
  144. vertices.push_back(glm::vec2(x, y));
  145. vertices.push_back(glm::vec2(x + w, y));
  146. vertices.push_back(glm::vec2(x, y));
  147. vertices.push_back(glm::vec2(x + w, y + h));
  148. uvs.push_back(glm::vec2(0.0f, 1.0f));
  149. uvs.push_back(glm::vec2(1.0f, 1.0f));
  150. uvs.push_back(glm::vec2(0.0f, 0.0f));
  151. uvs.push_back(glm::vec2(1.0f, 0.0f));
  152. uvs.push_back(glm::vec2(0.0f, 0.0f));
  153. uvs.push_back(glm::vec2(1.0f, 1.0f));
  154. static ShaderBuffer vert, uv;
  155. vert.bufferData(vertices);
  156. uv.bufferData(uvs);
  157. Shader::drawGL(vert, uv, color, texture, s);
  158. }
  159. static const int modeStringCount = 4;
  160. static const char* modeStrings[modeStringCount] = {
  161. "Splash", "Texture", "Wireframe", "Solid"
  162. };
  163. void Render::displayUI() {
  164. if (ImGui::CollapsingHeader("Render Settings##render")) {
  165. int item = 0;
  166. if (mode == RenderMode::Texture)
  167. item = 1;
  168. else if (mode == RenderMode::Wireframe)
  169. item = 2;
  170. else if (mode == RenderMode::Solid)
  171. item = 3;
  172. if (ImGui::Combo("Render Mode##render", &item, modeStrings, modeStringCount)) {
  173. if (item == 0)
  174. mode = RenderMode::LoadScreen;
  175. else if (item == 1)
  176. mode = RenderMode::Texture;
  177. else if (item == 2)
  178. mode = RenderMode::Wireframe;
  179. else if (item == 3)
  180. mode = RenderMode::Solid;
  181. }
  182. ImGui::Separator();
  183. ImGui::Text("Camera:");
  184. bool updateViewFrustum = Camera::getUpdateViewFrustum();
  185. if (ImGui::Checkbox("Update Frustum##render", &updateViewFrustum)) {
  186. Camera::setUpdateViewFrustum(updateViewFrustum);
  187. }
  188. ImGui::SameLine();
  189. ImGui::Checkbox("Show Frustum##render", &displayViewFrustum);
  190. ImGui::SameLine();
  191. bool showOverlay = Camera::getShowOverlay();
  192. if (ImGui::Checkbox("Overlay", &showOverlay)) {
  193. Camera::setShowOverlay(showOverlay);
  194. }
  195. if (ImGui::Button("Reset Room ID")) {
  196. Camera::setRoom(-1);
  197. }
  198. ImGui::Separator();
  199. ImGui::Text("Bounding Boxes:");
  200. bool showBoundingBox = Room::getShowBoundingBox();
  201. if (ImGui::Checkbox("Room##bbox", &showBoundingBox)) {
  202. Room::setShowBoundingBox(showBoundingBox);
  203. }
  204. ImGui::SameLine();
  205. bool showBoundingBox2 = StaticMesh::getShowBoundingBox();
  206. if (ImGui::Checkbox("StaticMesh##bbox", &showBoundingBox2)) {
  207. StaticMesh::setShowBoundingBox(showBoundingBox2);
  208. }
  209. ImGui::Separator();
  210. ImGui::Text("Renderable Objects:");
  211. ImGui::Text("Room: ");
  212. ImGui::SameLine();
  213. bool showRoomGeometry = Room::getShowRoomGeometry();
  214. if (ImGui::Checkbox("Geometry##renderroom", &showRoomGeometry)) {
  215. Room::setShowRoomGeometry(showRoomGeometry);
  216. }
  217. ImGui::SameLine();
  218. bool showRoomModels = Room::getShowRoomModels();
  219. if (ImGui::Checkbox("Models##renderroom", &showRoomModels)) {
  220. Room::setShowRoomModels(showRoomModels);
  221. }
  222. ImGui::SameLine();
  223. bool showRoomSprites = Room::getShowRoomSprites();
  224. if (ImGui::Checkbox("Sprites##renderroom", &showRoomSprites)) {
  225. Room::setShowRoomSprites(showRoomSprites);
  226. }
  227. ImGui::Text("Entity: ");
  228. ImGui::SameLine();
  229. bool showEntitySprites = Entity::getShowEntitySprites();
  230. if (ImGui::Checkbox("Sprites##renderentity", &showEntitySprites)) {
  231. Entity::setShowEntitySprites(showEntitySprites);
  232. }
  233. ImGui::SameLine();
  234. bool showEntityMeshes = Entity::getShowEntityMeshes();
  235. if (ImGui::Checkbox("Meshes##renderentity", &showEntityMeshes)) {
  236. Entity::setShowEntityMeshes(showEntityMeshes);
  237. }
  238. ImGui::SameLine();
  239. bool showEntityModels = Entity::getShowEntityModels();
  240. if (ImGui::Checkbox("Models##renderentity", &showEntityModels)) {
  241. Entity::setShowEntityModels(showEntityModels);
  242. }
  243. ImGui::Separator();
  244. if (ImGui::Button("New Splash##render")) {
  245. TextureManager::initializeSplash();
  246. }
  247. ImGui::SameLine();
  248. if (ImGui::Button("New Menu##render")) {
  249. Menu::initialize();
  250. }
  251. }
  252. }