istereo

annotate src/tex.c @ 27:fd39c0198935

normal mapped tunnel
author John Tsiombikas <nuclear@mutantstargoat.com>
date Thu, 08 Sep 2011 08:30:00 +0300
parents 862a3329a8f0
children ff055bff6a15
rev   line source
nuclear@14 1 #include <stdio.h>
nuclear@14 2 #include <stdlib.h>
nuclear@14 3 #include <string.h>
nuclear@14 4 #include <errno.h>
nuclear@14 5 #include "opengl.h"
nuclear@14 6 #include "tex.h"
nuclear@15 7 #include "config.h"
nuclear@26 8 #include "imago2.h"
nuclear@14 9
nuclear@14 10 unsigned int load_texture(const char *fname)
nuclear@14 11 {
nuclear@26 12 int xsz, ysz;
nuclear@14 13 unsigned int tex;
nuclear@26 14 void *pixels;
nuclear@14 15
nuclear@27 16 if(!fname) {
nuclear@27 17 return 0;
nuclear@27 18 }
nuclear@26 19 if(!(pixels = img_load_pixels(fname, &xsz, &ysz, IMG_FMT_RGBA32))) {
nuclear@26 20 fprintf(stderr, "failed to load image: %s\n", fname);
nuclear@14 21 return 0;
nuclear@14 22 }
nuclear@14 23
nuclear@14 24 glGenTextures(1, &tex);
nuclear@14 25 glBindTexture(GL_TEXTURE_2D, tex);
nuclear@14 26 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
nuclear@14 27 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
nuclear@14 28 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
nuclear@14 29 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
nuclear@25 30 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, xsz, ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
nuclear@26 31 img_free_pixels(pixels);
nuclear@14 32
nuclear@14 33 return tex;
nuclear@14 34 }
nuclear@15 35
nuclear@15 36 void bind_texture(unsigned int tex, int unit)
nuclear@15 37 {
nuclear@15 38 glActiveTexture(GL_TEXTURE0 + unit);
nuclear@15 39
nuclear@15 40 #ifndef IPHONE
nuclear@15 41 if(tex) {
nuclear@15 42 glEnable(GL_TEXTURE_2D);
nuclear@15 43 } else {
nuclear@15 44 glDisable(GL_TEXTURE_2D);
nuclear@15 45 }
nuclear@15 46 #endif
nuclear@15 47
nuclear@15 48 glBindTexture(GL_TEXTURE_2D, tex);
nuclear@15 49 }