]> git.tdb.fi Git - libs/gl.git/blob - source/core/texture2d.h
Use standard fixed-size integer types
[libs/gl.git] / source / core / texture2d.h
1 #ifndef MSP_GL_TEXTURE2D_H_
2 #define MSP_GL_TEXTURE2D_H_
3
4 #include <string>
5 #include <msp/graphics/image.h>
6 #include <msp/linal/vector.h>
7 #include "texture.h"
8
9 namespace Msp {
10 namespace GL {
11
12 /**
13 Two-dimensional texture.  Consists of an array of texels in the shape of a
14 rectangle.  Texture coordinate have a range of [0, 1].  Coordinates outside of
15 this range are subject to wrapping.  This is the most common type of texture.
16 */
17 class Texture2D: public Texture
18 {
19 public:
20         class Loader: public Msp::DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>
21         {
22         public:
23                 Loader(Texture2D &);
24                 Loader(Texture2D &, Collection &);
25         private:
26                 void init();
27
28                 void raw_data(const std::string &);
29                 void storage(PixelFormat, unsigned, unsigned);
30                 void storage_levels(PixelFormat, unsigned, unsigned, unsigned);
31         };
32
33 private:
34         class AsyncLoader;
35
36         unsigned width;
37         unsigned height;
38         unsigned levels;
39         unsigned allocated;
40
41 public:
42         Texture2D(ResourceManager * = 0);
43         virtual ~Texture2D();
44
45         /** Defines storage structure for the texture.  If lv is zero, the number
46         of mipmap levels is automatically determined from storage dimensions.
47
48         Must be called before an image can be uploaded.  Once storage is defined,
49         it can't be changed. */
50         void storage(PixelFormat fmt, unsigned wd, unsigned ht, unsigned lv = 0);
51
52         DEPRECATED void storage(PixelComponents cm, unsigned wd, unsigned ht, unsigned lv = 0)
53         { storage(make_pixelformat(cm, UNSIGNED_BYTE), wd, ht, lv); }
54
55         /** Allocates storage for the texture.  The contents are initially
56         undefined.  If storage has already been allocated, does nothing. */
57         void allocate(unsigned level);
58
59         /** Updates the contents of the entire texture.  Storage must be defined
60         beforehand.  The image data must have dimensions and format matching the
61         defined storage. */
62         virtual void image(unsigned level, const void *data);
63
64         DEPRECATED void image(unsigned level, PixelComponents fmt, DataType type, const void *data);
65
66         /** Updates a rectangular region of the texture.  Storage must be defined
67         beforehand.  The image data must be in a format mathing the defined storage
68         and the update region must be fully inside the texture. */
69         void sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, const void *data);
70
71         DEPRECATED void sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht,
72                 PixelComponents fmt, DataType type, const void *data);
73
74         /** Updates the contents of the entire texture from an image.  If storage
75         has not been defined, it will be set to match the image.  Otherwise the
76         image must match the defined storage. */
77         virtual void image(const Graphics::Image &, unsigned lv = 0);
78
79         using Texture::image;
80
81 private:
82         void image(const Graphics::Image &, unsigned, bool);
83
84 public:
85         unsigned get_width() const { return width; }
86         unsigned get_height() const { return height; }
87
88 private:
89         unsigned get_n_levels() const;
90         LinAl::Vector<unsigned, 2> get_level_size(unsigned) const;
91
92 public:
93         virtual Resource::AsyncLoader *load(IO::Seekable &, const Resources * = 0);
94         virtual std::uint64_t get_data_size() const;
95         virtual void unload();
96 };
97
98 } // namespace GL
99 } // namespace Msp
100
101 #endif