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