-
-
Save shanecelis/2779930 to your computer and use it in GitHub Desktop.
Example OpenGL3.2 buffer/shader usage
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
/* | |
Original: 1562391 by branan | |
Updated to use GLFW so it works on Mac OS X Lion | |
*/ | |
#include <unistd.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#define GLFW_NO_GLU | |
#define GLFW_INCLUDE_GL3 | |
#include <GL/glfw.h> | |
GLfloat vert_data[] = { | |
-1.0, -1.0, 0.0, | |
1.0, -1.0, 0.0, | |
0.0, 1.0, 0.0, | |
}; | |
GLfloat col_data[] = { | |
1.0, 0.0, 0.0, | |
0.0, 1.0, 0.0, | |
0.0, 0.0, 1.0 | |
}; | |
#define POSITION_ATTRIB 0 | |
#define COLOR_ATTRIB 1 | |
const char* vert_shader = | |
"\ | |
#version 150\n\ | |
\n\ | |
in vec3 position;\n\ | |
in vec3 color;\n\ | |
out vec3 vcol;\n\ | |
\n\ | |
void main(void) { \n\ | |
vcol = color;\n\ | |
gl_Position = vec4(position, 1.0);\n\ | |
}\n\ | |
"; | |
const char* frag_shader = | |
"\ | |
#version 150\n\ | |
\n\ | |
in vec3 vcol;\n\ | |
out vec4 fcol;\n\ | |
\n\ | |
void main(void) { \n\ | |
fcol = vec4(vcol,1.0);\n\ | |
}\n\ | |
"; | |
void init(void) { | |
GLuint vao; | |
GLuint bufs[2]; | |
glGenVertexArrays(1, &vao); | |
glGenBuffers(2, bufs); | |
glBindVertexArray(vao); | |
glEnableVertexAttribArray(POSITION_ATTRIB); | |
glEnableVertexAttribArray(COLOR_ATTRIB); | |
glBindBuffer(GL_ARRAY_BUFFER, bufs[0]); | |
glBufferData(GL_ARRAY_BUFFER, 3*3*sizeof(GLfloat), vert_data, GL_STATIC_DRAW); | |
glVertexAttribPointer(POSITION_ATTRIB, 3, GL_FLOAT, GL_FALSE, 0, 0); | |
glBindBuffer(GL_ARRAY_BUFFER, bufs[1]); | |
glBufferData(GL_ARRAY_BUFFER, 3*3*sizeof(GLfloat), col_data, GL_STATIC_DRAW); | |
glVertexAttribPointer(COLOR_ATTRIB, 3, GL_FLOAT, GL_FALSE, 0, 0); | |
GLuint prog; | |
GLuint vert; | |
GLuint frag; | |
prog = glCreateProgram(); | |
vert = glCreateShader(GL_VERTEX_SHADER); | |
frag = glCreateShader(GL_FRAGMENT_SHADER); | |
glShaderSource(vert, 1, &vert_shader, 0); | |
glShaderSource(frag, 1, &frag_shader, 0); | |
glCompileShader(vert); | |
glCompileShader(frag); | |
glAttachShader(prog, vert); | |
glAttachShader(prog, frag); | |
glBindAttribLocation(prog, POSITION_ATTRIB, "position"); | |
glBindAttribLocation(prog, COLOR_ATTRIB, "color"); | |
glLinkProgram(prog); | |
glUseProgram(prog); | |
} | |
void render(void) { | |
glClear(GL_COLOR_BUFFER_BIT); | |
glDrawArrays(GL_TRIANGLES, 0, 3); | |
glfwSwapBuffers(); | |
} | |
int main(int argc, char** argv) { | |
if (GL_TRUE != glfwInit()) | |
{ | |
fprintf(stderr, "ERROR: Unable to initialize GLFW\n"); | |
} | |
glfwEnable(GLFW_AUTO_POLL_EVENTS); /* No explicit call to glfwPollEvents() */ | |
glfwOpenWindowHint(GLFW_WINDOW_NO_RESIZE, GL_TRUE); | |
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3); | |
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2); | |
glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); | |
glfwOpenWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); | |
if (GL_TRUE != glfwOpenWindow(800, 600, 0, 0, 0, 0, 0, 0, GLFW_WINDOW)) | |
{ | |
fprintf(stderr, "ERROR: Unable to create the OpenGL context and associated window\n"); | |
exit(EXIT_FAILURE); | |
} | |
init(); | |
do { | |
render(); | |
} while(glfwGetKey( GLFW_KEY_ESC ) != GLFW_PRESS && | |
glfwGetKey( 'Q' ) != GLFW_PRESS && | |
glfwGetWindowParam( GLFW_OPENED ) ); | |
glfwTerminate(); | |
exit(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment