5c4bf3c7825d16209a21d80d630a756938c09d93
[laserbrain_demo] / src / texture.h
1 #ifndef TEXTURE_H_
2 #define TEXTURE_H_
3
4 #include "dataset.h"
5 #include "datamap.h"
6 #include "opengl.h"
7
8 class Image;
9
10 enum TextureType { TEX_2D, TEX_CUBE };
11
12 class Texture {
13 private:
14         unsigned int id;
15         unsigned int target;
16         unsigned int texfmt;
17         int sz[3];
18         Image *img;
19         static Image *default_img;
20
21         Texture(const Texture &tex) {}
22         Texture &operator =(const Texture &tex) { return *this; }
23
24         void set_image_2d(const Image &img);
25         bool set_image_cube(const Image &img, int idx);
26         bool set_image_cube(const Image &img);
27
28         bool load_cube(const char *fname);
29
30         /* for loading multiple cubemap faces from a single image */
31         bool set_cube_multi(const Image &img, const float *xoffsets, const float *yoffsets, float sz,
32                 unsigned int rotmask = 0);
33
34 public:
35         Texture();
36         ~Texture();
37
38         void set_wrapping(unsigned int wrap);
39         void set_filtering(unsigned int filt);
40         void set_filtering(unsigned int min_filt, unsigned int mag_filt);
41
42         unsigned int get_format() const;
43
44         int get_size(int dim) const;
45         int get_width() const;
46         int get_height() const;
47
48         void create(int xsz, int ysz, TextureType type = TEX_2D, unsigned int ifmt = GL_RGBA);
49         void create_default(TextureType type = TEX_2D);
50         void set_image(const Image &img, int idx = -1);
51
52         bool load(const char *fname);
53
54         unsigned int get_id() const;
55         TextureType get_type() const;
56
57         void bind(int tex_unit = 0) const;
58
59         friend class TextureSet;
60 };
61
62 void bind_texture(Texture *tex, int tunit = 0);
63
64 class TextureSet : public DataSet<Texture*> {
65 private:
66         static Texture *create_tex();
67         static bool load_tex(Texture *tex, const char *fname);
68         static bool done_tex(Texture *tex);
69         static void free_tex(Texture *tex);
70
71 public:
72         TextureSet();
73
74         Texture *get_texture(const char *name, TextureType type = TEX_2D, const DataMap *dmap = 0) const;
75 };
76
77 #endif  // TEXTURE_H_