intravenous

view 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 source
1 #include <imago2.h>
2 #include "opengl.h"
4 static void bind_common(GLenum type, unsigned int tex, int tex_unit);
6 unsigned int load_texture(const char *fname)
7 {
8 unsigned int tex;
9 struct img_pixmap img;
10 int intfmt, fmt, type;
12 img_init(&img);
13 if(img_load(&img, fname) == -1) {
14 fprintf(stderr, "failed to load texture: %s\n", fname);
15 return 0;
16 }
17 intfmt = img_glintfmt(&img);
18 fmt = img_glfmt(&img);
19 type = img_gltype(&img);
21 glGenTextures(1, &tex);
22 glBindTexture(GL_TEXTURE_2D, tex);
23 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
24 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
25 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
26 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
27 glTexImage2D(GL_TEXTURE_2D, 0, intfmt, img.width, img.height, 0, fmt, type, img.pixels);
28 img_destroy(&img);
29 return tex;
30 }
32 unsigned int load_texture_cube(const char *fname)
33 {
34 /* TODO implement */
35 return 0;
36 }
38 void free_texture(unsigned int tex)
39 {
40 if(tex && glIsTexture(tex)) {
41 glDeleteTextures(1, &tex);
42 }
43 }
45 void bind_texture(unsigned int tex, int tex_unit)
46 {
47 bind_common(GL_TEXTURE_2D, tex, tex_unit);
48 }
50 void bind_texture_cube(unsigned int tex, int tex_unit)
51 {
52 bind_common(GL_TEXTURE_CUBE_MAP, tex, tex_unit);
53 }
55 static void bind_common(GLenum type, unsigned int tex, int tex_unit)
56 {
57 glActiveTextureARB(GL_TEXTURE0 + (GLenum)tex_unit);
58 if(tex) {
59 glBindTexture(type, tex);
60 glEnable(type);
61 } else {
62 glDisable(type);
63 }
64 glActiveTextureARB(GL_TEXTURE0);
65 }