3 /* to avoid dependency to OpenGL, I'll define all the relevant GL macros manually */
4 #define GL_UNSIGNED_BYTE 0x1401
5 #define GL_FLOAT 0x1406
7 #define GL_LUMINANCE 0x1909
11 #define GL_RGBA32F 0x8814
12 #define GL_RGB32F 0x8815
13 #define GL_LUMINANCE32F 0x8818
15 #define GL_TEXTURE_2D 0x0de1
16 #define GL_TEXTURE_WRAP_S 0x2802
17 #define GL_TEXTURE_WRAP_T 0x2803
18 #define GL_TEXTURE_MAG_FILTER 0x2800
19 #define GL_TEXTURE_MIN_FILTER 0x2801
20 #define GL_LINEAR 0x2601
21 #define GL_REPEAT 0x2901
24 typedef unsigned int GLenum;
25 typedef unsigned int GLuint;
30 /* for the same reason I'll load GL functions dynamically */
32 typedef void (*gl_gen_textures_func)(GLsizei, GLuint*);
33 typedef void (*gl_bind_texture_func)(GLenum, GLuint);
34 typedef void (*gl_tex_parameteri_func)(GLenum, GLenum, GLint);
35 typedef void (*gl_tex_image2d_func)(GLenum, GLint, GLint, GLsizei, GLsizei, GLint, GLenum, GLenum, const GLvoid*);
37 typedef void (__stdcall *gl_gen_textures_func)(GLsizei, GLuint*);
38 typedef void (__stdcall *gl_bind_texture_func)(GLenum, GLuint);
39 typedef void (__stdcall *gl_tex_parameteri_func)(GLenum, GLenum, GLint);
40 typedef void (__stdcall *gl_tex_image2d_func)(GLenum, GLint, GLint, GLsizei, GLsizei, GLint, GLenum, GLenum, const GLvoid*);
43 static gl_gen_textures_func gl_gen_textures;
44 static gl_bind_texture_func gl_bind_texture;
45 static gl_tex_parameteri_func gl_tex_parameteri;
46 static gl_tex_image2d_func gl_tex_image2d;
48 static int load_glfunc(void);
50 unsigned int img_fmt_glfmt(enum img_fmt fmt)
71 unsigned int img_fmt_gltype(enum img_fmt fmt)
77 return GL_UNSIGNED_BYTE;
90 unsigned int img_fmt_glintfmt(enum img_fmt fmt)
100 return GL_LUMINANCE32F;
111 unsigned int img_glfmt(struct img_pixmap *img)
113 return img_fmt_glfmt(img->fmt);
116 unsigned int img_gltype(struct img_pixmap *img)
118 return img_fmt_gltype(img->fmt);
121 unsigned int img_glintfmt(struct img_pixmap *img)
123 return img_fmt_glintfmt(img->fmt);
126 unsigned int img_gltexture(struct img_pixmap *img)
129 unsigned int intfmt, fmt, type;
131 if(!gl_gen_textures) {
132 if(load_glfunc() == -1) {
133 fprintf(stderr, "imago: failed to initialize the OpenGL helpers\n");
138 intfmt = img_glintfmt(img);
139 fmt = img_glfmt(img);
140 type = img_gltype(img);
142 gl_gen_textures(1, &tex);
143 gl_bind_texture(GL_TEXTURE_2D, tex);
144 gl_tex_parameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
145 gl_tex_parameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
146 gl_tex_parameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
147 gl_tex_parameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
148 gl_tex_image2d(GL_TEXTURE_2D, 0, intfmt, img->width, img->height, 0, fmt, type, img->pixels);
152 unsigned int img_gltexture_load(const char *fname)
154 struct img_pixmap img;
158 if(img_load(&img, fname) == -1) {
163 tex = img_gltexture(&img);
168 unsigned int img_gltexture_read_file(FILE *fp)
170 struct img_pixmap img;
174 if(img_read_file(&img, fp) == -1) {
179 tex = img_gltexture(&img);
184 unsigned int img_gltexture_read(struct img_io *io)
186 struct img_pixmap img;
190 if(img_read(&img, io) == -1) {
195 tex = img_gltexture(&img);
200 #if (defined(__DOS__) || defined(__MSDOS__)) && defined(__unix__)
204 #if defined(__unix__) || defined(__APPLE__)
215 static int load_glfunc(void)
217 #if defined(__unix__) || defined(__APPLE__)
218 gl_gen_textures = (gl_gen_textures_func)dlsym(RTLD_DEFAULT, "glGenTextures");
219 gl_bind_texture = (gl_bind_texture_func)dlsym(RTLD_DEFAULT, "glBindTexture");
220 gl_tex_parameteri = (gl_tex_parameteri_func)dlsym(RTLD_DEFAULT, "glTexParameteri");
221 gl_tex_image2d = (gl_tex_image2d_func)dlsym(RTLD_DEFAULT, "glTexImage2D");
225 HANDLE dll = LoadLibrary("opengl32.dll");
227 gl_gen_textures = (gl_gen_textures_func)GetProcAddress(dll, "glGenTextures");
228 gl_bind_texture = (gl_bind_texture_func)GetProcAddress(dll, "glBindTexture");
229 gl_tex_parameteri = (gl_tex_parameteri_func)GetProcAddress(dll, "glTexParameteri");
230 gl_tex_image2d = (gl_tex_image2d_func)GetProcAddress(dll, "glTexImage2D");
234 return (gl_gen_textures && gl_bind_texture && gl_tex_parameteri && gl_tex_image2d) ? 0 : -1;