]> git.tdb.fi Git - libs/gl.git/blob - source/core/texture3d.h
21893abc07e5fc19c4915cb34c4882086d5bdf36
[libs/gl.git] / source / core / texture3d.h
1 #ifndef MSP_GL_TEXTURE3D_H_
2 #define MSP_GL_TEXTURE3D_H_
3
4 #include <string>
5 #include <msp/linal/vector.h>
6 #include "texture3d_backend.h"
7
8 namespace Msp {
9 namespace GL {
10
11 /**
12 Three-dimensional texture.  Consists of an array of texels in the shape of a
13 right cuboid.  Texture coordinates have a principal range of [0, 1].
14 */
15 class Texture3D: public Texture3DBackend
16 {
17         friend Texture3DBackend;
18
19 public:
20         class Loader: public Msp::DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>
21         {
22         public:
23                 Loader(Texture3D &);
24                 Loader(Texture3D &, Collection &);
25         private:
26                 void init();
27
28                 void raw_data(const std::string &);
29                 void storage(PixelFormat, unsigned, unsigned, unsigned);
30                 void storage_levels(PixelFormat, unsigned, unsigned, unsigned, unsigned);
31         };
32
33 protected:
34         unsigned width;
35         unsigned height;
36         unsigned depth;
37         unsigned levels;
38
39         Texture3D(unsigned);
40 public:
41         Texture3D();
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 dp, unsigned lv = 0);
49
50         /** Updates the contents of the entire texture.  Storage must be defined
51         beforehand.  The image data must have dimensions and format matching the
52         defined storage. */
53         void image(unsigned level, const void *data);
54
55         /** Updates a cuboid-shaped region of the texture.  Storage must be defined
56         beforehand.  The image data must be in a format mathing the defined storage
57         and the update region must be fully inside the texture. */
58         void sub_image(unsigned level, int x, int y, int z, unsigned wd, unsigned ht, unsigned dp, const void *data);
59
60         /** Updates the contents of the entire texture from an image.  If storage
61         has not been defined, it will be set to match the image.  In this case the
62         image will be treated as a stack of square layers and its height must be
63         divisible by its width.  Otherwise the image must match the defined
64         storage. */
65         virtual void image(const Graphics::Image &, unsigned = 0);
66
67         using Texture::image;
68
69         unsigned get_width() const { return width; }
70         unsigned get_height() const { return height; }
71         unsigned get_depth() const { return depth; }
72 protected:
73         unsigned get_n_levels() const;
74         LinAl::Vector<unsigned, 3> get_level_size(unsigned) const;
75
76 public:
77         virtual AsyncLoader *load(IO::Seekable &, const Resources * = 0) { return 0; }
78         virtual std::uint64_t get_data_size() const;
79         virtual void unload() { }
80 };
81
82 } // namespace GL
83 } // namespace Msp
84
85 #endif