1 #ifndef MSP_GL_TEXTURE_H_
2 #define MSP_GL_TEXTURE_H_
4 #include <msp/core/attributes.h>
5 #include <msp/datafile/objectloader.h>
6 #include <msp/graphics/image.h>
9 #include "pixelformat.h"
10 #include "predicate.h"
21 /// Bilinear filtering
24 /// Mipmapping without filtering
25 NEAREST_MIPMAP_NEAREST = GL_NEAREST_MIPMAP_NEAREST,
27 /// Linear filtering between two mipmap levels
28 NEAREST_MIPMAP_LINEAR = GL_NEAREST_MIPMAP_LINEAR,
30 /// Bilinear filtering on the closest mipmap level
31 LINEAR_MIPMAP_NEAREST = GL_LINEAR_MIPMAP_NEAREST,
33 /// Trilinear filtering between two mipmap levels
34 LINEAR_MIPMAP_LINEAR = GL_LINEAR_MIPMAP_LINEAR
40 /// Tile the texture infinitely
43 /// Extend the texels at the edge of the texture to infinity
44 CLAMP_TO_EDGE = GL_CLAMP_TO_EDGE,
46 /// Tile the texture, with every other repetition mirrored
47 MIRRORED_REPEAT = GL_MIRRORED_REPEAT
52 Base class for textures. This class only defines operations common for all
53 texture types and is not instantiable. For specifying images for textures, see
54 one of the dimensioned texture classes.
56 A texture is generally rendered at a size that's either smaller or larger than
57 its native size, so that the texture coordinates do not exactly correspond to
58 the texels of the texture. The kind of filtering used, if any, is determined
59 by the minification and magnification filter parameters. The default is LINEAR
60 for magnification and NEAREST_MIPMAP_LINEAR for minification.
62 When a mipmapped filter is in use, the texture consists of a stack of mipmap
63 images. Level 0 is the base image. Each level above 0 has half the size of
64 the previous level, rounded down and clamped to 1. The level with size 1 in
65 all dimensions is the last mipmap level. All levels must be allocated for the
68 If texture coordinates fall outside of the principal range of the texture,
69 wrapping is applied. The default for all directions is REPEAT.
71 class Texture: public Resource
74 class Loader: public DataFile::CollectionObjectLoader<Texture>
82 Loader(Texture &, Collection &);
86 void external_image(const std::string &);
87 void filter(TextureFilter);
88 void generate_mipmap(bool);
89 void image_data(const std::string &);
90 void mag_filter(TextureFilter);
91 void max_anisotropy(float);
92 void min_filter(TextureFilter);
93 void mipmap_levels(unsigned);
94 void wrap(TextureWrap);
95 void wrap_r(TextureWrap);
96 void wrap_s(TextureWrap);
97 void wrap_t(TextureWrap);
109 MAX_ANISOTROPY = 256,
117 RG_TO_LUMINANCE_ALPHA
123 FormatSwizzle swizzle;
124 TextureFilter min_filter;
125 TextureFilter mag_filter;
126 float max_anisotropy;
130 bool auto_gen_mipmap;
133 mutable int dirty_params;
135 static int swizzle_orders[];
137 Texture(GLenum, ResourceManager * = 0);
138 Texture(const Texture &);
139 Texture &operator=(const Texture &);
144 static DataType get_alloc_type(PixelFormat);
145 void set_internal_format(PixelFormat);
146 PixelFormat get_upload_format(PixelFormat) const;
148 void update_parameter(int) const;
149 void set_parameter_i(GLenum, int) const;
150 void set_parameter_f(GLenum, float) const;
152 void set_min_filter(TextureFilter);
153 void set_mag_filter(TextureFilter);
155 /** Sets filter for both minification and magnification. Since mipmapping
156 is not applicable to magnification, LINEAR is used instead. */
157 void set_filter(TextureFilter);
159 DEPRECATED void set_mipmap_levels(unsigned) { }
161 void set_max_anisotropy(float);
163 /** Sets the wrapping mode for all coordinates. */
164 void set_wrap(TextureWrap);
166 void set_wrap_s(TextureWrap);
167 void set_wrap_t(TextureWrap);
168 void set_wrap_r(TextureWrap);
170 static bool can_generate_mipmap();
172 void generate_mipmap();
174 /** Sets automatic mipmap generation. If enabled, mipmaps are generated
175 when a texture image is uploaded. */
176 void set_auto_generate_mipmap(bool);
178 /// Deprecated. Use set_auto_generate_mipmap instead.
179 DEPRECATED void set_generate_mipmap(bool g) { set_auto_generate_mipmap(g); }
181 /** Sets depth texture comparison. Has no effect on other formats. When
182 comparison is enabled, the third component of the texture coordinate is
183 compared against the texel value, and the result is returned as the texture
185 void set_compare_enabled(bool);
187 /** Sets the function to use for depth comparison. */
188 void set_compare_func(Predicate);
190 /// Loads a Graphics::Image from a file and uploads it to the texture.
191 virtual void load_image(const std::string &, bool srgb = false);
193 virtual void load_image(const std::string &, unsigned, bool srgb = false);
195 /** Uploads an image to the texture. If storage has not been defined, it
196 will be set to match the image. Otherwise the image must be compatible
197 with the defined storage. Semantics depend on the type of texture.
199 If srgb is true and storage is determined by this call, then an sRGB pixel
200 format will be used. */
201 virtual void image(const Graphics::Image &, bool srgb = false);
203 virtual void image(const Graphics::Image &, unsigned, bool srgb = false) = 0;
205 GLenum get_target() const { return target; }
206 unsigned get_id() const { return id; }
208 void bind() const { bind_to(0); }
209 void bind_to(unsigned) const;
211 static const Texture *current(unsigned = 0);
212 static void unbind() { unbind_from(0); }
213 static void unbind_from(unsigned);
215 virtual UInt64 get_data_size() const { return 0; }
219 bool is_mipmapped(TextureFilter);