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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*!
  2. * \file src/Render.cpp
  3. * \brief OpenRaider Renderer class
  4. *
  5. * \author Mongoose
  6. * \author xythobuz
  7. */
  8. #include <algorithm>
  9. #include <sstream>
  10. #include <glm/gtc/matrix_transform.hpp>
  11. #include <stdlib.h>
  12. #include <math.h>
  13. #include <string.h>
  14. #include "global.h"
  15. #include "Camera.h"
  16. #include "Game.h"
  17. #include "Render.h"
  18. #include "utils/strings.h"
  19. #include "utils/tga.h"
  20. #include "World.h"
  21. #include "system/Window.h"
  22. RenderMode Render::mode = RenderMode::Disabled;
  23. RenderMode Render::getMode() {
  24. return mode;
  25. }
  26. void Render::setMode(RenderMode m) {
  27. mode = m;
  28. switch (mode) {
  29. case RenderMode::Disabled:
  30. break;
  31. case RenderMode::Solid:
  32. case RenderMode::Wireframe:
  33. //glClearColor(PURPLE[0] / 256.0f, PURPLE[1] / 256.0f,
  34. // PURPLE[2] / 256.0f, PURPLE[3] / 256.0f);
  35. //glDisable(GL_TEXTURE_2D);
  36. break;
  37. default:
  38. //glClearColor(BLACK[0] / 256.0f, BLACK[1] / 256.0f,
  39. // BLACK[2] / 256.0f, BLACK[3] / 256.0f);
  40. //glEnable(GL_TEXTURE_2D);
  41. break;
  42. }
  43. }
  44. void Render::display() {
  45. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  46. if (mode == RenderMode::LoadScreen) {
  47. glm::vec4 color(1.0f, 1.0f, 1.0f, 1.0f);
  48. drawTexture(0.0f, 0.0f, getWindow().getWidth(), getWindow().getHeight(),
  49. color, TEXTURE_SPLASH, TextureManager::TextureStorage::SYSTEM);
  50. return;
  51. } else if (mode == RenderMode::Disabled) {
  52. return;
  53. }
  54. if (mode == RenderMode::Wireframe) {
  55. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  56. } else {
  57. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  58. }
  59. glm::mat4 view = Camera::getViewMatrix();
  60. static unsigned int w = getWindow().getWidth();
  61. static unsigned int h = getWindow().getHeight();
  62. static glm::mat4 projection = glm::perspective(45.0f, // Field of View
  63. (float)getWindow().getWidth()
  64. / (float)getWindow().getHeight(),
  65. 0.1f, // Min Distance
  66. 100000.0f); // Max Distance
  67. if ((w != getWindow().getWidth()) || (h != getWindow().getHeight())) {
  68. w = getWindow().getWidth();
  69. h = getWindow().getHeight();
  70. glm::mat4 projection = glm::perspective(45.0f, // Field of View
  71. (float)getWindow().getWidth() / (float)getWindow().getHeight(),
  72. 0.1f, // Min Distance
  73. 100000.0f); // Max Distance
  74. }
  75. // Just draw all rooms, as a test
  76. for (int i = 0; i < getWorld().sizeRoom(); i++)
  77. getWorld().getRoom(i).display(view, projection);
  78. if (mode == RenderMode::Wireframe) {
  79. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  80. }
  81. }
  82. void Render::screenShot(const char* filenameBase) {
  83. /*
  84. int sz = getWindow().getWidth() * getWindow().getHeight();
  85. unsigned char* image = new unsigned char[sz * 3];
  86. static int count = 0;
  87. bool done = false;
  88. assert(filenameBase != nullptr);
  89. // Don't overwrite files
  90. std::ostringstream filename;
  91. while (!done) {
  92. filename << filenameBase << "-" << count++ << ".tga";
  93. FILE* f = fopen(filename.str().c_str(), "rb");
  94. if (f) {
  95. fclose(f);
  96. } else {
  97. done = true;
  98. }
  99. }
  100. glReadPixels(0, 0, getWindow().getWidth(), getWindow().getHeight(), GL_BGR_EXT, GL_UNSIGNED_BYTE,
  101. image);
  102. tgaSave(filename.str().c_str(), image, getWindow().getWidth(), getWindow().getHeight(), 0);
  103. delete [] image;
  104. */
  105. }
  106. void Render::drawTexture(float x, float y, float w, float h, glm::vec4 color,
  107. unsigned int texture, TextureManager::TextureStorage s) {
  108. std::vector<glm::vec2> vertices;
  109. std::vector<glm::vec2> uvs;
  110. vertices.push_back(glm::vec2(x, y + h));
  111. vertices.push_back(glm::vec2(x, y));
  112. vertices.push_back(glm::vec2(x + w, y + h));
  113. vertices.push_back(glm::vec2(x + w, y));
  114. vertices.push_back(glm::vec2(x + w, y + h));
  115. vertices.push_back(glm::vec2(x, y));
  116. uvs.push_back(glm::vec2(0.0f, 1.0f));
  117. uvs.push_back(glm::vec2(0.0f, 0.0f));
  118. uvs.push_back(glm::vec2(1.0f, 1.0f));
  119. uvs.push_back(glm::vec2(1.0f, 0.0f));
  120. uvs.push_back(glm::vec2(1.0f, 1.0f));
  121. uvs.push_back(glm::vec2(0.0f, 0.0f));
  122. //! \fixme drawTextGL only uses SYSTEM textures!
  123. assert(s == TextureManager::TextureStorage::SYSTEM);
  124. Window::drawTextGL(vertices, uvs, color, texture);
  125. }