dungeon_crawler

annotate prototype/src/texman.cc @ 43:3fef65352b0b

- added mipmap generation
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 30 Aug 2012 05:58:09 +0300
parents e5567ddbf2ef
children f3030df27110
rev   line source
nuclear@11 1 #include <string.h>
nuclear@11 2 #include "opengl.h"
nuclear@11 3 #include "imago2.h"
nuclear@11 4 #include "texman.h"
nuclear@11 5 #include "datapath.h"
nuclear@11 6
nuclear@43 7 unsigned int load_texture(const char *fname);
nuclear@43 8
nuclear@11 9 TextureSet::~TextureSet()
nuclear@11 10 {
nuclear@11 11 for(auto iter : textures) {
nuclear@11 12 glDeleteTextures(1, &iter.second);
nuclear@11 13 }
nuclear@11 14 }
nuclear@11 15
nuclear@11 16 unsigned int TextureSet::get_texture(const char *fname) const
nuclear@11 17 {
nuclear@11 18 auto iter = textures.find(fname);
nuclear@11 19 if(iter != textures.end()) {
nuclear@11 20 return iter->second;
nuclear@11 21 }
nuclear@11 22
nuclear@11 23 const char *path, *slash;
nuclear@11 24 if((slash = strrchr(fname, '/'))) {
nuclear@11 25 path = slash + 1;
nuclear@11 26 }
nuclear@11 27 path = datafile_path(path);
nuclear@11 28
nuclear@11 29 printf("loading texture: %s\n", path);
nuclear@43 30 unsigned int tex = load_texture(path);
nuclear@11 31 if(tex) {
nuclear@11 32 textures[fname] = tex;
nuclear@11 33 } else {
nuclear@11 34 fprintf(stderr, "failed to load texture: %s\n", path);
nuclear@11 35 }
nuclear@11 36 return tex;
nuclear@11 37 }
nuclear@43 38
nuclear@43 39 unsigned int load_texture(const char *fname)
nuclear@43 40 {
nuclear@43 41 struct img_pixmap img;
nuclear@43 42
nuclear@43 43 img_init(&img);
nuclear@43 44 if(img_load(&img, fname) == -1) {
nuclear@43 45 img_destroy(&img);
nuclear@43 46 return 0;
nuclear@43 47 }
nuclear@43 48
nuclear@43 49 unsigned int intfmt = img_glintfmt(&img);
nuclear@43 50 unsigned int fmt = img_glfmt(&img);
nuclear@43 51 unsigned int type = img_gltype(&img);
nuclear@43 52
nuclear@43 53 unsigned int tex;
nuclear@43 54 glGenTextures(1, &tex);
nuclear@43 55 glBindTexture(GL_TEXTURE_2D, tex);
nuclear@43 56 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
nuclear@43 57 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
nuclear@43 58 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
nuclear@43 59 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
nuclear@43 60
nuclear@43 61 if(GLEW_SGIS_generate_mipmap) {
nuclear@43 62 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
nuclear@43 63 glTexImage2D(GL_TEXTURE_2D, 0, intfmt, img.width, img.height, 0, fmt, type, img.pixels);
nuclear@43 64 } else {
nuclear@43 65 gluBuild2DMipmaps(GL_TEXTURE_2D, intfmt, img.width, img.height, fmt, type, img.pixels);
nuclear@43 66 }
nuclear@43 67
nuclear@43 68 img_destroy(&img);
nuclear@43 69 return tex;
nuclear@43 70 }