This repository has been archived by the owner on Feb 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CTexture.cpp
370 lines (293 loc) · 10.3 KB
/
CTexture.cpp
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
// CTexture.cpp -
#include "main.h"
/////////////////////////////////////////////////////////////////////////////////////////////////
//// Name: CTexture()
//// Desc: class constructor
/////////////////////////////////////////////////////////////////////////////////////////////////
CTexture::CTexture()
{
m_textureloaded = FALSE;
}
/////////////////////////////////////////////////////////////////////////////////////////////////
//// Name: ~CTexture()
//// Desc: class destructor
/////////////////////////////////////////////////////////////////////////////////////////////////
CTexture::~CTexture()
{
//Free();
}
/////////////////////////////////////////////////////////////////////////////////////////////////
//// Name: Free()
//// Desc: osvobodi pametta
/////////////////////////////////////////////////////////////////////////////////////////////////
void CTexture::Free()
{
glDeleteTextures( 1, &m_textureID ); // iztrii indexa na texturata ot steck-a
m_textureloaded = FALSE;
}
/////////////////////////////////////////////////////////////////////////////////////////////////
//// Name: Activate()
//// Desc: activirai texturata za rendirane
/////////////////////////////////////////////////////////////////////////////////////////////////
int CTexture::Activate()
{
if ( !m_textureloaded ) return FALSE;
glBindTexture( GL_TEXTURE_2D, m_textureID );
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////////////////////////
//// Name: LoadTextureFromTGA()
//// Desc: opredeli kompresiqta na TGA texturata i q zaredi
/////////////////////////////////////////////////////////////////////////////////////////////////
int CTexture::LoadTextureFromTGA( const char *filename )
{
FILE *fp;
BYTE uTGA_header[12] = {0,0,2,0,0,0,0,0,0,0,0,0}; // nekompresirani TGA header
BYTE cTGA_header[12] = {0,0,10,0,0,0,0,0,0,0,0,0}; // kompresirani TGA header
BYTE szHeader[12];
memset( szHeader, NULL, sizeof(szHeader) );
if ( (fp=fopen( filename, "rb" )) == NULL )
{
// appendtolog
return FALSE;
}
// read header
if ( fread( szHeader, sizeof(szHeader), 1, fp ) == 0 )
{
// appendtolog
return FALSE;
}
// compare headers and determine compression
if ( memcmp( uTGA_header, szHeader, sizeof(szHeader) ) == 0 )
{
return LoadTGAUncompressed( filename, fp );
}
else if ( memcmp( cTGA_header, szHeader, sizeof(szHeader) ) == 0 )
{
return LoadTGACompressed( filename, fp );
}
// appendtolog - invalid file
return FALSE;
}
/////////////////////////////////////////////////////////////////////////////////////////////////
//// Name: LoadTGAUncompressed()
//// Desc: Zarejda nekompresirana textura
/////////////////////////////////////////////////////////////////////////////////////////////////
int CTexture::LoadTGAUncompressed( const char *filename, FILE *fpTGA )
{
struct_tga tga;
struct_texture tex;
// read 6 chars headers
if ( fread( &tga.header, sizeof(tga.header), 1, fpTGA ) == 0 )
{
// appendtolog
return FALSE;
}
// calculate TGA image properites
tex.width = tga.header[1] * 256 + tga.header[0];
tex.height = tga.header[3] * 256 + tga.header[2];
tex.bpp = tga.header[4];
tga.width = tex.width;
tga.height = tex.height;
tga.bpp = tex.bpp;
// check for valid info
if ( (tex.width <= 0) || (tex.height <= 0) || (tex.bpp != 24 && tex.bpp != 32) )
{
// appendtolog - invalid image properites
return FALSE;
}
// set image type
if ( tex.bpp == 24 )
{
tex.type = GL_RGB;
}
else
{
tex.type = GL_RGBA;
}
// caluclate bytes per pixel and total image data
tga.bytesPerPixel = tga.bpp / 8;
tga.imgsize = (tga.bytesPerPixel * tga.width * tga.height);
// rezervirai memory for the image data
//tex.imgdata = (BYTE *)malloc(tga.imgsize);
try {
tex.imgdata = new BYTE[tga.imgsize];
}
catch( std::bad_alloc ) {
// appendtolog - notenough memory to load texture
return FALSE;
}
// read image data
if ( fread( tex.imgdata, 1, tga.imgsize, fpTGA ) != tga.imgsize ) return FALSE;
// razmeni TGA BGR format s GL readable RGB format
for ( UINT i = 0; i < (UINT)tga.imgsize; i += tga.bytesPerPixel )
{
// shte izpolzwame XOR za da ramenim componentite ( 10x2 Steve Thomas )
tex.imgdata[i] ^= tex.imgdata[i+2] ^= tex.imgdata[i] ^= tex.imgdata[i+2];
}
fclose( fpTGA );
// generirai textura sys informaciqta ot TGA-to
GenerateTexture( &tex );
// osvobodi pametta na image-to
delete[] tex.imgdata;
return m_textureloaded = TRUE;
}
/////////////////////////////////////////////////////////////////////////////////////////////////
//// Name: LoadTGACompressed()
//// Desc: zarejda kompresirana (RLE) textura
/////////////////////////////////////////////////////////////////////////////////////////////////
int CTexture::LoadTGACompressed( const char *filename, FILE *fpTGA )
{
struct_tga tga;
struct_texture tex;
UINT pixelcount; // wsichko pixeli w image-to
UINT curpixel; // pixel ot koito chetem w momenta
UINT curbyte; // byte koito shte zapiswame w image-data
BYTE *clrbuf; // color buffer
// read 6 chars headers
if ( fread( &tga.header, sizeof(tga.header), 1, fpTGA ) == 0 )
{
// appendtolog
return FALSE;
}
// calculate TGA image properites
tex.width = tga.header[1] * 256 + tga.header[0];
tex.height = tga.header[3] * 256 + tga.header[2];
tex.bpp = tga.header[4];
tga.width = tex.width;
tga.height = tex.height;
tga.bpp = tex.bpp;
// check for valid info
if ( (tex.width <= 0) || (tex.height <= 0) || (tex.bpp != 24 && tex.bpp != 32) )
{
// appendtolog - invalid image properites
return FALSE;
}
// set image type
if ( tex.bpp == 24 )
{
tex.type = GL_RGB;
}
else
{
tex.type = GL_RGBA;
}
// caluclate bytes per pixel and total image data
tga.bytesPerPixel = tga.bpp / 8;
tga.imgsize = (tga.bytesPerPixel * tga.width * tga.height);
tex.imgdata = new BYTE[tga.imgsize];
try {
tex.imgdata = new BYTE[tga.imgsize];
}
catch( std::bad_alloc ) {
// appendtolog - notenough memory to load texture
return FALSE;
}
pixelcount = curpixel = curbyte = 0;
pixelcount = tga.width * tga.height;
clrbuf = new BYTE[tga.bytesPerPixel];
do
{
BYTE chunkheader = 0;
fread( &chunkheader, sizeof(BYTE), 1, fpTGA );
// proveri za RAW header, ako e takyw, togawa trqbwa da +1 za da namerim
// obshtiq broi na pixelite sled header-a
if ( chunkheader < 128 )
{
chunkheader++;
// procheti pixelite v colorbufer-a
for ( short i = 0; i < chunkheader; i++ )
{
fread( clrbuf, 1, tga.bytesPerPixel, fpTGA );
// sega shte vzemem pixelite ot colorbufera i shte gi zapishem w image-data kato
// ednovremenno razmenqme formata BGR(A) s RGB(A)
tex.imgdata[curbyte] = clrbuf[2]; // write R
tex.imgdata[curbyte + 1] = clrbuf[1]; // write G
tex.imgdata[curbyte + 2] = clrbuf[0]; // write B
if ( tga.bytesPerPixel == 4 )
tex.imgdata[curbyte + 3] = clrbuf[3]; // write A
curbyte += tga.bytesPerPixel; // sledwashtite 3/4 pixela
curpixel++; // uwelichi broqcha na pixelite
}
}
else
{
// RLE header, headeryt ima stoinost = 127 taka che
// shte izvadim 127 za da namerim broq na pixelite
chunkheader -= 127;
fread( clrbuf, 1, tga.bytesPerPixel, fpTGA );
// sledwashtiqt cikyl shte zapechatame w pametta tolkova pyti
// kolkoto e razultata ot header-a
for ( short j = 0; j < chunkheader; j++ )
{
tex.imgdata[curbyte] = clrbuf[2]; // write R
tex.imgdata[curbyte + 1] = clrbuf[1]; // write G
tex.imgdata[curbyte + 2] = clrbuf[0]; // write B
if ( tga.bytesPerPixel == 4 )
tex.imgdata[curbyte + 3] = clrbuf[3]; // write A
curbyte += tga.bytesPerPixel; // sledwashtite 3/4 pixela
curpixel++; // uwelichi broqcha na pixelite
}
}
} while ( curpixel < pixelcount );
fclose( fpTGA );
// generirai textura sys informaciqta ot TGA-to
GenerateTexture( &tex );
// osvobodi pametta na image-to
delete[] tex.imgdata;
return m_textureloaded = TRUE;
}
/////////////////////////////////////////////////////////////////////////////////////////////////
//// Name: LoadTextureFromBitmap()
//// Desc: Zarejda textura ot bitmap chrez AUX funkciq
/////////////////////////////////////////////////////////////////////////////////////////////////
int CTexture::LoadTextureFromBitmap( const char *filename )
{
AUX_RGBImageRec *buffer_image = NULL;
struct_texture tex;
FILE *fp = NULL;
// proveri parametyra (hah!)
if ( !filename ) return FALSE;
// proveri dali file-a syshtestvuva i go zaredi ako e taka!
if ( (fp = fopen( filename, "r" ) ))
{
fclose(fp);
if ( (buffer_image = auxDIBImageLoad(filename)) == NULL )
return FALSE;
// setupni localnata stuctura
tex.width = buffer_image->sizeX;
tex.height = buffer_image->sizeY;
tex.type = GL_RGB;
tex.imgdata = buffer_image->data;
tex.bpp = 24;
GenerateTexture( &tex );
// osvobodi zaetata pamet
if ( buffer_image )
{
if ( buffer_image->data ) free(buffer_image->data);
free(buffer_image);
}
return m_textureloaded = TRUE;
}
return FALSE;
}
/////////////////////////////////////////////////////////////////////////////////////////////////
//// Name: GenerateTexture()
//// Desc: generira textura
/////////////////////////////////////////////////////////////////////////////////////////////////
void CTexture::GenerateTexture( struct_texture *texture )
{
// kolko texturi da se generirat
glGenTextures( 1, &m_textureID );
glBindTexture( GL_TEXTURE_2D, m_textureID );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
// postroi bez mipmaps
// glTexImage2D( GL_TEXTURE_2D, 0, texture->bpp == 24 ? 3 : 4 , texture->width, texture->height, 0, texture->type,
// GL_UNSIGNED_BYTE, texture->imgdata );
// postroi s mipmaps
gluBuild2DMipmaps( GL_TEXTURE_2D, texture->bpp == 24 ? 3 : 4, \
texture->width, texture->height, \
texture->type, GL_UNSIGNED_BYTE, \
texture->imgdata );
}