goat3dgfx

view src/texture.cc @ 0:1873dfd13f2d

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 14 Nov 2013 05:27:09 +0200
parents
children 7bd5ebec3b6f
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) == -1) {
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_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
249 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
250 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
251 glTexParameteri(GL_TEXTURE_2D, 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 // TODO
261 }
263 bool TextureCube::load(const char *fname)
264 {
265 return false; // TODO
266 }
268 bool TextureCube::save(const char *fname) const
269 {
270 return false; // TODO
271 }
273 static int glifmt_from_ifmt(unsigned int ifmt)
274 {
275 #ifdef GL_ES_VERSION_2_0
276 switch(ifmt) {
277 case GL_LUMINANCE16F:
278 case GL_LUMINANCE32F:
279 ifmt = GL_LUMINANCE;
280 break;
282 case GL_RGB16F:
283 case GL_RGB32F:
284 ifmt = GL_RGB;
285 break;
287 case GL_RGBA16F:
288 case GL_RGBA32F:
289 ifmt = GL_RGBA;
290 break;
292 default:
293 break;
294 }
295 #endif
296 return ifmt; // by default just pass it through...
297 }
299 static int glfmt_from_ifmt(unsigned int ifmt)
300 {
301 switch(ifmt) {
302 case GL_LUMINANCE16F:
303 case GL_LUMINANCE32F:
304 return GL_LUMINANCE;
306 case GL_RGB16F:
307 case GL_RGB32F:
308 return GL_RGB;
310 case GL_RGBA16F:
311 case GL_RGBA32F:
312 return GL_RGBA;
314 default:
315 break;
316 }
317 return ifmt;
318 }
320 static int gltype_from_ifmt(unsigned int ifmt)
321 {
322 switch(ifmt) {
323 case GL_RGB16F:
324 case GL_RGBA16F:
325 case GL_LUMINANCE16F:
326 #ifdef GL_ES_VERSION_2_0
327 return GL_HALF_FLOAT_OES;
328 #endif
329 case GL_RGB32F:
330 case GL_RGBA32F:
331 case GL_LUMINANCE32F:
332 return GL_FLOAT;
334 default:
335 break;
336 }
337 return GL_UNSIGNED_BYTE;
338 }
340 static int glifmt_from_imgfmt(Image::Format fmt)
341 {
342 switch(fmt) {
343 case Image::FMT_GREY:
344 return GL_LUMINANCE;
345 case Image::FMT_GREY_FLOAT:
346 return GL_LUMINANCE16F;
347 case Image::FMT_RGB:
348 return GL_RGB;
349 case Image::FMT_RGB_FLOAT:
350 return GL_RGB16F;
351 case Image::FMT_RGBA:
352 return GL_RGBA;
353 case Image::FMT_RGBA_FLOAT:
354 return GL_RGBA16F;
355 default:
356 break;
357 }
358 return 0;
359 }
361 // ---- TextureSet ----
362 static void destroy_texture(Texture *tex)
363 {
364 delete tex;
365 }
367 TextureSet::TextureSet()
368 : DataSet<Texture*>(load_texture, destroy_texture)
369 {
370 }