Last active
August 26, 2021 22:12
-
-
Save EmmanuelOga/275a3ea5003f9f8574aa9ef4196e4d9d to your computer and use it in GitHub Desktop.
Draw a couple of triangles: OpenGL Basic GLFW/VAO[VBO] setup.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Local Headers | |
#include "glitter.hpp" | |
// System Headers | |
#include <glad/glad.h> | |
#include <GLFW/glfw3.h> | |
// Standard Headers | |
#include <iostream> | |
#include <cstdio> | |
#include <cstdlib> | |
#include <vector> | |
#include <iterator> | |
int main(int argc, char * argv[]) { | |
// Load GLFW and Create a Window | |
glfwInit(); | |
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); | |
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); | |
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); | |
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); | |
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); | |
auto mWindow = glfwCreateWindow(mWidth, mHeight, "OpenGL", nullptr, nullptr); | |
// Check for Valid Context | |
if (mWindow == nullptr) { | |
fprintf(stderr, "Failed to Create OpenGL Context"); | |
return EXIT_FAILURE; | |
} | |
// Create Context and Load OpenGL Functions | |
glfwMakeContextCurrent(mWindow); | |
gladLoadGL(); | |
fprintf(stderr, "OpenGL %s\n", glGetString(GL_VERSION)); | |
/* tell GL to only draw onto a pixel if the shape is closer to the viewer | |
than anything already drawn at that pixel */ | |
glEnable(GL_DEPTH_TEST); /* enable depth-testing */ | |
/* with LESS depth-testing interprets a smaller depth value as meaning "closer" */ | |
glDepthFunc(GL_LESS); | |
const char* vertex_shader_source = R"( | |
#version 410 | |
in vec3 vp; | |
void main() { | |
gl_Position = vec4(vp, 1.0); | |
} | |
)"; | |
const char* fragment_shader_source = R"( | |
#version 410 | |
out vec4 frag_color; | |
void main() { | |
frag_color = vec4(0.5, 0.0, 0.0, 0.0); | |
} | |
)"; | |
GLfloat points[] = { | |
0.0f, 0.5f, 0.0f, | |
0.5f, -0.5f, 0.0f, | |
-0.5f, -0.5f, 0.0f | |
}; | |
GLfloat points2[] = { | |
-0.0f, -0.5f, 0.0f, | |
-0.5f, 0.5f, 0.0f, | |
0.5f, 0.5f, 0.0f | |
}; | |
// Generate buffer and bind to data. | |
// We could ask for a reference to a fixed size array here, | |
// but instead we ask for a begin/end pointer as customary in other C++ APIs. | |
auto genVAO = [](GLfloat *beg, GLfloat *end) { | |
GLuint vbo = 0; | |
glGenBuffers(1, &vbo); | |
glBindBuffer(GL_ARRAY_BUFFER, vbo); | |
glBufferData(GL_ARRAY_BUFFER, (end - beg) * sizeof(GLfloat), beg, GL_STATIC_DRAW); | |
// Generate wrapping vertex array object. | |
GLuint vao; | |
glGenVertexArrays(1, &vao); | |
glBindVertexArray(vao); | |
glEnableVertexAttribArray(0); | |
glBindBuffer(GL_ARRAY_BUFFER, vbo); | |
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr); | |
return vao; | |
}; | |
GLuint vao = genVAO(std::begin(points), std::end(points)); | |
GLuint vao2 = genVAO(std::begin(points2), std::end(points2)); | |
GLuint vs = glCreateShader(GL_VERTEX_SHADER); | |
glShaderSource(vs, 1, &vertex_shader_source, nullptr); | |
glCompileShader(vs); | |
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER); | |
glShaderSource(fs, 1, &fragment_shader_source, nullptr); | |
glCompileShader(fs); | |
GLuint program = glCreateProgram(); | |
glAttachShader(program, vs); | |
glAttachShader(program, fs); | |
glLinkProgram(program); | |
glDeleteShader(vs); | |
glDeleteShader(fs); | |
int col = 0; | |
// Rendering Loop | |
while (glfwWindowShouldClose(mWindow) == false) { | |
if (glfwGetKey(mWindow, GLFW_KEY_ESCAPE) == GLFW_PRESS) | |
glfwSetWindowShouldClose(mWindow, true); | |
col++; | |
glClearColor(0.25f, 0.25f, (col % 100) / 100.0, 1.0f); | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
glUseProgram(program); | |
glBindVertexArray(vao); | |
glDrawArrays(GL_TRIANGLES, 0, 3); | |
glBindVertexArray(vao2); | |
glDrawArrays(GL_TRIANGLES, 0, 3); | |
// Flip Buffers and Draw | |
glfwSwapBuffers(mWindow); | |
glfwPollEvents(); | |
} glfwTerminate(); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment