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