typedef void (*GLLINKPROGRAMPROC) (GLuint program);
typedef GLint (*GLGETATTRIBLOCATIONPROC) (GLuint program, const GLchar *name);
typedef void (*GLUSEPROGRAMPROC) (GLuint program);
+
typedef void (*GLVERTEXATTRIBPOINTERPROC) (GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid *pointer);
typedef void (*GLENABLEVERTEXATTRIBARRAYPROC) (GLuint index);
typedef void (*GLGENBUFFERSPROC) (GLsizei n, GLuint *buffers);
typedef void (*GLBUFFERDATAPROC) (GLenum target, GLsizeiptr size, const GLvoid *data, GLenum usage);
typedef void (*GLDELETEBUFFERSPROC) (GLsizei n, const GLuint *buffers);
+#ifdef CON_GL3
+// VAOs are only supported in GL3+, and not needed in earler contexts
+typedef void (*GLBINDVERTEXARRAYPROC) (GLuint array);
+typedef void (*GLDELETEVERTEXARRAYSPROC) (GLsizei n, const GLuint *arrays);
+typedef void (*GLGENVERTEXARRAYSPROC) (GLsizei n, GLuint *arrays);
+#endif
+
GLGETSHADERIVPROC p_glGetShaderiv = NULL;
GLGETSHADERINFOLOGPROC p_glGetShaderInfoLog = NULL;
GLCREATESHADERPROC p_glCreateShader = NULL;
GLLINKPROGRAMPROC p_glLinkProgram = NULL;
GLGETATTRIBLOCATIONPROC p_glGetAttribLocation = NULL;
GLUSEPROGRAMPROC p_glUseProgram = NULL;
+
GLVERTEXATTRIBPOINTERPROC p_glVertexAttribPointer = NULL;
GLENABLEVERTEXATTRIBARRAYPROC p_glEnableVertexAttribArray = NULL;
GLGENBUFFERSPROC p_glGenBuffers = NULL;
GLBUFFERDATAPROC p_glBufferData = NULL;
GLDELETEBUFFERSPROC p_glDeleteBuffers = NULL;
+#ifdef CON_GL3
+GLBINDVERTEXARRAYPROC p_glBindVertexArray = NULL;
+GLDELETEVERTEXARRAYSPROC p_glDeleteVertexArrays = NULL;
+GLGENVERTEXARRAYSPROC p_glGenVertexArrays = NULL;
+#endif
+
// resolve function pointers
static T_proc resolveFunctionPointer(T_glGetProcAddress p_glGetProcAddress, const char *name)
{
p_glAttachShader = (GLATTACHSHADERPROC)resolveFunctionPointer(p_glGetProcAddress, "glAttachShader");
p_glLinkProgram = (GLLINKPROGRAMPROC)resolveFunctionPointer(p_glGetProcAddress, "glLinkProgram");
p_glGetAttribLocation = (GLGETATTRIBLOCATIONPROC)resolveFunctionPointer(p_glGetProcAddress, "glGetAttribLocation");
+
+#ifdef CON_GL3
+ p_glBindVertexArray = (GLBINDVERTEXARRAYPROC)resolveFunctionPointer(p_glGetProcAddress, "glBindVertexArray");
+ p_glDeleteVertexArrays = (GLDELETEVERTEXARRAYSPROC)resolveFunctionPointer(p_glGetProcAddress, "glDeleteVertexArrays");
+ p_glGenVertexArrays = (GLGENVERTEXARRAYSPROC)resolveFunctionPointer(p_glGetProcAddress, "glGenVertexArrays");
+#endif
+
p_glUseProgram = (GLUSEPROGRAMPROC)resolveFunctionPointer(p_glGetProcAddress, "glUseProgram");
p_glVertexAttribPointer = (GLVERTEXATTRIBPOINTERPROC)resolveFunctionPointer(p_glGetProcAddress, "glVertexAttribPointer");
p_glEnableVertexAttribArray = (GLENABLEVERTEXATTRIBARRAYPROC)resolveFunctionPointer(p_glGetProcAddress, "glEnableVertexAttribArray");
p_glDeleteBuffers = (GLDELETEBUFFERSPROC)resolveFunctionPointer(p_glGetProcAddress, "glDeleteBuffers");
}
+static const char *glErrorToString(GLenum e)
+{
+#define CASE(name) case name: return #name
+ switch (e) {
+ CASE(GL_NO_ERROR);
+ CASE(GL_INVALID_ENUM);
+ CASE(GL_INVALID_VALUE);
+ CASE(GL_INVALID_OPERATION);
+#ifndef CON_GLES2
+ CASE(GL_STACK_OVERFLOW);
+ CASE(GL_STACK_UNDERFLOW);
+#endif
+ CASE(GL_OUT_OF_MEMORY);
+ default: return "<unknown>";
+ }
+#undef CASE
+}
+
+static void checkGlError(const char *what)
+{
+ GLenum e = glGetError();
+ if (e == GL_NO_ERROR) return;
+ fprintf(stderr, "GL error %d (%s): %s\n", e, glErrorToString(e), what);
+ exit(1);
+}
+
// shaders
static const char *vertex_shader_source =
"#version 100 \n\
show_info_log(shader);
exit(1);
}
+ checkGlError("Compiling shader");
return shader;
}
p_glGenBuffers(1, &buffer);
p_glBindBuffer(GL_ARRAY_BUFFER, buffer);
p_glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);
+ checkGlError("Creating array buffer");
return buffer;
}
void drawQuad(GLfloat red, GLfloat green, GLfloat blue, GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2)
{
p_glUseProgram(program);
+ checkGlError("Enabling the shaders");
+
+#ifdef CON_GL3
+ // create a vertex array obect and use it
+ GLuint vao;
+ p_glGenVertexArrays(1, &vao);
+ p_glBindVertexArray(vao);
+ checkGlError("Creating a VAO");
+#endif
// send vertex data to card with given attribute
GLfloat vertex_buffer_data[] = {
0, /* stride */
(void*)0 /* array buffer offset */
);
+ checkGlError("Preparing vertex data");
p_glEnableVertexAttribArray(attributes.position);
+ checkGlError("Sending vertex data");
// same with color data
GLfloat color_buffer_data[] = {
0, /* stride */
(void*)0 /* array buffer offset */
);
+ checkGlError("Preparing color data");
p_glEnableVertexAttribArray(attributes.color);
+ checkGlError("Sending color data");
// draw
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
+ checkGlError("Drawing");
// cleanup
p_glDeleteBuffers(1, &vertex_buffer);
p_glDeleteBuffers(1, &color_buffer);
+#ifdef CON_GL3
+ p_glDeleteVertexArrays(1, &vao);
+#endif
+ checkGlError("Doing cleanup");
}