-
Notifications
You must be signed in to change notification settings - Fork 0
/
load3ds.c
408 lines (376 loc) · 11.1 KB
/
load3ds.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/* load 3ds file
*
* written by Alexander Zaprjagaev
*
* usage:
* float *Load3DS(char *name,int *num_vertex);
*/
#include <math.h>
#include <stdio.h>
#include <malloc.h>
#define CHUNK_MAIN 0x4d4d
#define CHUNK_OBJMESH 0x3d3d
#define CHUNK_OBJBLOCK 0x4000
#define CHUNK_TRIMESH 0x4100
#define CHUNK_VERTLIST 0x4110
#define CHUNK_FACELIST 0x4120
#define CHUNK_MAPLIST 0x4140
#define CHUNK_SMOOLIST 0x4150
typedef struct {
float x,y,z;
} vector_t;
typedef struct {
float u,v;
} uvmap_t;
typedef struct {
vector_t *vertex;
int num_vertex;
uvmap_t *uvmap;
int num_uvmap;
int *face;
int *smoothgroup;
vector_t *normal;
int num_face;
} trimesh_t;
typedef struct {
trimesh_t *trimesh;
int num_trimesh;
} mesh_t;
typedef int (*process_chunk)(FILE *file,
unsigned short type,int size,void *data);
/*
*
*/
static void vector_copy(vector_t *a,vector_t *b) {
b->x = a->x;
b->y = a->y;
b->z = a->z;
}
static void vector_add(vector_t *a,vector_t *b,vector_t *c) {
c->x = a->x + b->x;
c->y = a->y + b->y;
c->z = a->z + b->z;
}
static void vector_sub(vector_t *a,vector_t *b,vector_t *c) {
c->x = a->x - b->x;
c->y = a->y - b->y;
c->z = a->z - b->z;
}
static void vector_cross(vector_t *a,vector_t *b,vector_t *c) {
c->x = a->y * b->z - a->z * b->y;
c->y = a->z * b->x - a->x * b->z;
c->z = a->x * b->y - a->y * b->x;
}
static float vector_normalize(vector_t *a,vector_t *b) {
float length = sqrt(a->x * a->x + a->y * a->y + a->z * a->z);
if(length) {
float ilength = 1.0 / length;
b->x = a->x * ilength;
b->y = a->y * ilength;
b->z = a->z * ilength;
return length;
}
b->x = 0.0;
b->y = 0.0;
b->z = 0.0;
return 0.0;
}
/*
*
*/
static void skeep_bytes(FILE *file,int bytes) {
fseek(file,bytes,SEEK_CUR);
}
static int skeep_string(FILE *file) {
int i = 0;
while(fgetc(file) != '\0') i++;
return ++i;
}
static unsigned short read_ushort(FILE *file) {
unsigned short ret;
fread(&ret,1,sizeof(unsigned short),file);
return ret;
}
static int read_int(FILE *file) {
int ret;
fread(&ret,1,sizeof(int),file);
return ret;
}
static float read_float(FILE *file) {
float ret;
fread(&ret,1,sizeof(float),file);
return ret;
}
static int read_chunk(FILE *file,process_chunk chunk_func,void *data) {
unsigned short chunk_type;
int chunk_size,content_size;
chunk_type = read_ushort(file);
chunk_size = read_int(file);
content_size = chunk_size - 6;
if(!chunk_func(file,chunk_type,content_size,data))
skeep_bytes(file,content_size);
return chunk_size;
}
static int read_chunks(FILE *file,int bytes,
process_chunk chunk_func,void *data) {
int bytes_read = 0;
while(bytes_read < bytes)
bytes_read += read_chunk(file,chunk_func,data);
if(bytes_read != bytes)
fprintf(stderr,"expected %d bytes but read %d\n",bytes_read,bytes);
return bytes_read;
}
/*
*
*/
static int process_smoolist(FILE *file,unsigned short type,
int size,void *data) {
int i;
trimesh_t *trimesh = (trimesh_t*)data;
if(type == CHUNK_SMOOLIST) {
trimesh->smoothgroup = (int*)malloc(sizeof(int) * trimesh->num_face);
for(i = 0; i < trimesh->num_face; i++) {
trimesh->smoothgroup[i] = read_int(file);
}
return 1;
}
return 0;
}
static int process_trimesh(FILE *file,unsigned short type,
int size,void *data) {
int i;
trimesh_t *trimesh = (trimesh_t*)data;
if(type == CHUNK_VERTLIST) { /* vertlist */
trimesh->num_vertex = read_ushort(file);
trimesh->vertex = (vector_t*)malloc(sizeof(vector_t) *
trimesh->num_vertex);
for(i = 0; i < trimesh->num_vertex; i++) {
trimesh->vertex[i].x = read_float(file);
trimesh->vertex[i].y = read_float(file);
trimesh->vertex[i].z = read_float(file);
}
return 1;
} else if(type == CHUNK_MAPLIST) { /* maplist */
trimesh->num_uvmap = read_ushort(file);
trimesh->uvmap = (uvmap_t*)malloc(sizeof(uvmap_t) * trimesh->num_uvmap);
for(i = 0; i < trimesh->num_uvmap; i++) {
trimesh->uvmap[i].u = read_float(file);
trimesh->uvmap[i].v = 1.0 - read_float(file);
}
return 1;
} else if(type == CHUNK_FACELIST) { /* facelist */
int bytes_left;
trimesh->num_face = read_ushort(file);
trimesh->face = (int*)malloc(sizeof(int) * trimesh->num_face * 3);
for(i = 0; i < trimesh->num_face * 3; i += 3) {
trimesh->face[i + 0] = read_ushort(file);
trimesh->face[i + 1] = read_ushort(file);
trimesh->face[i + 2] = read_ushort(file);
read_ushort(file);
}
bytes_left = size - trimesh->num_face * sizeof(unsigned short) * 4 - 2;
if(bytes_left > 0)
read_chunks(file,bytes_left,process_smoolist,(void*)trimesh);
return 1;
}
return 0;
}
static int process_objblock(FILE *file,unsigned short type,
int size,void *data) {
mesh_t *mesh = (mesh_t*)data;
if(type == CHUNK_TRIMESH) {
mesh->num_trimesh++;
if(mesh->trimesh == NULL) mesh->trimesh =
(trimesh_t*)malloc(sizeof(trimesh_t));
else mesh->trimesh = (trimesh_t*)realloc(mesh->trimesh,
sizeof(trimesh_t) * mesh->num_trimesh);
memset(&mesh->trimesh[mesh->num_trimesh - 1],0,sizeof(trimesh_t));
read_chunks(file,size,process_trimesh,
(void*)&mesh->trimesh[mesh->num_trimesh - 1]);
return 1;
}
return 0;
}
static int process_objmesh(FILE *file,unsigned short type,
int size,void *data) {
if(type == CHUNK_OBJBLOCK) {
size -= skeep_string(file);
read_chunks(file,size,process_objblock,data);
return 1;
}
return 0;
}
static int process_main(FILE *file,unsigned short type,
int size,void *data) {
if(type == CHUNK_OBJMESH) {
read_chunks(file,size,process_objmesh,data);
return 1;
}
return 0;
}
/*
*
*/
static void create_trimesh(trimesh_t *trimesh) {
int i;
uvmap_t *uvmap;
vector_t *vertex,*normal_face,*normal_vertex;
int *face,*vertex_count,**vertex_face,*smoothgroup;
/* calc normals */
normal_face = (vector_t*)malloc(sizeof(vector_t) * trimesh->num_face);
memset(normal_face,0,sizeof(vector_t) * trimesh->num_face);
normal_vertex = (vector_t*)malloc(sizeof(vector_t) * trimesh->num_face * 3);
memset(normal_vertex,0,sizeof(vector_t) * trimesh->num_face * 3);
vertex_count = (int*)malloc(sizeof(int) * trimesh->num_face * 3);
memset(vertex_count,0,sizeof(int) * trimesh->num_face * 3);
vertex_face = (int**)malloc(sizeof(int*) * trimesh->num_face * 3);
memset(vertex_face,0,sizeof(int) * trimesh->num_face * 3);
trimesh->normal = normal_vertex;
vertex = trimesh->vertex;
face = trimesh->face;
smoothgroup = trimesh->smoothgroup;
for(i = 0; i < trimesh->num_face; i++) {
int j = i * 3;
vector_t a,b;
int v0 = face[j + 0];
int v1 = face[j + 1];
int v2 = face[j + 2];
vertex_count[v0]++;
vertex_count[v1]++;
vertex_count[v2]++;
vector_sub(&vertex[v1],&vertex[v0],&a);
vector_sub(&vertex[v2],&vertex[v0],&b);
vector_cross(&a,&b,&normal_face[i]);
vector_normalize(&normal_face[i],&normal_face[i]);
}
for(i = 0; i < trimesh->num_face * 3; i++) {
vertex_face[i] = (int*)malloc(sizeof(int) * (vertex_count[i] + 1));
vertex_face[i][0] = vertex_count[i];
}
for(i = 0; i < trimesh->num_face; i++) {
int j = i * 3;
int v0 = face[j + 0];
int v1 = face[j + 1];
int v2 = face[j + 2];
vertex_face[v0][vertex_count[v0]--] = i;
vertex_face[v1][vertex_count[v1]--] = i;
vertex_face[v2][vertex_count[v2]--] = i;
}
for(i = 0; i < trimesh->num_face; i++) {
int j = i * 3,k;
int v0 = face[j + 0];
int v1 = face[j + 1];
int v2 = face[j + 2];
for(k = 1; k <= vertex_face[v0][0]; k++) {
int l = vertex_face[v0][k];
if(i == l || (smoothgroup && smoothgroup[i] & smoothgroup[l]))
vector_add(&normal_vertex[j + 0],&normal_face[l],
&normal_vertex[j + 0]);
}
for(k = 1; k <= vertex_face[v1][0]; k++) {
int l = vertex_face[v1][k];
if(i == l || (smoothgroup && smoothgroup[i] & smoothgroup[l]))
vector_add(&normal_vertex[j + 1],&normal_face[l],
&normal_vertex[j + 1]);
}
for(k = 1; k <= vertex_face[v2][0]; k++) {
int l = vertex_face[v2][k];
if(i == l || (smoothgroup && smoothgroup[i] & smoothgroup[l]))
vector_add(&normal_vertex[j + 2],&normal_face[l],
&normal_vertex[j + 2]);
}
}
for(i = 0; i < trimesh->num_face * 3; i++) {
vector_normalize(&normal_vertex[i],&normal_vertex[i]);
free(vertex_face[i]);
}
free(normal_face);
free(vertex_count);
free(vertex_face);
/* create linear arrays for vertex and uvmap */
vertex = (vector_t*)malloc(sizeof(vector_t) * trimesh->num_face * 3);
memset(vertex,0,sizeof(vector_t) * trimesh->num_face * 3);
uvmap = (uvmap_t*)malloc(sizeof(uvmap_t) * trimesh->num_face * 3);
memset(uvmap,0,sizeof(uvmap_t) * trimesh->num_face * 3);
for(i = 0; i < trimesh->num_face * 3; i += 3) {
vector_copy(&trimesh->vertex[trimesh->face[i + 0]],&vertex[i + 0]);
vector_copy(&trimesh->vertex[trimesh->face[i + 1]],&vertex[i + 1]);
vector_copy(&trimesh->vertex[trimesh->face[i + 2]],&vertex[i + 2]);
}
free(trimesh->vertex);
trimesh->vertex = vertex;
trimesh->num_vertex = trimesh->num_face * 3;
if(trimesh->uvmap) {
for(i = 0; i < trimesh->num_face * 3; i += 3) {
uvmap[i + 0].u = trimesh->uvmap[trimesh->face[i + 0]].u;
uvmap[i + 0].v = trimesh->uvmap[trimesh->face[i + 0]].v;
uvmap[i + 1].u = trimesh->uvmap[trimesh->face[i + 1]].u;
uvmap[i + 1].v = trimesh->uvmap[trimesh->face[i + 1]].v;
uvmap[i + 2].u = trimesh->uvmap[trimesh->face[i + 2]].u;
uvmap[i + 2].v = trimesh->uvmap[trimesh->face[i + 2]].v;
}
free(trimesh->uvmap);
}
trimesh->uvmap = uvmap;
trimesh->num_uvmap = trimesh->num_face * 3;
free(trimesh->face);
trimesh->face = NULL;
}
static float *create_mesh(mesh_t *mesh,int *num_vertex) {
int i,j;
float *vertex;
for(i = 0, *num_vertex = 0; i < mesh->num_trimesh; i++) {
create_trimesh(&mesh->trimesh[i]);
*num_vertex += mesh->trimesh[i].num_face;
}
*num_vertex *= 3;
vertex = (float*)malloc(sizeof(float) * *num_vertex * 8);
for(i = 0, j = 0; i < mesh->num_trimesh; i++) {
int k;
for(k = 0; k < mesh->trimesh[i].num_face * 3; k++, j += 8) {
vertex[j + 0] = mesh->trimesh[i].vertex[k].x;
vertex[j + 1] = mesh->trimesh[i].vertex[k].y;
vertex[j + 2] = mesh->trimesh[i].vertex[k].z;
vertex[j + 3] = mesh->trimesh[i].normal[k].x;
vertex[j + 4] = mesh->trimesh[i].normal[k].y;
vertex[j + 5] = mesh->trimesh[i].normal[k].z;
vertex[j + 6] = mesh->trimesh[i].uvmap[k].u;
vertex[j + 7] = mesh->trimesh[i].uvmap[k].v;
}
if(mesh->trimesh[i].vertex) free(mesh->trimesh[i].vertex);
if(mesh->trimesh[i].normal) free(mesh->trimesh[i].normal);
if(mesh->trimesh[i].uvmap) free(mesh->trimesh[i].uvmap);
if(mesh->trimesh[i].face) free(mesh->trimesh[i].face);
if(mesh->trimesh[i].smoothgroup) free(mesh->trimesh[i].smoothgroup);
}
free(mesh->trimesh);
return vertex;
}
float *Load3DS(char *name,int *num_vertex) {
FILE *file;
mesh_t *mesh;
float *vertex;
unsigned short chunk_type;
int chunk_size,content_size;
file = fopen(name,"rb");
if(!file) {
fprintf(stderr,"error open %s file\n",name);
return NULL;
}
chunk_type = read_ushort(file);
chunk_size = read_int(file);
content_size = chunk_size - 6;
if(chunk_type != CHUNK_MAIN) {
fprintf(stderr,"wrong main chunk\n");
fclose(file);
return NULL;
}
mesh = (mesh_t*)malloc(sizeof(mesh_t));
memset(mesh,0,sizeof(mesh_t));
read_chunks(file,content_size,process_main,(void*)mesh);
fclose(file);
vertex = create_mesh(mesh,num_vertex);
free(mesh);
return vertex;
}