-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtexture.cpp
executable file
·270 lines (223 loc) · 6.68 KB
/
texture.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
#include <windows.h>
#include <gl/gl.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include "texture.h"
using namespace std;
static unsigned char tga_header_uncompressed[12] = {0,0,2,0,0,0,0,0,0,0,0,0};
static unsigned char tga_header_compressed[12] = {0,0,10,0,0,0,0,0,0,0,0,0};
// image_t::LoadUncompressedTGA
bool image_t::LoadUncompressedTGA( char *filename, fstream& fs )
{
// read the info from header
unsigned char header6[6];
fs.read( (char*)&header6[0], sizeof(header6) );
if( fs.gcount() != sizeof(header6) )
{
cout << "--ERROR (image_t::LoadUncompressedTGA): File \"" << filename << "\": Could not read info header." << endl;
return false;
}
width = header6[1] * 256 + header6[0];
height = header6[3] * 256 + header6[2];
bpp = header6[4];
if( (width <= 0) || (height <= 0) || ((bpp != 24) && (bpp !=32)) )
{
cout << "--ERROR (image_t::LoadUncompressedTGA): File \"" << filename << "\": Invalid image information." << endl;
return false;
}
if( bpp == 24 )
type = GL_RGB;
else
type = GL_RGBA;
// read the data
int bytes_per_pxl = (bpp / 8);
int image_size = bytes_per_pxl * width * height;
data = new char [ image_size ];
fs.read( data, image_size );
if( fs.gcount() != image_size )
{
cout << "--ERROR (image_t::LoadUncompressedTGA): File \"" << filename << "\": Could not read image data." << endl;
return false;
}
for( int cswap = 0; cswap < image_size; cswap += bytes_per_pxl )
{
data[cswap] ^= data[cswap+2] ^= data[cswap] ^= data[cswap+2];
}
return true;
}
// image_t::LoadCompressedTGA
bool image_t::LoadCompressedTGA( char * filename, fstream& fs )
{
unsigned char header6[6];
fs.read( (char*)&header6[0], sizeof(header6) );
if( fs.gcount() != sizeof(header6) )
{
cout << "--ERROR (image_t::LoadCompressedTGA): File \"" << filename << "\": Could not read info header." << endl;
return false;
}
width = header6[1] * 256 + header6[0];
height = header6[3] * 256 + header6[2];
bpp = header6[4];
if( (width <= 0) || (height <= 0) || ((bpp != 24) && (bpp !=32)) )
{
cout << "--ERROR (image_t::LoadCompressedTGA): File \"" << filename << "\": Invalid texture information." << endl;
return false;
}
if( bpp == 24 )
type = GL_RGB;
else
type = GL_RGBA;
int bytes_per_pxl = (bpp / 8);
int image_size = bytes_per_pxl * width * height;
data = new char [image_size];
unsigned int pixelcount = height * width;
unsigned int currentpixel = 0;
unsigned int currentbyte = 0;
unsigned char colorbuffer [4];
do
{
unsigned char chunkheader = 0;
fs.read( (char*)&chunkheader, sizeof(unsigned char) );
if( fs.gcount() != sizeof(unsigned char) )
{
cout << "--ERROR (image_t::LoadCompressedTGA): File \"" << filename << "\": Could not read RLE header." << endl;
return false;
}
if( chunkheader < 128 )
{
chunkheader++;
for( int counter = 0; counter < chunkheader; counter++ )
{
fs.read( (char*)&colorbuffer[0], bytes_per_pxl );
if( fs.gcount() != bytes_per_pxl )
{
cout << "--ERROR (image_t::LoadCompressedTGA): File \"" << filename << "\": Could not read image data." << endl;
return false;
}
data[currentbyte ] = colorbuffer[2];
data[currentbyte + 1] = colorbuffer[1];
data[currentbyte + 2] = colorbuffer[0];
if( bytes_per_pxl == 4 )
{
data[currentbyte + 3] = colorbuffer[3];
}
currentbyte += bytes_per_pxl;
currentpixel++;
if( currentpixel > pixelcount )
{
cout << "--ERROR (image_t::LoadCompressedTGA): File \"" << filename << "\": Too many pixels read." << endl;
return false;
}
}
}
else
{
chunkheader -= 127;
fs.read( (char*)&colorbuffer[0], bytes_per_pxl );
if( fs.gcount() != bytes_per_pxl )
{
cout << "--ERROR (image_t::LoadCompressedTGA): File \"" << filename << "\": Could not read from file." << endl;
return false;
}
for( int counter = 0; counter < chunkheader; counter++ )
{
data[currentbyte] = colorbuffer[2];
data[currentbyte+1] = colorbuffer[1];
data[currentbyte+2] = colorbuffer[0];
if( bytes_per_pxl == 4 )
{
data[currentbyte + 3] = colorbuffer[3];
}
currentbyte += bytes_per_pxl;
currentpixel++;
if( currentpixel > pixelcount )
{
cout << "--ERROR (image_t::LoadCompressedTGA): File \"" << filename << "\": Too many pixels read." << endl;
return false;
}
}
}
} while(currentpixel < pixelcount);
return true;
}
/*
image_t::LoadTGA
Load a tga using the help of the above
*/
bool image_t::LoadTGA( char* filename )
{
fstream fs;
char my_tga_header[12];
fs.open( filename, ios::in|ios::binary );
if( !fs.good() )
{
cout << "--ERROR (image_t::LoadTGA): File \"" << filename << "\": Could not open file." << endl;
return false;
}
fs.read( &my_tga_header[0], sizeof(my_tga_header) );
if( fs.gcount() != sizeof(my_tga_header) )
{
cout << "--ERROR (image_t::LoadTGA): File \"" << filename << "\": Could not read file header." << endl;
fs.close();
return false;
}
bool funcs_return;
if( memcmp(tga_header_uncompressed, &my_tga_header[0], sizeof(my_tga_header)) == 0 )
{
funcs_return = LoadUncompressedTGA(filename, fs);
}
else if( memcmp(tga_header_compressed, &my_tga_header[0], sizeof(my_tga_header)) == 0 )
{
funcs_return = LoadCompressedTGA(filename, fs);
}
else
{
cout << "--ERROR (image_t::LoadTGA): File \"" << filename << "\": Invalid image header." << endl;
funcs_return = false;
}
fs.close();
return funcs_return;
}
/*
texture_t::Load
Load a texture file (tga supported) and bind it
*/
bool texture_t::Load( char* filename )
{
char* c;
image_t* img = 0;
// get file extension
c = strrchr( filename, '.' );
++c;
if( strcmp(c, "tga")==0 )
{
img = new image_t;
// load it
if( !img->LoadTGA( filename ) ) { delete img; return false; }
}
else
{
cout << "--ERROR (texture_t::Load): File \"" << filename << "\" has unsupported extension." << endl;
return 0;
}
//
width = img->width;
height = img->height;
bpp = img->bpp;
type = img->type;
// bind the texture
glGenTextures( 1, (GLuint*)&id );
glBindTexture( GL_TEXTURE_2D, id );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexImage2D( GL_TEXTURE_2D, 0, img->bpp/8, img->width, img->height, 0, img->type, GL_UNSIGNED_BYTE, img->data );
delete img;
return true;
}
// texture_t::Unload
void texture_t::Unload()
{
glDeleteTextures( 1, (GLuint*)&id );
}