Created
March 24, 2015 12:50
-
-
Save pedrovanzella/af9b0e0b667976b57b51 to your computer and use it in GitHub Desktop.
Hello OpenGL
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
#include <GL/glew.h> | |
#define GLFW_DLL | |
#include <GLFW/glfw3.h> | |
#include <stdio.h> | |
int main(int argc, char* argv[]) | |
{ | |
// start GL context and OS window using GLFW | |
if (!glfwInit()) { | |
fprintf(stderr, "ERROR: could not start GLFW3\n"); | |
return 1; | |
} | |
GLFWwindow* window = glfwCreateWindow(640, 480, "Hello Triangle", NULL, NULL); | |
if (!window) { | |
fprintf(stderr, "ERROR: could not open window\n"); | |
glfwTerminate(); | |
return 1; | |
} | |
glfwMakeContextCurrent(window); | |
// start GLEW extension handler | |
glewExperimental = GL_TRUE; | |
glewInit(); | |
// get version info | |
const GLubyte* renderer = glGetString(GL_RENDERER); | |
const GLubyte* version = glGetString(GL_VERSION); | |
printf("Renderer: %s\n", renderer); | |
printf("OpenGL version: %s\n", version); | |
glEnable(GL_DEPTH_TEST); | |
glDepthFunc(GL_LESS); | |
/* OTHER STUFF GOES HERE NEXT */ | |
float points[] = { | |
0.0f, 0.5f, 0.0f, | |
0.5f, -0.5f, 0.0f, | |
-0.5f, -0.5f, 0.0f | |
}; | |
// Vertex Buffer Object | |
GLuint vbo = 0; | |
glGenBuffers(1, &vbo); | |
glBindBuffer(GL_ARRAY_BUFFER, vbo); | |
glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(float), points, GL_STATIC_DRAW); | |
// Vertex Attribute Object | |
GLuint vao = 0; | |
glGenVertexArrays(1, &vao); | |
glBindVertexArray(vao); | |
glEnableVertexAttribArray(0); | |
glBindBuffer(GL_ARRAY_BUFFER, vbo); | |
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL); | |
// Define shaders | |
/* http://en.wikipedia.org/wiki/OpenGL_Shading_Language */ | |
const char* vertex_shader = | |
"#version 130\n" /* 400 for OpenGL 4, 150 for OpenGL 3 */ | |
"in vec3 vp;" | |
"void main() {" | |
" gl_Position = vec4(vp, 1.0);" | |
"}"; | |
const char* fragment_shader = | |
"#version 130\n" | |
"out vec4 frag_colour;" | |
"void main() {" | |
" frag_colour = vec4(0.5, 0.0, 0.5, 1.0);" | |
"}"; | |
// Compile shaders | |
GLuint vs = glCreateShader(GL_VERTEX_SHADER); | |
glShaderSource(vs, 1, &vertex_shader, NULL); | |
glCompileShader(vs); | |
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER); | |
glShaderSource(fs, 1, &fragment_shader, NULL); | |
glCompileShader(fs); | |
// Create Shader Program | |
GLuint shader_programme = glCreateProgram(); | |
glAttachShader(shader_programme, fs); | |
glAttachShader(shader_programme, vs); | |
glLinkProgram(shader_programme); | |
// Draw loop | |
while (!glfwWindowShouldClose(window)) { | |
// wipe the drawing surface | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
glUseProgram(shader_programme); | |
glBindVertexArray(vao); | |
// draw points 0-3 from the currently bound VAO with shaders | |
glDrawArrays(GL_TRIANGLES, 0, 3); | |
// update other events like input handling | |
glfwPollEvents(); | |
// put the stuff we've been drawing on the display | |
glfwSwapBuffers(window); | |
} | |
// close GL context and any other GLFW resources | |
glfwTerminate(); | |
return 0; | |
} |
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
all: hellogl | |
hellogl: main.c | |
clang -o hellogl main.c -lglfw -lGL -lGLEW |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment