1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
#include "glt_texture.h"
/*! \file
\ingroup GLT
$Id: texture.cpp,v 1.26 2002/10/09 15:09:38 nigels Exp $
$Log: texture.cpp,v $
Revision 1.26 2002/10/09 15:09:38 nigels
Added RCS Id and Log tags
*/
#include "glt_gl.h"
#include "glt_glu.h"
#include "glt_error.h"
//#include "glt_compress.h"
//#include "glt_image.h"
#if !defined(NDEBUG)
#include <iostream>
#endif
#include <cassert>
#include <cstdio>
#include <string>
using namespace std;
//////////////////////////////////////////////////////////////////////////
GltTexture::GltTexture(const GLenum target)
:
_target(target),
_components(0),
_width(0),
_height(0),
_border(0),
_format(0),
_type(0),
_alignment(0),
_wrapS(GL_REPEAT),
_wrapT(GL_REPEAT),
_filterMin(GL_LINEAR),
_filterMag(GL_LINEAR),
_gamma(1.0),
_hue(0.0),
_saturation(0.0),
_value(0.0),
_id(0)
{
}
GltTexture::GltTexture(const GltTexture &)
{
// Can't copy textures
assert(0);
}
GltTexture::~GltTexture()
{
#if !defined(NDEBUG)
if (_id)
cerr << "WARNING: Potential OpenGL texture leak (" << this << ")" << endl;
#endif
clear();
}
GltTexture &
GltTexture::operator=(const GltTexture &)
{
// Can't copy textures
assert(0);
return *this;
}
void
GltTexture::setWrap(const GLenum s,const GLenum t)
{
_wrapS = s;
_wrapT = t;
if (_id!=0)
{
glPushAttrib(GL_TEXTURE_BIT);
glBindTexture(_target,_id);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,_wrapS);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,_wrapT);
glPopAttrib();
}
}
void
GltTexture::setFilter(const GLenum min,const GLenum mag)
{
_filterMin = min;
_filterMag = mag;
if (_id!=0)
{
glPushAttrib(GL_TEXTURE_BIT);
glBindTexture(_target,_id);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,_filterMin);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,_filterMag);
glPopAttrib();
}
}
void
GltTexture::setGamma(const real gamma)
{
_gamma = gamma;
}
void
GltTexture::setHSVAdjust(const real hue,const real saturation,const real value)
{
_hue = hue;
_saturation = saturation;
_value = value;
}
#if 0
bool
GltTexture::init(const void *data,const bool mipmap)
{
GLERROR
clear();
if (!data)
return true;
// Extract the header
int type,width,height,alignment,compressed;
void *pixels = getHeader((const byte *) data,type,width,height,alignment,compressed);
bool deletePixels = false;
// If the header is valid, initialise texture
assert(pixels);
if (pixels)
{
switch (type)
{
case TEXTURE_TYPE_RGB: _components = 3; _format = GL_RGB; break;
case TEXTURE_TYPE_RGBA: _components = 4; _format = GL_RGBA; break;
case TEXTURE_TYPE_GREY: _components = 1; _format = GL_LUMINANCE; break;
case TEXTURE_TYPE_GREYA: _components = 2; _format = GL_LUMINANCE_ALPHA; break;
case TEXTURE_TYPE_ALPHA: _components = 1; _format = GL_ALPHA; break;
case TEXTURE_TYPE_BITMAP: _components = 1; _format = TEXTURE_TYPE_BITMAP;break;
case TEXTURE_TYPE_INDEXED_RGB: _components = 3; _format = GL_RGB; break;
case TEXTURE_TYPE_INDEXED_RGBA: _components = 4; _format = GL_RGBA; break;
default: assert(0); break;
}
if (compressed)
{
string tmp;
bool ok = decompress(tmp,pixels);
assert(ok);
assert(tmp.size());
if (ok && tmp.size())
{
const int n = width*height*_components;
pixels = new unsigned char[n];
deletePixels = true;
if (type==TEXTURE_TYPE_INDEXED_RGB)
{
string rgb;
indexed2rgb(rgb,tmp);
if (_gamma!=1.0)
adjustGamma(rgb,_gamma);
if (_hue!=0.0 || _saturation!=0.0 || _value!=0.0)
adjustHSV(rgb,width,height,_hue,_saturation,_value);
memcpy(pixels,rgb.c_str(),n);
/* TODO - Deal with indexed data */
}
else
{
if (_gamma!=1.0)
adjustGamma(tmp,_gamma);
if (_hue!=0.0 || _saturation!=0.0 || _value!=0.0)
adjustHSV(tmp,width,height,_hue,_saturation,_value);
memcpy(pixels,tmp.c_str(),n);
}
}
}
/* TODO - Deal with uncompressed indexed data */
_width = width;
_height = height;
_type = GL_UNSIGNED_BYTE;
}
assert(_components!=0);
if (pixels && _components!=0)
{
GLERROR
if (_target==GL_TEXTURE_2D)
{
glGenTextures(1,&_id);
glBindTexture(GL_TEXTURE_2D,_id);
glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,_filterMin);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,_filterMag);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, _wrapS);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, _wrapT);
}
if (mipmap)
gluBuild2DMipmaps(_target,_components,_width,_height,_format,_type,pixels);
else
glTexImage2D(_target,0,_components,_width,_height,_border,_format,_type,pixels);
if (deletePixels)
delete [] (unsigned char *) pixels;
GLERROR
return true;
}
GLERROR
return false;
}
bool
GltTexture::init(const GLsizei width,const GLsizei height,const std::string &image,const bool mipmap)
{
clear();
GLenum mode = 0;
int channels = 0;
if (width*height == (GLsizei)image.size()) { mode = GL_LUMINANCE; channels = 1; }
if (width*height*2== (GLsizei)image.size()) { mode = GL_LUMINANCE_ALPHA; channels = 2; }
if (width*height*3== (GLsizei)image.size()) { mode = GL_RGB; channels = 3; }
if (width*height*4== (GLsizei)image.size()) { mode = GL_RGBA; channels = 3; }
if (!mode || !channels)
return false;
if (_target==GL_TEXTURE_2D)
{
glGenTextures(1,&_id);
glBindTexture(GL_TEXTURE_2D,_id);
glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,_filterMin);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,_filterMag);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, _wrapS);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, _wrapT);
}
if (mipmap)
gluBuild2DMipmaps(_target,channels,width,height,mode,GL_UNSIGNED_BYTE,image.data());
else
glTexImage2D(_target,0,channels,width,height,0,mode,GL_UNSIGNED_BYTE,image.data());
return true;
}
#endif
bool
GltTexture::init(const GLsizei width,const GLsizei height,const byte *image,const GLsizei channels,const bool mipmap)
{
clear();
GLenum mode = 0;
switch (channels)
{
case 1: mode = GL_LUMINANCE; break;
case 2: mode = GL_LUMINANCE_ALPHA; break;
case 3: mode = GL_RGB; break;
case 4: mode = GL_RGBA; break;
default: return false;
}
if (_target==GL_TEXTURE_2D)
{
glGenTextures(1,&_id);
glBindTexture(GL_TEXTURE_2D,_id);
glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,_filterMin);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,_filterMag);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, _wrapS);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, _wrapT);
}
if (mipmap)
gluBuild2DMipmaps(_target,channels,width,height,mode,GL_UNSIGNED_BYTE,image);
else
glTexImage2D(_target,0,channels,width,height,0,mode,GL_UNSIGNED_BYTE,image);
return true;
}
void
GltTexture::clear()
{
if (_id)
glDeleteTextures(1,&_id);
_id = 0;
_components = 0;
_width = 0;
_height = 0;
_border = 0;
_format = 0;
_type = 0;
_alignment = 0;
}
void
GltTexture::set() const
{
if (_id!=0 && _target==GL_TEXTURE_2D)
glBindTexture(_target,_id);
}
const GLsizei &GltTexture::width() const { return _width; }
const GLsizei &GltTexture::height() const { return _height; }
const GLuint GltTexture::id() const { return _id; }
////////////////////////////////////////////////////////////////////////////
// Create variable-length header for texture data
bool GltTexture::makeHeader
(
string &header,
const int type,
const int width,
const int height,
const int alignment,
const int compressed
)
{
// 11 characters is big enough for integer 2^32 + \0
char buffer[5+11+11+11+11+11];
sprintf(buffer,"GLTT %u %u %u %u %u",type,width,height,alignment,compressed);
header = buffer;
header += '\0';
return true;
}
// Decode variable-length header for texture data
void *GltTexture::getHeader
(
const void * const data,
int &type,
int &width,
int &height,
int &alignment,
int &compressed
)
{
const char * const h = (const char * const) data;
if (h[0]=='G' && h[1]=='L' && h[2]=='T' && h[3]=='T' && h[4]==' ')
{
if (sscanf(h+5,"%i %i %i %i %i",&type,&width,&height,&alignment,&compressed)==5)
return (void *) (h + strlen(h) + 1);
}
return NULL;
}
|