10 static int glifmt_from_ifmt(unsigned int ifmt);
11 static int glfmt_from_ifmt(unsigned int ifmt);
12 static int gltype_from_ifmt(unsigned int ifmt);
14 static int glifmt_from_imgfmt(Image::Format fmt);
16 static unsigned int type_to_target(TextureType type);
17 static TextureType target_to_type(unsigned int targ);
19 static unsigned int cur_target[8] = {
20 GL_TEXTURE_2D, GL_TEXTURE_2D, GL_TEXTURE_2D, GL_TEXTURE_2D,
21 GL_TEXTURE_2D, GL_TEXTURE_2D, GL_TEXTURE_2D, GL_TEXTURE_2D
24 static unsigned int cube_faces[] = {
25 GL_TEXTURE_CUBE_MAP_POSITIVE_X,
26 GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
27 GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
28 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
29 GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
30 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
34 void bind_texture(Texture *tex, int tunit)
39 glActiveTexture(GL_TEXTURE0 + tunit);
40 glBindTexture(cur_target[tunit], 0);
41 glDisable(cur_target[tunit]);
42 assert(glGetError() == GL_NO_ERROR);
43 glActiveTexture(GL_TEXTURE0);
48 Image *Texture::default_img;
53 sz[0] = sz[1] = sz[2] = 0;
57 glGenTextures(1, &id);
63 glDeleteTextures(1, &id);
70 void Texture::set_wrapping(unsigned int wrap)
76 glBindTexture(target, id);
77 assert(glGetError() == GL_NO_ERROR);
78 glTexParameteri(target, GL_TEXTURE_WRAP_S, wrap);
79 glTexParameteri(target, GL_TEXTURE_WRAP_T, wrap);
80 glTexParameteri(target, GL_TEXTURE_WRAP_R, wrap);
83 void Texture::set_filtering(unsigned int filt)
85 unsigned int mag_filter;
92 case GL_LINEAR_MIPMAP_NEAREST:
93 case GL_LINEAR_MIPMAP_LINEAR:
94 mag_filter = GL_LINEAR;
97 case GL_NEAREST_MIPMAP_NEAREST:
98 case GL_NEAREST_MIPMAP_LINEAR:
99 mag_filter = GL_NEAREST;
106 set_filtering(filt, mag_filter);
109 void Texture::set_filtering(unsigned int min_filt, unsigned int mag_filt)
111 glBindTexture(target, id);
112 assert(glGetError() == GL_NO_ERROR);
113 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, min_filt);
114 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, mag_filt);
117 unsigned int Texture::get_format() const
122 int Texture::get_size(int dim) const
124 if(dim < 0 || dim >= 3) {
130 unsigned int Texture::get_id() const
135 TextureType Texture::get_type() const
137 return target_to_type(target);
140 void Texture::bind(int tex_unit) const
142 glActiveTexture(GL_TEXTURE0 + tex_unit);
144 glBindTexture(target, id);
145 assert(glGetError() == GL_NO_ERROR);
146 glActiveTexture(GL_TEXTURE0);
148 cur_target[tex_unit] = target;
152 void Texture::create(int xsz, int ysz, TextureType textype, unsigned int ifmt)
154 if(textype == TEX_CUBE && xsz != ysz) {
155 error_log("trying to create cubemap with different width and height (%dx%d)\n", xsz, ysz);
159 int fmt = glfmt_from_ifmt(ifmt);
160 int type = gltype_from_ifmt(ifmt);
162 target = type_to_target(textype);
164 glBindTexture(target, id);
165 assert(glGetError() == GL_NO_ERROR);
166 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
167 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
171 glTexImage2D(GL_TEXTURE_2D, 0, glifmt_from_ifmt(ifmt), xsz, ysz, 0, fmt, type, 0);
175 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
176 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
177 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
178 for(int i=0; i<6; i++) {
179 glTexImage2D(cube_faces[i], 0, ifmt, xsz, ysz, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
189 #define DEF_IMAGE_SIZE 64
190 void Texture::create_default(TextureType type)
193 default_img = new Image;
194 default_img->create(DEF_IMAGE_SIZE, DEF_IMAGE_SIZE, Image::FMT_RGBA);
196 unsigned char *pixels = (unsigned char*)default_img->get_pixels();
197 for(int i=0; i<DEF_IMAGE_SIZE; i++) {
198 for(int j=0; j<DEF_IMAGE_SIZE; j++) {
199 bool chess = ((i >> 3) & 1) == ((j >> 3) & 1);
200 pixels[0] = chess ? 255 : 32;
202 pixels[2] = chess ? 32 : 255;
211 set_image(*default_img);
215 for(int i=0; i<6; i++) {
216 set_image(*default_img, i);
222 void Texture::set_image(const Image &img, int idx)
224 if(idx >= 0 && idx < 6) {
225 set_image_cube(img, idx);
227 if(!set_image_cube(img)) {
233 void Texture::set_image_2d(const Image &img)
235 texfmt = glifmt_from_imgfmt(img.get_format());
236 unsigned int fmt = glfmt_from_ifmt(texfmt);
237 unsigned int type = gltype_from_ifmt(texfmt);
239 sz[0] = img.get_width();
240 sz[1] = img.get_height();
242 target = GL_TEXTURE_2D;
243 glBindTexture(target, id);
244 assert(glGetError() == GL_NO_ERROR);
245 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
246 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
247 glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_REPEAT);
248 glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_REPEAT);
250 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
253 if(GLEW_SGIS_generate_mipmap) {
254 glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
256 glTexImage2D(target, 0, texfmt, sz[0], sz[1], 0, fmt, type, img.get_pixels());
259 gluBuild2DMipmaps(target, texfmt, sz[0], sz[1], fmt, type, img.get_pixels());
263 #ifdef GL_ES_VERSION_2_0
264 glGenerateMipmap(target);
268 bool Texture::set_image_cube(const Image &img, int idx)
271 if(idx < 0 || idx >= 6) {
275 texfmt = glifmt_from_imgfmt(img.get_format());
276 unsigned int fmt = glfmt_from_ifmt(texfmt);
277 unsigned int type = gltype_from_ifmt(texfmt);
279 sz[0] = img.get_width();
280 sz[1] = img.get_height();
282 target = GL_TEXTURE_CUBE_MAP;
283 glBindTexture(target, id);
284 if((err = glGetError()) == GL_INVALID_OPERATION) {
285 return false; // probably not a cubemap
287 assert(err == GL_NO_ERROR);
288 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
289 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
290 glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
291 glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
292 glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
294 glTexImage2D(cube_faces[idx], 0, texfmt, sz[0], sz[1], 0, fmt, type, img.get_pixels());
298 bool Texture::set_image_cube(const Image &img)
300 static const float one_third = 1.0 / 3.0;
301 static const float two_thirds = 2.0 / 3.0;
302 static const float hcross[2][6] = {
303 {0.5, 0.0, 0.25, 0.25, 0.25, 0.75}, {one_third, one_third, 0.0, two_thirds, one_third, one_third} };
304 static const float vcross[2][6] = {
305 {two_thirds, 0.0, one_third, one_third, one_third, one_third}, {0.25, 0.25, 0.0, 0.5, 0.25, 0.75} };
306 static const float hsix[2][6] = {
307 {0.0, 0.0, one_third, one_third, two_thirds, two_thirds}, {0.0, 0.5, 0.0, 0.5, 0.0, 0.5} };
309 int xsz = img.get_width();
310 int ysz = img.get_height();
312 if((xsz << 8) / 4 == (ysz << 8) / 3) {
313 // horizontal cross, assume the vertical bit is center-left
314 return set_cube_multi(img, hcross[0], hcross[1], xsz / 4);
316 if((xsz << 8) / 3 == (ysz << 8) / 4) {
317 // vertical cross, assume the horizontal bit is center-top (180-rotated image 5)
318 return set_cube_multi(img, vcross[0], vcross[1], ysz / 4, (1 << 5));
320 if((xsz << 8) / 3 == (ysz << 8) / 2) {
321 // horizontal sixpack
322 return set_cube_multi(img, hsix[0], hsix[1], ysz / 2);
329 bool Texture::load(const char *fname)
332 if(!img.load(fname)) {
333 error_log("failed to load 2D texture: %s\n", fname);
338 info_log("loaded 2D texture: %s\n", fname);
342 bool Texture::load_cube(const char *fname)
345 if(!img.load(fname)) {
348 return set_image_cube(img);
351 bool Texture::set_cube_multi(const Image &img, const float *xoffsets, const float *yoffsets, float sz,
352 unsigned int rotmask)
354 for(int i=0; i<6; i++) {
357 int xoffs = xoffsets[i] * img.get_width();
358 int yoffs = yoffsets[i] * img.get_height();
360 if(!face.set_pixels(sz, sz, img.get_pixels(), xoffs, yoffs, img.get_width(), img.get_format())) {
364 if(rotmask & (1 << i)) {
367 set_image_cube(face, i);
372 static int glifmt_from_ifmt(unsigned int ifmt)
374 #ifdef GL_ES_VERSION_2_0
376 case GL_LUMINANCE16F_ARB:
377 case GL_LUMINANCE32F_ARB:
395 return ifmt; // by default just pass it through...
398 static int glfmt_from_ifmt(unsigned int ifmt)
401 case GL_LUMINANCE16F_ARB:
402 case GL_LUMINANCE32F_ARB:
422 static int gltype_from_ifmt(unsigned int ifmt)
427 case GL_LUMINANCE16F_ARB:
428 #ifdef GL_ES_VERSION_2_0
429 return GL_HALF_FLOAT_OES;
433 case GL_LUMINANCE32F_ARB:
439 return GL_UNSIGNED_BYTE;
442 static int glifmt_from_imgfmt(Image::Format fmt)
445 case Image::FMT_GREY:
446 return GL_SLUMINANCE;
447 case Image::FMT_GREY_FLOAT:
448 return GL_LUMINANCE16F_ARB;
451 case Image::FMT_RGB_FLOAT:
453 case Image::FMT_RGBA:
454 return GL_SRGB_ALPHA;
455 case Image::FMT_RGBA_FLOAT:
463 static unsigned int type_to_target(TextureType type)
465 return type == TEX_CUBE ? GL_TEXTURE_CUBE_MAP : GL_TEXTURE_2D;
468 static TextureType target_to_type(unsigned int targ)
470 return targ == GL_TEXTURE_CUBE_MAP ? TEX_CUBE : TEX_2D;
473 // ---- TextureSet ----
474 TextureSet::TextureSet()
475 : DataSet<Texture*>(create_tex, load_tex, done_tex, free_tex)
479 Texture *TextureSet::get_texture(const char *name, TextureType type, const DataMap *dmap) const
482 int nsize = dmap ? dmap->path_size(name) : 0;
484 fname = (char*)alloca(nsize);
485 dmap->lookup(name, fname, nsize);
486 //debug_log("texture lookup: %s -> %s\n", name, fname);
489 //debug_log("texture lookup failed, using: %s\n", fname);
492 std::map<std::string, Texture*>::const_iterator iter = data.find(fname);
493 if(iter != data.end()) {
497 Texture *res = create();
499 res->create_default(type);
500 resman_lookup(rman, fname, res);
506 Texture *TextureSet::create_tex()
511 bool TextureSet::load_tex(Texture *tex, const char *fname)
513 Image *img = new Image;
515 if(!img->load(fname)) {
526 bool TextureSet::done_tex(Texture *tex)
528 //debug_log("TextureSet::done_tex [%s]\n", tex->img->name.c_str());
533 tex->set_image(*tex->img);
537 void TextureSet::free_tex(Texture *tex)