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