]> git.tdb.fi Git - libs/gl.git/blob - source/texture.h
Comment updates for texture and framebuffer classes.
[libs/gl.git] / source / texture.h
1 #ifndef MSP_GL_TEXTURE_H_
2 #define MSP_GL_TEXTURE_H_
3
4 #include <msp/datafile/objectloader.h>
5 #include "gl.h"
6 #include "predicate.h"
7
8 namespace Msp {
9 namespace GL {
10
11 enum TextureFilter
12 {
13         /// No filtering
14         NEAREST = GL_NEAREST,
15
16         /// Bilinear filtering
17         LINEAR = GL_LINEAR,
18
19         /// Mipmapping without filtering
20         NEAREST_MIPMAP_NEAREST = GL_NEAREST_MIPMAP_NEAREST,
21
22         /// Linear filtering between two mipmap levels
23         NEAREST_MIPMAP_LINEAR = GL_NEAREST_MIPMAP_LINEAR,
24
25         /// Bilinear filtering on the closest mipmap level
26         LINEAR_MIPMAP_NEAREST = GL_LINEAR_MIPMAP_NEAREST,
27
28         /// Trilinear filtering between two mipmap levels
29         LINEAR_MIPMAP_LINEAR = GL_LINEAR_MIPMAP_LINEAR
30 };
31
32
33 enum TextureWrap
34 {
35         /// Tile the texture infinitely
36         REPEAT = GL_REPEAT,
37
38         /// Extend the texels at the edge of the texture to infinity
39         CLAMP_TO_EDGE = GL_CLAMP_TO_EDGE,
40
41         /// Tile the texture, with every other repetition mirrored
42         MIRRORED_REPEAT = GL_MIRRORED_REPEAT
43 };
44
45
46 /**
47 Base class for textures.  This class only defines operations common for all
48 texture types and is not instantiable.  For specifying images for textures, see
49 one of the dimensioned texture classes.
50
51 A texture is generally rendered at a size that's either smaller or larger than
52 its native size, so that the texture coordinates do not exactly correspond to
53 the texels of the texture.  The kind of filtering used, if any, is determined
54 by the minification and magnification filter parameters.  The default is LINEAR
55 for magnification and NEAREST_MIPMAP_LINEAR for minification.
56
57 When a mipmapped filter is in use, the texture consists of a stack of mipmap
58 images.  Level 0 is the base image.  Each level above 0 has half the size of
59 the previous level, rounded down and clamped to 1.  The level with size 1 in
60 all dimensions is the last mipmap level.  All levels must be allocated for the
61 texture to be usable.
62
63 If texture coordinates fall outside of the principal range of the texture,
64 wrapping is applied.  The default for all directions is REPEAT.
65 */
66 class Texture
67 {
68 protected:
69         class Loader: public DataFile::ObjectLoader<Texture>
70         {
71         public:
72                 Loader(Texture &);
73                 void generate_mipmap(bool);
74                 void mag_filter(TextureFilter);
75                 void min_filter(TextureFilter);
76                 void wrap(TextureWrap);
77                 void wrap_r(TextureWrap);
78                 void wrap_s(TextureWrap);
79                 void wrap_t(TextureWrap);
80         };
81
82         enum ParameterMask
83         {
84                 MIN_FILTER = 1,
85                 MAG_FILTER = 2,
86                 WRAP_S = 4,
87                 WRAP_T = 8,
88                 WRAP_R = 16,
89                 GENERATE_MIPMAP = 32,
90                 COMPARE = 64,
91                 COMPARE_FUNC = 128
92         };
93
94         unsigned id;
95         GLenum target;
96         TextureFilter min_filter;
97         TextureFilter mag_filter;
98         TextureWrap wrap_s;
99         TextureWrap wrap_t;
100         TextureWrap wrap_r;
101         bool gen_mipmap;
102         bool compare;
103         Predicate cmp_func;
104         mutable int dirty_params;
105
106         Texture(GLenum);
107         Texture(const Texture &);
108         Texture &operator=(const Texture &);
109 public:
110         ~Texture();
111
112 protected:
113         void update_parameter(int) const;
114 public:
115         void set_min_filter(TextureFilter);
116         void set_mag_filter(TextureFilter);
117
118         /** Sets the wrapping mode for all coordinates. */
119         void set_wrap(TextureWrap);
120
121         void set_wrap_s(TextureWrap);
122         void set_wrap_t(TextureWrap);
123         void set_wrap_r(TextureWrap);
124
125         /** Sets automatic mipmap generation.  If enabled, mipmaps are generated
126         when a texture image is uploaded. */
127         void set_generate_mipmap(bool);
128
129         /** Sets depth texture comparison.  Has no effect on other formats.  When
130         comparison is enabled, the third component of the texture coordinate is
131         compared against the texel value, and the result is returned as the texture
132         sample. */
133         void set_compare_enabled(bool);
134
135         /** Sets the function to use for depth comparison. */
136         void set_compare_func(Predicate);
137
138         GLenum get_target() const { return target; }
139         unsigned get_id() const { return id; }
140
141         void bind() const;
142         void bind_to(unsigned) const;
143
144         static const Texture *current();
145         static void unbind();
146         static void unbind_from(unsigned);
147 };
148
149 } // namespace GL
150 } // namespace Msp
151
152 #endif