]> git.tdb.fi Git - libs/gl.git/blob - source/texture3d.h
Allow texture mipmap levels to be specified in datafiles
[libs/gl.git] / source / texture3d.h
1 #ifndef MSP_GL_TEXTURE3D_H_
2 #define MSP_GL_TEXTURE3D_H_
3
4 #include <string>
5 #include "texture.h"
6
7 namespace Msp {
8 namespace GL {
9
10 /**
11 Three-dimensional texture.  Consists of an array of texels in the shape of a
12 right cuboid.  Texture coordinates have a principal range of [0, 1].
13 */
14 class Texture3D: public Texture
15 {
16 public:
17         class Loader: public Msp::DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>
18         {
19         public:
20                 Loader(Texture3D &);
21                 Loader(Texture3D &, Collection &);
22         private:
23                 void init();
24
25                 void raw_data(const std::string &);
26                 void storage(PixelFormat, unsigned, unsigned, unsigned);
27                 void storage_levels(PixelFormat, unsigned, unsigned, unsigned, unsigned);
28         };
29
30 private:
31         unsigned width;
32         unsigned height;
33         unsigned depth;
34         unsigned levels;
35         unsigned allocated;
36
37 protected:
38         Texture3D(GLenum);
39 public:
40         Texture3D();
41
42         /** Defines storage structure for the texture.  If lv is zero, the number
43         of mipmap levels is automatically determined from storage dimensions.
44
45         Must be called before an image can be uploaded.  Once storage is defined,
46         it can't be changed. */
47         void storage(PixelFormat fmt, unsigned wd, unsigned ht, unsigned dp, unsigned lv = 0);
48
49         /** Allocates storage for the texture.  The contents are initially
50         undefined.  If storage has already been allocated, does nothing. */
51         void allocate(unsigned level);
52
53         /** Uploads an image to the texture.  Storage must be defined beforehand.
54         The image data must have dimensions and format compatible with the defined
55         storage. */
56         void image(unsigned level, PixelFormat fmt, DataType type, const void *data);
57
58         /** Updates a cuboid-shaped region of the texture.  Storage must be defined
59         and allocated beforehand.  The update region must be fully inside the
60         texture. */
61         void sub_image(unsigned level,
62                 int x, int y, int z, unsigned wd, unsigned ht, unsigned dp,
63                 PixelFormat fmt, DataType type, const void *data);
64
65         /** Loads an image from a file and uploads it to the texture.  If storage
66         has not been defined, it will be set to match the loaded image.  To
67         construct a three-dimensional texture from a two-dimensional image, the
68         image is interpreted as an array of consecutive images.  If dp is -1, the
69         texture's width and height are equal.  If dp is -2, the texture's height and
70         depth are equal.  Otherwise, dp must be positive and determines the
71         texture's depth.  In all cases, the image's height must equal the texture's
72         height times its depth.
73         
74         Deprecated in favor of the base class version.*/
75         DEPRECATED void load_image(const std::string &fn, int dp = -1);
76
77         using Texture::load_image;
78
79         /** Uploads an image to the texture.  If storage has not been defined, it
80         will be set to match the image.  In this case the image will be treated as
81         a stack of square layers and its height must be divisible by its width.
82         Otherwise the image must be compatible with the defined storage.
83
84         If srgb is true and storage is determined by this call, then an sRGB pixel
85         format will be used. */
86         virtual void image(const Graphics::Image &, unsigned, bool = false);
87
88         using Texture::image;
89
90         unsigned get_width() const { return width; }
91         unsigned get_height() const { return height; }
92         unsigned get_depth() const { return depth; }
93 protected:
94         unsigned get_n_levels() const;
95         void get_level_size(unsigned, unsigned &, unsigned &, unsigned &) const;
96
97 public:
98         virtual AsyncLoader *load(IO::Seekable &, const Resources * = 0) { return 0; }
99         virtual UInt64 get_data_size() const;
100         virtual void unload() { }
101 };
102
103 } // namespace GL
104 } // namespace Msp
105
106 #endif