visibility determination
[vrlugburz] / src / opengl.c
1 #include <stdio.h>
2 #include <string.h>
3 #include "opengl.h"
4 #include <GL/glx.h>
5
6 #define LOADPROC(type, func) \
7         do { \
8                 if(!(func = (type)glXGetProcAddress((unsigned char*)#func))) { \
9                         fputs("failed to load entry point: " #func, stderr); \
10                         return -1; \
11                 } \
12         } while(0)
13
14 int init_opengl(void)
15 {
16         const char *glext, *glver;
17
18         if(!(glver = (const char*)glGetString(GL_VERSION))) {
19                 fprintf(stderr, "failed to retrieve OpenGL version string\n");
20                 return -1;
21         }
22         glcaps.ver_major = 1;
23         glcaps.ver_minor = 0;
24         sscanf(glver, "%d.%d", &glcaps.ver_major, &glcaps.ver_minor);
25
26         if(!(glext = (const char*)glGetString(GL_EXTENSIONS))) {
27                 fprintf(stderr, "failed to retrieve OpenGL extensions string\n");
28                 return -1;
29         }
30
31         if(glcaps.ver_major >= 2 || glcaps.ver_minor >= 5 || (strstr(glext, "GL_ARB_vertex_buffer_object"))) {
32                 glcaps.vbo = 1;
33         }
34
35         if(glcaps.ver_major >= 2 || (strstr(glext, "GL_ARB_vertex_shader") && strstr(glext, "GL_ARB_fragment_shader"))) {
36                 glcaps.sdr = 1;
37         }
38
39         if(glcaps.vbo) {
40                 LOADPROC(PFNGLGENBUFFERSPROC, glGenBuffers);
41                 LOADPROC(PFNGLDELETEBUFFERSPROC, glDeleteBuffers);
42                 LOADPROC(PFNGLBINDBUFFERPROC, glBindBuffer);
43                 LOADPROC(PFNGLBUFFERDATAPROC, glBufferData);
44         }
45
46         if(glcaps.sdr) {
47                 LOADPROC(PFNGLCREATEPROGRAMPROC, glCreateProgram);
48                 LOADPROC(PFNGLDELETEPROGRAMPROC, glDeleteProgram);
49                 LOADPROC(PFNGLATTACHSHADERPROC, glAttachShader);
50                 LOADPROC(PFNGLLINKPROGRAMPROC, glLinkProgram);
51                 LOADPROC(PFNGLGETPROGRAMIVPROC, glGetProgramiv);
52                 LOADPROC(PFNGLGETPROGRAMINFOLOGPROC, glGetProgramInfoLog);
53                 LOADPROC(PFNGLUSEPROGRAMPROC, glUseProgram);
54                 LOADPROC(PFNGLGETUNIFORMLOCATIONPROC, glGetUniformLocation);
55                 LOADPROC(PFNGLUNIFORM1IPROC, glUniform1i);
56                 LOADPROC(PFNGLUNIFORM1FPROC, glUniform1f);
57                 LOADPROC(PFNGLUNIFORM2FPROC, glUniform2f);
58                 LOADPROC(PFNGLUNIFORM3FPROC, glUniform3f);
59                 LOADPROC(PFNGLUNIFORM4FPROC, glUniform4f);
60                 LOADPROC(PFNGLUNIFORMMATRIX4FVPROC, glUniformMatrix4fv);
61                 LOADPROC(PFNGLGETATTRIBLOCATIONPROC, glGetAttribLocation);
62                 LOADPROC(PFNGLVERTEXATTRIB3FPROC, glVertexAttrib3f);
63                 LOADPROC(PFNGLCREATESHADERPROC, glCreateShader);
64                 LOADPROC(PFNGLDELETESHADERPROC, glDeleteShader);
65                 LOADPROC(PFNGLSHADERSOURCEPROC, glShaderSource);
66                 LOADPROC(PFNGLCOMPILESHADERPROC, glCompileShader);
67                 LOADPROC(PFNGLGETSHADERIVPROC, glGetShaderiv);
68                 LOADPROC(PFNGLGETSHADERINFOLOGPROC, glGetShaderInfoLog);
69                 LOADPROC(PFNGLBINDATTRIBLOCATIONPROC, glBindAttribLocation);
70                 LOADPROC(PFNGLENABLEVERTEXATTRIBARRAYPROC, glEnableVertexAttribArray);
71                 LOADPROC(PFNGLDISABLEVERTEXATTRIBARRAYPROC, glDisableVertexAttribArray);
72                 LOADPROC(PFNGLVERTEXATTRIBPOINTERPROC, glVertexAttribPointer);
73         }
74
75         return 0;
76 }