1 #ifndef MSP_GL_TEXTURE_H_
2 #define MSP_GL_TEXTURE_H_
4 #include <msp/datafile/objectloader.h>
5 #include <msp/graphics/image.h>
8 #include "pixelformat.h"
20 /// Bilinear filtering
23 /// Mipmapping without filtering
24 NEAREST_MIPMAP_NEAREST = GL_NEAREST_MIPMAP_NEAREST,
26 /// Linear filtering between two mipmap levels
27 NEAREST_MIPMAP_LINEAR = GL_NEAREST_MIPMAP_LINEAR,
29 /// Bilinear filtering on the closest mipmap level
30 LINEAR_MIPMAP_NEAREST = GL_LINEAR_MIPMAP_NEAREST,
32 /// Trilinear filtering between two mipmap levels
33 LINEAR_MIPMAP_LINEAR = GL_LINEAR_MIPMAP_LINEAR
39 /// Tile the texture infinitely
42 /// Extend the texels at the edge of the texture to infinity
43 CLAMP_TO_EDGE = GL_CLAMP_TO_EDGE,
45 /// Tile the texture, with every other repetition mirrored
46 MIRRORED_REPEAT = GL_MIRRORED_REPEAT
51 Base class for textures. This class only defines operations common for all
52 texture types and is not instantiable. For specifying images for textures, see
53 one of the dimensioned texture classes.
55 A texture is generally rendered at a size that's either smaller or larger than
56 its native size, so that the texture coordinates do not exactly correspond to
57 the texels of the texture. The kind of filtering used, if any, is determined
58 by the minification and magnification filter parameters. The default is LINEAR
59 for magnification and NEAREST_MIPMAP_LINEAR for minification.
61 When a mipmapped filter is in use, the texture consists of a stack of mipmap
62 images. Level 0 is the base image. Each level above 0 has half the size of
63 the previous level, rounded down and clamped to 1. The level with size 1 in
64 all dimensions is the last mipmap level. All levels must be allocated for the
67 If texture coordinates fall outside of the principal range of the texture,
68 wrapping is applied. The default for all directions is REPEAT.
70 class Texture: public Resource
73 class Loader: public DataFile::CollectionObjectLoader<Texture>
80 Loader(Texture &, Collection &);
84 void filter(TextureFilter);
85 void generate_mipmap(bool);
86 void image_data(const std::string &);
87 void mag_filter(TextureFilter);
88 void max_anisotropy(float);
89 void min_filter(TextureFilter);
90 void wrap(TextureWrap);
91 void wrap_r(TextureWrap);
92 void wrap_s(TextureWrap);
93 void wrap_t(TextureWrap);
103 GENERATE_MIPMAP = 32,
111 TextureFilter min_filter;
112 TextureFilter mag_filter;
113 float max_anisotropy;
120 mutable int dirty_params;
122 Texture(GLenum, ResourceManager * = 0);
123 Texture(const Texture &);
124 Texture &operator=(const Texture &);
129 static DataType get_alloc_type(PixelFormat);
131 void update_parameter(int) const;
133 void set_min_filter(TextureFilter);
134 void set_mag_filter(TextureFilter);
136 /** Sets filter for both minification and magnification. Since mipmapping
137 is not applicable to magnification, LINEAR is used instead. */
138 void set_filter(TextureFilter);
140 void set_max_anisotropy(float);
142 /** Sets the wrapping mode for all coordinates. */
143 void set_wrap(TextureWrap);
145 void set_wrap_s(TextureWrap);
146 void set_wrap_t(TextureWrap);
147 void set_wrap_r(TextureWrap);
149 /** Sets automatic mipmap generation. If enabled, mipmaps are generated
150 when a texture image is uploaded. */
151 void set_generate_mipmap(bool);
154 void auto_generate_mipmap();
157 /** Sets depth texture comparison. Has no effect on other formats. When
158 comparison is enabled, the third component of the texture coordinate is
159 compared against the texel value, and the result is returned as the texture
161 void set_compare_enabled(bool);
163 /** Sets the function to use for depth comparison. */
164 void set_compare_func(Predicate);
166 /// Loads a Graphics::Image from a file and uploads it to the texture.
167 virtual void load_image(const std::string &, bool srgb = false);
169 /** Uploads an image to the texture. If storage has not been defined, it
170 will be set to match the image. Otherwise the image must be compatible
171 with the defined storage. Semantics depend on the type of texture.
173 If srgb is true and storage is determined by this call, then an sRGB pixel
174 format will be used. */
175 virtual void image(const Graphics::Image &, bool srgb = false) = 0;
177 GLenum get_target() const { return target; }
178 unsigned get_id() const { return id; }
180 void bind() const { bind_to(0); }
181 void bind_to(unsigned) const;
183 static const Texture *current(unsigned = 0);
184 static void unbind() { unbind_from(0); }
185 static void unbind_from(unsigned);
187 virtual UInt64 get_data_size() const { return 0; }