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.5KB

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