goat3dgfx

annotate src/texture.cc @ 9:25b911c7c35c

fixed some line endings fixed the cubemap2.jpg file in examples/cubemap/data which was resized improperly causing seams...
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 18 Nov 2013 04:10:19 +0200
parents 3d96734fd477
children 7d6b667821cf
rev   line source
nuclear@9 1 #include <math.h>
nuclear@0 2 #include "texture.h"
nuclear@0 3 #include "image.h"
nuclear@0 4 #include "opengl.h"
nuclear@0 5 #include "imago2.h"
nuclear@0 6 #include "logger.h"
nuclear@0 7 #include "datapath.h"
nuclear@0 8
nuclear@0 9 static int glifmt_from_ifmt(unsigned int ifmt);
nuclear@0 10 static int glfmt_from_ifmt(unsigned int ifmt);
nuclear@0 11 static int gltype_from_ifmt(unsigned int ifmt);
nuclear@0 12
nuclear@0 13 static int glifmt_from_imgfmt(Image::Format fmt);
nuclear@0 14
nuclear@0 15 static unsigned int cur_target[8] = {
nuclear@0 16 GL_TEXTURE_2D, GL_TEXTURE_2D, GL_TEXTURE_2D, GL_TEXTURE_2D,
nuclear@0 17 GL_TEXTURE_2D, GL_TEXTURE_2D, GL_TEXTURE_2D, GL_TEXTURE_2D
nuclear@0 18 };
nuclear@0 19
nuclear@0 20 void set_texture(Texture *tex, int tunit)
nuclear@0 21 {
nuclear@0 22 if(tex) {
nuclear@0 23 tex->bind(tunit);
nuclear@0 24 } else {
nuclear@0 25 glActiveTexture(GL_TEXTURE0 + tunit);
nuclear@0 26 glBindTexture(cur_target[tunit], 0);
nuclear@0 27 glActiveTexture(GL_TEXTURE0);
nuclear@0 28 }
nuclear@0 29 }
nuclear@0 30
nuclear@0 31 Texture *load_texture(const char *fname)
nuclear@0 32 {
nuclear@0 33 TextureCube *texcube = new TextureCube;
nuclear@0 34 if(texcube->load(fname)) {
nuclear@0 35 return texcube;
nuclear@0 36 }
nuclear@0 37 delete texcube;
nuclear@0 38
nuclear@0 39 Texture2D *tex = new Texture2D;
nuclear@0 40 if(tex->load(fname)) {
nuclear@0 41 return tex;
nuclear@0 42 }
nuclear@0 43 delete tex;
nuclear@0 44 return 0;
nuclear@0 45 }
nuclear@0 46
nuclear@0 47
nuclear@0 48 Texture::Texture()
nuclear@0 49 {
nuclear@0 50 target = 0;
nuclear@0 51 sz[0] = sz[1] = sz[2] = 0;
nuclear@0 52 texfmt = 0;
nuclear@0 53
nuclear@0 54 glGenTextures(1, &id);
nuclear@0 55 }
nuclear@0 56
nuclear@0 57 Texture::~Texture()
nuclear@0 58 {
nuclear@0 59 if(id) {
nuclear@0 60 glDeleteTextures(1, &id);
nuclear@0 61 }
nuclear@0 62 }
nuclear@0 63
nuclear@0 64 void Texture::set_wrapping(unsigned int wrap)
nuclear@0 65 {
nuclear@0 66 if(!target) {
nuclear@0 67 return;
nuclear@0 68 }
nuclear@0 69
nuclear@0 70 glBindTexture(target, id);
nuclear@0 71 glTexParameteri(target, GL_TEXTURE_WRAP_S, wrap);
nuclear@0 72 glTexParameteri(target, GL_TEXTURE_WRAP_T, wrap);
nuclear@0 73 }
nuclear@0 74
nuclear@0 75 void Texture::set_filtering(unsigned int filt)
nuclear@0 76 {
nuclear@0 77 unsigned int mag_filter;
nuclear@0 78
nuclear@0 79 if(!target) {
nuclear@0 80 return;
nuclear@0 81 }
nuclear@0 82
nuclear@0 83 switch(filt) {
nuclear@0 84 case GL_LINEAR_MIPMAP_NEAREST:
nuclear@0 85 case GL_LINEAR_MIPMAP_LINEAR:
nuclear@0 86 mag_filter = GL_LINEAR;
nuclear@0 87 break;
nuclear@0 88
nuclear@0 89 case GL_NEAREST_MIPMAP_NEAREST:
nuclear@0 90 case GL_NEAREST_MIPMAP_LINEAR:
nuclear@0 91 mag_filter = GL_NEAREST;
nuclear@0 92 break;
nuclear@0 93
nuclear@0 94 default:
nuclear@0 95 mag_filter = filt;
nuclear@0 96 }
nuclear@0 97
nuclear@0 98 set_filtering(filt, mag_filter);
nuclear@0 99 }
nuclear@0 100
nuclear@0 101 void Texture::set_filtering(unsigned int min_filt, unsigned int mag_filt)
nuclear@0 102 {
nuclear@0 103 glBindTexture(target, id);
nuclear@0 104 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, min_filt);
nuclear@0 105 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, mag_filt);
nuclear@0 106 }
nuclear@0 107
nuclear@0 108 unsigned int Texture::get_format() const
nuclear@0 109 {
nuclear@0 110 return texfmt;
nuclear@0 111 }
nuclear@0 112
nuclear@0 113 int Texture::get_size(int dim) const
nuclear@0 114 {
nuclear@0 115 if(dim < 0 || dim >= 3) {
nuclear@0 116 return 0;
nuclear@0 117 }
nuclear@0 118 return sz[dim];
nuclear@0 119 }
nuclear@0 120
nuclear@0 121 unsigned int Texture::get_id() const
nuclear@0 122 {
nuclear@0 123 return id;
nuclear@0 124 }
nuclear@0 125
nuclear@0 126 void Texture::bind(int tex_unit) const
nuclear@0 127 {
nuclear@0 128 glActiveTexture(GL_TEXTURE0 + tex_unit);
nuclear@0 129 glBindTexture(target, id);
nuclear@0 130 glActiveTexture(GL_TEXTURE0);
nuclear@0 131
nuclear@0 132 cur_target[tex_unit] = target;
nuclear@0 133 }
nuclear@0 134
nuclear@0 135
nuclear@0 136 // ---- Texture2D ----
nuclear@0 137
nuclear@0 138 Texture2D::Texture2D()
nuclear@0 139 {
nuclear@0 140 target = GL_TEXTURE_2D;
nuclear@0 141 }
nuclear@0 142
nuclear@0 143 void Texture2D::create(int xsz, int ysz, unsigned int ifmt)
nuclear@0 144 {
nuclear@0 145 int fmt = glfmt_from_ifmt(ifmt);
nuclear@0 146 int type = gltype_from_ifmt(ifmt);
nuclear@0 147
nuclear@0 148 glBindTexture(GL_TEXTURE_2D, id);
nuclear@0 149 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
nuclear@0 150 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
nuclear@0 151 glTexImage2D(GL_TEXTURE_2D, 0, glifmt_from_ifmt(ifmt), xsz, ysz, 0, fmt, type, 0);
nuclear@0 152 CHECKGLERR;
nuclear@0 153 sz[0] = xsz;
nuclear@0 154 sz[1] = ysz;
nuclear@0 155 texfmt = ifmt;
nuclear@0 156 }
nuclear@0 157
nuclear@0 158 void Texture2D::set_image(const Image &img, int idx)
nuclear@0 159 {
nuclear@0 160 texfmt = glifmt_from_imgfmt(img.get_format());
nuclear@0 161 unsigned int fmt = glfmt_from_ifmt(texfmt);
nuclear@0 162 unsigned int type = gltype_from_ifmt(texfmt);
nuclear@0 163
nuclear@0 164 sz[0] = img.get_width();
nuclear@0 165 sz[1] = img.get_height();
nuclear@0 166
nuclear@0 167 glBindTexture(GL_TEXTURE_2D, id);
nuclear@0 168 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
nuclear@0 169 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
nuclear@0 170 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
nuclear@0 171 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
nuclear@0 172
nuclear@0 173 #ifdef __GLEW_H__
nuclear@0 174 if(GLEW_SGIS_generate_mipmap) {
nuclear@0 175 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
nuclear@0 176 #endif
nuclear@0 177 glTexImage2D(GL_TEXTURE_2D, 0, texfmt, sz[0], sz[1], 0, fmt, type, img.get_pixels());
nuclear@0 178 #ifdef __GLEW_H__
nuclear@0 179 } else {
nuclear@0 180 gluBuild2DMipmaps(GL_TEXTURE_2D, texfmt, sz[0], sz[1], fmt, type, img.get_pixels());
nuclear@0 181 }
nuclear@0 182 #endif
nuclear@0 183
nuclear@0 184 #ifdef GL_ES_VERSION_2_0
nuclear@0 185 glGenerateMipmap(GL_TEXTURE_2D);
nuclear@0 186 #endif
nuclear@0 187 }
nuclear@0 188
nuclear@0 189 bool Texture2D::load(const char *fname)
nuclear@0 190 {
nuclear@0 191 Image img;
nuclear@2 192 if(!img.load(fname)) {
nuclear@0 193 error_log("failed to load 2D texture: %s\n", fname);
nuclear@0 194 return false;
nuclear@0 195 }
nuclear@0 196 set_image(img);
nuclear@0 197
nuclear@0 198 info_log("loaded 2D texture: %s\n", fname);
nuclear@0 199 return true;
nuclear@0 200 }
nuclear@0 201
nuclear@0 202 bool Texture2D::save(const char *fname) const
nuclear@0 203 {
nuclear@0 204 #ifndef GL_ES_VERSION_2_0
nuclear@0 205 unsigned char *pixels = new unsigned char[sz[0] * sz[1] * 4];
nuclear@0 206
nuclear@0 207 glBindTexture(GL_TEXTURE_2D, id);
nuclear@0 208 glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
nuclear@0 209
nuclear@0 210 if(img_save_pixels(fname, pixels, sz[0], sz[1]) == -1) {
nuclear@0 211 error_log("failed to save 2D texture: %s\n", fname);
nuclear@0 212 delete [] pixels;
nuclear@0 213 return false;
nuclear@0 214 }
nuclear@0 215
nuclear@0 216 info_log("saved 2D texture: %s\n", fname);
nuclear@0 217 delete [] pixels;
nuclear@0 218 return true;
nuclear@0 219 #else
nuclear@0 220 return false; // TODO
nuclear@0 221 #endif
nuclear@0 222 }
nuclear@0 223
nuclear@0 224 // ---- TextureCube ----
nuclear@0 225 static unsigned int cube_faces[] = {
nuclear@0 226 GL_TEXTURE_CUBE_MAP_POSITIVE_X,
nuclear@0 227 GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
nuclear@0 228 GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
nuclear@0 229 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
nuclear@0 230 GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
nuclear@0 231 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
nuclear@0 232 };
nuclear@0 233
nuclear@0 234 TextureCube::TextureCube()
nuclear@0 235 {
nuclear@0 236 target = GL_TEXTURE_CUBE_MAP;
nuclear@0 237 }
nuclear@0 238
nuclear@0 239 void TextureCube::create(int xsz, int ysz, unsigned int ifmt)
nuclear@0 240 {
nuclear@0 241 if(xsz != ysz) {
nuclear@0 242 error_log("trying to create cubemap with different width and height (%dx%d)\n", xsz, ysz);
nuclear@0 243 return;
nuclear@0 244 }
nuclear@0 245
nuclear@0 246 texfmt = ifmt;
nuclear@0 247
nuclear@0 248 glBindTexture(GL_TEXTURE_CUBE_MAP, id);
nuclear@6 249 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
nuclear@6 250 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
nuclear@6 251 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
nuclear@6 252 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
nuclear@9 253 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
nuclear@0 254
nuclear@0 255 for(int i=0; i<6; i++) {
nuclear@0 256 glTexImage2D(cube_faces[i], 0, ifmt, xsz, ysz, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
nuclear@0 257 }
nuclear@0 258 }
nuclear@0 259
nuclear@0 260 void TextureCube::set_image(const Image &img, int idx)
nuclear@0 261 {
nuclear@6 262 texfmt = glifmt_from_imgfmt(img.get_format());
nuclear@6 263 unsigned int fmt = glfmt_from_ifmt(texfmt);
nuclear@6 264 unsigned int type = gltype_from_ifmt(texfmt);
nuclear@6 265
nuclear@6 266 sz[0] = img.get_width();
nuclear@6 267 sz[1] = img.get_height();
nuclear@6 268
nuclear@6 269 glBindTexture(GL_TEXTURE_CUBE_MAP, id);
nuclear@6 270 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
nuclear@6 271 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
nuclear@6 272 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
nuclear@6 273 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
nuclear@9 274 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
nuclear@6 275
nuclear@6 276 glTexImage2D(cube_faces[idx], 0, texfmt, sz[0], sz[1], 0, fmt, type, img.get_pixels());
nuclear@0 277 }
nuclear@0 278
nuclear@0 279 bool TextureCube::load(const char *fname)
nuclear@0 280 {
nuclear@6 281 static const float one_third = 1.0 / 3.0;
nuclear@6 282 static const float two_thirds = 2.0 / 3.0;
nuclear@6 283 static const float hcross[2][6] = {
nuclear@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} };
nuclear@6 285 static const float vcross[2][6] = {
nuclear@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} };
nuclear@6 287 static const float hsix[2][6] = {
nuclear@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} };
nuclear@6 289
nuclear@6 290 Image img;
nuclear@6 291 if(!img.load(fname)) {
nuclear@6 292 return false;
nuclear@6 293 }
nuclear@6 294
nuclear@6 295 int xsz = img.get_width();
nuclear@6 296 int ysz = img.get_height();
nuclear@6 297
nuclear@6 298 if(xsz / 4 == ysz / 3) {
nuclear@6 299 // horizontal cross, assume the vertical bit is center-left
nuclear@6 300 return load_multi(img, hcross[0], hcross[1], xsz / 4);
nuclear@6 301 }
nuclear@6 302 if(xsz / 3 == ysz / 4) {
nuclear@6 303 // vertical cross, assume the horizontal bit is center-top (180-rotated image 5)
nuclear@6 304 return load_multi(img, vcross[0], vcross[1], ysz / 4, (1 << 5));
nuclear@6 305 }
nuclear@6 306 if(xsz / 3 == ysz / 2) {
nuclear@6 307 // horizontal sixpack
nuclear@6 308 return load_multi(img, hsix[0], hsix[1], ysz / 2);
nuclear@6 309 }
nuclear@6 310
nuclear@6 311 error_log("failed to load %s: unknown cubemap configuration\n", fname);
nuclear@6 312 return false;
nuclear@0 313 }
nuclear@0 314
nuclear@0 315 bool TextureCube::save(const char *fname) const
nuclear@0 316 {
nuclear@0 317 return false; // TODO
nuclear@0 318 }
nuclear@0 319
nuclear@6 320 bool TextureCube::load_multi(const Image &img, const float *xoffsets, const float *yoffsets, float sz,
nuclear@6 321 unsigned int rotmask)
nuclear@6 322 {
nuclear@6 323 for(int i=0; i<6; i++) {
nuclear@6 324 Image face;
nuclear@6 325
nuclear@6 326 int xoffs = xoffsets[i] * img.get_width();
nuclear@6 327 int yoffs = yoffsets[i] * img.get_height();
nuclear@6 328
nuclear@6 329 if(!face.set_pixels(sz, sz, img.get_pixels(), xoffs, yoffs, img.get_width(), img.get_format())) {
nuclear@6 330 return false;
nuclear@6 331 }
nuclear@6 332
nuclear@6 333 if(rotmask & (1 << i)) {
nuclear@6 334 face.rotate_180();
nuclear@6 335 }
nuclear@6 336 set_image(face, i);
nuclear@6 337 }
nuclear@6 338 return true;
nuclear@6 339 }
nuclear@6 340
nuclear@0 341 static int glifmt_from_ifmt(unsigned int ifmt)
nuclear@0 342 {
nuclear@0 343 #ifdef GL_ES_VERSION_2_0
nuclear@0 344 switch(ifmt) {
nuclear@0 345 case GL_LUMINANCE16F:
nuclear@0 346 case GL_LUMINANCE32F:
nuclear@0 347 ifmt = GL_LUMINANCE;
nuclear@0 348 break;
nuclear@0 349
nuclear@0 350 case GL_RGB16F:
nuclear@0 351 case GL_RGB32F:
nuclear@0 352 ifmt = GL_RGB;
nuclear@0 353 break;
nuclear@0 354
nuclear@0 355 case GL_RGBA16F:
nuclear@0 356 case GL_RGBA32F:
nuclear@0 357 ifmt = GL_RGBA;
nuclear@0 358 break;
nuclear@0 359
nuclear@0 360 default:
nuclear@0 361 break;
nuclear@0 362 }
nuclear@0 363 #endif
nuclear@0 364 return ifmt; // by default just pass it through...
nuclear@0 365 }
nuclear@0 366
nuclear@0 367 static int glfmt_from_ifmt(unsigned int ifmt)
nuclear@0 368 {
nuclear@0 369 switch(ifmt) {
nuclear@0 370 case GL_LUMINANCE16F:
nuclear@0 371 case GL_LUMINANCE32F:
nuclear@0 372 return GL_LUMINANCE;
nuclear@0 373
nuclear@0 374 case GL_RGB16F:
nuclear@0 375 case GL_RGB32F:
nuclear@0 376 return GL_RGB;
nuclear@0 377
nuclear@0 378 case GL_RGBA16F:
nuclear@0 379 case GL_RGBA32F:
nuclear@0 380 return GL_RGBA;
nuclear@0 381
nuclear@0 382 default:
nuclear@0 383 break;
nuclear@0 384 }
nuclear@0 385 return ifmt;
nuclear@0 386 }
nuclear@0 387
nuclear@0 388 static int gltype_from_ifmt(unsigned int ifmt)
nuclear@0 389 {
nuclear@0 390 switch(ifmt) {
nuclear@0 391 case GL_RGB16F:
nuclear@0 392 case GL_RGBA16F:
nuclear@0 393 case GL_LUMINANCE16F:
nuclear@0 394 #ifdef GL_ES_VERSION_2_0
nuclear@0 395 return GL_HALF_FLOAT_OES;
nuclear@0 396 #endif
nuclear@0 397 case GL_RGB32F:
nuclear@0 398 case GL_RGBA32F:
nuclear@0 399 case GL_LUMINANCE32F:
nuclear@0 400 return GL_FLOAT;
nuclear@0 401
nuclear@0 402 default:
nuclear@0 403 break;
nuclear@0 404 }
nuclear@0 405 return GL_UNSIGNED_BYTE;
nuclear@0 406 }
nuclear@0 407
nuclear@0 408 static int glifmt_from_imgfmt(Image::Format fmt)
nuclear@0 409 {
nuclear@0 410 switch(fmt) {
nuclear@0 411 case Image::FMT_GREY:
nuclear@0 412 return GL_LUMINANCE;
nuclear@0 413 case Image::FMT_GREY_FLOAT:
nuclear@0 414 return GL_LUMINANCE16F;
nuclear@0 415 case Image::FMT_RGB:
nuclear@0 416 return GL_RGB;
nuclear@0 417 case Image::FMT_RGB_FLOAT:
nuclear@0 418 return GL_RGB16F;
nuclear@0 419 case Image::FMT_RGBA:
nuclear@0 420 return GL_RGBA;
nuclear@0 421 case Image::FMT_RGBA_FLOAT:
nuclear@0 422 return GL_RGBA16F;
nuclear@0 423 default:
nuclear@0 424 break;
nuclear@0 425 }
nuclear@0 426 return 0;
nuclear@0 427 }
nuclear@0 428
nuclear@0 429 // ---- TextureSet ----
nuclear@0 430 static void destroy_texture(Texture *tex)
nuclear@0 431 {
nuclear@0 432 delete tex;
nuclear@0 433 }
nuclear@0 434
nuclear@0 435 TextureSet::TextureSet()
nuclear@0 436 : DataSet<Texture*>(load_texture, destroy_texture)
nuclear@0 437 {
nuclear@0 438 }