Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <GL/glew.h>
- #include <GLFW/glfw3.h>
- #include <GL/GL.h>
- #include <SOIL.h>
- #include <glm/glm.hpp>
- #include <glm/gtc/type_ptr.hpp>
- const char* VERTEX_SRC = R"(
- #version 330 core
- layout(location = 0) in vec2 a_Position;
- layout(location = 1) in vec3 a_Color;
- layout(location = 2) in vec2 a_TexCoord;
- uniform float u_Time;
- uniform mat4 u_Transform;
- out vec2 f_TexCoord;
- out vec3 f_Color;
- void main()
- {
- gl_Position = u_Transform * vec4(a_Position, 0.0, 1.0);
- f_TexCoord = a_TexCoord;
- f_Color = a_Color;
- }
- )";
- const char* FRAGMENT_SRC = R"(
- #version 330 core
- uniform sampler2D u_Sampler1;
- uniform sampler2D u_Sampler2;
- uniform float u_Time;
- in vec2 f_TexCoord;
- in vec3 f_Color;
- out vec4 o_Color;
- void main()
- {
- vec2 TexCoord = f_TexCoord;
- TexCoord.y = 1.0 - TexCoord.y;
- vec4 Cheryl1 = texture(u_Sampler1, TexCoord);
- vec4 Cheryl2 = texture(u_Sampler2, TexCoord);
- o_Color = mix(Cheryl1, Cheryl2, sin(u_Time) * 0.5 + 0.5);
- }
- )";
- bool ShouldQuit = false;
- int quit_sequence[] = { GLFW_KEY_Q, GLFW_KEY_U, GLFW_KEY_I, GLFW_KEY_T };
- int quit_sequence_index = 0;
- void OnKeyEvent(GLFWwindow* Window, int Key, int Scancode, int Action, int Mods)
- {
- if (Action == GLFW_PRESS)
- {
- printf("OnKeyEvent( %d )\n", Key);
- if (Key == GLFW_KEY_ESCAPE)
- ShouldQuit = true;
- // Advance quit sequence
- if (Key == quit_sequence[quit_sequence_index])
- {
- quit_sequence_index++;
- if (quit_sequence_index >= 4)
- ShouldQuit = true;
- }
- else
- {
- quit_sequence_index = 0;
- }
- }
- }
- struct mat4
- {
- float m00;
- float m10;
- float m20;
- //...
- };
- int main()
- {
- /* WINDOW STUFF */
- // Initialize GLFW, create a window and context
- glfwInit();
- GLFWwindow* Window;
- Window = glfwCreateWindow(1024, 600, "This is OpenGL my dude", nullptr, nullptr);
- glfwSetKeyCallback(Window, OnKeyEvent);
- glfwMakeContextCurrent(Window);
- // After the context is bound, we can import the OpenGL extensions, through the extension wrangler
- glewInit();
- /* VERTEX STUFF */
- // Triangle, bejbii
- GLuint TriVAO;
- glGenVertexArrays(1, &TriVAO);
- glBindVertexArray(TriVAO);
- GLuint TriVertexBuffer;
- glGenBuffers(1, &TriVertexBuffer);
- glBindBuffer(GL_ARRAY_BUFFER, TriVertexBuffer);
- float TriData[] = {
- -0.5f, -0.5f, 1.f, 0.f, 0.f, 0.0f, 0.0f,
- +0.5f, -0.5f, 0.f, 1.f, 0.f, 1.0f, 0.0f,
- +0.0f, +0.5f, 0.f, 0.f, 1.f, 0.5f, 1.0f,
- };
- glBufferData(GL_ARRAY_BUFFER, sizeof(TriData), TriData, GL_STATIC_DRAW);
- // Bind attributes
- glEnableVertexAttribArray(0);
- glEnableVertexAttribArray(1);
- glEnableVertexAttribArray(2);
- glVertexAttribPointer(0, 2, GL_FLOAT, false, sizeof(float) * (2 + 3 + 2), 0);
- glVertexAttribPointer(1, 3, GL_FLOAT, false, sizeof(float) * (2 + 3 + 2), (void*)(sizeof(float) * 2));
- glVertexAttribPointer(2, 2, GL_FLOAT, false, sizeof(float) * (2 + 3 + 2), (void*)(sizeof(float) * (2 + 3)));
- // Make da quad
- GLuint QuadVAO;
- glGenVertexArrays(1, &QuadVAO);
- glBindVertexArray(QuadVAO);
- GLuint QuadVertexBuffer;
- glGenBuffers(1, &QuadVertexBuffer);
- glBindBuffer(GL_ARRAY_BUFFER, QuadVertexBuffer);
- float QuadData[] = {
- -0.5f, -0.5f, 1.f, 0.f, 0.f, 0.f, 0.f,
- +0.5f, -0.5f, 0.f, 1.f, 0.f, 1.f, 0.f,
- -0.5f, +0.5f, 0.f, 0.f, 1.f, 0.f, 1.f,
- +0.5f, +0.5f, 0.f, 0.f, 1.f, 1.f, 1.f,
- };
- glBufferData(GL_ARRAY_BUFFER, sizeof(QuadData), QuadData, GL_STATIC_DRAW);
- glEnableVertexAttribArray(0);
- glEnableVertexAttribArray(1);
- glEnableVertexAttribArray(2);
- glVertexAttribPointer(0, 2, GL_FLOAT, false, sizeof(float) * (2 + 3 + 2), 0);
- glVertexAttribPointer(1, 3, GL_FLOAT, false, sizeof(float) * (2 + 3 + 2), (void*)(sizeof(float) * 2));
- glVertexAttribPointer(2, 2, GL_FLOAT, false, sizeof(float) * (2 + 3 + 2), (void*)(sizeof(float) * (2 + 3)));
- glBindBuffer(GL_ARRAY_BUFFER, QuadVertexBuffer);
- glBindBuffer(GL_ARRAY_BUFFER, TriVertexBuffer);
- /* SHADER STUFF */
- // Create vertex and fragment shader
- GLuint VertexShader = glCreateShader(GL_VERTEX_SHADER);
- GLuint FragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
- // Upload shader code and compile!
- glShaderSource(VertexShader, 1, &VERTEX_SRC, nullptr);
- glCompileShader(VertexShader);
- glShaderSource(FragmentShader, 1, &FRAGMENT_SRC, nullptr);
- glCompileShader(FragmentShader);
- // Make and link shader program
- GLuint ShaderProgram = glCreateProgram();
- glAttachShader(ShaderProgram, VertexShader);
- glAttachShader(ShaderProgram, FragmentShader);
- glLinkProgram(ShaderProgram);
- glUseProgram(ShaderProgram);
- // Check for compile errors
- char InfoLogBuffer[1024];
- glGetProgramInfoLog(ShaderProgram, 1024, nullptr, InfoLogBuffer);
- printf("SHADER RESULT:\n%s\n", InfoLogBuffer);
- {
- int Width;
- int Height;
- int Channels;
- unsigned char* ImagePixels = SOIL_load_image("Res/img_cheryl.jpg", &Width, &Height, &Channels, SOIL_LOAD_RGBA);
- GLuint Texture;
- glGenTextures(1, &Texture);
- glBindTexture(GL_TEXTURE_2D, Texture);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Width, Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, ImagePixels);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
- glBindTexture(GL_TEXTURE_2D, Texture);
- }
- glActiveTexture(GL_TEXTURE1);
- {
- int Width;
- int Height;
- int Channels;
- unsigned char* ImagePixels = SOIL_load_image("Res/img_cheryl_new.jpg", &Width, &Height, &Channels, SOIL_LOAD_RGBA);
- GLuint Texture;
- glGenTextures(1, &Texture);
- glBindTexture(GL_TEXTURE_2D, Texture);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Width, Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, ImagePixels);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
- }
- // Get the time uniform location
- GLuint u_Time = glGetUniformLocation(ShaderProgram, "u_Time");
- GLuint u_Offset = glGetUniformLocation(ShaderProgram, "u_Offset");
- GLuint u_Sampler1 = glGetUniformLocation(ShaderProgram, "u_Sampler1");
- glUniform1i(u_Sampler1, 1);
- GLuint u_Transform = glGetUniformLocation(ShaderProgram, "u_Transform");
- float Ratio = 1024.f / 600.f;
- float OrthoSize = 2.f;
- glm::mat4 Projection;
- Projection = glm::perspective(glm::radians(60.f), Ratio, 0.2f, 100.f);
- //Projection = glm::ortho(-OrthoSize * Ratio, OrthoSize * Ratio, -OrthoSize, OrthoSize, -50.f, 50.f);
- glm::mat4 View;
- // Main loop of the program
- while (!glfwWindowShouldClose(Window) && !ShouldQuit)
- {
- 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));
- // Clear the screen!
- glClearColor(0.1f, 0.1f, 0.1f, 1.f);
- glClear(GL_COLOR_BUFFER_BIT);
- // Bind triangle
- glBindVertexArray(TriVAO);
- glUniform1f(u_Time, glfwGetTime());
- // Bind quad
- glBindVertexArray(QuadVAO);
- {
- glm::mat4 Translation = glm::translate(glm::mat4(1.f), glm::vec3(sin(glfwGetTime()), 0.f, 0.f));
- glm::mat4 Rotation = glm::rotate(glm::mat4(1.f), (float)glfwGetTime(), glm::vec3(0.f, 0.f, 1.f));
- glm::mat4 Scale = glm::scale(glm::mat4(1.f), glm::vec3(1.f, 0.5f, 1.f));
- glm::mat4 Transform = Projection * View * Translation * Rotation * Scale;
- glUniformMatrix4fv(u_Transform, 1, false, glm::value_ptr(Transform));
- }
- glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
- {
- //glm::mat4 Translation = glm::translate(glm::mat4(1.f), glm::vec3(sin(glfwGetTime() * 0.2f), cos(glfwGetTime() * 0.2f), 0.f));
- glm::mat4 Translation = glm::translate(glm::mat4(1.f), glm::vec3(0.f, 0.f, sin(glfwGetTime()) * 1.5f));
- 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));
- 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));
- glm::mat4 Transform = Projection * View * Translation * Rotation * Scale;
- printf("%f\n", sin(glfwGetTime()) * 1.5f);
- glUniformMatrix4fv(u_Transform, 1, false, glm::value_ptr(Transform));
- }
- glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
- {
- //glm::mat4 Translation = glm::translate(glm::mat4(1.f), glm::vec3(sin(glfwGetTime() * 0.2f), cos(glfwGetTime() * 0.2f), 0.f));
- glm::mat4 Translation = glm::translate(glm::mat4(1.f), glm::vec3(0.f, -0.5f, 0.f));
- 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));
- 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));
- glm::mat4 Transform = Projection * View * Translation * Rotation * Scale;
- printf("%f\n", sin(glfwGetTime()) * 1.5f);
- glUniformMatrix4fv(u_Transform, 1, false, glm::value_ptr(Transform));
- }
- glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
- // Swap the back-buffer to the front and poll and handle window events
- glfwSwapBuffers(Window);
- glfwPollEvents();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement