goat3dgfx
changeset 33:253542d715f4
default texture
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 02 Mar 2014 06:32:37 +0200 |
parents | 3ba80928b530 |
children | 3eb6c8f89fe1 |
files | src/texture.cc src/texture.h |
diffstat | 2 files changed, 65 insertions(+), 0 deletions(-) [+] |
line diff
1.1 --- a/src/texture.cc Sun Mar 02 04:20:00 2014 +0200 1.2 +++ b/src/texture.cc Sun Mar 02 06:32:37 2014 +0200 1.3 @@ -43,6 +43,9 @@ 1.4 } 1.5 } 1.6 1.7 + 1.8 +Image *Texture::default_img; 1.9 + 1.10 Texture::Texture() 1.11 { 1.12 target = 0; 1.13 @@ -178,6 +181,38 @@ 1.14 texfmt = ifmt; 1.15 } 1.16 1.17 +#define DEF_IMAGE_SIZE 64 1.18 +void Texture::create_default(TextureType type) 1.19 +{ 1.20 + if(!default_img) { 1.21 + default_img = new Image; 1.22 + default_img->create(DEF_IMAGE_SIZE, DEF_IMAGE_SIZE, Image::FMT_RGBA); 1.23 + 1.24 + unsigned char *pixels = (unsigned char*)default_img->get_pixels(); 1.25 + for(int i=0; i<DEF_IMAGE_SIZE; i++) { 1.26 + for(int j=0; j<DEF_IMAGE_SIZE; j++) { 1.27 + bool chess = ((i >> 3) & 1) == ((j >> 3) & 1); 1.28 + pixels[0] = chess ? 255 : 32; 1.29 + pixels[1] = 64; 1.30 + pixels[2] = chess ? 32 : 255; 1.31 + pixels[3] = 255; 1.32 + } 1.33 + } 1.34 + } 1.35 + 1.36 + switch(type) { 1.37 + case TEX_2D: 1.38 + set_image(*default_img); 1.39 + break; 1.40 + 1.41 + case TEX_CUBE: 1.42 + for(int i=0; i<6; i++) { 1.43 + set_image(*default_img, i); 1.44 + } 1.45 + break; 1.46 + } 1.47 +} 1.48 + 1.49 void Texture::set_image(const Image &img, int idx) 1.50 { 1.51 if(idx >= 0 && idx < 6) { 1.52 @@ -422,6 +457,32 @@ 1.53 { 1.54 } 1.55 1.56 +Texture *TextureSet::get_texture(const char *name, TextureType type) const 1.57 +{ 1.58 + typename std::map<std::string, Texture*>::const_iterator iter = data.find(name); 1.59 + if(iter != data.end()) { 1.60 + return iter->second; 1.61 + } 1.62 + 1.63 + const char *fname, *slash; 1.64 + if((slash = strrchr(name, '/'))) { 1.65 + fname = slash + 1; 1.66 + } else { 1.67 + fname = name; 1.68 + } 1.69 + 1.70 + std::string path = datafile_path(fname); 1.71 + if(path.empty()) { 1.72 + fprintf(stderr, "can't find data file: %s\n", name); 1.73 + return 0; 1.74 + } 1.75 + 1.76 + Texture *res = create(); 1.77 + res->create_default(type); 1.78 + resman_lookup(rman, path.c_str(), res); 1.79 + return res; 1.80 +} 1.81 + 1.82 // static callbacks 1.83 1.84 Texture *TextureSet::create_tex()
2.1 --- a/src/texture.h Sun Mar 02 04:20:00 2014 +0200 2.2 +++ b/src/texture.h Sun Mar 02 06:32:37 2014 +0200 2.3 @@ -17,6 +17,7 @@ 2.4 unsigned int texfmt; 2.5 int sz[3]; 2.6 Image *img; 2.7 + static Image *default_img; 2.8 2.9 Texture(const Texture &tex) {} 2.10 Texture &operator =(const Texture &tex) { return *this; } 2.11 @@ -44,6 +45,7 @@ 2.12 int get_size(int dim) const; 2.13 2.14 void create(int xsz, int ysz, TextureType type = TEX_2D, unsigned int ifmt = GL_RGBA); 2.15 + void create_default(TextureType type = TEX_2D); 2.16 void set_image(const Image &img, int idx = -1); 2.17 2.18 bool load(const char *fname); 2.19 @@ -67,6 +69,8 @@ 2.20 2.21 public: 2.22 TextureSet(); 2.23 + 2.24 + Texture *get_texture(const char *name, TextureType type = TEX_2D) const; 2.25 }; 2.26 2.27 } // namespace goatgfx