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.

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