Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <GL/glew.h>
  3. #include <GLFW/glfw3.h>
  4. #include <GL/GL.h>
  5. #include <SOIL.h>
  6. #include <glm/glm.hpp>
  7. #include <glm/gtc/type_ptr.hpp>
  8.  
  9. const char* VERTEX_SRC = R"(
  10. #version 330 core
  11. layout(location = 0) in vec2 a_Position;
  12. layout(location = 1) in vec3 a_Color;
  13. layout(location = 2) in vec2 a_TexCoord;
  14.  
  15. uniform float u_Time;
  16. uniform mat4 u_Transform;
  17.  
  18. out vec2 f_TexCoord;
  19. out vec3 f_Color;
  20.  
  21. void main()
  22. {
  23. gl_Position = u_Transform * vec4(a_Position, 0.0, 1.0);
  24.  
  25. f_TexCoord = a_TexCoord;
  26. f_Color = a_Color;
  27. }
  28. )";
  29.  
  30. const char* FRAGMENT_SRC = R"(
  31. #version 330 core
  32. uniform sampler2D u_Sampler1;
  33. uniform sampler2D u_Sampler2;
  34.  
  35. uniform float u_Time;
  36.  
  37. in vec2 f_TexCoord;
  38. in vec3 f_Color;
  39. out vec4 o_Color;
  40.  
  41. void main()
  42. {
  43. vec2 TexCoord = f_TexCoord;
  44. TexCoord.y = 1.0 - TexCoord.y;
  45.  
  46. vec4 Cheryl1 = texture(u_Sampler1, TexCoord);
  47. vec4 Cheryl2 = texture(u_Sampler2, TexCoord);
  48.  
  49. o_Color = mix(Cheryl1, Cheryl2, sin(u_Time) * 0.5 + 0.5);
  50. }
  51. )";
  52.  
  53. bool ShouldQuit = false;
  54. int quit_sequence[] = { GLFW_KEY_Q, GLFW_KEY_U, GLFW_KEY_I, GLFW_KEY_T };
  55. int quit_sequence_index = 0;
  56.  
  57. void OnKeyEvent(GLFWwindow* Window, int Key, int Scancode, int Action, int Mods)
  58. {
  59. if (Action == GLFW_PRESS)
  60. {
  61. printf("OnKeyEvent( %d )\n", Key);
  62.  
  63. if (Key == GLFW_KEY_ESCAPE)
  64. ShouldQuit = true;
  65.  
  66. // Advance quit sequence
  67. if (Key == quit_sequence[quit_sequence_index])
  68. {
  69. quit_sequence_index++;
  70. if (quit_sequence_index >= 4)
  71. ShouldQuit = true;
  72. }
  73. else
  74. {
  75. quit_sequence_index = 0;
  76. }
  77. }
  78. }
  79.  
  80. struct mat4
  81. {
  82. float m00;
  83. float m10;
  84. float m20;
  85. //...
  86. };
  87.  
  88. int main()
  89. {
  90. /* WINDOW STUFF */
  91. // Initialize GLFW, create a window and context
  92. glfwInit();
  93.  
  94. GLFWwindow* Window;
  95. Window = glfwCreateWindow(1024, 600, "This is OpenGL my dude", nullptr, nullptr);
  96.  
  97. glfwSetKeyCallback(Window, OnKeyEvent);
  98. glfwMakeContextCurrent(Window);
  99.  
  100. // After the context is bound, we can import the OpenGL extensions, through the extension wrangler
  101. glewInit();
  102.  
  103. /* VERTEX STUFF */
  104.  
  105. // Triangle, bejbii
  106. GLuint TriVAO;
  107. glGenVertexArrays(1, &TriVAO);
  108. glBindVertexArray(TriVAO);
  109.  
  110. GLuint TriVertexBuffer;
  111. glGenBuffers(1, &TriVertexBuffer);
  112. glBindBuffer(GL_ARRAY_BUFFER, TriVertexBuffer);
  113.  
  114. float TriData[] = {
  115. -0.5f, -0.5f, 1.f, 0.f, 0.f, 0.0f, 0.0f,
  116. +0.5f, -0.5f, 0.f, 1.f, 0.f, 1.0f, 0.0f,
  117. +0.0f, +0.5f, 0.f, 0.f, 1.f, 0.5f, 1.0f,
  118. };
  119.  
  120. glBufferData(GL_ARRAY_BUFFER, sizeof(TriData), TriData, GL_STATIC_DRAW);
  121.  
  122. // Bind attributes
  123. glEnableVertexAttribArray(0);
  124. glEnableVertexAttribArray(1);
  125. glEnableVertexAttribArray(2);
  126. glVertexAttribPointer(0, 2, GL_FLOAT, false, sizeof(float) * (2 + 3 + 2), 0);
  127. glVertexAttribPointer(1, 3, GL_FLOAT, false, sizeof(float) * (2 + 3 + 2), (void*)(sizeof(float) * 2));
  128. glVertexAttribPointer(2, 2, GL_FLOAT, false, sizeof(float) * (2 + 3 + 2), (void*)(sizeof(float) * (2 + 3)));
  129.  
  130. // Make da quad
  131. GLuint QuadVAO;
  132. glGenVertexArrays(1, &QuadVAO);
  133. glBindVertexArray(QuadVAO);
  134.  
  135. GLuint QuadVertexBuffer;
  136. glGenBuffers(1, &QuadVertexBuffer);
  137. glBindBuffer(GL_ARRAY_BUFFER, QuadVertexBuffer);
  138.  
  139. float QuadData[] = {
  140. -0.5f, -0.5f, 1.f, 0.f, 0.f, 0.f, 0.f,
  141. +0.5f, -0.5f, 0.f, 1.f, 0.f, 1.f, 0.f,
  142. -0.5f, +0.5f, 0.f, 0.f, 1.f, 0.f, 1.f,
  143. +0.5f, +0.5f, 0.f, 0.f, 1.f, 1.f, 1.f,
  144. };
  145.  
  146. glBufferData(GL_ARRAY_BUFFER, sizeof(QuadData), QuadData, GL_STATIC_DRAW);
  147. glEnableVertexAttribArray(0);
  148. glEnableVertexAttribArray(1);
  149. glEnableVertexAttribArray(2);
  150. glVertexAttribPointer(0, 2, GL_FLOAT, false, sizeof(float) * (2 + 3 + 2), 0);
  151. glVertexAttribPointer(1, 3, GL_FLOAT, false, sizeof(float) * (2 + 3 + 2), (void*)(sizeof(float) * 2));
  152. glVertexAttribPointer(2, 2, GL_FLOAT, false, sizeof(float) * (2 + 3 + 2), (void*)(sizeof(float) * (2 + 3)));
  153.  
  154. glBindBuffer(GL_ARRAY_BUFFER, QuadVertexBuffer);
  155. glBindBuffer(GL_ARRAY_BUFFER, TriVertexBuffer);
  156.  
  157. /* SHADER STUFF */
  158. // Create vertex and fragment shader
  159. GLuint VertexShader = glCreateShader(GL_VERTEX_SHADER);
  160. GLuint FragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  161.  
  162. // Upload shader code and compile!
  163. glShaderSource(VertexShader, 1, &VERTEX_SRC, nullptr);
  164. glCompileShader(VertexShader);
  165. glShaderSource(FragmentShader, 1, &FRAGMENT_SRC, nullptr);
  166. glCompileShader(FragmentShader);
  167.  
  168. // Make and link shader program
  169. GLuint ShaderProgram = glCreateProgram();
  170. glAttachShader(ShaderProgram, VertexShader);
  171. glAttachShader(ShaderProgram, FragmentShader);
  172. glLinkProgram(ShaderProgram);
  173. glUseProgram(ShaderProgram);
  174.  
  175. // Check for compile errors
  176. char InfoLogBuffer[1024];
  177. glGetProgramInfoLog(ShaderProgram, 1024, nullptr, InfoLogBuffer);
  178. printf("SHADER RESULT:\n%s\n", InfoLogBuffer);
  179.  
  180. {
  181. int Width;
  182. int Height;
  183. int Channels;
  184. unsigned char* ImagePixels = SOIL_load_image("Res/img_cheryl.jpg", &Width, &Height, &Channels, SOIL_LOAD_RGBA);
  185.  
  186. GLuint Texture;
  187. glGenTextures(1, &Texture);
  188. glBindTexture(GL_TEXTURE_2D, Texture);
  189. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Width, Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, ImagePixels);
  190.  
  191. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  192. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  193. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  194. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  195.  
  196. glBindTexture(GL_TEXTURE_2D, Texture);
  197. }
  198.  
  199. glActiveTexture(GL_TEXTURE1);
  200.  
  201. {
  202. int Width;
  203. int Height;
  204. int Channels;
  205. unsigned char* ImagePixels = SOIL_load_image("Res/img_cheryl_new.jpg", &Width, &Height, &Channels, SOIL_LOAD_RGBA);
  206.  
  207. GLuint Texture;
  208. glGenTextures(1, &Texture);
  209. glBindTexture(GL_TEXTURE_2D, Texture);
  210. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Width, Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, ImagePixels);
  211.  
  212. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  213. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  214. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  215. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  216. }
  217.  
  218. // Get the time uniform location
  219. GLuint u_Time = glGetUniformLocation(ShaderProgram, "u_Time");
  220. GLuint u_Offset = glGetUniformLocation(ShaderProgram, "u_Offset");
  221.  
  222. GLuint u_Sampler1 = glGetUniformLocation(ShaderProgram, "u_Sampler1");
  223. glUniform1i(u_Sampler1, 1);
  224.  
  225. GLuint u_Transform = glGetUniformLocation(ShaderProgram, "u_Transform");
  226.  
  227. float Ratio = 1024.f / 600.f;
  228. float OrthoSize = 2.f;
  229.  
  230. glm::mat4 Projection;
  231. Projection = glm::perspective(glm::radians(60.f), Ratio, 0.2f, 100.f);
  232. //Projection = glm::ortho(-OrthoSize * Ratio, OrthoSize * Ratio, -OrthoSize, OrthoSize, -50.f, 50.f);
  233.  
  234. glm::mat4 View;
  235.  
  236. // Main loop of the program
  237. while (!glfwWindowShouldClose(Window) && !ShouldQuit)
  238. {
  239. View = glm::lookAt(glm::vec3(sin(glfwGetTime()), 1.f, cos(glfwGetTime())) * 3.f, glm::vec3(0.f, 0.f, 0.f), glm::vec3(0.f, 1.f, 0.f));
  240.  
  241. // Clear the screen!
  242. glClearColor(0.1f, 0.1f, 0.1f, 1.f);
  243. glClear(GL_COLOR_BUFFER_BIT);
  244.  
  245. // Bind triangle
  246. glBindVertexArray(TriVAO);
  247. glUniform1f(u_Time, glfwGetTime());
  248.  
  249. // Bind quad
  250. glBindVertexArray(QuadVAO);
  251. {
  252. glm::mat4 Translation = glm::translate(glm::mat4(1.f), glm::vec3(sin(glfwGetTime()), 0.f, 0.f));
  253. glm::mat4 Rotation = glm::rotate(glm::mat4(1.f), (float)glfwGetTime(), glm::vec3(0.f, 0.f, 1.f));
  254. glm::mat4 Scale = glm::scale(glm::mat4(1.f), glm::vec3(1.f, 0.5f, 1.f));
  255. glm::mat4 Transform = Projection * View * Translation * Rotation * Scale;
  256. glUniformMatrix4fv(u_Transform, 1, false, glm::value_ptr(Transform));
  257. }
  258. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  259.  
  260. {
  261. //glm::mat4 Translation = glm::translate(glm::mat4(1.f), glm::vec3(sin(glfwGetTime() * 0.2f), cos(glfwGetTime() * 0.2f), 0.f));
  262. glm::mat4 Translation = glm::translate(glm::mat4(1.f), glm::vec3(0.f, 0.f, sin(glfwGetTime()) * 1.5f));
  263. glm::mat4 Rotation = glm::mat4(1.f);// glm::rotate(glm::mat4(1.f), glm::radians(-20.f), glm::vec3(0.f, 0.f, 1.f));
  264. glm::mat4 Scale = glm::mat4(1.f); // glm::scale(glm::mat4(1.f), glm::vec3(1.f + sin(glfwGetTime()) * 0.2f, 1.f + sin(glfwGetTime()) * 0.4f, 1.f));
  265. glm::mat4 Transform = Projection * View * Translation * Rotation * Scale;
  266.  
  267. printf("%f\n", sin(glfwGetTime()) * 1.5f);
  268. glUniformMatrix4fv(u_Transform, 1, false, glm::value_ptr(Transform));
  269. }
  270. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  271.  
  272. {
  273. //glm::mat4 Translation = glm::translate(glm::mat4(1.f), glm::vec3(sin(glfwGetTime() * 0.2f), cos(glfwGetTime() * 0.2f), 0.f));
  274. glm::mat4 Translation = glm::translate(glm::mat4(1.f), glm::vec3(0.f, -0.5f, 0.f));
  275. glm::mat4 Rotation = glm::rotate(glm::mat4(1.f), glm::radians(90.f), glm::vec3(1.f, 0.f, 0.f));// glm::rotate(glm::mat4(1.f), glm::radians(-20.f), glm::vec3(0.f, 0.f, 1.f));
  276. glm::mat4 Scale = glm::mat4(1.f); // glm::scale(glm::mat4(1.f), glm::vec3(1.f + sin(glfwGetTime()) * 0.2f, 1.f + sin(glfwGetTime()) * 0.4f, 1.f));
  277. glm::mat4 Transform = Projection * View * Translation * Rotation * Scale;
  278.  
  279. printf("%f\n", sin(glfwGetTime()) * 1.5f);
  280. glUniformMatrix4fv(u_Transform, 1, false, glm::value_ptr(Transform));
  281. }
  282. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  283.  
  284. // Swap the back-buffer to the front and poll and handle window events
  285. glfwSwapBuffers(Window);
  286. glfwPollEvents();
  287. }
  288.  
  289. return 0;
  290. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement