4 /* to avoid dependency to OpenGL, I'll define all the relevant GL macros manually */
5 #define GL_UNSIGNED_BYTE 0x1401
6 #define GL_FLOAT 0x1406
8 #define GL_LUMINANCE 0x1909
10 #define GL_RGBA 0x1908
12 #define GL_RGBA32F 0x8814
13 #define GL_RGB32F 0x8815
14 #define GL_LUMINANCE32F 0x8818
16 #define GL_TEXTURE_2D 0x0de1
17 #define GL_TEXTURE_WRAP_S 0x2802
18 #define GL_TEXTURE_WRAP_T 0x2803
19 #define GL_TEXTURE_MAG_FILTER 0x2800
20 #define GL_TEXTURE_MIN_FILTER 0x2801
21 #define GL_LINEAR 0x2601
22 #define GL_REPEAT 0x2901
25 typedef unsigned int GLenum;
26 typedef unsigned int GLuint;
31 /* 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 static gl_gen_textures_func gl_gen_textures;
38 static gl_bind_texture_func gl_bind_texture;
39 static gl_tex_parameteri_func gl_tex_parameteri;
40 static gl_tex_image2d_func gl_tex_image2d;
42 static int load_glfunc(void);
44 unsigned int img_fmt_glfmt(enum img_fmt fmt)
65 unsigned int img_fmt_gltype(enum img_fmt fmt)
71 return GL_UNSIGNED_BYTE;
84 unsigned int img_fmt_glintfmt(enum img_fmt fmt)
94 return GL_LUMINANCE32F;
105 unsigned int img_glfmt(struct img_pixmap *img)
107 return img_fmt_glfmt(img->fmt);
110 unsigned int img_gltype(struct img_pixmap *img)
112 return img_fmt_gltype(img->fmt);
115 unsigned int img_glintfmt(struct img_pixmap *img)
117 return img_fmt_glintfmt(img->fmt);
120 unsigned int img_gltexture(struct img_pixmap *img)
123 unsigned int intfmt, fmt, type;
125 if(!gl_gen_textures) {
126 if(load_glfunc() == -1) {
127 fprintf(stderr, "imago: failed to initialize the OpenGL helpers\n");
132 intfmt = img_glintfmt(img);
133 fmt = img_glfmt(img);
134 type = img_gltype(img);
136 gl_gen_textures(1, &tex);
137 gl_bind_texture(GL_TEXTURE_2D, tex);
138 gl_tex_parameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
139 gl_tex_parameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
140 gl_tex_parameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
141 gl_tex_parameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
142 gl_tex_image2d(GL_TEXTURE_2D, 0, intfmt, img->width, img->height, 0, fmt, type, img->pixels);
146 unsigned int img_gltexture_load(const char *fname)
148 struct img_pixmap img;
152 if(img_load(&img, fname) == -1) {
157 tex = img_gltexture(&img);
162 unsigned int img_gltexture_read_file(FILE *fp)
164 struct img_pixmap img;
168 if(img_read_file(&img, fp) == -1) {
173 tex = img_gltexture(&img);
178 unsigned int img_gltexture_read(struct img_io *io)
180 struct img_pixmap img;
184 if(img_read(&img, io) == -1) {
189 tex = img_gltexture(&img);
194 #if defined(__unix__) || defined(__APPLE__)
205 static int load_glfunc(void)
207 #if defined(__unix__) || defined(__APPLE__)
208 gl_gen_textures = (gl_gen_textures_func)dlsym(RTLD_DEFAULT, "glGenTextures");
209 gl_bind_texture = (gl_bind_texture_func)dlsym(RTLD_DEFAULT, "glBindTexture");
210 gl_tex_parameteri = (gl_tex_parameteri_func)dlsym(RTLD_DEFAULT, "glTexParameteri");
211 gl_tex_image2d = (gl_tex_image2d_func)dlsym(RTLD_DEFAULT, "glTexImage2D");
215 HMODULE handle = GetModuleHandle(0);
216 gl_gen_textures = (gl_gen_textures_func)GetProcAddress(handle, "glGenTextures");
217 gl_bind_texture = (gl_bind_texture_func)GetProcAddress(handle, "glBindTexture");
218 gl_tex_parameteri = (gl_tex_parameteri_func)GetProcAddress(handle, "glTexParameteri");
219 gl_tex_image2d = (gl_tex_image2d_func)GetProcAddress(handle, "glTexImage2D");
222 return (gl_gen_textures && gl_bind_texture && gl_tex_parameteri && gl_tex_image2d) ? 0 : -1;