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

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