goat3dgfx

view src/texture.cc @ 6:3d96734fd477

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