Skip to content

Instantly share code, notes, and snippets.

@nickav
Created May 8, 2019 18:19
Show Gist options
  • Save nickav/773136c15cbd857d4d5f21c99f2c8148 to your computer and use it in GitHub Desktop.
Save nickav/773136c15cbd857d4d5f21c99f2c8148 to your computer and use it in GitHub Desktop.
#include "deps/glfw/include/GLFW/glfw3.h"
#include <stdio.h>
void shader_print_errors(GLuint id) {
GLint success;
glGetShaderiv(id, GL_COMPILE_STATUS, &success);
if (!success) {
GLchar infoLog[1024];
glGetShaderInfoLog(id, 1024, NULL, infoLog);
printf("%s\n", infoLog);
}
}
int main(int argc, char **argv) {
// config
int window_width = 640;
int window_height = 480;
const char *window_title = "OpenGL";
// 1. make a window (to get the OpenGL context)
glfwInit();
GLFWwindow *window =
glfwCreateWindow(window_width, window_height, window_title, NULL, NULL);
// tell glfw to use this window for all OpenGL rendering calls:
glfwMakeContextCurrent(window);
// 2. create and compile shaders (which are uploaded to the GPU)
// create vertex shader
const char *vert = R"(
attribute vec2 position;
void main() {
gl_Position = vec4(position, 0.0, 1.0);
}
)";
GLuint vertId = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertId, 1, &vert, NULL);
glCompileShader(vertId);
// did we get any errors during compilation?
shader_print_errors(vertId);
// create fragment (pixel) shader
const char *frag = R"(
void main() {
gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
}
)";
GLuint fragId = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragId, 1, &frag, NULL);
glCompileShader(fragId);
// did we get any errors during compilation?
shader_print_errors(fragId);
// 3. link shaders together (into a pipeline program)
GLuint shaderId = glCreateProgram();
glAttachShader(shaderId, vertId);
glAttachShader(shaderId, fragId);
glLinkProgram(shaderId);
// delete the shaders because they're linked to our program now
glDeleteShader(vertId);
glDeleteShader(fragId);
// 4. create vertex buffer (array of floats)
float vertices[] = {
0.0f, 0.5f, // (x0, y0)
0.5f, -0.5f, // (x1, y1)
-0.5f, -0.5f, // (x2, y2)
};
GLuint bufferId;
glGenBuffers(1, &bufferId);
glBindBuffer(GL_ARRAY_BUFFER, bufferId);
// uplaod the array to the GPU
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// unbind the buffer (in case there were more buffers)
glBindBuffer(GL_ARRAY_BUFFER, 0);
// render loop
while (!glfwWindowShouldClose(window)) {
// 5. set the background color
glClearColor(1.0, 0.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
// 6. render our shader
// use our vertex buffer
glBindBuffer(GL_ARRAY_BUFFER, bufferId);
// use our shader
glUseProgram(shaderId);
// tell OpenGL the layout of our vertices array
GLsizei stride = 2 * sizeof(GLfloat);
GLint loc = glGetAttribLocation(shaderId, "position");
glEnableVertexAttribArray(loc);
glVertexAttribPointer(loc, 2, GL_FLOAT, GL_FALSE, stride, (void *)(0));
// this line actually renders stuff to the screen with the current buffer
// and shader that we bound above
glDrawArrays(GL_TRIANGLES, 0, 6);
// do glfw window-y things
glfwSwapBuffers(window);
glfwPollEvents();
}
// delete opengl objects
glDeleteProgram(shaderId);
glDeleteBuffers(1, &bufferId);
// done with our window
glfwDestroyWindow(window);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment