/
cTexture.cpp
320 lines (265 loc) · 11.4 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
/*$T MATTS/cTexture.cpp GC 1.140 07/01/09 21:12:09 */
/*
* ;
* Name: cRender.cpp ;
* Last Modified: 20/12/05 ;
* ;
* Purpose: Render the topology ;
*/
#include "cTexture.h"
#include "cModel.h"
#include "cModelTexture.h"
#include <png.h>
#define PNG_BYTES_TO_CHECK (4)
/*
=======================================================================================================================
* TODO:
=======================================================================================================================
*/
cTexture::cTexture () {
nFloatingNum = 0;
/* Load the textures */
aunTextureName[TEXNAME_TRON] = LoadTextureJPEG ("Resources\\Tron.jpg");
aunTextureName[TEXNAME_JMUMAP] = LoadTextureJPEG ("Resources\\jmumap.jpg");
aunTextureName[TEXNAME_RENDERMAP] = LoadTextureJPEG ("Resources\\RenderMap.jpg");
aunTextureName[TEXNAME_REDBLOB] = LoadTexturePNG ("Resources\\AnalyseRed.png");
aunTextureName[TEXNAME_AMBERBLOB] = LoadTexturePNG ("Resources\\AnalyseAmber.png");
aunTextureName[TEXNAME_GREENBLOB] = LoadTexturePNG ("Resources\\AnalyseGreen.png");
aunTextureName[TEXNAME_RADIORANGE1] = LoadTexturePNG ("Resources\\RangeCore.png");
aunTextureName[TEXNAME_RADIORANGE2] = LoadTexturePNG ("Resources\\RangeEdge.png");
/* HUD Buttons */
aunTextureName[TEXNAME_BUTTONSELECT] = LoadTexturePNG ("Resources\\ButtonSelect.png");
aunTextureName[TEXNAME_BUTTONSELECTD] = LoadTexturePNG ("Resources\\ButtonSelectD.png");
aunTextureName[TEXNAME_BUTTONLINKS] = LoadTexturePNG ("Resources\\ButtonLinks.png");
aunTextureName[TEXNAME_BUTTONLINKSD] = LoadTexturePNG ("Resources\\ButtonLinksD.png");
aunTextureName[TEXNAME_BUTTONSPIN] = LoadTexturePNG ("Resources\\ButtonSpin.png");
aunTextureName[TEXNAME_BUTTONSPIND] = LoadTexturePNG ("Resources\\ButtonSpinD.png");
aunTextureName[TEXNAME_BUTTONNET] = LoadTexturePNG ("Resources\\ButtonNet.png");
aunTextureName[TEXNAME_BUTTONNETD] = LoadTexturePNG ("Resources\\ButtonNetD.png");
aunTextureName[TEXNAME_BUTTONDETAILS] = LoadTexturePNG ("Resources\\ButtonDetails.png");
aunTextureName[TEXNAME_BUTTONDETAILSD] = LoadTexturePNG ("Resources\\ButtonDetailsD.png");
aunTextureName[TEXNAME_BUTTONLEFT] = LoadTexturePNG ("Resources\\ButtonLeft.png");
aunTextureName[TEXNAME_BUTTONLEFTD] = LoadTexturePNG ("Resources\\ButtonLeftD.png");
aunTextureName[TEXNAME_BUTTONBLANK] = LoadTexturePNG ("Resources\\ButtonBlank.png");
aunTextureName[TEXNAME_BUTTONBLANKD] = LoadTexturePNG ("Resources\\ButtonBlankD.png");
aunTextureName[TEXNAME_BUTTONRIGHT] = LoadTexturePNG ("Resources\\ButtonRight.png");
aunTextureName[TEXNAME_BUTTONRIGHTD] = LoadTexturePNG ("Resources\\ButtonRightD.png");
aunTextureName[TEXNAME_LEVEL] = LoadTexturePNG ("Resources\\Level.png");
//aunTextureName[TEXNAME_LOGO] = LoadTexturePNG ("Resources\\logo-128x128.png");
}
/*
=======================================================================================================================
* TODO:
=======================================================================================================================
*/
cTexture::~cTexture () {
/* Delete the textures */
glDeleteTextures (TEXNAME_NUM, aunTextureName);
glDeleteTextures (nFloatingNum, aunTextureName + TEXNAME_NUM);
}
/*
=======================================================================================================================
* TODO:
=======================================================================================================================
*/
int cTexture::LoadFloatingTexture (char const * szFilename) {
int nTexture;
if (nFloatingNum < FLOATING_MAX) {
nTexture = TEXNAME_NUM + nFloatingNum;
aunTextureName[nTexture] = LoadTexturePNG (szFilename);
nFloatingNum++;
}
else {
nTexture = 0;
}
return (nTexture);
}
/*
=======================================================================================================================
* TODO:
=======================================================================================================================
*/
int cTexture::GetFloatingNum () {
return nFloatingNum;
}
/*
=======================================================================================================================
* TODO:
=======================================================================================================================
*/
GLuint cTexture::LoadTextureRaw (char const *const szFilename, int const nWidth, int const nHeight, bool boAlpha) {
GLuint unTextureName;
void* cData;
FILE* hFile;
int nComponents;
int uFormat;
unTextureName = 0;
if (boAlpha) {
nComponents = 4;
uFormat = GL_RGBA;
} else {
nComponents = 3;
uFormat = GL_RGB;
}
hFile = fopen (szFilename, "rb");
if (hFile) {
cData = malloc (nWidth * nHeight * nComponents);
if (cData) {
fread (cData, nWidth * nHeight * nComponents, 1, hFile);
fclose (hFile);
glGenTextures (1, &unTextureName);
glBindTexture (GL_TEXTURE_2D, unTextureName);
glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
gluBuild2DMipmaps (GL_TEXTURE_2D, nComponents, nWidth, nHeight, uFormat, GL_UNSIGNED_BYTE, cData);
free (cData);
}
}
return unTextureName;
}
/*
=======================================================================================================================
* TODO:
=======================================================================================================================
*/
GLuint cTexture::LoadTextureJPEG (char const *const szFilename) {
GLuint unTextureName;
cModelTexture* psTexture;
unTextureName = 0;
psTexture = new cModelTexture (szFilename);
if ((psTexture) && (psTexture->bitmap) && (psTexture->bitmap->pixels)) {
glGenTextures (1, &unTextureName);
glBindTexture (GL_TEXTURE_2D, unTextureName);
glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
gluBuild2DMipmaps (GL_TEXTURE_2D, 3, psTexture->bitmap->w, psTexture->bitmap->h, GL_RGB, GL_UNSIGNED_BYTE,
psTexture->bitmap->pixels);
}
if (psTexture) {
delete psTexture;
}
return unTextureName;
}
/*
=======================================================================================================================
* TODO:
=======================================================================================================================
*/
GLuint cTexture::LoadTexturePNG (char const *const szFilename) {
GLuint unTextureName;
png_structp psPng;
png_infop psInfo;
unsigned int uSigRead;
png_uint_32 uWidth;
png_uint_32 uHeight;
int nBitDepth;
int nColourType;
int nInterlaceType;
png_byte acTestBuffer[PNG_BYTES_TO_CHECK];
void* cData;
FILE* hFile;
float fScreenGamma; /* Apparently a good guess for a PC monitors in a dimly lit room */
int nIntent;
double fImageGamma;
unsigned int uRow;
png_bytep* apRowPointers;
unTextureName = 0;
psPng = NULL;
psInfo = NULL;
uSigRead = 0;
fScreenGamma = 2.2;
hFile = fopen (szFilename, "rb");
if (hFile) {
acTestBuffer[0] = 0;
uSigRead = (unsigned int) fread (acTestBuffer, 1, PNG_BYTES_TO_CHECK, hFile);
/* Check if this is a PNG file */
if (png_sig_cmp (acTestBuffer, (png_size_t) 0, uSigRead) == 0) {
/* Set up the PNG structure */
psPng = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (psPng) {
/* Set up image information structure */
psInfo = png_create_info_struct (psPng);
}
/* Check the structures have been set up and set the error handler */
if (psPng && psInfo && (!setjmp (png_jmpbuf (psPng)))) {
/* Use standard C stream as I/O method */
png_init_io (psPng, hFile);
/* Skip the bit we already read in */
png_set_sig_bytes (psPng, uSigRead);
/* Read all of the info about the PNG from the file */
png_read_info (psPng, psInfo);
png_get_IHDR (psPng, psInfo, &uWidth, &uHeight, &nBitDepth, &nColourType, &nInterlaceType, NULL, NULL);
/* Reduce 16 bits per colour down to 8 bits per colour if necessary */
png_set_strip_16 (psPng);
/* Expand paletted colours to RGB if necessary */
if (nColourType == PNG_COLOR_TYPE_PALETTE) {
png_set_palette_to_rgb (psPng);
}
/* Expand greyscale images to 8 bits per colour if necessary */
if ((nColourType == PNG_COLOR_TYPE_GRAY) && (nBitDepth < 8)) {
png_set_gray_to_rgb (psPng);
}
/* Expand image to be in RGBA quartets if necessary */
if (png_get_valid (psPng, psInfo, PNG_INFO_tRNS)) {
png_set_tRNS_to_alpha (psPng);
}
/* Deal with Gamma correction and stuff like that */
if (png_get_sRGB (psPng, psInfo, &nIntent)) {
png_set_gamma (psPng, fScreenGamma, 0.45455);
} else {
if (png_get_gAMA (psPng, psInfo, &fImageGamma)) {
png_set_gamma (psPng, fScreenGamma, fImageGamma);
} else {
png_set_gamma (psPng, fScreenGamma, 0.45455);
}
}
png_read_update_info (psPng, psInfo);
/* Allocate memory for the row pointers */
apRowPointers = (png_bytep*) malloc (uHeight * sizeof (png_bytep));
/* Allocate memory for the pixel data */
cData = png_malloc (psPng, (uHeight * png_get_rowbytes (psPng, psInfo)));
if (cData) {
/* Set up row data pointers to allocated memory */
for (uRow = 0; uRow < uHeight; uRow++) {
apRowPointers[uRow] = (png_bytep) cData + (uRow * png_get_rowbytes (psPng, psInfo));
}
/* Read the image into the memory */
png_read_image (psPng, apRowPointers);
/* Read anything left over */
png_read_end (psPng, psInfo);
/* We don't need the row pointers anymore */
free (apRowPointers);
/* Now convert the data into an OpenGL texture */
glGenTextures (1, &unTextureName);
glBindTexture (GL_TEXTURE_2D, unTextureName);
glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
gluBuild2DMipmaps (GL_TEXTURE_2D, 4, uWidth, uHeight, GL_RGBA, GL_UNSIGNED_BYTE, cData);
/* Don't need the pixel data anymore */
png_free (psPng, cData);
}
}
/* Clean things up and free allocated memory */
png_destroy_read_struct (&psPng, &psInfo, NULL);
}
fclose (hFile);
}
return unTextureName;
}
/*
=======================================================================================================================
* TODO:
=======================================================================================================================
*/
GLuint cTexture::GetTexture (TEXNAME eTexture) {
return aunTextureName[eTexture];
}