dungeon_crawler

view 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
line source
1 #include <string.h>
2 #include "opengl.h"
3 #include "imago2.h"
4 #include "texman.h"
5 #include "datapath.h"
7 unsigned int load_texture(const char *fname);
9 TextureSet::~TextureSet()
10 {
11 for(auto iter : textures) {
12 glDeleteTextures(1, &iter.second);
13 }
14 }
16 unsigned int TextureSet::get_texture(const char *fname) const
17 {
18 auto iter = textures.find(fname);
19 if(iter != textures.end()) {
20 return iter->second;
21 }
23 const char *path, *slash;
24 if((slash = strrchr(fname, '/'))) {
25 path = slash + 1;
26 }
27 path = datafile_path(path);
29 printf("loading texture: %s\n", path);
30 unsigned int tex = load_texture(path);
31 if(tex) {
32 textures[fname] = tex;
33 } else {
34 fprintf(stderr, "failed to load texture: %s\n", path);
35 }
36 return tex;
37 }
39 unsigned int load_texture(const char *fname)
40 {
41 struct img_pixmap img;
43 img_init(&img);
44 if(img_load(&img, fname) == -1) {
45 img_destroy(&img);
46 return 0;
47 }
49 unsigned int intfmt = img_glintfmt(&img);
50 unsigned int fmt = img_glfmt(&img);
51 unsigned int type = img_gltype(&img);
53 unsigned int tex;
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_TEXTURE_WRAP_S, GL_REPEAT);
59 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
61 if(GLEW_SGIS_generate_mipmap) {
62 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
63 glTexImage2D(GL_TEXTURE_2D, 0, intfmt, img.width, img.height, 0, fmt, type, img.pixels);
64 } else {
65 gluBuild2DMipmaps(GL_TEXTURE_2D, intfmt, img.width, img.height, fmt, type, img.pixels);
66 }
68 img_destroy(&img);
69 return tex;
70 }