-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtexture.c
341 lines (296 loc) · 8.79 KB
/
texture.c
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
//========================================================
// Scaled Simulator Engine
//
// Author: www.ac3d.org
//
// Description: AC3D Importer/Exporter
//
// Notes:
//
// AC3DVIEW version 1.13
//
// This is some sample code to load and view an AC3D file. AC3D is 3D modeling software and
// AC3D models (.ac files) contain the data for three dimensional objects. The .ac file format
// is a simple ASCII text format and is explained at the main website - www.ac3d.org.
//
// This code would be a good basis for any application that loads and displays AC3D files e.g.
// a game.
//
//
// SOME THINGS NOT HANDLED FROM THE .ac FILE
//
// 1) This loader does not use the rotation matrices from the AC3D
// file (if you create a file using AC3D, it will not have any).
//
// 2) Polygons are not triangluated. If there are polygons with > 4 vertices
// and they are concave, you may not get what you see in AC3D. (to fix this
// just select the surface/s in AC3D and Surface->triangulate).
//
// 3) Lights from the .ac file are not drawn (there is a single light in the scene).
//
// 4) The object crease angle attribute is ignored.
//
// All the files here are free of copyright so you can use them as you please. If you do use them
// them please give credit to AC3D.
//
// www.ac3d.org
//
//=========================================================
#include "ac3d.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef SEEK_SET
# define SEEK_SET 0
#endif
#define GLuint unsigned int
#define GLint int
#define GL_FALSE (0)
#define GL_TRUE (!GL_FALSE)
#define ALPHA_NONE (0x0000) /* no alpha info */
#define ALPHA_OPAQUE (0x0001<<0) /* alpha = 1 */
#define ALPHA_INVIS (0x0001<<1) /* alpha = 0 */
#define ALPHA_TRANSP (0x0004<<2) /* 0 < alpha < 1 */
/******************************************************************************/
typedef struct _rawImageRec {
unsigned short imagic;
unsigned short type;
unsigned short dim;
unsigned short sizeX, sizeY, sizeZ;
unsigned long min, max;
unsigned long wasteBytes;
char name[80];
unsigned long colorMap;
FILE *file;
unsigned char *tmp, *tmpR, *tmpG, *tmpB, *tmpA;
unsigned long rleEnd;
GLuint *rowStart;
GLint *rowSize;
} rawImageRec;
/******************************************************************************/
Private void ConvertShort(unsigned short *array, long length)
{
unsigned long b1, b2;
unsigned char *ptr;
ptr = (unsigned char *)array;
while (length--) {
b1 = *ptr++;
b2 = *ptr++;
*array++ = (unsigned short)((b1 << 8) | (b2));
}
}
Private void ConvertLong(GLuint *array, long length)
{
unsigned long b1, b2, b3, b4;
unsigned char *ptr;
ptr = (unsigned char *)array;
while (length--) {
b1 = *ptr++;
b2 = *ptr++;
b3 = *ptr++;
b4 = *ptr++;
*array++ = (b1 << 24) | (b2 << 16) | (b3 << 8) | (b4);
}
}
Private rawImageRec *RawImageOpen(char *fileName)
{
union {
int testWord;
char testByte[4];
} endianTest;
rawImageRec *raw;
int swapFlag;
int x;
endianTest.testWord = 1;
if (endianTest.testByte[0] == 1) {
swapFlag = GL_TRUE;
} else {
swapFlag = GL_FALSE;
}
raw = (rawImageRec *)malloc(sizeof(rawImageRec));
if (raw == NULL) {
fprintf(stderr, "Out of memory!\n");
return(NULL);
}
if ((raw->file = fopen(fileName, "rb")) == NULL) {
perror(fileName);
return(NULL);
}
fread(raw, 1, 12, raw->file);
if (swapFlag) {
ConvertShort(&raw->imagic, 6);
}
raw->tmp = (unsigned char *)malloc(raw->sizeX*256);
raw->tmpR = (unsigned char *)malloc(raw->sizeX*256);
raw->tmpG = (unsigned char *)malloc(raw->sizeX*256);
raw->tmpB = (unsigned char *)malloc(raw->sizeX*256);
raw->tmpA = (unsigned char *)malloc(raw->sizeX*256);
if (raw->tmp == NULL || raw->tmpR == NULL || raw->tmpG == NULL ||
raw->tmpB == NULL) {
fprintf(stderr, "Out of memory!\n");
return(NULL);
}
if ((raw->type & 0xFF00) == 0x0100) {
x = raw->sizeY * raw->sizeZ * sizeof(GLuint);
raw->rowStart = (GLuint *)malloc(x);
raw->rowSize = (GLint *)malloc(x);
if (raw->rowStart == NULL || raw->rowSize == NULL) {
fprintf(stderr, "Out of memory!\n");
return(NULL);
}
raw->rleEnd = 512 + (2 * x);
fseek(raw->file, 512, SEEK_SET);
fread(raw->rowStart, 1, x, raw->file);
fread(raw->rowSize, 1, x, raw->file);
if (swapFlag) {
ConvertLong(raw->rowStart, x/sizeof(GLuint));
ConvertLong((GLuint *)raw->rowSize, x/sizeof(GLint));
}
}
return raw;
}
Private void RawImageClose(rawImageRec *raw)
{
fclose(raw->file);
free(raw->tmp);
free(raw->tmpR);
free(raw->tmpG);
free(raw->tmpB);
free(raw);
}
Private void RawImageGetRow(rawImageRec *raw, unsigned char *buf, int y, int z)
{
unsigned char *iPtr, *oPtr, pixel;
int count;
if ((raw->type & 0xFF00) == 0x0100) {
fseek(raw->file, raw->rowStart[y+z*raw->sizeY], SEEK_SET);
fread(raw->tmp, 1, (unsigned int)raw->rowSize[y+z*raw->sizeY],
raw->file);
iPtr = raw->tmp;
oPtr = buf;
while (1) {
pixel = *iPtr++;
count = (int)(pixel & 0x7F);
if (!count) {
return;
}
if (pixel & 0x80) {
while (count--) {
*oPtr++ = *iPtr++;
}
} else {
pixel = *iPtr++;
while (count--) {
*oPtr++ = pixel;
}
}
}
} else {
fseek(raw->file, 512+(y*raw->sizeX)+(z*raw->sizeX*raw->sizeY),
SEEK_SET);
fread(buf, 1, raw->sizeX, raw->file);
}
}
Private void RawImageGetData(rawImageRec *raw, ACImage *final)
{
unsigned char *ptr;
int i, j;
final->data = (unsigned char *)myalloc((raw->sizeX+1)*(raw->sizeY+1)*raw->sizeZ);
if (final->data == NULL) {
fprintf(stderr, "Out of memory!\n");
return;
}
ptr = (unsigned char *)final->data;
/*
debugf("raw image depth %d", raw->sizeZ);
*/
if (raw->sizeZ == 1) {
for (i = 0; i < raw->sizeY; i++) {
RawImageGetRow(raw, raw->tmpR, i, 0);
for (j = 0; j < raw->sizeX; j++) { /* packing */
*ptr++ = *(raw->tmpR + j);
/**ptr++ = *(raw->tmpR + j);
*ptr++ = *(raw->tmpR + j);
*ptr++ = 255;*/
}
}
}
if (raw->sizeZ == 2) {
for (i = 0; i < raw->sizeY; i++) {
RawImageGetRow(raw, raw->tmpR, i, 0);
RawImageGetRow(raw, raw->tmpA, i, 1);
for (j = 0; j < raw->sizeX; j++) { /* packing */
*ptr++ = *(raw->tmpR + j);
/**ptr++ = *(raw->tmpR + j);
*ptr++ = *(raw->tmpR + j);*/
*ptr++ = *(raw->tmpA + j);
final->amask |= ((*(raw->tmpA + j) == 255) ? ALPHA_OPAQUE : 0);
final->amask |= ((*(raw->tmpA + j) == 0) ? ALPHA_INVIS : 0);
final->amask |= (((*(raw->tmpA + j)>0) && (*(raw->tmpA + j)<255)) ? ALPHA_TRANSP : 0);
}
}
}
else if (raw->sizeZ == 3) {
for (i = 0; i < raw->sizeY; i++) {
RawImageGetRow(raw, raw->tmpR, i, 0);
RawImageGetRow(raw, raw->tmpG, i, 1);
RawImageGetRow(raw, raw->tmpB, i, 2);
for (j = 0; j < raw->sizeX; j++) { /* packing */
*ptr++ = *(raw->tmpR + j);
*ptr++ = *(raw->tmpG + j);
*ptr++ = *(raw->tmpB + j);
/**ptr++ = 255;*/
}
}
}
else if (raw->sizeZ == 4) {
for (i = 0; i < raw->sizeY; i++) {
RawImageGetRow(raw, raw->tmpR, i, 0);
RawImageGetRow(raw, raw->tmpG, i, 1);
RawImageGetRow(raw, raw->tmpB, i, 2);
RawImageGetRow(raw, raw->tmpA, i, 3);
for (j = 0; j < raw->sizeX; j++) { /* packing */
*ptr++ = *(raw->tmpR + j);
*ptr++ = *(raw->tmpG + j);
*ptr++ = *(raw->tmpB + j);
*ptr++ = *(raw->tmpA + j);
final->amask |= ((*(raw->tmpA + j) == 255) ? ALPHA_OPAQUE : 0);
final->amask |= ((*(raw->tmpA + j) == 0) ? ALPHA_INVIS : 0);
final->amask |= (((*(raw->tmpA + j)>0) && (*(raw->tmpA + j)<255)) ? ALPHA_TRANSP : 0);
}
}
}
}
#define MAX_TEXTURES 100
static ACImage texture[MAX_TEXTURES];
static int num_texture = 0;
Prototype ACImage *ac_get_texture(int ind)
{
return(&texture[ind]);
}
Prototype int ac_load_rgb_image(char *fileName)
{
rawImageRec *raw;
ACImage *final;
printf("Loading texture: %s\n", fileName);
raw = RawImageOpen(fileName);
if (raw == NULL)
{
fprintf(stderr, "error opening rgb file\n");
return(-1);
}
final = &texture[num_texture];
if (num_texture == MAX_TEXTURES)
{
printf("out of texture space - change MAX_TEXTURES in texture.c\n");
exit(0);
}
final->width = raw->sizeX;
final->height = raw->sizeY;
final->depth = raw->sizeZ;
RawImageGetData(raw, final);
RawImageClose(raw);
printf("loaded texture %dx%d (%d)\n", final->width, final->height, final->depth);
num_texture++;
return(num_texture - 1);
}