]> git.tdb.fi Git - libs/gl.git/blob - source/core/texturecube.h
Rewrite state management
[libs/gl.git] / source / core / texturecube.h
1 #ifndef MSP_GL_TEXTURECUBE_H_
2 #define MSP_GL_TEXTURECUBE_H_
3
4 #include <msp/gl/extensions/arb_texture_cube_map.h>
5 #include <msp/graphics/image.h>
6 #include "texture.h"
7 #include "vector.h"
8
9 namespace Msp {
10 namespace GL {
11
12 enum TextureCubeFace
13 {
14         POSITIVE_X = GL_TEXTURE_CUBE_MAP_POSITIVE_X,
15         NEGATIVE_X = GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
16         POSITIVE_Y = GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
17         NEGATIVE_Y = GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
18         POSITIVE_Z = GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
19         NEGATIVE_Z = GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
20 };
21
22 /**
23 Cube map texture, consisting of six square faces.  All of the faces must be of
24 the same size.  A cube map texture is addressed by three-dimensional texture
25 coordinates, with a principal range of [-1, 1].  The face is first selected
26 according to the largest coordinate, and the remaining two coordinates are used
27 to sample the face image.  The images are oriented so that the cross product of
28 the s and t axes will point into the cube.
29
30 All faces of a cube map texture must be allocated for it to be usable.
31
32 Requires OpenGL version 1.3.
33 */
34 class TextureCube: public Texture
35 {
36 public:
37         class Loader: public Msp::DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>
38         {
39         public:
40                 Loader(TextureCube &);
41                 Loader(TextureCube &, Collection &);
42         private:
43                 void init();
44
45                 void external_image(TextureCubeFace, const std::string &);
46                 void image_data(TextureCubeFace, const std::string &);
47                 void raw_data(TextureCubeFace, const std::string &);
48                 void storage(PixelFormat, unsigned);
49                 void storage_levels(PixelFormat, unsigned, unsigned);
50         };
51
52 private:
53         unsigned size;
54         unsigned levels;
55         /* Lowest six bits track allocation status of faces on the base level.  Bit
56         seven is set if the entire base level is allocated. */
57         unsigned allocated;
58
59         static TextureCubeFace face_order[6];
60         static Vector3 directions[6];
61         static unsigned orientations[12];
62
63 public:
64         TextureCube();
65
66         /** Defines storage structure for the texture.  If lv is zero, the number
67         of mipmap levels is automatically determined from storage dimensions.
68
69         Must be called before an image can be uploaded.  Once storage is defined,
70         it can't be changed. */
71         void storage(PixelFormat fmt, unsigned size, unsigned lv = 0);
72
73         DEPRECATED void storage(PixelComponents c, unsigned s, unsigned l = 0)
74         { storage(make_pixelformat(c, UNSIGNED_BYTE), s, l); }
75
76         /** Allocates storage for the cube faces.  The contents are initially
77         undefined.  If storage has already been allocated, does nothing. */
78         void allocate(unsigned level);
79
80 private:
81         void allocate_(unsigned);
82
83 public:
84         /** Updates the contents of a face.  Storage must be defined beforehand.
85         The image data must have dimensions and format matching the defined
86         storage. */
87         void image(TextureCubeFace face, unsigned level, const void *data);
88
89 private:
90         void image_(TextureCubeFace, unsigned, const void *);
91
92 public:
93         DEPRECATED void image(TextureCubeFace face, unsigned level,
94                 PixelComponents comp, DataType type, const void *data);
95
96         /** Updates a rectangular region of a face.  Storage must be defined
97         beforehand.  The image data must be in a format mathing the defined storage
98         and the update region must be fully inside the face. */
99         void sub_image(TextureCubeFace face, unsigned level, int x, int y, unsigned w, unsigned h, const void *data);
100
101         DEPRECATED void sub_image(TextureCubeFace face, unsigned level,
102                 int x, int y, unsigned w, unsigned h,
103                 PixelComponents comp, DataType type, const void *data);
104
105         void image(TextureCubeFace, const Graphics::Image &);
106
107         DEPRECATED void image(TextureCubeFace, const Graphics::Image &, bool);
108
109         virtual void image(const Graphics::Image &, unsigned = 0);
110         using Texture::image;
111
112         unsigned get_size() const { return size; }
113 private:
114         unsigned get_n_levels() const;
115         unsigned get_level_size(unsigned) const;
116
117 public:
118         /** Translates indices into face constants.  Valid indices are between 0
119         and 5, inclusive. */
120         static TextureCubeFace enumerate_faces(unsigned);
121
122         static unsigned get_face_index(TextureCubeFace);
123
124         /** Returns a vector pointing out of the face. */
125         static const Vector3 &get_face_direction(TextureCubeFace);
126
127         /** Returns a vector in the direction of the s axis of the face. */
128         static const Vector3 &get_s_direction(TextureCubeFace);
129
130         /** Returns a vector in the direction of the t axis of the face. */
131         static const Vector3 &get_t_direction(TextureCubeFace);
132
133         /** Returns a vector pointing to the center a texel. */
134         Vector3 get_texel_direction(TextureCubeFace, unsigned, unsigned);
135
136         virtual AsyncLoader *load(IO::Seekable &, const Resources * = 0) { return 0; }
137         virtual UInt64 get_data_size() const;
138         virtual void unload() { }
139 };
140
141 void operator>>(const LexicalConverter &, TextureCubeFace &);
142
143 } // namespace GL
144 } // namespace Msp
145
146 #endif