Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* put these on vShader.vs & fShader.fs
- ________________________________________________________________
- vShader.vs :
- #version 330 core
- layout (location = 0) in vec3 aPos;
- layout (location = 1) in vec3 aColor;
- uniform float xPos;
- uniform float yPos;
- out vec3 ourColor;
- void main()
- {
- gl_Position = vec4(aPos.x + xPos,aPos.y + yPos,aPos.z, 1.0);
- ourColor = aColor;
- }
- ________________________________________________________________
- fShader.fs :
- #version 330 core
- out vec4 FragColor;
- in vec3 ourColor;
- void main()
- {
- FragColor = vec4(ourColor, 1.0f);
- }
- ________________________________________________________________
- */
- #include <iostream>
- #include <glad\glad.h>
- #include <GLFW\glfw3.h>
- #include <GLFW\shader_s.h>
- bool pressedOnce = false;
- float xOffset = 0.0f;
- float yOffset = 0.0f;
- void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
- {
- if (glfwGetKey(window, GLFW_KEY_ENTER) == GLFW_PRESS)
- pressedOnce = true;
- if (glfwGetKey(window, GLFW_KEY_ENTER) == GLFW_RELEASE && pressedOnce == true)
- glfwSetWindowShouldClose(window, true);
- if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
- yOffset += 5;
- if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
- yOffset -= 5;
- if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
- xOffset += 5;
- if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
- xOffset -= 5;
- }
- void GLAD_Check()
- {
- if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
- std::cout << "Failed to initialize GLAD" << std::endl;
- }
- void Resizer(GLFWwindow *window, int width , int height)
- {
- glViewport(0, 0, width, height);
- }
- int main()
- {
- // Setup ------------------------------------
- // window*********
- glfwInit();
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- GLFWwindow *window = glfwCreateWindow(800, 600, "Shaders", NULL, NULL);
- glfwMakeContextCurrent(window);
- GLAD_Check();
- glViewport(0, 0, 800, 600);
- glfwSetFramebufferSizeCallback(window, Resizer);
- float vertcies[] =
- { // vertcies // color
- 0.0f, 0.5f, 1.0f, 1.0f, 0.0f, 0.0f, // top middle
- - 0.5f, -0.5f, 1.0f, 0.0f, 1.0f, 0.0f,// bot left
- 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f// bot right
- };
- // VBO & VAO ********
- unsigned int VBO;
- glGenBuffers(1, &VBO);
- glBindBuffer(GL_ARRAY_BUFFER, VBO);
- glBufferData(GL_ARRAY_BUFFER, sizeof(vertcies), vertcies,GL_STATIC_DRAW);
- glCompileShader(VBO);
- unsigned int VAO;
- glGenVertexArrays(1, &VAO);
- glBindVertexArray(VAO);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
- glEnableVertexAttribArray(0);
- glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
- glEnableVertexAttribArray(1);
- Shader programShader("Shaders/vShader.vs", "Shaders/fShader.fs"); // fragment/vertex shader Path
- while (!glfwWindowShouldClose(window))
- {
- glfwSetKeyCallback(window, key_callback);
- glClear(GL_COLOR_BUFFER_BIT);
- glClearColor(0.1f, 0.3f, 0.6f,1.0f);
- programShader.use();
- glUniform1f(glGetUniformLocation(programShader.ID, "xPos"), xOffset / 800);
- glUniform1f(glGetUniformLocation(programShader.ID, "yPos"), yOffset / 600);
- glBindVertexArray(VAO);
- glDrawArrays(GL_TRIANGLES,0,3);
- glfwPollEvents();
- glfwSwapBuffers(window);
- }
- glfwTerminate();
- return 0;
- }
Add Comment
Please, Sign In to add comment