forked from Milez213/CityTrial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoadTexture.cpp
264 lines (207 loc) · 7.45 KB
/
LoadTexture.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
#include "LoadTexture.h"
#include "stdio.h"
#include <iostream>
#include <stdlib.h>
using namespace std;
Image *TextureImage;
RGB myimage[64][64];
RGB* g_pixel;
// texture with alpha
GLvoid LoadTextureAlpha(const char* image_file, int texID) {
TextureImage = (Image *) malloc(sizeof(Image));
if (TextureImage == NULL) {
printf("Error allocating space for image");
exit(1);
}
//cout << "trying to load " << image_file << endl;
if (!ImageLoad32bpp(image_file, TextureImage)) {
exit(1);
}
/* 2d texture, level of detail 0 (normal), 3 components (red, green, blue), */
/* x size from image, y size from image, */
/* border 0 (normal), rgb color data, unsigned byte data, data */
glBindTexture(GL_TEXTURE_2D, texID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
TextureImage->sizeX, TextureImage->sizeY,
0, GL_RGBA, GL_UNSIGNED_BYTE, TextureImage->data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); /* cheap scaling when image bigger than texture */
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); /* cheap scaling when image smalled than texture*/
}
//routines to load in a bmp files - must be 2^nx2^m and a 24bit bmp
GLvoid LoadTexture(const char* image_file, int texID) {
TextureImage = (Image *) malloc(sizeof(Image));
if (TextureImage == NULL) {
printf("Error allocating space for image");
exit(1);
}
//cout << "trying to load " << image_file << endl;
if (!ImageLoad(image_file, TextureImage)) {
exit(1);
}
/* 2d texture, level of detail 0 (normal), 3 components (red, green, blue), */
/* x size from image, y size from image, */
/* border 0 (normal), rgb color data, unsigned byte data, data */
glBindTexture(GL_TEXTURE_2D, texID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
TextureImage->sizeX, TextureImage->sizeY,
0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage->data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); /* cheap scaling when image bigger than texture */
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); /* cheap scaling when image smalled than texture*/
}
//code to create a checker board texture...
void makeCheckerBoard ( int nRows, int nCols )
{
g_pixel = new RGB[nRows * nCols];
int c;
long count = 0;
for ( int i=0; i < nRows; i++ ) {
for ( int j=0; j < nCols; j++ ) {
count = j*nCols +i;
c = (((i/8) + (j/8)) %2) * 255;
g_pixel[count].r = c;
g_pixel[count].g = c;
g_pixel[count].b = c;
}
}
/* set up the checker board texture as well */
glBindTexture(GL_TEXTURE_2D, 2);
glTexImage2D(GL_TEXTURE_2D, 0, 3, 64, 64, 0, GL_RGB, GL_UNSIGNED_BYTE, g_pixel);
}
/* BMP file loader loads a 24-bit bmp file only */
/*
* getint and getshort are help functions to load the bitmap byte by byte
*/
static unsigned int getint(FILE *fp) {
int c, c1, c2, c3;
/* get 4 bytes */
c = getc(fp);
c1 = getc(fp);
c2 = getc(fp);
c3 = getc(fp);
return ((unsigned int) c) +
(((unsigned int) c1) << 8) +
(((unsigned int) c2) << 16) +
(((unsigned int) c3) << 24);
}
static unsigned int getshort(FILE *fp){
int c, c1;
/* get 2 bytes*/
c = getc(fp);
c1 = getc(fp);
return ((unsigned int) c) + (((unsigned int) c1) << 8);
}
/* quick and dirty bitmap loader...for 24 bit bitmaps with 1 plane only. */
int ImageLoad(const char *filename, Image *image) {
FILE *file;
unsigned long size; /* size of the image in bytes. */
unsigned long i; /* standard counter. */
unsigned short int planes; /* number of planes in image (must be 1) */
unsigned short int bpp; /* number of bits per pixel (must be 24) */
char temp; /* used to convert bgr to rgb color. */
/* make sure the file is there. */
if ((file = fopen(filename, "rb"))==NULL) {
printf("File Not Found : %s\n",filename);
return 0;
}
/* seek through the bmp header, up to the width height: */
fseek(file, 18, SEEK_CUR);
/* No 100% errorchecking anymore!!! */
/* read the width */
image->sizeX = getint (file);
/* read the height */
image->sizeY = getint (file);
/* calculate the size (assuming 24 bits or 3 bytes per pixel). */
size = image->sizeX * image->sizeY * 3;
/* read the planes */
planes = getshort(file);
if (planes != 1) {
printf("Planes from %s is not 1: %u\n", filename, planes);
return 0;
}
/* read the bpp */
bpp = getshort(file);
if (bpp != 24) {
printf("Bpp from %s is not 24: %u\n", filename, bpp);
return 0;
}
/* seek past the rest of the bitmap header. */
fseek(file, 24, SEEK_CUR);
/* read the data. */
image->data = (char *) malloc(size);
if (image->data == NULL) {
printf("Error allocating memory for color-corrected image data");
return 0;
}
if ((i = fread(image->data, size, 1, file)) != 1) {
printf("Error reading image data from %s.\n", filename);
return 0;
}
for (i=0;i<size;i+=3) { /* reverse all of the colors. (bgr -> rgb) */
temp = image->data[i];
image->data[i] = image->data[i+2];
image->data[i+2] = temp;
}
fclose(file); /* Close the file and release the filedes */
/* we're done. */
return 1;
}
/* modified from 24bit by Mustafa Khafateh */
int ImageLoad32bpp(const char *filename, Image *image) {
FILE *file;
unsigned long size; /* size of the image in bytes. */
unsigned long i; /* standard counter. */
unsigned short int planes; /* number of planes in image (must be 1) */
unsigned short int bpp; /* number of bits per pixel (must be 24) */
char r,g,b; /* used to convert bgr to rgb color. */
/* make sure the file is there. */
if ((file = fopen(filename, "rb"))==NULL) {
printf("File Not Found : %s\n",filename);
return 0;
}
/* seek through the bmp header, up to the width height: */
fseek(file, 18, SEEK_CUR);
/* No 100% errorchecking anymore!!! */
/* read the width */
image->sizeX = getint (file);
/* read the height */
image->sizeY = getint (file);
/* calculate the size (assuming 32 bits or 4 bytes per pixel). */
size = image->sizeX * image->sizeY * 4;
/* read the planes */
planes = getshort(file);
if (planes != 1) {
printf("Planes from %s is not 1: %u\n", filename, planes);
return 0;
}
/* read the bpp */
bpp = getshort(file);
if (bpp != 32) {
printf("Bpp from %s is not 32: %u\n", filename, bpp);
return 0;
}
/* seek past the rest of the bitmap header. */
fseek(file, 24, SEEK_CUR);
/* read the data. */
image->data = (char *) malloc(size);
if (image->data == NULL) {
printf("Error allocating memory for color-corrected image data");
return 0;
}
if ((i = fread(image->data, size, 1, file)) != 1) {
printf("Error reading image data from %s.\n", filename);
return 0;
}
for (i=0;i<size;i+=4) { /* reverse all of the colors. (bgr -> rgb) */
b = image->data[i];
g = image->data[i+1];
r = image->data[i+2];
// a = image->data[i+3];
image->data[i] = r;
image->data[i+1] = g;
image->data[i+2] = b;
//image->data[i+3] = a;
}
fclose(file); /* Close the file and release the filedes */
/* we're done. */
return 1;
}