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