-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathg_tex.cpp
More file actions
344 lines (304 loc) · 9.3 KB
/
Copy pathg_tex.cpp
File metadata and controls
344 lines (304 loc) · 9.3 KB
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
// g_tex.cpp
// texture loading and manipulations
#include <windows.h>
#include <GL/gl.h>
#include <stdio.h>
#include <stdlib.h>
#include "image.h"
/*
* getint and getshort arehelp functions to load the bitmap byte by byte on
* SPARC platform.
* I've got them from xv bitmap load routinebecause the original bmp loader didn't work
* I've tried to change as less code as possible.
*/
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. */
/* See http://www.dcs.ed.ac.uk/~mxr/gfx/2d/BMP.txt for more info.*/
int ImageLoad(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; /* temporary color storage for bgr-rgb conversion.*/
/* 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);
printf("Width of %s: %lu\n", filename, image->sizeX);
/* read the height */
image->sizeY = getint (file);
printf("Height of %s: %lu\n", filename, image->sizeY);
/* 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;
}
/* we're done.*/
return 1;
}
void make_Textures( GLuint texture[5] ) {
/* Load Texture*/
Image *image1, *image2, *image3,
*image4, *image5, *image6;//,
//*image7, *image8, *image9,
//*image10, *image11, *image12,
//*image13, *image14, *image15,
//*image16;
/* allocate space for texture*/
image1 = (Image *) malloc(sizeof(Image));
if (image1 == NULL) {
printf("Error allocating space for image");
exit(0);
}
image2 = (Image *) malloc(sizeof(Image));
if (image2 == NULL) {
printf("Error allocating space for image");
exit(0);
}
image3 = (Image *) malloc(sizeof(Image));
if (image3 == NULL) {
printf("Error allocating space for image");
exit(0);
}
image4 = (Image *) malloc(sizeof(Image));
if (image4 == NULL) {
printf("Error allocating space for image");
exit(0);
}
image5 = (Image *) malloc(sizeof(Image));
if (image5 == NULL) {
printf("Error allocating space for image");
exit(0);
}
image6 = (Image *) malloc(sizeof(Image));
if (image6 == NULL) {
printf("Error allocating space for image");
exit(0);
}
/*
image7 = (Image *) malloc(sizeof(Image));
if (image7 == NULL) {
printf("Error allocating space for image");
exit(0);
}
image8 = (Image *) malloc(sizeof(Image));
if (image8 == NULL) {
printf("Error allocating space for image");
exit(0);
}
image9 = (Image *) malloc(sizeof(Image));
if (image9 == NULL) {
printf("Error allocating space for image");
exit(0);
}
image10 = (Image *) malloc(sizeof(Image));
if (image10 == NULL) {
printf("Error allocating space for image");
exit(0);
}
image11 = (Image *) malloc(sizeof(Image));
if (image11 == NULL) {
printf("Error allocating space for image");
exit(0);
}
image12 = (Image *) malloc(sizeof(Image));
if (image12 == NULL) {
printf("Error allocating space for image");
exit(0);
}
image13 = (Image *) malloc(sizeof(Image));
if (image13 == NULL) {
printf("Error allocating space for image");
exit(0);
}
image14 = (Image *) malloc(sizeof(Image));
if (image14 == NULL) {
printf("Error allocating space for image");
exit(0);
}
image15 = (Image *) malloc(sizeof(Image));
if (image15 == NULL) {
printf("Error allocating space for image");
exit(0);
}
image16 = (Image *) malloc(sizeof(Image));
if (image16 == NULL) {
printf("Error allocating space for image");
exit(0);
}
*/
if (!ImageLoad("tile1.bmp", image1)) {
exit(1);
}
if (!ImageLoad("robot.bmp", image2)) {
exit(1);
}
if (!ImageLoad("gun.bmp", image3)) {
exit(1);
}
if (!ImageLoad("grass.bmp", image4)) {
exit(1);
}
if (!ImageLoad("crate.bmp", image5)) {
exit(1);
}
if (!ImageLoad("brick.bmp", image6)) {
exit(1);
}
/*
if (!ImageLoad("brick.bmp", image7)) {
exit(1);
}
if (!ImageLoad("wierdbrick.bmp", image8)) {
exit(1);
}
if (!ImageLoad("crate.bmp", image9)) {
exit(1);
}
if (!ImageLoad("tile3.bmp", image10)) {
exit(1);
}
if (!ImageLoad("tile3.bmp", image11)) {
exit(1);
}
if (!ImageLoad("tile3.bmp", image12)) {
exit(1);
}
if (!ImageLoad("tile3.bmp", image13)) {
exit(1);
}
if (!ImageLoad("tile3.bmp", image14)) {
exit(1);
}
if (!ImageLoad("tile3.bmp", image15)) {
exit(1);
}
if (!ImageLoad("tile3.bmp", image16)) {
exit(1);
}
*/
glGenTextures(5, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D, 0, 3, image1->sizeX, image1->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, image1->data);
glBindTexture(GL_TEXTURE_2D, texture[1]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D, 0, 3, image2->sizeX, image2->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, image2->data);
glBindTexture(GL_TEXTURE_2D, texture[2]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D, 0, 3, image3->sizeX, image3->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, image3->data);
glBindTexture(GL_TEXTURE_2D, texture[3]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D, 0, 3, image4->sizeX, image4->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, image4->data);
glBindTexture(GL_TEXTURE_2D, texture[4]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D, 0, 3, image5->sizeX, image5->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, image5->data);
glBindTexture(GL_TEXTURE_2D, texture[5]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D, 0, 3, image6->sizeX, image6->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, image6->data);
free(image1->data);
free(image1);
free(image2->data);
free(image2);
free(image3->data);
free(image3);
free(image4->data);
free(image4);
free(image5->data);
free(image5);
free(image6->data);
free(image6);
/*
free(image7->data);
free(image7);
free(image8->data);
free(image8);
free(image9->data);
free(image9);
free(image10->data);
free(image10);
free(image11->data);
free(image11);
free(image12->data);
free(image12);
free(image13->data);
free(image13);
free(image14->data);
free(image14);
free(image15->data);
free(image15);
free(image16->data);
free(image16);
*/
}