Created
November 28, 2011 18:03
-
-
Save hashmal/1401335 to your computer and use it in GitHub Desktop.
OpenGL 3.2 Core Profile, GLFW, Mac OS X Lion [FIXED]
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
// The following code doesn't work on Mac OS X Lion | |
// (I don't know for other platforms): | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <assert.h> | |
#include <GL/glfw.h> | |
char *VS = "#version 150\n in vec2 position; void main() {gl_Position = vec4(position, 0.0, 1.0);}"; | |
char *FS = "#version 150\n out vec4 fragColor; void main() {fragColor = vec4(1.0, 0.0, 0.0, 1.0);}"; | |
// quad vertices | |
GLfloat quad[] = {-0.5, -0.5, 0.5, -0.5, 0.5, 0.5, -0.5, 0.5}; | |
// quad indices | |
GLubyte indices[] = {0, 1, 2, 3}; | |
GLuint vao; | |
GLuint vertex_buffer, element_buffer; | |
GLuint quad_program; | |
void initQuad (void) { | |
// SHADERS ----------------------------------------------------------- | |
// vertex shader | |
GLuint v_shader = glCreateShader(GL_VERTEX_SHADER); | |
glShaderSource(v_shader, 1, (const GLchar**)&VS, 0); | |
glCompileShader(v_shader); | |
// fragment shader | |
GLuint f_shader = glCreateShader(GL_FRAGMENT_SHADER); | |
glShaderSource(f_shader, 1, (const GLchar**)&FS, 0); | |
glCompileShader(f_shader); | |
// quad program | |
quad_program = glCreateProgram(); | |
glAttachShader(quad_program, v_shader); | |
glAttachShader(quad_program, f_shader); | |
glLinkProgram(quad_program); | |
// BUFFERS ----------------------------------------------------------- | |
// vertex buffer (quad) | |
glGenBuffers(1, &vertex_buffer); | |
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); | |
glBufferData(GL_ARRAY_BUFFER, sizeof(quad), quad, GL_STATIC_DRAW); | |
// element buffer (indices) | |
glGenBuffers(1, &element_buffer); | |
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, element_buffer); | |
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, | |
GL_STATIC_DRAW); | |
glGenVertexArrays(1, &vao); | |
} | |
void drawQuad (void) { | |
glBindVertexArray(vao); | |
// bind buffers | |
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); | |
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, element_buffer); | |
// bind shaders | |
glUseProgram(quad_program); | |
GLint pos = glGetAttribLocation(quad_program, "position"); | |
glVertexAttribPointer(pos, 2, GL_FLOAT, GL_FALSE, 0, (void*)0); | |
// setup | |
assert(glGetError() == GL_NO_ERROR); // no error here | |
glEnableVertexAttribArray(pos); | |
assert(glGetError() == GL_NO_ERROR); // ERROR: GL_INVALID_OPERATION | |
// draw (GL_INVALID_OPERATION here too) | |
glDrawElements(GL_TRIANGLE_FAN, 4, GL_UNSIGNED_BYTE, (void *)0); | |
// cleanup (GL_INVALID_OPERATION here too) | |
glDisableVertexAttribArray(pos); | |
} | |
int main (int argc, char const *argv[]) | |
{ | |
glfwInit(); | |
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3); | |
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2); | |
glfwOpenWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); | |
glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); | |
glfwOpenWindow(640, 480, 8, 8, 8, 8, 0, 0, GLFW_WINDOW); | |
initQuad(); | |
while (!glfwGetKey(GLFW_KEY_ESC) && glfwGetWindowParam(GLFW_OPENED)) { | |
glClear(GL_COLOR_BUFFER_BIT); | |
drawQuad(); | |
glfwSwapBuffers(); | |
} | |
glfwTerminate(); | |
exit(EXIT_SUCCESS); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment