]> git.tdb.fi Git - libs/gl.git/blob - source/texture2d.h
Enable resource management on Texture2D
[libs/gl.git] / source / 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 "datatype.h"
7 #include "pixelformat.h"
8 #include "resource.h"
9 #include "texture.h"
10
11 namespace Msp {
12 namespace GL {
13
14 /**
15 Two-dimensional texture.  Consists of an array of texels in the shape of a
16 rectangle.  Texture coordinate have a range of [0, 1].  Coordinates outside of
17 this range are subject to wrapping.  This is the most common type of texture.
18 */
19 class Texture2D: public Texture
20 {
21 public:
22         class Loader: public Msp::DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>
23         {
24         public:
25                 Loader(Texture2D &);
26                 Loader(Texture2D &, Collection &);
27         private:
28                 void init();
29
30                 void image_data(const std::string &);
31                 void raw_data(const std::string &);
32                 void storage(PixelFormat, unsigned, unsigned);
33                 void storage_b(PixelFormat, unsigned, unsigned, unsigned);
34         };
35
36 private:
37         class AsyncLoader;
38
39         PixelFormat ifmt;
40         unsigned width;
41         unsigned height;
42         unsigned allocated;
43
44 public:
45         Texture2D(ResourceManager * = 0);
46
47         /** Defines storage structure for the texture.  Must be called before an
48         image can be uploaded.  Once storage is defined, it can't be changed. */
49         void storage(PixelFormat fmt, unsigned wd, unsigned ht);
50
51         /** Allocates storage for the texture.  The contents are initially
52         undefined.  If storage has already been allocated, does nothing. */
53         void allocate(unsigned level);
54
55         /** Uploads an image to the texture.  Storage must be defined beforehand.
56         The image data must have dimensions and format compatible with the defined
57         storage. */
58         void image(unsigned level, PixelFormat fmt, DataType type, const void *data);
59
60         /** Updates a rectangular region of the texture.  Storage must be defined
61         and allocated beforehand.  The update region must be fully inside the
62         texture. */
63         void sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht,
64                 PixelFormat fmt, DataType type, const void *data);
65
66         /// Loads a Graphics::Image from a file and uploads it to the texture.
67         void load_image(const std::string &fn, bool srgb = false);
68
69         /** Uploads an image to the texture.  If storage has not been defined, it
70         will be set to match the image.  Otherwise the image must be compatible with
71         the defined storage.
72
73         If srgb is true and storage is determined by this call, then an sRGB pixel
74         format will be used. */
75         void image(const Graphics::Image &, bool srgb = false);
76
77 private:
78         void image(const Graphics::Image &, bool, bool);
79
80 public:
81         unsigned get_width() const { return width; }
82         unsigned get_height() const { return height; }
83
84 private:
85         void get_level_size(unsigned, unsigned &, unsigned &);
86
87 public:
88         virtual Resource::AsyncLoader *load(IO::Seekable &);
89         virtual void unload();
90 };
91
92 } // namespace GL
93 } // namespace Msp
94
95 #endif