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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 "Menu.h"
  14. #include "StaticMesh.h"
  15. #include "World.h"
  16. #include "system/Font.h"
  17. #include "system/Shader.h"
  18. #include "system/Window.h"
  19. #include "Render.h"
  20. #include <glm/gtc/matrix_transform.hpp>
  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. glClear(GL_COLOR_BUFFER_BIT | 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. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  36. } else {
  37. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  38. }
  39. if (Camera::update()) {
  40. int r = Camera::getRoom();
  41. if (r < 0) {
  42. clearRoomList();
  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. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  64. }
  65. }
  66. void Render::buildRoomList(int room) {
  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);
  72. return;
  73. }
  74. }
  75. buildRoomList(-1);
  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. buildRoomList(r);
  98. }
  99. }
  100. }
  101. }
  102. void Render::screenShot(const char* filenameBase) {
  103. assert(filenameBase != nullptr);
  104. int w = Window::getSize().x;
  105. int h = Window::getSize().y;
  106. int sz = w * h;
  107. unsigned char* image = new unsigned char[sz * 3];
  108. glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, image);
  109. unsigned char* buffer = new unsigned char[sz * 3];
  110. for (int x = 0; x < w; x++) {
  111. for (int y = 0; y < h; y++) {
  112. buffer[3 * (x + (y * w))] = image[3 * (x + ((h - y - 1) * w))];
  113. buffer[(3 * (x + (y * w))) + 1] = image[(3 * (x + ((h - y - 1) * w))) + 1];
  114. buffer[(3 * (x + (y * w))) + 2] = image[(3 * (x + ((h - y - 1) * w))) + 2];
  115. }
  116. }
  117. // Don't overwrite files
  118. static int count = 0;
  119. bool done = false;
  120. std::string f;
  121. while (!done) {
  122. std::ostringstream filename;
  123. filename << filenameBase << "-" << count++ << ".png";
  124. f = filename.str();
  125. std::ifstream fs(f);
  126. done = !fs.good();
  127. fs.close();
  128. }
  129. if (!stbi_write_png(f.c_str(), w, h, 3, buffer, 0)) {
  130. Log::get(LOG_ERROR) << "Error saving image \"" << f << "\"!" << Log::endl;
  131. }
  132. delete [] image;
  133. delete [] buffer;
  134. }
  135. void Render::drawTexture(float x, float y, float w, float h, glm::vec4 color,
  136. unsigned int texture, TextureStorage s) {
  137. std::vector<glm::vec2> vertices;
  138. std::vector<glm::vec2> uvs;
  139. vertices.push_back(glm::vec2(x, y + h));
  140. vertices.push_back(glm::vec2(x + w, y + h));
  141. vertices.push_back(glm::vec2(x, y));
  142. vertices.push_back(glm::vec2(x + w, y));
  143. vertices.push_back(glm::vec2(x, y));
  144. vertices.push_back(glm::vec2(x + w, y + h));
  145. uvs.push_back(glm::vec2(0.0f, 1.0f));
  146. uvs.push_back(glm::vec2(1.0f, 1.0f));
  147. uvs.push_back(glm::vec2(0.0f, 0.0f));
  148. uvs.push_back(glm::vec2(1.0f, 0.0f));
  149. uvs.push_back(glm::vec2(0.0f, 0.0f));
  150. uvs.push_back(glm::vec2(1.0f, 1.0f));
  151. static ShaderBuffer vert, uv;
  152. vert.bufferData(vertices);
  153. uv.bufferData(uvs);
  154. Shader::drawGL(vert, uv, color, texture, s);
  155. }
  156. static const int modeStringCount = 4;
  157. static const char* modeStrings[modeStringCount] = {
  158. "Splash", "Texture", "Wireframe", "Solid"
  159. };
  160. void Render::displayUI() {
  161. if (ImGui::CollapsingHeader("Render Settings##render")) {
  162. int item = 0;
  163. if (mode == RenderMode::Texture)
  164. item = 1;
  165. else if (mode == RenderMode::Wireframe)
  166. item = 2;
  167. else if (mode == RenderMode::Solid)
  168. item = 3;
  169. if (ImGui::Combo("Render Mode##render", &item, modeStrings, modeStringCount)) {
  170. if (item == 0)
  171. mode = RenderMode::LoadScreen;
  172. else if (item == 1)
  173. mode = RenderMode::Texture;
  174. else if (item == 2)
  175. mode = RenderMode::Wireframe;
  176. else if (item == 3)
  177. mode = RenderMode::Solid;
  178. }
  179. ImGui::Separator();
  180. ImGui::Text("Camera:");
  181. bool updateViewFrustum = Camera::getUpdateViewFrustum();
  182. if (ImGui::Checkbox("Update Frustum##render", &updateViewFrustum)) {
  183. Camera::setUpdateViewFrustum(updateViewFrustum);
  184. }
  185. ImGui::SameLine();
  186. ImGui::Checkbox("Show Frustum##render", &displayViewFrustum);
  187. ImGui::SameLine();
  188. bool showOverlay = Camera::getShowOverlay();
  189. if (ImGui::Checkbox("Overlay", &showOverlay)) {
  190. Camera::setShowOverlay(showOverlay);
  191. }
  192. if (ImGui::Button("Reset Room ID")) {
  193. Camera::setRoom(-1);
  194. }
  195. ImGui::Separator();
  196. ImGui::Text("Bounding Boxes:");
  197. bool showBoundingBox = Room::getShowBoundingBox();
  198. if (ImGui::Checkbox("Room##bbox", &showBoundingBox)) {
  199. Room::setShowBoundingBox(showBoundingBox);
  200. }
  201. ImGui::SameLine();
  202. bool showBoundingBox2 = StaticMesh::getShowBoundingBox();
  203. if (ImGui::Checkbox("StaticMesh##bbox", &showBoundingBox2)) {
  204. StaticMesh::setShowBoundingBox(showBoundingBox2);
  205. }
  206. ImGui::SameLine();
  207. bool showFontBox = Font::getShowFontBox();
  208. if (ImGui::Checkbox("Font##bbox", &showFontBox)) {
  209. Font::setShowFontBox(showFontBox);
  210. }
  211. ImGui::Separator();
  212. ImGui::Text("Renderable Objects:");
  213. ImGui::Text("Room: ");
  214. ImGui::SameLine();
  215. bool showRoomGeometry = Room::getShowRoomGeometry();
  216. if (ImGui::Checkbox("Geometry##renderroom", &showRoomGeometry)) {
  217. Room::setShowRoomGeometry(showRoomGeometry);
  218. }
  219. ImGui::SameLine();
  220. bool showRoomModels = Room::getShowRoomModels();
  221. if (ImGui::Checkbox("Models##renderroom", &showRoomModels)) {
  222. Room::setShowRoomModels(showRoomModels);
  223. }
  224. ImGui::SameLine();
  225. bool showRoomSprites = Room::getShowRoomSprites();
  226. if (ImGui::Checkbox("Sprites##renderroom", &showRoomSprites)) {
  227. Room::setShowRoomSprites(showRoomSprites);
  228. }
  229. ImGui::Text("Entity: ");
  230. ImGui::SameLine();
  231. bool showEntitySprites = Entity::getShowEntitySprites();
  232. if (ImGui::Checkbox("Sprites##renderentity", &showEntitySprites)) {
  233. Entity::setShowEntitySprites(showEntitySprites);
  234. }
  235. ImGui::SameLine();
  236. bool showEntityMeshes = Entity::getShowEntityMeshes();
  237. if (ImGui::Checkbox("Meshes##renderentity", &showEntityMeshes)) {
  238. Entity::setShowEntityMeshes(showEntityMeshes);
  239. }
  240. ImGui::SameLine();
  241. bool showEntityModels = Entity::getShowEntityModels();
  242. if (ImGui::Checkbox("Models##renderentity", &showEntityModels)) {
  243. Entity::setShowEntityModels(showEntityModels);
  244. }
  245. ImGui::Separator();
  246. if (ImGui::Button("New Splash##render")) {
  247. TextureManager::initializeSplash();
  248. }
  249. ImGui::SameLine();
  250. if (ImGui::Button("New Menu##render")) {
  251. Menu::initialize();
  252. }
  253. }
  254. }