Open Source Tomb Raider Engine
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Window.cpp 9.5KB

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