intravenous
diff src/tex.c @ 3:94d4c60af435
some progress
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 22 Apr 2012 03:35:18 +0300 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/tex.c Sun Apr 22 03:35:18 2012 +0300 1.3 @@ -0,0 +1,65 @@ 1.4 +#include <imago2.h> 1.5 +#include "opengl.h" 1.6 + 1.7 +static void bind_common(GLenum type, unsigned int tex, int tex_unit); 1.8 + 1.9 +unsigned int load_texture(const char *fname) 1.10 +{ 1.11 + unsigned int tex; 1.12 + struct img_pixmap img; 1.13 + int intfmt, fmt, type; 1.14 + 1.15 + img_init(&img); 1.16 + if(img_load(&img, fname) == -1) { 1.17 + fprintf(stderr, "failed to load texture: %s\n", fname); 1.18 + return 0; 1.19 + } 1.20 + intfmt = img_glintfmt(&img); 1.21 + fmt = img_glfmt(&img); 1.22 + type = img_gltype(&img); 1.23 + 1.24 + glGenTextures(1, &tex); 1.25 + glBindTexture(GL_TEXTURE_2D, tex); 1.26 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 1.27 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 1.28 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 1.29 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 1.30 + glTexImage2D(GL_TEXTURE_2D, 0, intfmt, img.width, img.height, 0, fmt, type, img.pixels); 1.31 + img_destroy(&img); 1.32 + return tex; 1.33 +} 1.34 + 1.35 +unsigned int load_texture_cube(const char *fname) 1.36 +{ 1.37 + /* TODO implement */ 1.38 + return 0; 1.39 +} 1.40 + 1.41 +void free_texture(unsigned int tex) 1.42 +{ 1.43 + if(tex && glIsTexture(tex)) { 1.44 + glDeleteTextures(1, &tex); 1.45 + } 1.46 +} 1.47 + 1.48 +void bind_texture(unsigned int tex, int tex_unit) 1.49 +{ 1.50 + bind_common(GL_TEXTURE_2D, tex, tex_unit); 1.51 +} 1.52 + 1.53 +void bind_texture_cube(unsigned int tex, int tex_unit) 1.54 +{ 1.55 + bind_common(GL_TEXTURE_CUBE_MAP, tex, tex_unit); 1.56 +} 1.57 + 1.58 +static void bind_common(GLenum type, unsigned int tex, int tex_unit) 1.59 +{ 1.60 + glActiveTextureARB(GL_TEXTURE0 + (GLenum)tex_unit); 1.61 + if(tex) { 1.62 + glBindTexture(type, tex); 1.63 + glEnable(type); 1.64 + } else { 1.65 + glDisable(type); 1.66 + } 1.67 + glActiveTextureARB(GL_TEXTURE0); 1.68 +}