added the capability to crudely resize images by half in image.cc
[laserbrain_demo] / src / texture.cc
1 #include <math.h>
2 #include <assert.h>
3 #include "texture.h"
4 #include "datamap.h"
5 #include "image.h"
6 #include "opengl.h"
7 #include "imago2.h"
8 #include "logger.h"
9
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);
13
14 static int glifmt_from_imgfmt(Image::Format fmt);
15
16 static unsigned int type_to_target(TextureType type);
17 static TextureType target_to_type(unsigned int targ);
18
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
22 };
23
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
31 };
32
33
34 void bind_texture(Texture *tex, int tunit)
35 {
36         if(tex) {
37                 tex->bind(tunit);
38         } else {
39                 glActiveTexture(GL_TEXTURE0 + tunit);
40                 glBindTexture(cur_target[tunit], 0);
41                 assert(glGetError() == GL_NO_ERROR);
42                 glActiveTexture(GL_TEXTURE0);
43         }
44 }
45
46
47 Image *Texture::default_img;
48
49 Texture::Texture()
50 {
51         target = 0;
52         sz[0] = sz[1] = sz[2] = 0;
53         texfmt = 0;
54
55         img = 0;
56         glGenTextures(1, &id);
57 }
58
59 Texture::~Texture()
60 {
61         if(id) {
62                 glDeleteTextures(1, &id);
63         }
64         if(img) {
65                 delete img;
66         }
67 }
68
69 void Texture::set_wrapping(unsigned int wrap)
70 {
71         if(!target) {
72                 return;
73         }
74
75         glBindTexture(target, id);
76         assert(glGetError() == GL_NO_ERROR);
77         glTexParameteri(target, GL_TEXTURE_WRAP_S, wrap);
78         glTexParameteri(target, GL_TEXTURE_WRAP_T, wrap);
79         glTexParameteri(target, GL_TEXTURE_WRAP_R, wrap);
80 }
81
82 void Texture::set_filtering(unsigned int filt)
83 {
84         unsigned int mag_filter;
85
86         if(!target) {
87                 return;
88         }
89
90         switch(filt) {
91         case GL_LINEAR_MIPMAP_NEAREST:
92         case GL_LINEAR_MIPMAP_LINEAR:
93                 mag_filter = GL_LINEAR;
94                 break;
95
96         case GL_NEAREST_MIPMAP_NEAREST:
97         case GL_NEAREST_MIPMAP_LINEAR:
98                 mag_filter = GL_NEAREST;
99                 break;
100
101         default:
102                 mag_filter = filt;
103         }
104
105         set_filtering(filt, mag_filter);
106 }
107
108 void Texture::set_filtering(unsigned int min_filt, unsigned int mag_filt)
109 {
110         glBindTexture(target, id);
111         assert(glGetError() == GL_NO_ERROR);
112         glTexParameteri(target, GL_TEXTURE_MIN_FILTER, min_filt);
113         glTexParameteri(target, GL_TEXTURE_MAG_FILTER, mag_filt);
114 }
115
116 unsigned int Texture::get_format() const
117 {
118         return texfmt;
119 }
120
121 int Texture::get_size(int dim) const
122 {
123         if(dim < 0 || dim >= 3) {
124                 return 0;
125         }
126         return sz[dim];
127 }
128
129 unsigned int Texture::get_id() const
130 {
131         return id;
132 }
133
134 TextureType Texture::get_type() const
135 {
136         return target_to_type(target);
137 }
138
139 void Texture::bind(int tex_unit) const
140 {
141         glActiveTexture(GL_TEXTURE0 + tex_unit);
142         glBindTexture(target, id);
143         assert(glGetError() == GL_NO_ERROR);
144         glActiveTexture(GL_TEXTURE0);
145
146         cur_target[tex_unit] = target;
147 }
148
149
150 void Texture::create(int xsz, int ysz, TextureType textype, unsigned int ifmt)
151 {
152         if(textype == TEX_CUBE && xsz != ysz) {
153                 error_log("trying to create cubemap with different width and height (%dx%d)\n", xsz, ysz);
154                 return;
155         }
156
157         int fmt = glfmt_from_ifmt(ifmt);
158         int type = gltype_from_ifmt(ifmt);
159
160         target = type_to_target(textype);
161
162         glBindTexture(target, id);
163         assert(glGetError() == GL_NO_ERROR);
164         glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
165         glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
166
167         switch(type) {
168         case TEX_2D:
169                 glTexImage2D(GL_TEXTURE_2D, 0, glifmt_from_ifmt(ifmt), xsz, ysz, 0, fmt, type, 0);
170                 break;
171
172         case TEX_CUBE:
173                 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
174                 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
175                 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
176                 for(int i=0; i<6; i++) {
177                         glTexImage2D(cube_faces[i], 0, ifmt, xsz, ysz, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
178                 }
179                 break;
180         }
181
182         sz[0] = xsz;
183         sz[1] = ysz;
184         texfmt = ifmt;
185 }
186
187 #define DEF_IMAGE_SIZE  64
188 void Texture::create_default(TextureType type)
189 {
190         if(!default_img) {
191                 default_img = new Image;
192                 default_img->create(DEF_IMAGE_SIZE, DEF_IMAGE_SIZE, Image::FMT_RGBA);
193
194                 unsigned char *pixels = (unsigned char*)default_img->get_pixels();
195                 for(int i=0; i<DEF_IMAGE_SIZE; i++) {
196                         for(int j=0; j<DEF_IMAGE_SIZE; j++) {
197                                 bool chess = ((i >> 3) & 1) == ((j >> 3) & 1);
198                                 pixels[0] = chess ? 255 : 32;
199                                 pixels[1] = 64;
200                                 pixels[2] = chess ? 32 : 255;
201                                 pixels[3] = 255;
202                                 pixels += 4;
203                         }
204                 }
205         }
206
207         switch(type) {
208         case TEX_2D:
209                 set_image(*default_img);
210                 break;
211
212         case TEX_CUBE:
213                 for(int i=0; i<6; i++) {
214                         set_image(*default_img, i);
215                 }
216                 break;
217         }
218 }
219
220 void Texture::set_image(const Image &img, int idx)
221 {
222         if(idx >= 0 && idx < 6) {
223                 set_image_cube(img, idx);
224         } else {
225                 if(!set_image_cube(img)) {
226                         set_image_2d(img);
227                 }
228         }
229 }
230
231 void Texture::set_image_2d(const Image &img)
232 {
233         texfmt = glifmt_from_imgfmt(img.get_format());
234         unsigned int fmt = glfmt_from_ifmt(texfmt);
235         unsigned int type = gltype_from_ifmt(texfmt);
236
237         sz[0] = img.get_width();
238         sz[1] = img.get_height();
239
240         target = GL_TEXTURE_2D;
241         glBindTexture(target, id);
242         assert(glGetError() == GL_NO_ERROR);
243         glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
244         glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
245         glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_REPEAT);
246         glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_REPEAT);
247
248         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
249
250 #ifdef __GLEW_H__
251         if(GLEW_SGIS_generate_mipmap) {
252                 glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
253 #endif
254                 glTexImage2D(target, 0, texfmt, sz[0], sz[1], 0, fmt, type, img.get_pixels());
255 #ifdef __GLEW_H__
256         } else {
257                 gluBuild2DMipmaps(target, texfmt, sz[0], sz[1], fmt, type, img.get_pixels());
258         }
259 #endif
260
261 #ifdef GL_ES_VERSION_2_0
262         glGenerateMipmap(target);
263 #endif
264 }
265
266 bool Texture::set_image_cube(const Image &img, int idx)
267 {
268         unsigned int err;
269         if(idx < 0 || idx >= 6) {
270                 return false;
271         }
272
273         texfmt = glifmt_from_imgfmt(img.get_format());
274         unsigned int fmt = glfmt_from_ifmt(texfmt);
275         unsigned int type = gltype_from_ifmt(texfmt);
276
277         sz[0] = img.get_width();
278         sz[1] = img.get_height();
279
280         target = GL_TEXTURE_CUBE_MAP;
281         glBindTexture(target, id);
282         if((err = glGetError()) == GL_INVALID_OPERATION) {
283                 return false;   // probably not a cubemap
284         }
285         assert(err == GL_NO_ERROR);
286         glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
287         glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
288         glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
289         glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
290         glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
291
292         glTexImage2D(cube_faces[idx], 0, texfmt, sz[0], sz[1], 0, fmt, type, img.get_pixels());
293         return true;
294 }
295
296 bool Texture::set_image_cube(const Image &img)
297 {
298         static const float one_third = 1.0 / 3.0;
299         static const float two_thirds = 2.0 / 3.0;
300         static const float hcross[2][6] = {
301                 {0.5, 0.0, 0.25, 0.25, 0.25, 0.75}, {one_third, one_third, 0.0, two_thirds, one_third, one_third} };
302         static const float vcross[2][6] = {
303                 {two_thirds, 0.0, one_third, one_third, one_third, one_third}, {0.25, 0.25, 0.0, 0.5, 0.25, 0.75} };
304         static const float hsix[2][6] = {
305                 {0.0, 0.0, one_third, one_third, two_thirds, two_thirds}, {0.0, 0.5, 0.0, 0.5, 0.0, 0.5} };
306
307         int xsz = img.get_width();
308         int ysz = img.get_height();
309
310         if((xsz << 8) / 4 == (ysz << 8) / 3) {
311                 // horizontal cross, assume the vertical bit is center-left
312                 return set_cube_multi(img, hcross[0], hcross[1], xsz / 4);
313         }
314         if((xsz << 8) / 3 == (ysz << 8) / 4) {
315                 // vertical cross, assume the horizontal bit is center-top (180-rotated image 5)
316                 return set_cube_multi(img, vcross[0], vcross[1], ysz / 4, (1 << 5));
317         }
318         if((xsz << 8) / 3 == (ysz << 8) / 2) {
319                 // horizontal sixpack
320                 return set_cube_multi(img, hsix[0], hsix[1], ysz / 2);
321         }
322
323         return false;
324 }
325
326
327 bool Texture::load(const char *fname)
328 {
329         Image img;
330         if(!img.load(fname)) {
331                 error_log("failed to load 2D texture: %s\n", fname);
332                 return false;
333         }
334         set_image(img);
335
336         info_log("loaded 2D texture: %s\n", fname);
337         return true;
338 }
339
340 bool Texture::load_cube(const char *fname)
341 {
342         Image img;
343         if(!img.load(fname)) {
344                 return false;
345         }
346         return set_image_cube(img);
347 }
348
349 bool Texture::set_cube_multi(const Image &img, const float *xoffsets, const float *yoffsets, float sz,
350                 unsigned int rotmask)
351 {
352         for(int i=0; i<6; i++) {
353                 Image face;
354
355                 int xoffs = xoffsets[i] * img.get_width();
356                 int yoffs = yoffsets[i] * img.get_height();
357
358                 if(!face.set_pixels(sz, sz, img.get_pixels(), xoffs, yoffs, img.get_width(), img.get_format())) {
359                         return false;
360                 }
361
362                 if(rotmask & (1 << i)) {
363                         face.rotate_180();
364                 }
365                 set_image_cube(face, i);
366         }
367         return true;
368 }
369
370 static int glifmt_from_ifmt(unsigned int ifmt)
371 {
372 #ifdef GL_ES_VERSION_2_0
373         switch(ifmt) {
374         case GL_LUMINANCE16F_ARB:
375         case GL_LUMINANCE32F_ARB:
376                 ifmt = GL_LUMINANCE;
377                 break;
378
379         case GL_RGB16F:
380         case GL_RGB32F:
381                 ifmt = GL_RGB;
382                 break;
383
384         case GL_RGBA16F:
385         case GL_RGBA32F:
386                 ifmt = GL_RGBA;
387                 break;
388
389         default:
390                 break;
391         }
392 #endif
393         return ifmt;    // by default just pass it through...
394 }
395
396 static int glfmt_from_ifmt(unsigned int ifmt)
397 {
398         switch(ifmt) {
399         case GL_LUMINANCE16F_ARB:
400         case GL_LUMINANCE32F_ARB:
401         case GL_SLUMINANCE:
402                 return GL_LUMINANCE;
403
404         case GL_RGB16F:
405         case GL_RGB32F:
406         case GL_SRGB:
407                 return GL_RGB;
408
409         case GL_RGBA16F:
410         case GL_RGBA32F:
411         case GL_SRGB_ALPHA:
412                 return GL_RGBA;
413
414         default:
415                 break;
416         }
417         return ifmt;
418 }
419
420 static int gltype_from_ifmt(unsigned int ifmt)
421 {
422         switch(ifmt) {
423         case GL_RGB16F:
424         case GL_RGBA16F:
425         case GL_LUMINANCE16F_ARB:
426 #ifdef GL_ES_VERSION_2_0
427                 return GL_HALF_FLOAT_OES;
428 #endif
429         case GL_RGB32F:
430         case GL_RGBA32F:
431         case GL_LUMINANCE32F_ARB:
432                 return GL_FLOAT;
433
434         default:
435                 break;
436         }
437         return GL_UNSIGNED_BYTE;
438 }
439
440 static int glifmt_from_imgfmt(Image::Format fmt)
441 {
442         switch(fmt) {
443         case Image::FMT_GREY:
444                 return GL_SLUMINANCE;
445         case Image::FMT_GREY_FLOAT:
446                 return GL_LUMINANCE16F_ARB;
447         case Image::FMT_RGB:
448                 return GL_SRGB;
449         case Image::FMT_RGB_FLOAT:
450                 return GL_RGB16F;
451         case Image::FMT_RGBA:
452                 return GL_SRGB_ALPHA;
453         case Image::FMT_RGBA_FLOAT:
454                 return GL_RGBA16F;
455         default:
456                 break;
457         }
458         return 0;
459 }
460
461 static unsigned int type_to_target(TextureType type)
462 {
463         return type == TEX_CUBE ? GL_TEXTURE_CUBE_MAP : GL_TEXTURE_2D;
464 }
465
466 static TextureType target_to_type(unsigned int targ)
467 {
468         return targ == GL_TEXTURE_CUBE_MAP ? TEX_CUBE : TEX_2D;
469 }
470
471 // ---- TextureSet ----
472 TextureSet::TextureSet()
473         : DataSet<Texture*>(create_tex, load_tex, done_tex, free_tex)
474 {
475 }
476
477 Texture *TextureSet::get_texture(const char *name, TextureType type, const DataMap *dmap) const
478 {
479         char *fname;
480         int nsize = dmap ? dmap->path_size(name) : 0;
481         if(nsize) {
482                 fname = (char*)alloca(nsize);
483                 dmap->lookup(name, fname, nsize);
484                 //debug_log("texture lookup: %s -> %s\n", name, fname);
485         } else {
486                 fname = (char*)name;
487                 //debug_log("texture lookup failed, using: %s\n", fname);
488         }
489
490         std::map<std::string, Texture*>::const_iterator iter = data.find(fname);
491         if(iter != data.end()) {
492                 return iter->second;
493         }
494
495         Texture *res = create();
496         data[fname] = res;
497         res->create_default(type);
498         resman_lookup(rman, fname, res);
499         return res;
500 }
501
502 // static callbacks
503
504 Texture *TextureSet::create_tex()
505 {
506         return new Texture;
507 }
508
509 bool TextureSet::load_tex(Texture *tex, const char *fname)
510 {
511         Image *img = new Image;
512         img->name = fname;
513         if(!img->load(fname)) {
514                 delete img;
515                 return false;
516         }
517
518         delete tex->img;
519         tex->img = img;
520
521         return true;
522 }
523
524 bool TextureSet::done_tex(Texture *tex)
525 {
526         //debug_log("TextureSet::done_tex [%s]\n", tex->img->name.c_str());
527         if(!tex->img) {
528                 return false;
529         }
530
531         tex->set_image(*tex->img);
532         return true;
533 }
534
535 void TextureSet::free_tex(Texture *tex)
536 {
537         delete tex;
538 }