Skip to content

Instantly share code, notes, and snippets.

@hashmal
Created October 19, 2011 18:43
Show Gist options
  • Save hashmal/1299245 to your computer and use it in GitHub Desktop.
Save hashmal/1299245 to your computer and use it in GitHub Desktop.
OpenGL 3.2 Core Profile, GLFW, Mac OS X Lion
// 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 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);
}
void drawQuad (void) {
// 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);
}
@hashmal
Copy link
Author

hashmal commented Oct 19, 2011

I fail to understand. It works perfectly if I comment lines (78 .. 81) and adapt the shaders for version 120. However, with OpenGL 3.2 Core Profile, nothing is displayed (there should be a red quad on the screen).

Does anyone have a clue?

@hashmal
Copy link
Author

hashmal commented Oct 20, 2011

Well, I finally found my mistake: use a vertex array object!

GLuint vao; // declare this global variable
// ...
void initQuad (void)
{
        // ...
        glGenVertexArrays(1, &vao);
}

void drawQuad (void)
{
        glBindVertexArray(vao);
        // ...
}

@nuraahamdan
Copy link

Can you please share the working code?

@hashmal
Copy link
Author

hashmal commented Nov 28, 2011

The following should work (although I didn't run it):

https://gist.github.com/1401335

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment