7 static int glifmt_from_ifmt(unsigned int ifmt);
8 static int glfmt_from_ifmt(unsigned int ifmt);
9 static int gltype_from_ifmt(unsigned int ifmt);
11 static int glifmt_from_imgfmt(Image::Format fmt);
13 static unsigned int type_to_target(TextureType type);
14 static TextureType target_to_type(unsigned int targ);
16 static unsigned int cur_target[8] = {
17 GL_TEXTURE_2D, GL_TEXTURE_2D, GL_TEXTURE_2D, GL_TEXTURE_2D,
18 GL_TEXTURE_2D, GL_TEXTURE_2D, GL_TEXTURE_2D, GL_TEXTURE_2D
21 static unsigned int cube_faces[] = {
22 GL_TEXTURE_CUBE_MAP_POSITIVE_X,
23 GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
24 GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
25 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
26 GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
27 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
31 void bind_texture(Texture *tex, int tunit)
36 glActiveTexture(GL_TEXTURE0 + tunit);
37 glBindTexture(cur_target[tunit], 0);
38 glActiveTexture(GL_TEXTURE0);
43 Image *Texture::default_img;
48 sz[0] = sz[1] = sz[2] = 0;
52 glGenTextures(1, &id);
58 glDeleteTextures(1, &id);
65 void Texture::set_wrapping(unsigned int wrap)
71 glBindTexture(target, id);
72 glTexParameteri(target, GL_TEXTURE_WRAP_S, wrap);
73 glTexParameteri(target, GL_TEXTURE_WRAP_T, wrap);
74 glTexParameteri(target, GL_TEXTURE_WRAP_R, wrap);
77 void Texture::set_filtering(unsigned int filt)
79 unsigned int mag_filter;
86 case GL_LINEAR_MIPMAP_NEAREST:
87 case GL_LINEAR_MIPMAP_LINEAR:
88 mag_filter = GL_LINEAR;
91 case GL_NEAREST_MIPMAP_NEAREST:
92 case GL_NEAREST_MIPMAP_LINEAR:
93 mag_filter = GL_NEAREST;
100 set_filtering(filt, mag_filter);
103 void Texture::set_filtering(unsigned int min_filt, unsigned int mag_filt)
105 glBindTexture(target, id);
106 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, min_filt);
107 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, mag_filt);
110 unsigned int Texture::get_format() const
115 int Texture::get_size(int dim) const
117 if(dim < 0 || dim >= 3) {
123 unsigned int Texture::get_id() const
128 TextureType Texture::get_type() const
130 return target_to_type(target);
133 void Texture::bind(int tex_unit) const
135 glActiveTexture(GL_TEXTURE0 + tex_unit);
136 glBindTexture(target, id);
137 glActiveTexture(GL_TEXTURE0);
139 cur_target[tex_unit] = target;
143 void Texture::create(int xsz, int ysz, TextureType textype, unsigned int ifmt)
145 if(textype == TEX_CUBE && xsz != ysz) {
146 fprintf(stderr, "trying to create cubemap with different width and height (%dx%d)\n", xsz, ysz);
150 int fmt = glfmt_from_ifmt(ifmt);
151 int type = gltype_from_ifmt(ifmt);
153 target = type_to_target(textype);
155 glBindTexture(target, id);
156 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
157 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
161 glTexImage2D(GL_TEXTURE_2D, 0, glifmt_from_ifmt(ifmt), xsz, ysz, 0, fmt, type, 0);
165 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
166 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
167 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
168 for(int i=0; i<6; i++) {
169 glTexImage2D(cube_faces[i], 0, ifmt, xsz, ysz, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
179 #define DEF_IMAGE_SIZE 64
180 void Texture::create_default(TextureType type)
183 default_img = new Image;
184 default_img->create(DEF_IMAGE_SIZE, DEF_IMAGE_SIZE, Image::FMT_RGBA);
186 unsigned char *pixels = (unsigned char*)default_img->get_pixels();
187 for(int i=0; i<DEF_IMAGE_SIZE; i++) {
188 for(int j=0; j<DEF_IMAGE_SIZE; j++) {
189 bool chess = ((i >> 3) & 1) == ((j >> 3) & 1);
190 pixels[0] = chess ? 255 : 32;
192 pixels[2] = chess ? 32 : 255;
200 set_image(*default_img);
204 for(int i=0; i<6; i++) {
205 set_image(*default_img, i);
211 void Texture::set_image(const Image &img, int idx)
213 if(idx >= 0 && idx < 6) {
214 set_image_cube(img, idx);
216 if(!set_image_cube(img)) {
222 void Texture::set_image_2d(const Image &img)
224 texfmt = glifmt_from_imgfmt(img.get_format());
225 unsigned int fmt = glfmt_from_ifmt(texfmt);
226 unsigned int type = gltype_from_ifmt(texfmt);
228 sz[0] = img.get_width();
229 sz[1] = img.get_height();
231 target = GL_TEXTURE_2D;
232 glBindTexture(target, id);
233 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
234 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
235 glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_REPEAT);
236 glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_REPEAT);
239 if(GLEW_SGIS_generate_mipmap) {
240 glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
242 glTexImage2D(target, 0, texfmt, sz[0], sz[1], 0, fmt, type, img.get_pixels());
245 gluBuild2DMipmaps(target, texfmt, sz[0], sz[1], fmt, type, img.get_pixels());
249 #ifdef GL_ES_VERSION_2_0
250 glGenerateMipmap(target);
254 bool Texture::set_image_cube(const Image &img, int idx)
256 if(idx < 0 || idx >= 6) {
260 texfmt = glifmt_from_imgfmt(img.get_format());
261 unsigned int fmt = glfmt_from_ifmt(texfmt);
262 unsigned int type = gltype_from_ifmt(texfmt);
264 sz[0] = img.get_width();
265 sz[1] = img.get_height();
267 target = GL_TEXTURE_CUBE_MAP;
268 glBindTexture(target, id);
269 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
270 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
271 glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
272 glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
273 glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
275 glTexImage2D(cube_faces[idx], 0, texfmt, sz[0], sz[1], 0, fmt, type, img.get_pixels());
279 bool Texture::set_image_cube(const Image &img)
281 static const float one_third = 1.0 / 3.0;
282 static const float two_thirds = 2.0 / 3.0;
283 static const float hcross[2][6] = {
284 {0.5, 0.0, 0.25, 0.25, 0.25, 0.75}, {one_third, one_third, 0.0, two_thirds, one_third, one_third} };
285 static const float vcross[2][6] = {
286 {two_thirds, 0.0, one_third, one_third, one_third, one_third}, {0.25, 0.25, 0.0, 0.5, 0.25, 0.75} };
287 static const float hsix[2][6] = {
288 {0.0, 0.0, one_third, one_third, two_thirds, two_thirds}, {0.0, 0.5, 0.0, 0.5, 0.0, 0.5} };
290 int xsz = img.get_width();
291 int ysz = img.get_height();
293 if(xsz / 4 == ysz / 3) {
294 // horizontal cross, assume the vertical bit is center-left
295 return set_cube_multi(img, hcross[0], hcross[1], xsz / 4);
297 if(xsz / 3 == ysz / 4) {
298 // vertical cross, assume the horizontal bit is center-top (180-rotated image 5)
299 return set_cube_multi(img, vcross[0], vcross[1], ysz / 4, (1 << 5));
301 if(xsz / 3 == ysz / 2) {
302 // horizontal sixpack
303 return set_cube_multi(img, hsix[0], hsix[1], ysz / 2);
310 bool Texture::load(const char *fname)
313 if(!img.load(fname)) {
314 fprintf(stderr, "failed to load 2D texture: %s\n", fname);
319 printf("loaded 2D texture: %s\n", fname);
323 bool Texture::load_cube(const char *fname)
326 if(!img.load(fname)) {
329 return set_image_cube(img);
332 bool Texture::set_cube_multi(const Image &img, const float *xoffsets, const float *yoffsets, float sz,
333 unsigned int rotmask)
335 for(int i=0; i<6; i++) {
338 int xoffs = xoffsets[i] * img.get_width();
339 int yoffs = yoffsets[i] * img.get_height();
341 if(!face.set_pixels(sz, sz, img.get_pixels(), xoffs, yoffs, img.get_width(), img.get_format())) {
345 if(rotmask & (1 << i)) {
348 set_image_cube(face, i);
353 static int glifmt_from_ifmt(unsigned int ifmt)
355 #ifdef GL_ES_VERSION_2_0
357 case GL_LUMINANCE16F_ARB:
358 case GL_LUMINANCE32F_ARB:
376 return ifmt; // by default just pass it through...
379 static int glfmt_from_ifmt(unsigned int ifmt)
382 case GL_LUMINANCE16F_ARB:
383 case GL_LUMINANCE32F_ARB:
400 static int gltype_from_ifmt(unsigned int ifmt)
405 case GL_LUMINANCE16F_ARB:
406 #ifdef GL_ES_VERSION_2_0
407 return GL_HALF_FLOAT_OES;
411 case GL_LUMINANCE32F_ARB:
417 return GL_UNSIGNED_BYTE;
420 static int glifmt_from_imgfmt(Image::Format fmt)
423 case Image::FMT_GREY:
425 case Image::FMT_GREY_FLOAT:
426 return GL_LUMINANCE16F_ARB;
429 case Image::FMT_RGB_FLOAT:
431 case Image::FMT_RGBA:
433 case Image::FMT_RGBA_FLOAT:
441 static unsigned int type_to_target(TextureType type)
443 return type == TEX_CUBE ? GL_TEXTURE_CUBE_MAP : GL_TEXTURE_2D;
446 static TextureType target_to_type(unsigned int targ)
448 return targ == GL_TEXTURE_CUBE_MAP ? TEX_CUBE : TEX_2D;
451 // ---- TextureSet ----
452 TextureSet::TextureSet()
453 : DataSet<Texture*>(create_tex, load_tex, done_tex, free_tex)
457 Texture *TextureSet::get_texture(const char *name, TextureType type) const
459 typename std::map<std::string, Texture*>::const_iterator iter = data.find(name);
460 if(iter != data.end()) {
464 Texture *res = create();
465 res->create_default(type);
466 resman_add(rman, name, res);
472 Texture *TextureSet::create_tex()
477 bool TextureSet::load_tex(Texture *tex, const char *fname)
479 Image *img = new Image;
480 if(!img->load(fname)) {
491 bool TextureSet::done_tex(Texture *tex)
497 tex->set_image(*tex->img);
501 void TextureSet::free_tex(Texture *tex)