8 static unsigned int gl_ifmt[2][NUM_IMG_FMT] = {
10 {GL_LUMINANCE, GL_RGB, GL_RGBA, GL_LUMINANCE16F_ARB, GL_RGB16F, GL_RGBA16F, GL_RGBA, GL_RGB},
12 {GL_SLUMINANCE, GL_SRGB, GL_SRGB_ALPHA, GL_LUMINANCE16F_ARB, GL_RGB16F, GL_RGBA16F, GL_SRGB_ALPHA, GL_SRGB}
14 static unsigned int gl_fmt[] = {
15 GL_LUMINANCE, GL_RGB, GL_RGBA, GL_LUMINANCE, GL_RGB, GL_RGBA, GL_BGRA, GL_RGB
17 static unsigned int gl_type[] = {
18 GL_UNSIGNED_BYTE, GL_UNSIGNED_BYTE, GL_UNSIGNED_BYTE, GL_FLOAT, GL_FLOAT, GL_FLOAT, GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT_5_6_5
22 struct texture *load_texture(const char *fname)
24 struct img_pixmap img;
29 if(img_load(&img, fname) == -1) {
30 fprintf(stderr, "failed to load texture: %s\n", fname);
33 ifmt = gl_ifmt[opt.srgb][img.fmt];
35 if(!(tex = malloc(sizeof *tex))) {
37 fprintf(stderr, "failed to allocate texture\n");
41 glGenTextures(1, &tex->id);
42 glBindTexture(GL_TEXTURE_2D, tex->id);
43 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
44 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
45 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, 1);
46 glTexImage2D(GL_TEXTURE_2D, 0, ifmt, img.width, img.height, 0, gl_fmt[img.fmt],
47 gl_type[img.fmt], img.pixels);
49 tex->width = img.width;
50 tex->height = img.height;
51 tex->pixels = img.pixels;
55 void free_texture(struct texture *tex)
60 glDeleteTextures(1, &tex->id);