vrshoot

annotate libs/imago/imago_gl.c @ 0:b2f14e535253

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 01 Feb 2014 19:58:19 +0200
parents
children
rev   line source
nuclear@0 1 #include "imago2.h"
nuclear@0 2
nuclear@0 3
nuclear@0 4 /* to avoid dependency to OpenGL, I'll define all the relevant GL macros manually */
nuclear@0 5 #define GL_UNSIGNED_BYTE 0x1401
nuclear@0 6 #define GL_FLOAT 0x1406
nuclear@0 7
nuclear@0 8 #define GL_LUMINANCE 0x1909
nuclear@0 9 #define GL_RGB 0x1907
nuclear@0 10 #define GL_RGBA 0x1908
nuclear@0 11
nuclear@0 12 #define GL_RGBA32F 0x8814
nuclear@0 13 #define GL_RGB32F 0x8815
nuclear@0 14 #define GL_LUMINANCE32F 0x8818
nuclear@0 15
nuclear@0 16 #define GL_TEXTURE_2D 0x0de1
nuclear@0 17 #define GL_TEXTURE_WRAP_S 0x2802
nuclear@0 18 #define GL_TEXTURE_WRAP_T 0x2803
nuclear@0 19 #define GL_TEXTURE_MAG_FILTER 0x2800
nuclear@0 20 #define GL_TEXTURE_MIN_FILTER 0x2801
nuclear@0 21 #define GL_LINEAR 0x2601
nuclear@0 22 #define GL_REPEAT 0x2901
nuclear@0 23
nuclear@0 24
nuclear@0 25 typedef unsigned int GLenum;
nuclear@0 26 typedef unsigned int GLuint;
nuclear@0 27 typedef int GLint;
nuclear@0 28 typedef int GLsizei;
nuclear@0 29 typedef void GLvoid;
nuclear@0 30
nuclear@0 31 /* for the same reason I'll load GL functions dynamically */
nuclear@0 32 typedef void (*gl_gen_textures_func)(GLsizei, GLuint*);
nuclear@0 33 typedef void (*gl_bind_texture_func)(GLenum, GLuint);
nuclear@0 34 typedef void (*gl_tex_parameteri_func)(GLenum, GLenum, GLint);
nuclear@0 35 typedef void (*gl_tex_image2d_func)(GLenum, GLint, GLint, GLsizei, GLsizei, GLint, GLenum, GLenum, const GLvoid*);
nuclear@0 36
nuclear@0 37 static gl_gen_textures_func gl_gen_textures;
nuclear@0 38 static gl_bind_texture_func gl_bind_texture;
nuclear@0 39 static gl_tex_parameteri_func gl_tex_parameteri;
nuclear@0 40 static gl_tex_image2d_func gl_tex_image2d;
nuclear@0 41
nuclear@0 42 static int load_glfunc(void);
nuclear@0 43
nuclear@0 44 unsigned int img_fmt_glfmt(enum img_fmt fmt)
nuclear@0 45 {
nuclear@0 46 switch(fmt) {
nuclear@0 47 case IMG_FMT_GREY8:
nuclear@0 48 case IMG_FMT_GREYF:
nuclear@0 49 return GL_LUMINANCE;
nuclear@0 50
nuclear@0 51 case IMG_FMT_RGB24:
nuclear@0 52 case IMG_FMT_RGBF:
nuclear@0 53 return GL_RGB;
nuclear@0 54
nuclear@0 55 case IMG_FMT_RGBA32:
nuclear@0 56 case IMG_FMT_RGBAF:
nuclear@0 57 return GL_RGBA;
nuclear@0 58
nuclear@0 59 default:
nuclear@0 60 break;
nuclear@0 61 }
nuclear@0 62 return 0;
nuclear@0 63 }
nuclear@0 64
nuclear@0 65 unsigned int img_fmt_gltype(enum img_fmt fmt)
nuclear@0 66 {
nuclear@0 67 switch(fmt) {
nuclear@0 68 case IMG_FMT_GREY8:
nuclear@0 69 case IMG_FMT_RGB24:
nuclear@0 70 case IMG_FMT_RGBA32:
nuclear@0 71 return GL_UNSIGNED_BYTE;
nuclear@0 72
nuclear@0 73 case IMG_FMT_GREYF:
nuclear@0 74 case IMG_FMT_RGBF:
nuclear@0 75 case IMG_FMT_RGBAF:
nuclear@0 76 return GL_FLOAT;
nuclear@0 77
nuclear@0 78 default:
nuclear@0 79 break;
nuclear@0 80 }
nuclear@0 81 return 0;
nuclear@0 82 }
nuclear@0 83
nuclear@0 84 unsigned int img_fmt_glintfmt(enum img_fmt fmt)
nuclear@0 85 {
nuclear@0 86 switch(fmt) {
nuclear@0 87 case IMG_FMT_GREY8:
nuclear@0 88 return GL_LUMINANCE;
nuclear@0 89 case IMG_FMT_RGB24:
nuclear@0 90 return GL_RGB;
nuclear@0 91 case IMG_FMT_RGBA32:
nuclear@0 92 return GL_RGBA;
nuclear@0 93 case IMG_FMT_GREYF:
nuclear@0 94 return GL_LUMINANCE32F;
nuclear@0 95 case IMG_FMT_RGBF:
nuclear@0 96 return GL_RGB32F;
nuclear@0 97 case IMG_FMT_RGBAF:
nuclear@0 98 return GL_RGBA32F;
nuclear@0 99 default:
nuclear@0 100 break;
nuclear@0 101 }
nuclear@0 102 return 0;
nuclear@0 103 }
nuclear@0 104
nuclear@0 105 unsigned int img_glfmt(struct img_pixmap *img)
nuclear@0 106 {
nuclear@0 107 return img_fmt_glfmt(img->fmt);
nuclear@0 108 }
nuclear@0 109
nuclear@0 110 unsigned int img_gltype(struct img_pixmap *img)
nuclear@0 111 {
nuclear@0 112 return img_fmt_gltype(img->fmt);
nuclear@0 113 }
nuclear@0 114
nuclear@0 115 unsigned int img_glintfmt(struct img_pixmap *img)
nuclear@0 116 {
nuclear@0 117 return img_fmt_glintfmt(img->fmt);
nuclear@0 118 }
nuclear@0 119
nuclear@0 120 unsigned int img_gltexture(struct img_pixmap *img)
nuclear@0 121 {
nuclear@0 122 unsigned int tex;
nuclear@0 123 unsigned int intfmt, fmt, type;
nuclear@0 124
nuclear@0 125 if(!gl_gen_textures) {
nuclear@0 126 if(load_glfunc() == -1) {
nuclear@0 127 fprintf(stderr, "imago: failed to initialize the OpenGL helpers\n");
nuclear@0 128 return 0;
nuclear@0 129 }
nuclear@0 130 }
nuclear@0 131
nuclear@0 132 intfmt = img_glintfmt(img);
nuclear@0 133 fmt = img_glfmt(img);
nuclear@0 134 type = img_gltype(img);
nuclear@0 135
nuclear@0 136 gl_gen_textures(1, &tex);
nuclear@0 137 gl_bind_texture(GL_TEXTURE_2D, tex);
nuclear@0 138 gl_tex_parameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
nuclear@0 139 gl_tex_parameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
nuclear@0 140 gl_tex_parameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
nuclear@0 141 gl_tex_parameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
nuclear@0 142 gl_tex_image2d(GL_TEXTURE_2D, 0, intfmt, img->width, img->height, 0, fmt, type, img->pixels);
nuclear@0 143 return tex;
nuclear@0 144 }
nuclear@0 145
nuclear@0 146 unsigned int img_gltexture_load(const char *fname)
nuclear@0 147 {
nuclear@0 148 struct img_pixmap img;
nuclear@0 149 unsigned int tex;
nuclear@0 150
nuclear@0 151 img_init(&img);
nuclear@0 152 if(img_load(&img, fname) == -1) {
nuclear@0 153 img_destroy(&img);
nuclear@0 154 return 0;
nuclear@0 155 }
nuclear@0 156
nuclear@0 157 tex = img_gltexture(&img);
nuclear@0 158 img_destroy(&img);
nuclear@0 159 return tex;
nuclear@0 160 }
nuclear@0 161
nuclear@0 162 unsigned int img_gltexture_read_file(FILE *fp)
nuclear@0 163 {
nuclear@0 164 struct img_pixmap img;
nuclear@0 165 unsigned int tex;
nuclear@0 166
nuclear@0 167 img_init(&img);
nuclear@0 168 if(img_read_file(&img, fp) == -1) {
nuclear@0 169 img_destroy(&img);
nuclear@0 170 return 0;
nuclear@0 171 }
nuclear@0 172
nuclear@0 173 tex = img_gltexture(&img);
nuclear@0 174 img_destroy(&img);
nuclear@0 175 return tex;
nuclear@0 176 }
nuclear@0 177
nuclear@0 178 unsigned int img_gltexture_read(struct img_io *io)
nuclear@0 179 {
nuclear@0 180 struct img_pixmap img;
nuclear@0 181 unsigned int tex;
nuclear@0 182
nuclear@0 183 img_init(&img);
nuclear@0 184 if(img_read(&img, io) == -1) {
nuclear@0 185 img_destroy(&img);
nuclear@0 186 return 0;
nuclear@0 187 }
nuclear@0 188
nuclear@0 189 tex = img_gltexture(&img);
nuclear@0 190 img_destroy(&img);
nuclear@0 191 return tex;
nuclear@0 192 }
nuclear@0 193
nuclear@0 194 #if defined(__unix__) || defined(__APPLE__)
nuclear@0 195 #ifndef __USE_GNU
nuclear@0 196 #define __USE_GNU
nuclear@0 197 #endif
nuclear@0 198
nuclear@0 199 #include <dlfcn.h>
nuclear@0 200 #endif
nuclear@0 201 #ifdef WIN32
nuclear@0 202 #include <windows.h>
nuclear@0 203 #endif
nuclear@0 204
nuclear@0 205 static int load_glfunc(void)
nuclear@0 206 {
nuclear@0 207 #if defined(__unix__) || defined(__APPLE__)
nuclear@0 208 gl_gen_textures = (gl_gen_textures_func)dlsym(RTLD_DEFAULT, "glGenTextures");
nuclear@0 209 gl_bind_texture = (gl_bind_texture_func)dlsym(RTLD_DEFAULT, "glBindTexture");
nuclear@0 210 gl_tex_parameteri = (gl_tex_parameteri_func)dlsym(RTLD_DEFAULT, "glTexParameteri");
nuclear@0 211 gl_tex_image2d = (gl_tex_image2d_func)dlsym(RTLD_DEFAULT, "glTexImage2D");
nuclear@0 212 #endif
nuclear@0 213
nuclear@0 214 #ifdef WIN32
nuclear@0 215 HMODULE handle = GetModuleHandle(0);
nuclear@0 216 gl_gen_textures = (gl_gen_textures_func)GetProcAddress(handle, "glGenTextures");
nuclear@0 217 gl_bind_texture = (gl_bind_texture_func)GetProcAddress(handle, "glBindTexture");
nuclear@0 218 gl_tex_parameteri = (gl_tex_parameteri_func)GetProcAddress(handle, "glTexParameteri");
nuclear@0 219 gl_tex_image2d = (gl_tex_image2d_func)GetProcAddress(handle, "glTexImage2D");
nuclear@0 220 #endif
nuclear@0 221
nuclear@0 222 return (gl_gen_textures && gl_bind_texture && gl_tex_parameteri && gl_tex_image2d) ? 0 : -1;
nuclear@0 223 }