labyrinth

view src/texture.c @ 7:b557812c45db

generating the android (ant) project stuff from the makefile
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 04 May 2015 02:46:35 +0300
parents 8ba79034e8a6
children
line source
1 #include <stdio.h>
2 #include "texture.h"
3 #include "image.h"
4 #include "opengl.h"
6 #ifndef GL_GENERATE_MIPMAP_SGIS
7 #define GL_GENERATE_MIPMAP_SGIS 0x8191
8 #endif
10 static unsigned int default_texture(void);
12 unsigned int load_texture(const char *fname)
13 {
14 unsigned int tex;
15 struct image *img;
17 if(!(img = load_image(fname))) {
18 fprintf(stderr, "failed to load image: %s\n", fname);
19 return default_texture();
20 }
22 glGenTextures(1, &tex);
23 glBindTexture(GL_TEXTURE_2D, tex);
24 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
25 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
26 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
27 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->width, img->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img->pixels);
28 free_image(img);
29 return tex;
30 }
32 #define DEF_TEX_SZ 64
34 static unsigned int default_texture(void)
35 {
36 static unsigned int tex;
37 static unsigned char pixels[DEF_TEX_SZ * DEF_TEX_SZ * 3];
39 if(!tex) {
40 /* generate it */
41 int i, j;
42 unsigned char *pptr = pixels;
44 for(i=0; i<DEF_TEX_SZ; i++) {
45 for(j=0; j<DEF_TEX_SZ; j++) {
46 int c = ((i >> 3) & 1) == ((j >> 3) & 1);
48 *pptr++ = c ? 255 : 64;
49 *pptr++ = 100;
50 *pptr++ = c ? 64 : 255;
51 }
52 }
54 glGenTextures(1, &tex);
55 glBindTexture(GL_TEXTURE_2D, tex);
56 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
57 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
58 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
59 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, DEF_TEX_SZ, DEF_TEX_SZ, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
60 }
62 return tex;
63 }