4 #include "texturecube.h"
11 Vector3 TextureCube::directions[6] =
21 TextureCube::TextureCube():
22 Texture(GL_TEXTURE_CUBE_MAP),
26 static RequireVersion _ver(1, 3);
29 void TextureCube::storage(PixelFormat fmt, unsigned sz)
32 throw invalid_operation("TextureCube::storage");
34 throw invalid_argument("TextureCube::storage");
40 void TextureCube::allocate(unsigned level)
42 if(allocated&(1<<level))
45 for(unsigned i=0; i<6; ++i)
46 image(enumerate_faces(i), level, get_base_pixelformat(ifmt), UNSIGNED_BYTE, 0);
49 void TextureCube::image(TextureCubeFace face, unsigned level, PixelFormat fmt, DataType type, const void *data)
52 throw invalid_operation("TextureCube::image");
54 unsigned s = get_level_size(level);
56 throw invalid_argument("TextureCube::image");
58 Bind _bind(this, true);
59 glTexImage2D(face, level, ifmt, s, s, 0, fmt, type, data);
61 // XXX Allocation should be tracked per-face, but we'll run out of bits
62 allocated |= 1<<level;
63 if(gen_mipmap && level==0)
65 for(; s; s>>=1, ++level) ;
66 allocated |= (1<<level)-1;
70 unsigned TextureCube::get_level_size(unsigned level)
75 TextureCubeFace TextureCube::enumerate_faces(unsigned i)
79 case 0: return POSITIVE_X;
80 case 1: return NEGATIVE_X;
81 case 2: return POSITIVE_Y;
82 case 3: return NEGATIVE_Y;
83 case 4: return POSITIVE_Z;
84 case 5: return NEGATIVE_Z;
85 default: throw out_of_range("TextureCube::enumerate_faces");
89 const Vector3 &TextureCube::get_face_direction(TextureCubeFace face)
93 case POSITIVE_X: return directions[0];
94 case NEGATIVE_X: return directions[1];
95 case POSITIVE_Y: return directions[2];
96 case NEGATIVE_Y: return directions[3];
97 case POSITIVE_Z: return directions[4];
98 case NEGATIVE_Z: return directions[5];
99 default: throw invalid_argument("TextureCube::get_face_direction");
103 const Vector3 &TextureCube::get_s_direction(TextureCubeFace face)
107 case POSITIVE_X: return directions[5];
108 case NEGATIVE_X: return directions[4];
109 case POSITIVE_Y: return directions[0];
110 case NEGATIVE_Y: return directions[0];
111 case POSITIVE_Z: return directions[0];
112 case NEGATIVE_Z: return directions[1];
113 default: throw invalid_argument("TextureCube::get_s_direction");
117 const Vector3 &TextureCube::get_t_direction(TextureCubeFace face)
121 case POSITIVE_X: return directions[3];
122 case NEGATIVE_X: return directions[3];
123 case POSITIVE_Y: return directions[4];
124 case NEGATIVE_Y: return directions[5];
125 case POSITIVE_Z: return directions[3];
126 case NEGATIVE_Z: return directions[3];
127 default: throw invalid_argument("TextureCube::get_t_direction");
131 Vector3 TextureCube::get_texel_direction(TextureCubeFace face, unsigned u, unsigned v)
133 float s = (u+0.5f)*2.0f/size-1.0f;
134 float t = (v+0.5f)*2.0f/size-1.0f;
135 const Vector3 &fv = get_face_direction(face);
136 const Vector3 &sv = get_s_direction(face);
137 const Vector3 &tv = get_t_direction(face);
138 return Vector3(fv.x+s*sv.x, fv.y+t*tv.y, fv.z+s*sv.z+t*tv.z);