cell grid and UI split
[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 || (strstr(glext, "GL_ARB_vertex_shader") && strstr(glext, "GL_ARB_fragment_shader"))) {
32                 glcaps.sdr = 1;
33         }
34
35 #ifndef LOADEXT_SDR
36         if(glcaps.sdr) {
37                 LOADPROC(PFNGLCREATEPROGRAMPROC, glCreateProgram);
38                 LOADPROC(PFNGLDELETEPROGRAMPROC, glDeleteProgram);
39                 LOADPROC(PFNGLATTACHSHADERPROC, glAttachShader);
40                 LOADPROC(PFNGLLINKPROGRAMPROC, glLinkProgram);
41                 LOADPROC(PFNGLGETPROGRAMIVPROC, glGetProgramiv);
42                 LOADPROC(PFNGLGETPROGRAMINFOLOGPROC, glGetProgramInfoLog);
43                 LOADPROC(PFNGLUSEPROGRAMPROC, glUseProgram);
44                 LOADPROC(PFNGLGETUNIFORMLOCATIONPROC, glGetUniformLocation);
45                 LOADPROC(PFNGLUNIFORM1IPROC, glUniform1i);
46                 LOADPROC(PFNGLUNIFORM1FPROC, glUniform1f);
47                 LOADPROC(PFNGLUNIFORM2FPROC, glUniform2f);
48                 LOADPROC(PFNGLUNIFORM3FPROC, glUniform3f);
49                 LOADPROC(PFNGLUNIFORM4FPROC, glUniform4f);
50                 LOADPROC(PFNGLUNIFORMMATRIX4FVPROC, glUniformMatrix4fv);
51                 LOADPROC(PFNGLGETATTRIBLOCATIONPROC, glGetAttribLocation);
52                 LOADPROC(PFNGLVERTEXATTRIB3FPROC, glVertexAttrib3f);
53                 LOADPROC(PFNGLCREATESHADERPROC, glCreateShader);
54                 LOADPROC(PFNGLDELETESHADERPROC, glDeleteShader);
55                 LOADPROC(PFNGLSHADERSOURCEPROC, glShaderSource);
56                 LOADPROC(PFNGLCOMPILESHADERPROC, glCompileShader);
57                 LOADPROC(PFNGLGETSHADERIVPROC, glGetShaderiv);
58                 LOADPROC(PFNGLGETSHADERINFOLOGPROC, glGetShaderInfoLog);
59         }
60 #endif
61
62         return 0;
63 }