X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=demo_prior;a=blobdiff_plain;f=src%2Ftexture.c;fp=src%2Ftexture.c;h=c0a80b2cb80fb3c833a35f3327430a6178890fbd;hp=0000000000000000000000000000000000000000;hb=3a9f6854df479d81442273c9d0b133c49c5c8f66;hpb=0b24071f728b7c8550daa1b7aa7c4012cb70ef4c diff --git a/src/texture.c b/src/texture.c new file mode 100644 index 0000000..c0a80b2 --- /dev/null +++ b/src/texture.c @@ -0,0 +1,64 @@ +#include +#include +#include "opengl.h" +#include "texture.h" +#include "imago2.h" +#include "opt.h" + +static unsigned int gl_ifmt[2][NUM_IMG_FMT] = { + /* opt.srgb == 0 */ + {GL_LUMINANCE, GL_RGB, GL_RGBA, GL_LUMINANCE16F_ARB, GL_RGB16F, GL_RGBA16F, GL_RGBA, GL_RGB}, + /* opt.srgb == 1 */ + {GL_SLUMINANCE, GL_SRGB, GL_SRGB_ALPHA, GL_LUMINANCE16F_ARB, GL_RGB16F, GL_RGBA16F, GL_SRGB_ALPHA, GL_SRGB} +}; +static unsigned int gl_fmt[] = { + GL_LUMINANCE, GL_RGB, GL_RGBA, GL_LUMINANCE, GL_RGB, GL_RGBA, GL_BGRA, GL_RGB +}; +static unsigned int gl_type[] = { + GL_UNSIGNED_BYTE, GL_UNSIGNED_BYTE, GL_UNSIGNED_BYTE, GL_FLOAT, GL_FLOAT, GL_FLOAT, GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT_5_6_5 +}; + +struct texture *load_texture(const char *fname) +{ + struct img_pixmap img; + struct texture *tex; + int ifmt; + + img_init(&img); + if(img_load(&img, fname) == -1) { + fprintf(stderr, "failed to load texture: %s\n", fname); + return 0; + } + ifmt = gl_ifmt[opt.srgb][img.fmt]; + + if(!(tex = malloc(sizeof *tex))) { + img_destroy(&img); + fprintf(stderr, "failed to allocate texture\n"); + return 0; + } + + glGenTextures(1, &tex->id); + glBindTexture(GL_TEXTURE_2D, tex->id); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, 1); + glTexImage2D(GL_TEXTURE_2D, 0, ifmt, img.width, img.height, 0, gl_fmt[img.fmt], + gl_type[img.fmt], img.pixels); + + tex->width = img.width; + tex->height = img.height; + tex->pixels = img.pixels; + return tex; +} + +void free_texture(struct texture *tex) +{ + if(!tex) return; + + if(tex->id) { + glDeleteTextures(1, &tex->id); + } + + free(tex->pixels); + free(tex); +}