nuclear@0: #include nuclear@0: #include "texture.h" nuclear@0: #include "image.h" nuclear@0: #include "opengl.h" nuclear@0: nuclear@0: #ifndef GL_GENERATE_MIPMAP_SGIS nuclear@0: #define GL_GENERATE_MIPMAP_SGIS 0x8191 nuclear@0: #endif nuclear@0: nuclear@5: static unsigned int default_texture(void); nuclear@5: nuclear@0: unsigned int load_texture(const char *fname) nuclear@0: { nuclear@0: unsigned int tex; nuclear@0: struct image *img; nuclear@0: nuclear@0: if(!(img = load_image(fname))) { nuclear@0: fprintf(stderr, "failed to load image: %s\n", fname); nuclear@5: return default_texture(); nuclear@0: } nuclear@0: nuclear@0: glGenTextures(1, &tex); nuclear@0: glBindTexture(GL_TEXTURE_2D, tex); nuclear@0: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); nuclear@0: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); nuclear@0: glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE); nuclear@0: glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->width, img->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img->pixels); nuclear@0: free_image(img); nuclear@0: return tex; nuclear@0: } nuclear@5: nuclear@5: #define DEF_TEX_SZ 64 nuclear@5: nuclear@5: static unsigned int default_texture(void) nuclear@5: { nuclear@5: static unsigned int tex; nuclear@5: static unsigned char pixels[DEF_TEX_SZ * DEF_TEX_SZ * 3]; nuclear@5: nuclear@5: if(!tex) { nuclear@5: /* generate it */ nuclear@5: int i, j; nuclear@5: unsigned char *pptr = pixels; nuclear@5: nuclear@5: for(i=0; i> 3) & 1) == ((j >> 3) & 1); nuclear@5: nuclear@5: *pptr++ = c ? 255 : 64; nuclear@5: *pptr++ = 100; nuclear@5: *pptr++ = c ? 64 : 255; nuclear@5: } nuclear@5: } nuclear@5: nuclear@5: glGenTextures(1, &tex); nuclear@5: glBindTexture(GL_TEXTURE_2D, tex); nuclear@5: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); nuclear@5: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); nuclear@5: glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE); nuclear@5: glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, DEF_TEX_SZ, DEF_TEX_SZ, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels); nuclear@5: } nuclear@5: nuclear@5: return tex; nuclear@5: }