rev |
line source |
nuclear@0
|
1 #include <stdio.h>
|
nuclear@0
|
2 #include "texture.h"
|
nuclear@0
|
3 #include "image.h"
|
nuclear@0
|
4 #include "opengl.h"
|
nuclear@0
|
5
|
nuclear@0
|
6 #ifndef GL_GENERATE_MIPMAP_SGIS
|
nuclear@0
|
7 #define GL_GENERATE_MIPMAP_SGIS 0x8191
|
nuclear@0
|
8 #endif
|
nuclear@0
|
9
|
nuclear@5
|
10 static unsigned int default_texture(void);
|
nuclear@5
|
11
|
nuclear@0
|
12 unsigned int load_texture(const char *fname)
|
nuclear@0
|
13 {
|
nuclear@0
|
14 unsigned int tex;
|
nuclear@0
|
15 struct image *img;
|
nuclear@0
|
16
|
nuclear@0
|
17 if(!(img = load_image(fname))) {
|
nuclear@0
|
18 fprintf(stderr, "failed to load image: %s\n", fname);
|
nuclear@5
|
19 return default_texture();
|
nuclear@0
|
20 }
|
nuclear@0
|
21
|
nuclear@0
|
22 glGenTextures(1, &tex);
|
nuclear@0
|
23 glBindTexture(GL_TEXTURE_2D, tex);
|
nuclear@0
|
24 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
nuclear@0
|
25 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
nuclear@0
|
26 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
|
nuclear@0
|
27 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->width, img->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img->pixels);
|
nuclear@0
|
28 free_image(img);
|
nuclear@0
|
29 return tex;
|
nuclear@0
|
30 }
|
nuclear@5
|
31
|
nuclear@5
|
32 #define DEF_TEX_SZ 64
|
nuclear@5
|
33
|
nuclear@5
|
34 static unsigned int default_texture(void)
|
nuclear@5
|
35 {
|
nuclear@5
|
36 static unsigned int tex;
|
nuclear@5
|
37 static unsigned char pixels[DEF_TEX_SZ * DEF_TEX_SZ * 3];
|
nuclear@5
|
38
|
nuclear@5
|
39 if(!tex) {
|
nuclear@5
|
40 /* generate it */
|
nuclear@5
|
41 int i, j;
|
nuclear@5
|
42 unsigned char *pptr = pixels;
|
nuclear@5
|
43
|
nuclear@5
|
44 for(i=0; i<DEF_TEX_SZ; i++) {
|
nuclear@5
|
45 for(j=0; j<DEF_TEX_SZ; j++) {
|
nuclear@5
|
46 int c = ((i >> 3) & 1) == ((j >> 3) & 1);
|
nuclear@5
|
47
|
nuclear@5
|
48 *pptr++ = c ? 255 : 64;
|
nuclear@5
|
49 *pptr++ = 100;
|
nuclear@5
|
50 *pptr++ = c ? 64 : 255;
|
nuclear@5
|
51 }
|
nuclear@5
|
52 }
|
nuclear@5
|
53
|
nuclear@5
|
54 glGenTextures(1, &tex);
|
nuclear@5
|
55 glBindTexture(GL_TEXTURE_2D, tex);
|
nuclear@5
|
56 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
nuclear@5
|
57 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
nuclear@5
|
58 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
|
nuclear@5
|
59 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, DEF_TEX_SZ, DEF_TEX_SZ, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
|
nuclear@5
|
60 }
|
nuclear@5
|
61
|
nuclear@5
|
62 return tex;
|
nuclear@5
|
63 }
|