vrshoot

annotate src/texture.cc @ 2:334d17aed7de

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