1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
#include "../include/mgl/gl.h"
#include <dlfcn.h>
/*#include <GL/gl.h>*/
#include <stdio.h>
typedef struct {
void **func;
const char *name;
} dlsym_assign;
static void* dlsym_print_fail(void *handle, const char *name) {
dlerror();
void *sym = dlsym(handle, name);
char *err_str = dlerror();
if(!sym)
fprintf(stderr, "dlsym(handle, \"%s\") failed, error: %s\n", name, err_str ? err_str : "(null)");
return sym;
}
int mgl_gl_load(mgl_gl *self) {
const char *glx_path = "/usr/lib/libGL.so.1";
self->handle = dlopen(glx_path, RTLD_LAZY);
if(!self->handle) {
fprintf(stderr, "dlopen(\"%s\", RTLD_LAZY) failed\n", glx_path);
return -1;
}
const dlsym_assign required_dlsym[] = {
{ &self->glXChooseVisual, "glXChooseVisual" },
{ &self->glXCreateContext, "glXCreateContext" },
{ &self->glXDestroyContext, "glXDestroyContext" },
{ &self->glXMakeCurrent, "glXMakeCurrent" },
{ &self->glXSwapBuffers, "glXSwapBuffers" },
{ &self->glViewport, "glViewport" },
{ &self->glClearColor, "glClearColor" },
{ &self->glClear, "glClear" },
{ &self->glEnable, "glEnable" },
{ &self->glBlendFunc, "glBlendFunc" },
{ &self->glGenTextures, "glGenTextures" },
{ &self->glDeleteTextures, "glDeleteTextures" },
{ &self->glTexImage2D, "glTexImage2D" },
{ &self->glCompressedTexImage2D, "glCompressedTexImage2D" },
{ &self->glBindTexture, "glBindTexture" },
{ &self->glTexParameteri, "glTexParameteri" },
{ &self->glHint, "glHint" },
{ &self->glBegin, "glBegin" },
{ &self->glEnd, "glEnd" },
{ &self->glVertex3f, "glVertex3f" },
{ &self->glColor4ub, "glColor4ub" },
{ &self->glTexCoord2f, "glTexCoord2f" },
{ &self->glOrtho, "glOrtho" },
{ &self->glMatrixMode, "glMatrixMode" },
{ &self->glPushMatrix, "glPushMatrix" },
{ &self->glPopMatrix, "glPopMatrix" },
{ &self->glLoadIdentity, "glLoadIdentity" },
{ &self->glTranslatef, "glTranslatef" },
{ &self->glGenBuffers, "glGenBuffers" },
{ &self->glBindBuffer, "glBindBuffer" },
{ &self->glDeleteBuffers, "glDeleteBuffers" },
{ &self->glBufferData, "glBufferData" },
{ &self->glBufferSubData, "glBufferSubData" },
{ &self->glDrawArrays, "glDrawArrays" },
{ &self->glEnableClientState, "glEnableClientState" },
{ &self->glVertexPointer, "glVertexPointer" },
{ &self->glColorPointer, "glColorPointer" },
{ &self->glTexCoordPointer, "glTexCoordPointer" },
{ &self->glCompileShader, "glCompileShader" },
{ &self->glCreateProgram, "glCreateProgram" },
{ &self->glCreateShader, "glCreateShader" },
{ &self->glDeleteProgram, "glDeleteProgram" },
{ &self->glDeleteShader, "glDeleteShader" },
{ &self->glGetShaderiv, "glGetShaderiv" },
{ &self->glGetShaderInfoLog, "glGetShaderInfoLog" },
{ &self->glGetProgramiv, "glGetProgramiv" },
{ &self->glGetProgramInfoLog, "glGetProgramInfoLog" },
{ &self->glLinkProgram, "glLinkProgram" },
{ &self->glShaderSource, "glShaderSource" },
{ &self->glUseProgram, "glUseProgram" },
{ &self->glAttachShader, "glAttachShader" },
{ &self->glGetUniformLocation, "glGetUniformLocation" },
{ &self->glUniform2fv, "glUniform2fv" },
{ &self->glGetError, "glGetError" },
{ &self->glGetString, "glGetString" },
{ NULL, NULL }
};
for(int i = 0; required_dlsym[i].func; ++i) {
*required_dlsym[i].func = dlsym_print_fail(self->handle, required_dlsym[i].name);
if(!required_dlsym[i].func) {
mgl_gl_unload(self);
return -1;
}
}
const dlsym_assign optional_dlsym[] = {
{ &self->glXSwapIntervalEXT, "glXSwapIntervalEXT" },
{ &self->glXSwapIntervalMESA, "glXGetSwapIntervalMESA" },
{ &self->glXSwapIntervalSGI, "glXSwapIntervalSGI" },
{ NULL, NULL }
};
for(int i = 0; optional_dlsym[i].func; ++i) {
*optional_dlsym[i].func = dlsym_print_fail(self->handle, optional_dlsym[i].name);
}
return 0;
}
void mgl_gl_unload(mgl_gl *self) {
if(self->handle) {
dlclose(self->handle);
self->handle = NULL;
}
}
|