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.

Window.cpp 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*!
  2. * \file src/system/Window.cpp
  3. * \brief windowing implementation
  4. *
  5. * \author xythobuz
  6. */
  7. #include "global.h"
  8. #include "Camera.h"
  9. #include "Log.h"
  10. #include "UI.h"
  11. #include "utils/strings.h"
  12. #include "system/Window.h"
  13. #ifdef USING_SDL
  14. #include "system/WindowSDL.h"
  15. #elif defined(USING_GLFW)
  16. #include "system/WindowGLFW.h"
  17. #else
  18. #error "No windowing library selected!"
  19. #endif
  20. int Window::initialize() {
  21. int res;
  22. #ifdef USING_SDL
  23. res = WindowSDL::initialize();
  24. #elif defined(USING_GLFW)
  25. res = WindowGLFW::initialize();
  26. #else
  27. res = -1;
  28. #endif
  29. initializeGL();
  30. return res;
  31. }
  32. void Window::eventHandling() {
  33. #ifdef USING_SDL
  34. WindowSDL::eventHandling();
  35. #elif defined(USING_GLFW)
  36. WindowGLFW::eventHandling();
  37. #endif
  38. }
  39. void Window::swapBuffers() {
  40. #ifdef USING_SDL
  41. WindowSDL::swapBuffers();
  42. #elif defined(USING_GLFW)
  43. WindowGLFW::swapBuffers();
  44. #endif
  45. }
  46. void Window::shutdown() {
  47. shutdownGL();
  48. #ifdef USING_SDL
  49. WindowSDL::shutdown();
  50. #elif defined(USING_GLFW)
  51. WindowGLFW::shutdown();
  52. #endif
  53. }
  54. void Window::setSize(glm::i32vec2 s) {
  55. #ifdef USING_SDL
  56. WindowSDL::setSize(s);
  57. #elif defined(USING_GLFW)
  58. WindowGLFW::setSize(s);
  59. #endif
  60. UI::setSize(s);
  61. Camera::setSize(s);
  62. glViewport(0, 0, s.x, s.y);
  63. }
  64. glm::i32vec2 Window::getSize() {
  65. glm::i32vec2 ret(-1, -1);
  66. #ifdef USING_SDL
  67. ret = WindowSDL::getSize();
  68. #elif defined(USING_GLFW)
  69. ret = WindowGLFW::getSize();
  70. #endif
  71. return ret;
  72. }
  73. void Window::setFullscreen(bool f) {
  74. #ifdef USING_SDL
  75. WindowSDL::setFullscreen(f);
  76. #elif defined(USING_GLFW)
  77. WindowGLFW::setFullscreen(f);
  78. #endif
  79. }
  80. bool Window::getFullscreen() {
  81. bool ret;
  82. #ifdef USING_SDL
  83. ret = WindowSDL::getFullscreen();
  84. #elif defined(USING_GLFW)
  85. ret = WindowGLFW::getFullscreen();
  86. #else
  87. ret = false;
  88. #endif
  89. return ret;
  90. }
  91. void Window::setMousegrab(bool g) {
  92. #ifdef USING_SDL
  93. WindowSDL::setMousegrab(g);
  94. #elif defined(USING_GLFW)
  95. WindowGLFW::setMousegrab(g);
  96. #endif
  97. }
  98. bool Window::getMousegrab() {
  99. bool ret;
  100. #ifdef USING_SDL
  101. ret = WindowSDL::getMousegrab();
  102. #elif defined(USING_GLFW)
  103. ret = WindowGLFW::getMousegrab();
  104. #else
  105. ret = false;
  106. #endif
  107. return ret;
  108. }
  109. void Window::setTextInput(bool t) {
  110. #ifdef USING_SDL
  111. WindowSDL::setTextInput(t);
  112. #elif defined(USING_GLFW)
  113. WindowGLFW::setTextInput(t);
  114. #endif
  115. }
  116. bool Window::getTextInput() {
  117. bool ret;
  118. #ifdef USING_SDL
  119. ret = WindowSDL::getTextInput();
  120. #elif defined(USING_GLFW)
  121. ret = WindowGLFW::getTextInput();
  122. #else
  123. ret = false;
  124. #endif
  125. return ret;
  126. }
  127. // --------------------------------------
  128. Shader Window::textShader;
  129. Shader Window::imguiShader;
  130. Shader Window::textureShader;
  131. Shader Window::colorShader;
  132. unsigned int Window::vertexArrayID = 0;
  133. int Window::initializeGL() {
  134. getLog() << "GL Ven.: " << glGetString(GL_VENDOR) << Log::endl;
  135. getLog() << "GL Ren.: " << glGetString(GL_RENDERER) << Log::endl;
  136. getLog() << "GL Ver.: " << glGetString(GL_VERSION) << Log::endl;
  137. getLog() << "GLSL V.: " << glGetString(GL_SHADING_LANGUAGE_VERSION) << Log::endl;
  138. glGenVertexArrays(1, &vertexArrayID);
  139. glBindVertexArray(vertexArrayID);
  140. // Set background to black
  141. //glClearColor(BLACK[0] / 256.0f, BLACK[1] / 256.0f, BLACK[2] / 256.0f, BLACK[3] / 256.0f);
  142. glClearColor(0.0f, 0.0f, 0.4f, 1.0f);
  143. // Set up Z buffer
  144. glEnable(GL_DEPTH_TEST);
  145. // Accept fragment if closer to camera
  146. glDepthFunc(GL_LESS);
  147. // Set up culling
  148. //glEnable(GL_CULL_FACE); //! \todo Transparency?
  149. glPointSize(5.0f);
  150. if (textShader.compile(textShaderVertex, textShaderFragment) < 0)
  151. return -1;
  152. if (textShader.addUniform("screen") < 0)
  153. return -2;
  154. if (textShader.addUniform("textureSampler") < 0)
  155. return -3;
  156. if (textShader.addUniform("colorVar") < 0)
  157. return -4;
  158. textShader.addBuffer(2);
  159. if (imguiShader.compile(imguiShaderVertex, imguiShaderFragment) < 0)
  160. return -5;
  161. if (imguiShader.addUniform("screen") < 0)
  162. return -6;
  163. if (imguiShader.addUniform("textureSampler") < 0)
  164. return -7;
  165. imguiShader.addBuffer(3);
  166. if (textureShader.compile(textureShaderVertex, textureShaderFragment) < 0)
  167. return -8;
  168. if (textureShader.addUniform("MVP") < 0)
  169. return -9;
  170. if (textureShader.addUniform("textureSampler") < 0)
  171. return -10;
  172. textureShader.addBuffer(3);
  173. if (colorShader.compile(colorShaderVertex, colorShaderFragment) < 0)
  174. return -11;
  175. if (colorShader.addUniform("MVP") < 0)
  176. return -12;
  177. colorShader.addBuffer(3);
  178. glEnable(GL_BLEND);
  179. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  180. return 0;
  181. }
  182. void Window::shutdownGL() {
  183. glDeleteVertexArrays(1, &vertexArrayID);
  184. }
  185. void Window::drawTextGL(std::vector<glm::vec2>& vertices, std::vector<glm::vec2>& uvs,
  186. glm::vec4 color, unsigned int texture) {
  187. assert(vertices.size() == uvs.size());
  188. assert((vertices.size() % 3) == 0);
  189. textShader.bufferData(0, vertices);
  190. textShader.bufferData(1, uvs);
  191. textShader.use();
  192. textShader.loadUniform(0, getSize());
  193. textShader.loadUniform(1, texture, TextureManager::TextureStorage::SYSTEM);
  194. textShader.loadUniform(2, color);
  195. textShader.bindBuffer(0, 0, 2);
  196. textShader.bindBuffer(1, 1, 2);
  197. glDisable(GL_DEPTH_TEST);
  198. glDrawArrays(GL_TRIANGLES, 0, vertices.size());
  199. glEnable(GL_DEPTH_TEST);
  200. textShader.disableAttribs();
  201. }
  202. void Window::drawGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec2>& uvs,
  203. std::vector<unsigned short>& indices, glm::mat4 MVP, unsigned int texture) {
  204. assert(vertices.size() == uvs.size());
  205. assert((indices.size() % 3) == 0);
  206. textureShader.bufferData(0, vertices);
  207. textureShader.bufferData(1, uvs);
  208. textureShader.bufferData(2, indices);
  209. textureShader.use();
  210. textureShader.loadUniform(0, MVP);
  211. textureShader.loadUniform(1, texture, TextureManager::TextureStorage::GAME);
  212. textureShader.bindBuffer(0, 0, 3);
  213. textureShader.bindBuffer(1, 1, 2);
  214. textureShader.bindBuffer(2);
  215. glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_SHORT, nullptr);
  216. textureShader.disableAttribs();
  217. }
  218. void Window::drawGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec3>& colors,
  219. std::vector<unsigned short>& indices, glm::mat4 MVP, int mode) {
  220. assert(vertices.size() == colors.size());
  221. colorShader.bufferData(0, vertices);
  222. colorShader.bufferData(1, colors);
  223. colorShader.bufferData(2, indices);
  224. colorShader.use();
  225. colorShader.loadUniform(0, MVP);
  226. colorShader.bindBuffer(0, 0, 3);
  227. colorShader.bindBuffer(1, 1, 3);
  228. colorShader.bindBuffer(2);
  229. glDrawElements(mode, indices.size(), GL_UNSIGNED_SHORT, nullptr);
  230. colorShader.disableAttribs();
  231. }
  232. // --------------------------------------
  233. // *INDENT-OFF*
  234. const char* Window::textShaderVertex = R"!?!(
  235. #version 330 core
  236. layout(location = 0) in vec2 vertexPosition_screen;
  237. layout(location = 1) in vec2 vertexUV;
  238. out vec2 UV;
  239. uniform vec2 screen;
  240. void main() {
  241. vec2 halfScreen = screen / 2;
  242. vec2 vertexPosition_homogenous = (vertexPosition_screen - halfScreen) / halfScreen;
  243. gl_Position = vec4(vertexPosition_homogenous.x, -vertexPosition_homogenous.y, 0, 1);
  244. UV = vertexUV;
  245. }
  246. )!?!";
  247. const char* Window::textShaderFragment = R"!?!(
  248. #version 330 core
  249. in vec2 UV;
  250. out vec4 color;
  251. uniform sampler2D textureSampler;
  252. uniform vec4 colorVar;
  253. void main() {
  254. color = texture(textureSampler, UV) * colorVar;
  255. }
  256. )!?!";
  257. // --------------------------------------
  258. const char* Window::imguiShaderVertex = R"!?!(
  259. #version 330 core
  260. layout(location = 0) in vec2 vertexPosition_screen;
  261. layout(location = 1) in vec2 vertexUV;
  262. layout(location = 2) in vec4 vertexColor;
  263. out vec2 UV;
  264. out vec4 FragColor;
  265. uniform vec2 screen;
  266. void main() {
  267. vec2 halfScreen = screen / 2;
  268. vec2 vertexPosition_homogenous = (vertexPosition_screen - halfScreen) / halfScreen;
  269. gl_Position = vec4(vertexPosition_homogenous.x, -vertexPosition_homogenous.y, 0, 1);
  270. UV = vertexUV;
  271. FragColor = vertexColor;
  272. }
  273. )!?!";
  274. const char* Window::imguiShaderFragment = R"!?!(
  275. #version 330 core
  276. in vec2 UV;
  277. in vec4 FragColor;
  278. out vec4 color;
  279. uniform sampler2D textureSampler;
  280. void main() {
  281. color = texture(textureSampler, UV) * FragColor;
  282. }
  283. )!?!";
  284. // --------------------------------------
  285. const char* Window::textureShaderVertex = R"!?!(
  286. #version 330 core
  287. layout(location = 0) in vec3 vertexPosition_modelspace;
  288. layout(location = 1) in vec2 vertexUV;
  289. out vec2 UV;
  290. uniform mat4 MVP;
  291. void main() {
  292. vec4 pos = MVP * vec4(vertexPosition_modelspace.x,
  293. -vertexPosition_modelspace.y,
  294. vertexPosition_modelspace.z,
  295. 1);
  296. gl_Position = vec4(-pos.x, pos.yzw);
  297. UV = vertexUV;
  298. }
  299. )!?!";
  300. const char* Window::textureShaderFragment = R"!?!(
  301. #version 330 core
  302. in vec2 UV;
  303. out vec4 color;
  304. uniform sampler2D textureSampler;
  305. void main() {
  306. color = texture(textureSampler, UV);
  307. }
  308. )!?!";
  309. // --------------------------------------
  310. const char* Window::colorShaderVertex = R"!?!(
  311. #version 330 core
  312. layout(location = 0) in vec3 vertexPosition_modelspace;
  313. layout(location = 1) in vec3 vertexColor;
  314. out vec3 Color;
  315. uniform mat4 MVP;
  316. void main() {
  317. vec4 pos = MVP * vec4(vertexPosition_modelspace.x,
  318. -vertexPosition_modelspace.y,
  319. vertexPosition_modelspace.z,
  320. 1);
  321. gl_Position = vec4(-pos.x, pos.yzw);
  322. Color = vertexColor;
  323. }
  324. )!?!";
  325. const char* Window::colorShaderFragment = R"!?!(
  326. #version 330 core
  327. in vec3 Color;
  328. out vec4 color;
  329. void main() {
  330. color = vec4(Color, 1);
  331. }
  332. )!?!";
  333. // --------------------------------------
  334. // *INDENT-ON*