]> git.tdb.fi Git - libs/gl.git/blob - source/texturecube.h
Lookup functions to determine the orientation of cube map faces
[libs/gl.git] / source / texturecube.h
1 #ifndef MSP_GL_TEXTURECUBE_H_
2 #define MSP_GL_TEXTURECUBE_H_
3
4 #include "datatype.h"
5 #include "pixelformat.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 private:
37         PixelFormat ifmt;
38         unsigned size;
39         unsigned allocated;
40
41         static Vector3 directions[6];
42
43 public:
44         TextureCube();
45
46         /** Defines storage structure for the texture.  Must be called before an
47         image can be uploaded.  Once storage is defined, it can't be changed. */
48         void storage(PixelFormat fmt, unsigned size);
49
50         /** Allocates storage for the cube faces.  The contents are initially
51         undefined.  If storage has already been allocated, does nothing. */
52         void allocate(unsigned level);
53
54         /** Uploads image data to a face.  Storage must be defined beforehand.  The
55         image data must have dimensions and format compatible with the defined
56         storage. */
57         void image(TextureCubeFace face, unsigned level,
58                 PixelFormat fmt, DataType type, const void *data);
59
60         /** Updates a rectangular region of a face.  Storage must be defined and
61         allocated beforehand.  The update region must be fully inside the texture.
62         The data format must be compatible with the defined storage. */
63         void sub_image(TextureCubeFace face, unsigned level,
64                 int x, int y, unsigned w, unsigned h,
65                 PixelFormat fmt, DataType type, const void *data);
66
67         unsigned get_size() const { return size; }
68 private:
69         unsigned get_level_size(unsigned);
70
71 public:
72         /** Translates indices into face constants.  Valid indices are between 0
73         and 5, inclusive. */
74         static TextureCubeFace enumerate_faces(unsigned);
75
76         /** Returns a vector pointing out of the face. */
77         static const Vector3 &get_face_direction(TextureCubeFace);
78
79         /** Returns a vector in the direction of the s axis of the face. */
80         static const Vector3 &get_s_direction(TextureCubeFace);
81
82         /** Returns a vector in the direction of the t axis of the face. */
83         static const Vector3 &get_t_direction(TextureCubeFace);
84
85         /** Returns a vector pointing to the center a texel. */
86         Vector3 get_texel_direction(TextureCubeFace, unsigned, unsigned);
87 };
88
89 } // namespace GL
90 } // namespace Msp
91
92 #endif