srgb textures, cubemap support
[demo] / src / texture.cc
1 #include <string.h>
2 #include "texture.h"
3
4 Texture::Texture()
5 {
6 }
7
8 Texture::~Texture()
9 {
10 }
11
12 bool Texture::load(const char *fname)
13 {
14         Image img;
15
16         if(img.load(fname)) {
17                 images.clear();
18                 images.push_back(img);
19
20                 update();
21                 return true;
22         }
23
24         /* check if it is a cubemap */
25         return load_cubemap(fname); 
26 }
27
28 bool Texture::load_cubemap(const char *fname)
29 {
30         const char *suffixes[] = {
31                 "_px", "_py", "_pz",
32                 "_nx", "_ny", "_nz"
33         };
34
35         for(int i=0; i<6; i++) {
36                 char *buf = new char[strlen(fname) + 3 + 1];
37                 strcpy(buf, fname);
38                 char *suffix = strrchr(buf, '.');
39
40                 if(suffix) {
41                         memmove(suffix + 3, suffix, strlen(suffix) + 1);
42                         memcpy(suffix, suffixes[i], 3);
43                 } else {
44                         strcat(buf, suffixes[i]);
45                 }
46
47                 Image img;
48                 if(!img.load(buf)) {
49                         images.clear();
50                         return false;
51                 }
52                 images.push_back(img);  
53         }
54
55         update();
56         return true;
57 }
58
59 bool Texture::is_cubemap() const
60 {
61         return images.size() > 1;
62 }