-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbumpobj.c
518 lines (404 loc) · 12.4 KB
/
bumpobj.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
#include <stdio.h>
#include <stdlib.h>
#include <string.h> // for memset
#include "bumpobj.h"
#include "template.h"
#define PROJ_COEFF 256.f
typedef struct
{
int v1, v2, v3;
} facevertices_t;
/*
void sort_faces(object3d_t *);
void update_object3d(object3d_t *);
void reset_object3d(object3d_t *);
void reset_and_scale_object3d(object3d_t *, float);
*/
void compute_vertex_normals(object3d_t *);
void compute_face_normals(object3d_t* obj);
int create_face_from_template(facedata_t *, const facetempl_t *, vertexdata_t *, const char **, texturemanager_t *);
int create_from_template(object3d_t *, const point3d_t *, const facetempl_t *, const char **, int, int, texturemanager_t *);
int allocate_object3d(object3d_t* obj);
int compare_faces(const void *, const void *);
int allocate_object3d(object3d_t* obj)
{
int is_success = 0;
obj->vertices = (vertexdata_t *)malloc(obj->numpoints * sizeof(vertexdata_t));
if (obj->vertices) {
obj->faces = (facedata_t *)malloc(obj->numfaces * sizeof(facedata_t));
if (obj->faces) {
obj->faceorder = (faceorder_t *)malloc(obj->numfaces * sizeof(faceorder_t));
if (obj->faceorder) {
is_success = 1;
printf("Allocated %d vertices and %d faces (total %d bytes)\n",
obj->numpoints, obj->numfaces,
sizeof(vertexdata_t) * obj->numpoints +
(sizeof(facedata_t) + sizeof(faceorder_t)) * obj->numfaces);
}
}
}
if (!is_success) {
unload_object3d(obj);
}
return is_success;
}
void unload_object3d(object3d_t *obj)
{
if (obj->vertices) {
free(obj->vertices);
if (obj->faces) {
free(obj->faces);
if (obj->faceorder) {
free(obj->faceorder);
}
}
}
}
int create_cube(object3d_t* obj, const char** texturefiles, int numtextures, texturemanager_t* tm)
{
return create_from_template(obj, cubepoints, cubefaces, texturefiles, 8, 12, tm);
}
int create_from_template(object3d_t *obj, const point3d_t* points, const facetempl_t* faces, const char** texturefiles, int numpoints, int numfaces, texturemanager_t* tm)
{
int i, is_success;
obj->numpoints = numpoints;
obj->numfaces = numfaces;
is_success = allocate_object3d(obj);
if (is_success) {
for (i = 0; i < obj->numpoints; i++) {
copy_vector(&obj->vertices[i].point, &points[i]);
}
for (i = 0; i < obj->numfaces; i++) {
is_success = create_face_from_template(&obj->faces[i], &faces[i], obj->vertices, texturefiles, tm);
if (!is_success) {
break;
}
}
if (is_success) {
compute_face_normals(obj);
compute_vertex_normals(obj);
}
}
if (!is_success) {
unload_object3d(obj);
}
return is_success;
}
int create_face_from_template(facedata_t* face, const facetempl_t* templ, vertexdata_t* vertices, const char** texturefiles, texturemanager_t* tm)
{
int is_good;
face->v1 = &vertices[templ->v1];
face->v2 = &vertices[templ->v2];
face->v3 = &vertices[templ->v3];
// Assume all textures are 128x128
face->mapper.s1 = templ->mapper.s1 << 7;
face->mapper.t1 = templ->mapper.t1 << 7;
face->mapper.s2 = templ->mapper.s2 << 7;
face->mapper.t2 = templ->mapper.t2 << 7;
face->mapper.s3 = templ->mapper.s3 << 7;
face->mapper.t3 = templ->mapper.t3 << 7;
face->mapper.texture = get_texture(texturefiles[templ->mapper.texture_idx], tm);
is_good = (face->mapper.texture != NULL) ? 1 : 0;
return is_good;
}
void compute_face_normals(object3d_t* obj)
{
int i;
vector3d_t va, vb;
facedata_t* f = obj->faces;
for (i = 0; i < obj->numfaces; i++) {
subtract_vector(&f->v2->point, &f->v1->point, &va);
subtract_vector(&f->v3->point, &f->v2->point, &vb);
cross_product(&f->face_normal, &va, &vb);
normalize_vector(&f->face_normal);
f++;
}
}
void compute_vertex_normals(object3d_t *obj)
{
int i, j, face_hit;
vector3d_t N;
facedata_t* f;
vertexdata_t* v = obj->vertices;
for (i = 0; i < obj->numpoints; i++) {
N.x = 0;
N.y = 0;
N.z = 0;
face_hit = 0;
f = obj->faces;
for (j = 0; j < obj->numfaces; j++) {
if (f->v1 == v || f->v2 == v || f->v3 == v) {
add_vector(&N, &f->face_normal, &N);
face_hit++;
}
f++;
}
scale_vector(1 / (float)face_hit, &N, &N);
copy_vector(&v->normal, &N);
v++;
}
}
void reset_object3d(object3d_t *obj)
{
obj->x0 = 0;
obj->y0 = 0;
obj->z0 = 0;
obj->ax = 0;
obj->ay = 0;
obj->az = 0;
obj->adx = 0;
obj->ady = 0;
obj->adz = 0;
}
void reset_and_scale_object3d(object3d_t *obj, float scalefactor)
{
int i;
float maxd, currd, scale;
vector3d_t center;
vertexdata_t* v = obj->vertices;
center.x = 0;
center.y = 0;
center.z = 0;
for (i = 0; i < obj->numpoints; i++) {
add_vector(¢er, &v->point, ¢er);
v++;
}
scale_vector(1 / (float)obj->numpoints, ¢er, ¢er);
v = obj->vertices;
for (i = 0; i < obj->numpoints; i++) {
subtract_vector(&v->point, ¢er, &v->point);
v++;
}
maxd = 0;
v = obj->vertices;
for (i = 0; i < obj->numpoints; i++) {
currd = vector_len(&v->point);
if (currd > maxd) {
maxd = currd;
}
v++;
}
scale = scalefactor / maxd;
v = obj->vertices;
for (i = 0; i < obj->numpoints; i++) {
scale_vector(scale, &v->point, &v->point);
v++;
}
reset_object3d(obj);
}
void sort_faces(object3d_t *obj, vector3d_t* lookvector)
{
int i;
float x1, y1, x2, y2, x3, y3;
facedata_t* f = obj->faces;
faceorder_t* fo = obj->faceorder;
obj->numvisible = 0;
for (i = 0; i < obj->numfaces; i++) {
x1 = f->v1->translated_point.x;
y1 = f->v1->translated_point.y;
x2 = f->v2->translated_point.x;
y2 = f->v2->translated_point.y;
x3 = f->v3->translated_point.x;
y3 = f->v3->translated_point.y;
if (((y1 - y3) * (x2 - x1) - (x1 - x3) * (y2 - y1)) <= 0) {
//if (dot_product(&f->rotated_normal, lookvector) >= 0) {
fo->face = f;
fo->depth = f->v1->rotated_point.x;
fo->depth += f->v2->rotated_point.x;
fo->depth += f->v3->rotated_point.x;
obj->numvisible++;
fo++;
}
f++;
}
qsort(obj->faceorder, obj->numvisible, sizeof(faceorder_t), compare_faces);
}
int compare_faces(const void* a, const void* b)
{
float da, db;
da = ((faceorder_t *) a)->depth;
db = ((faceorder_t *) b)->depth;
if (da > db)
return -1;
if (da < db)
return 1;
return 0;
}
void update_object3d(object3d_t *obj)
{
int i;
matrix_t m;
facedata_t* f = obj->faces;
vertexdata_t* v = obj->vertices;
obj->ax += obj->adx;
obj->ay += obj->ady;
obj->az += obj->adz;
compute_rotation_matrix(m, obj->ax, obj->ay, obj->az);
for (i = 0; i < obj->numfaces; i++) {
transform_vector(&f->face_normal, m, &f->rotated_normal);
f++;
}
for (i = 0; i < obj->numpoints; i++) {
transform_vector(&v->point, m, &v->rotated_point);
transform_vector(&v->normal, m, &v->rotated_normal);
if (v->rotated_point.x == 0.)
v->ooz = 1. / (v->rotated_point.x + 0.0001);
else
v->ooz = 1. / v->rotated_point.x;
if ((v->rotated_point.x + PROJ_COEFF) == 0.)
v->oow = 1. / (v->rotated_point.x + PROJ_COEFF + 0.0001);
else
v->oow = 1. / (v->rotated_point.x + PROJ_COEFF);
v->translated_point.x = v->rotated_point.y * PROJ_COEFF * v->oow;
v->translated_point.y = -v->rotated_point.z * PROJ_COEFF * v->oow;
v++;
}
}
int set_envmap(object3d_t* obj, char* envmap_file, texturemanager_t* tm)
{
int i, is_good = 0;
facedata_t* f = obj->faces;
texture_t* envmap = get_texture(envmap_file, tm);
if (envmap != NULL) {
is_good = 1;
for (i = 0; i < obj->numfaces; i++, f++) {
f->mapper.texture = envmap;
}
}
return is_good;
}
void load_object_data(object3d_t* obj, FILE* p)
{
int i;
unsigned short tmpvertices[3];
vertexdata_t* v = obj->vertices;
facedata_t* f = obj->faces;
// read vertices
for (i = 0; i < obj->numpoints; i++) {
fread(&v->point, sizeof(point3d_t), 1, p);
v++;
}
// read faces
for (i = 0; i < obj->numfaces; i++) {
fread(tmpvertices, sizeof(tmpvertices), 1, p);
f->v1 = &obj->vertices[tmpvertices[0]];
f->v2 = &obj->vertices[tmpvertices[1]];
f->v3 = &obj->vertices[tmpvertices[2]];
// No texture available
f->mapper.texture = NULL;
f++;
}
}
int load_object3d(object3d_t* obj, char* filename)
{
FILE* p;
char header[5];
int is_success = 0;
p = fopen(filename, "r+b");
if (p) {
fread(header, 1, 4, p);
header[4] = 0;
if (strcmp(header, "MF3D") == 0) {
memset(obj, 0, sizeof(object3d_t));
fread(&obj->numpoints, 2, 1, p);
fread(&obj->numfaces, 2, 1, p);
is_success = allocate_object3d(obj);
if (is_success) {
load_object_data(obj, p);
compute_face_normals(obj);
compute_vertex_normals(obj);
}
}
fclose(p);
}
return is_success;
}
void load_off_vertices(object3d_t* obj, FILE* p)
{
int i;
char line[128];
vertexdata_t* v = obj->vertices;
for (i = 0; i < obj->numpoints; i++) {
if (fgets(line, 128, p)) {
sscanf(line, " %f %f %f ", &v->point.x, &v->point.y, &v->point.z);
v++;
}
}
}
void load_off_faces(object3d_t* obj, FILE* p)
{
int i;
int facetype, num1, num2, num3;
char line[128];
facedata_t* f = obj->faces;
for (i = 0; i < obj->numfaces; i++) {
if (fgets(line, 128, p)) {
sscanf(line, " %d %d %d %d ", &facetype, &num1, &num2, &num3);
if (facetype == 3) {
f->v1 = &obj->vertices[num1];
f->v2 = &obj->vertices[num2];
f->v3 = &obj->vertices[num3];
// No texture available
f->mapper.texture = NULL;
f++;
}
}
}
}
int load_off_object(object3d_t* obj, char* filename)
{
FILE* p;
int is_success = 0, dummy;
char line[128];
p = fopen(filename, "r+b");
if (p) {
fgets(line, 128, p);
if (strcmp(line, "OFF\n") == 0) {
fgets(line, 128, p);
sscanf(line, " %d %d %d ", &obj->numpoints, &obj->numfaces, &dummy);
if (allocate_object3d(obj)) {
load_off_vertices(obj, p);
load_off_faces(obj, p);
compute_face_normals(obj);
compute_vertex_normals(obj);
is_success = 1;
}
}
fclose(p);
}
return is_success;
}
int find_idx(vertexdata_t* v, object3d_t* obj)
{
int i;
vertexdata_t* vo = obj->vertices;
for (i = 0; i < obj->numpoints; i++) {
if (v == vo) {
return i;
}
vo++;
}
return -1; // should never happen
}
void save_txt_object3d(object3d_t* obj, char* filename)
{
FILE* fp;
int i;
fp = fopen(filename, "w");
if (fp != NULL) {
fprintf(fp, "%d\n%d\n", obj->numpoints, obj->numfaces);
for (i = 0; i < obj->numpoints; i++) {
fprintf(fp, "v %f, %f, %f\n", obj->vertices[i].point.x, obj->vertices[i].point.y, obj->vertices[i].point.z);
}
for (i = 0; i < obj->numfaces; i++) {
fprintf(fp, "f %d, %d, %d\n", find_idx(obj->faces[i].v1, obj), find_idx(obj->faces[i].v2, obj), find_idx(obj->faces[i].v3, obj));
}
for (i = 0; i < obj->numpoints; i++) {
fprintf(fp, "vn %f, %f, %f\n", obj->vertices[i].normal.x, obj->vertices[i].normal.y, obj->vertices[i].normal.z);
}
for (i = 0; i < obj->numfaces; i++) {
fprintf(fp, "fn %f, %f, %f\n", obj->faces[i].face_normal.x, obj->faces[i].face_normal.y, obj->faces[i].face_normal.z);
}
fclose(fp);
}
}