Skip to content

Instantly share code, notes, and snippets.

@junodeveloper
Created March 21, 2019 08:42
Show Gist options
  • Save junodeveloper/8c8de42e929a076d6d0134008b6db953 to your computer and use it in GitHub Desktop.
Save junodeveloper/8c8de42e929a076d6d0134008b6db953 to your computer and use it in GitHub Desktop.
InActivity 2
#version 330 core
out vec4 FragColor;
in vec3 ourColor;
void main()
{
FragColor = vec4(ourColor, 1.0f);
}
// 09_ShaderClass
// - Shader setup process simplified using Shader class
// - Color interpolation
// - ESC to quit
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <shader.h> // include from utils
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow *window);
void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods);
// settings
unsigned int SCR_WIDTH = 800;
unsigned int SCR_HEIGHT = 600;
float deltaX = 0, deltaY = 0;
bool isRed = false;
float vertices[] = {
// positions // colors
0.2f, 0.2f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom right
-0.2f, 0.2f, 0.0f, 1.0f, 1.0f, 0.0f, // bottom left
-0.2f, -0.2f, 0.0f, 0.0f, 0.0f, 1.0f, // top
0.2f, -0.2f, 0.0f, 0.0f, 1.0f, 0.0f // bottom left
};
int main()
{
// glfw: initialize and configure
// ------------------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
// glfw window creation
// --------------------
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "Shader Class", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetKeyCallback(window, key_callback);
// Allow modern extension features
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK)
{
printf("GLEW initialisation failed!");
glfwDestroyWindow(window);
glfwTerminate();
return 1;
}
// build and compile our shader program
// ------------------------------------
Shader interpolateShader("3.3.shader.vs", "3.3.shader.fs"); // you can name your shader files however you like
Shader solidShader("3.3.shader.vs", "3.3.shader_2.fs");
// set up vertex data (and buffer(s)) and configure vertex attributes
// ------------------------------------------------------------------
unsigned int VBO, VAO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// color attribute
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
// You can unbind the VAO afterwards so other VAO calls won't accidentally modify this VAO, but this rarely happens. Modifying other
// VAOs requires a call to glBindVertexArray anyways so we generally don't unbind VAOs (nor VBOs) when it's not directly necessary.
// glBindVertexArray(0);
// render loop
// -----------
while (!glfwWindowShouldClose(window))
{
// render
// ------
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// render the rectangle
if(isRed) {
solidShader.use();
solidShader.setVec2("deltaPos", deltaX, deltaY);
}
else {
interpolateShader.use();
interpolateShader.setVec2("deltaPos", deltaX, deltaY);
}
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
glfwSwapBuffers(window);
glfwPollEvents();
}
// optional: de-allocate all resources once they've outlived their purpose:
// ------------------------------------------------------------------------
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
// glfw: terminate, clearing all previously allocated GLFW resources.
// ------------------------------------------------------------------
glfwTerminate();
return 0;
}
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
// make sure the viewport matches the new window dimensions; note that width and
// height will be significantly larger than specified on retina displays.
glViewport(0, 0, width, height);
SCR_WIDTH = width;
SCR_HEIGHT = height;
}
bool within_range(float x, float y) {
const double C = 1.1;
return x >= -C && x <= C && y >= -C && y <= C;
}
void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) {
float oldX = deltaX, oldY = deltaY;
if (key == GLFW_KEY_LEFT && (action == GLFW_PRESS || action == GLFW_REPEAT)) {
deltaX -= 0.1f;
}
else if (key == GLFW_KEY_RIGHT && (action == GLFW_PRESS || action == GLFW_REPEAT)) {
deltaX += 0.1f;
}
else if (key == GLFW_KEY_UP && (action == GLFW_PRESS || action == GLFW_REPEAT)) {
deltaY += 0.1f;
}
else if (key == GLFW_KEY_DOWN && (action == GLFW_PRESS || action == GLFW_REPEAT)) {
deltaY -= 0.1f;
}
bool outOfRange = false;
for(int i=0; i<4; i++) {
float x = vertices[i * 6] + deltaX;
float y = vertices[i * 6 + 1] + deltaY;
if(!within_range(x, y)) {
outOfRange = true;
break;
}
}
if(outOfRange) {
deltaX = oldX;
deltaY = oldY;
}
if(action != GLFW_RELEASE) isRed = outOfRange;
}
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
out vec3 ourColor;
uniform vec2 deltaPos;
void main()
{
gl_Position = vec4(aPos.x + deltaPos.x, aPos.y + deltaPos.y, aPos.z, 1.0);
ourColor = aColor;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment