]> Dogcows Code - chaz/rasterize/blob - model.c
import project 3 files; added blending
[chaz/rasterize] / model.c
1
2 /*
3 * CS5600 University of Utah
4 * Charles McGarvey
5 * mcgarvey@eng.utah.edu
6 */
7
8 #include <errno.h>
9
10 #include "array.h"
11 #include "tri.h"
12 #include "model.h"
13
14
15 // create an interface for a vector array
16 DEFINE_ARRAY_TYPE(vec);
17
18
19 struct model
20 {
21 list_t* triangles;
22 mat_t model;
23 color_t specular;
24 scal_t shininess;
25 char* name;
26 int count;
27 };
28
29
30 static int _model_read_raw(model_t* m, const char* filename);
31 static int _model_read_obj(model_t* m, const char* filename);
32
33 static int _model_try_read_cache(const char* filename, list_t** l);
34 static void _model_write_cache(const char* filename, int count, list_t* l);
35
36
37 model_t* model_alloc(const char* filename)
38 {
39 int type = 0;
40 char* ext = strrchr(filename, '.');
41 if (ext == NULL) {
42 goto fail;
43 }
44 ++ext;
45
46 if (strcmp(ext, "raw") == 0) {
47 return model_alloc2(filename, MODEL_TYPE_RAW);
48 }
49 if (strcmp(ext, "obj") == 0) {
50 return model_alloc2(filename, MODEL_TYPE_OBJ);
51 }
52
53 fail:
54 fprintf(stderr, "Unknown file type: %s", filename);
55 return NULL;
56 }
57
58 model_t* model_alloc2(const char* filename, int type)
59 {
60 model_t* m = (model_t*)mem_alloc(sizeof(model_t));
61 m->triangles = NULL;
62 m->model = MAT_IDENTITY;
63 m->specular = COLOR_WHITE;
64 m->shininess = S(64.0);
65 m->name = mem_strdup(filename);
66 m->count = 0;
67
68 #if CACHE_GEOMETRY
69 int count = _model_try_read_cache(filename, &m->triangles);
70 if (0 < count) {
71 return m;
72 }
73 #endif
74
75 int load;
76 switch (type) {
77 case MODEL_TYPE_RAW:
78 load = _model_read_raw(m, filename);
79 break;
80 case MODEL_TYPE_OBJ:
81 load = _model_read_obj(m, filename);
82 break;
83 }
84 if (load != 0) {
85 model_destroy(m);
86 return NULL;
87 }
88
89 #if CACHE_GEOMETRY
90 _model_write_cache(filename, m->count, m->triangles);
91 #endif
92
93 return m;
94 }
95
96 void model_destroy(model_t* m)
97 {
98 list_destroy(&m->triangles);
99 mem_free(m->name);
100 mem_free(m);
101 }
102
103
104 const list_t* model_geometry(const model_t* m)
105 {
106 return m->triangles;
107 }
108
109 int model_size(const model_t* m)
110 {
111 return m->count;
112 }
113
114 const char* model_name(const model_t* m)
115 {
116 return m->name;
117 }
118
119 color_t model_specular(const model_t* m)
120 {
121 return m->specular;
122 }
123
124 scal_t model_shininess(const model_t* m)
125 {
126 return m->shininess;
127 }
128
129 void model_transformation(const model_t* m, mat_t* transform)
130 {
131 *transform = m->model;
132 }
133
134
135 void model_transform(model_t* m, const mat_t* transform)
136 {
137 m->model = mat_mult(m->model, *transform);
138 }
139
140 void model_material(model_t* m, color_t specular, scal_t shininess)
141 {
142 m->specular = specular;
143 m->shininess = shininess;
144 }
145
146
147 #if CALC_NORMALS
148
149 #include "map.h"
150 DEFINE_MAP_TYPE3(vec_t, list_t*, vnorm, vec_compare(*a, *b));
151
152
153 /*
154 * Associate a triangle with one of its vertices.
155 */
156 static void _find_normals_add_vertex(map_t* m, vec_t v, tri_t* t)
157 {
158 list_t** l = map_vnorm_search(m, v);
159 if (l == NULL) {
160 map_vnorm_data_t* d = map_vnorm_insert(m, v, NULL);
161 l = &d->val;
162 }
163 list_push(l, t);
164 }
165
166 /*
167 * Associate a triangle with all of its vertices.
168 */
169 static void _find_normals_add_triangle(map_t* m, tri_t* t)
170 {
171 _find_normals_add_vertex(m, t->a.v, t);
172 _find_normals_add_vertex(m, t->b.v, t);
173 _find_normals_add_vertex(m, t->c.v, t);
174 }
175
176 /*
177 * Calculate an averaged normal from a list of triangles that share a common
178 * vertex.
179 */
180 static void _find_normals_average(const vec_t* v, list_t** l)
181 {
182 // first, compute the average normal
183 vec_t n = VEC_ZERO;
184 for (list_t* i = *l; i; i = i->link) {
185 tri_t* t = (tri_t*)i->val;
186 n = vec_add(n, tri_normal(*t));
187 }
188 n = vec_normalize(n);
189
190 // set the normal on each triangle's vertex that is shared
191 while (*l) {
192 tri_t* t = (tri_t*)(*l)->val;
193 if (vec_isequal(*v, t->a.v)) {
194 t->a.n = n;
195 }
196 else if (vec_isequal(*v, t->b.v)) {
197 t->b.n = n;
198 }
199 else if (vec_isequal(*v, t->c.v)) {
200 t->c.n = n;
201 }
202 list_pop(l);
203 }
204 }
205
206 #endif // CALC_NORMALS
207
208
209 static int _model_read_raw(model_t* m, const char* filename)
210 {
211 FILE* file = fopen(filename, "r");
212 if (file == NULL) {
213 fprintf(stderr, "Cannot read %s: %s\n", filename, strerror(errno));
214 return -1;
215 }
216
217 #if CALC_NORMALS
218 map_t* nlookup = map_vnorm_alloc();
219 #endif
220
221 double x1, y1, z1, x2, y2, z2, x3, y3, z3;
222 while (fscanf(file, " %lf %lf %lf %lf %lf %lf %lf %lf %lf",
223 &x1, &y1, &z1, &x2, &y2, &z2, &x3, &y3, &z3) == 9) {
224 tri_t* t = tri_alloc(
225 vert_new2((scal_t)x1, (scal_t)y1, (scal_t)z1),
226 vert_new2((scal_t)x2, (scal_t)y2, (scal_t)z2),
227 vert_new2((scal_t)x3, (scal_t)y3, (scal_t)z3)
228 );
229 list_push2(&m->triangles, t, mem_free);
230 ++m->count;
231
232 #if CALC_NORMALS
233 _find_normals_add_triangle(nlookup, t);
234 #else
235 vec_t n = vec_normalize(tri_normal(*t));
236 t->a.n = n;
237 t->b.n = n;
238 t->c.n = n;
239 #endif
240 }
241
242 #if CALC_NORMALS
243 map_vnorm_call(nlookup, _find_normals_average);
244 rbtree_destroy(nlookup);
245 #endif
246
247 fclose(file);
248 if (m->triangles == NULL) {
249 fprintf(stderr, "No triangles read from %s\n", filename);
250 return -1;
251 }
252 return 0;
253 }
254
255 static int _model_read_obj(model_t* m, const char* filename)
256 {
257 FILE* file = fopen(filename, "r");
258 if (file == NULL) {
259 fprintf(stderr, "Cannot read %s: %s\n", filename, strerror(errno));
260 return -1;
261 }
262
263 #if CALC_NORMALS
264 map_t* nlookup = map_vnorm_alloc();
265 #endif
266
267 array_t* v = array_vec_alloc();
268 array_t* vt = array_vec_alloc();
269 array_t* vn = array_vec_alloc();
270
271 array_vec_push(v, VEC_ZERO);
272 array_vec_push(vt, VEC_ZERO);
273 array_vec_push(vn, VEC_ZERO);
274
275 char line[4096];
276 while (fgets(line, 4096, file)) {
277 char name[4096];
278 double f1, f2, f3, f4;
279 int i1, i2, i3, i4, i5, i6, i7, i8, i9;
280 if (sscanf(line, "v %lf %lf %lf %lf ", &f1, &f2, &f3, &f4) == 4) {
281 array_vec_push(v, vec_new2((scal_t)f1, (scal_t)f2, (scal_t)f3, (scal_t)f4));
282 }
283 else if (sscanf(line, "v %lf %lf %lf ", &f1, &f2, &f3) == 3) {
284 array_vec_push(v, vec_new((scal_t)f1, (scal_t)f2, (scal_t)f3));
285 }
286 else if (sscanf(line, "vt %lf %lf ", &f1, &f2) == 2) {
287 array_vec_push(vt, vec_new((scal_t)f1, (scal_t)f2, S(0.0)));
288 }
289 else if (sscanf(line, "vn %lf %lf %lf ", &f1, &f2, &f3) == 3) {
290 array_vec_push(vn, vec_new((scal_t)f1, (scal_t)f2, (scal_t)f3));
291 }
292 else if (sscanf(line, "f %d %d %d ", &i1, &i2, &i3) == 3) {
293 tri_t* t = tri_alloc(
294 vert_new(*array_vec_index(v, i1)),
295 vert_new(*array_vec_index(v, i2)),
296 vert_new(*array_vec_index(v, i3))
297 );
298 list_push2(&m->triangles, t, mem_free);
299 ++m->count;
300
301 #if CALC_NORMALS
302 _find_normals_add_triangle(nlookup, t);
303 #else
304 vec_t n = vec_normalize(tri_normal(*t));
305 t->a.n = n;
306 t->b.n = n;
307 t->c.n = n;
308 #endif
309 }
310 else if (sscanf(line, "f %d/%d %d/%d %d/%d ", &i1, &i2, &i3, &i4, &i5, &i6) == 6) {
311 tri_t* t = tri_alloc(
312 vert_new(*array_vec_index(v, i1)),
313 vert_new(*array_vec_index(v, i3)),
314 vert_new(*array_vec_index(v, i5))
315 );
316 list_push2(&m->triangles, t, mem_free);
317 ++m->count;
318
319 #if CALC_NORMALS
320 _find_normals_add_triangle(nlookup, t);
321 #else
322 vec_t n = vec_normalize(tri_normal(*t));
323 t->a.n = n;
324 t->b.n = n;
325 t->c.n = n;
326 #endif
327 }
328 else if (sscanf(line, "f %d//%d %d//%d %d//%d ",
329 &i1, &i2, &i3, &i4, &i5, &i6) == 6) {
330 tri_t* t = tri_alloc(
331 vert_new(*array_vec_index(v, i1)),
332 vert_new(*array_vec_index(v, i3)),
333 vert_new(*array_vec_index(v, i5))
334 );
335 list_push2(&m->triangles, t, mem_free);
336 ++m->count;
337 t->a.n = *array_vec_index(vn, i2);
338 t->b.n = *array_vec_index(vn, i4);
339 t->c.n = *array_vec_index(vn, i6);
340 }
341 else if (sscanf(line, "f %d/%d/%d %d/%d/%d %d/%d/%d ",
342 &i1, &i2, &i3, &i4, &i5, &i6, &i7, &i8, &i9) == 9) {
343 tri_t* t = tri_alloc(
344 vert_new(*array_vec_index(v, i1)),
345 vert_new(*array_vec_index(v, i4)),
346 vert_new(*array_vec_index(v, i7))
347 );
348 list_push2(&m->triangles, t, mem_free);
349 ++m->count;
350 t->a.n = *array_vec_index(vn, i3);
351 t->b.n = *array_vec_index(vn, i6);
352 t->c.n = *array_vec_index(vn, i9);
353 }
354 }
355
356 array_destroy(v);
357 array_destroy(vt);
358 array_destroy(vn);
359
360 #if CALC_NORMALS
361 map_vnorm_call(nlookup, _find_normals_average);
362 rbtree_destroy(nlookup);
363 #endif
364
365 fclose(file);
366 if (m->triangles == NULL) {
367 fprintf(stderr, "No triangles read from %s\n", filename);
368 return -1;
369 }
370 return 0;
371 }
372
373
374 #define _CHECK_IO(X) if ((X) <= 0) goto fail
375
376 /*
377 * Try to read the triangle geometry from the cache file.
378 */
379 static int _model_try_read_cache(const char* filename, list_t** l)
380 {
381 int count = 0;
382 char* cachename = mem_strcat(".", filename);
383 FILE* file = fopen(cachename, "rb");
384 if (file == NULL) {
385 goto fail;
386 }
387
388 _CHECK_IO(fread(&count, sizeof(count), 1, file));
389
390 float x1, y1, z1, x2, y2, z2, x3, y3, z3;
391 for (int i = 0; i < count; ++i) {
392 _CHECK_IO(fread(&x1, sizeof(float), 1, file));
393 _CHECK_IO(fread(&y1, sizeof(float), 1, file));
394 _CHECK_IO(fread(&z1, sizeof(float), 1, file));
395 _CHECK_IO(fread(&x2, sizeof(float), 1, file));
396 _CHECK_IO(fread(&y2, sizeof(float), 1, file));
397 _CHECK_IO(fread(&z2, sizeof(float), 1, file));
398 _CHECK_IO(fread(&x3, sizeof(float), 1, file));
399 _CHECK_IO(fread(&y3, sizeof(float), 1, file));
400 _CHECK_IO(fread(&z3, sizeof(float), 1, file));
401 tri_t* t = tri_alloc(
402 vert_new2((scal_t)x1, (scal_t)y1, (scal_t)z1),
403 vert_new2((scal_t)x2, (scal_t)y2, (scal_t)z2),
404 vert_new2((scal_t)x3, (scal_t)y3, (scal_t)z3)
405 );
406 list_push2(l, t, mem_free);
407 _CHECK_IO(fread(&x1, sizeof(float), 1, file));
408 _CHECK_IO(fread(&y1, sizeof(float), 1, file));
409 _CHECK_IO(fread(&z1, sizeof(float), 1, file));
410 _CHECK_IO(fread(&x2, sizeof(float), 1, file));
411 _CHECK_IO(fread(&y2, sizeof(float), 1, file));
412 _CHECK_IO(fread(&z2, sizeof(float), 1, file));
413 _CHECK_IO(fread(&x3, sizeof(float), 1, file));
414 _CHECK_IO(fread(&y3, sizeof(float), 1, file));
415 _CHECK_IO(fread(&z3, sizeof(float), 1, file));
416 t->a.n = vec_new((scal_t)x1, (scal_t)y1, (scal_t)z1);
417 t->b.n = vec_new((scal_t)x2, (scal_t)y2, (scal_t)z2);
418 t->c.n = vec_new((scal_t)x3, (scal_t)y3, (scal_t)z3);
419 }
420
421 fail:
422 mem_free(cachename);
423 if (file != NULL) {
424 fclose(file);
425 }
426
427 return count;
428 }
429
430 /*
431 * Write the triangle data to the cache.
432 */
433 static void _model_write_cache(const char* filename, int count, list_t* l)
434 {
435 char* cachename = mem_strcat(".", filename);
436 FILE* file = fopen(cachename, "wb");
437 if (file == NULL) {
438 fprintf(stderr, "Cannot write %s: %s\n", cachename, strerror(errno));
439 goto fail;
440 }
441
442 _CHECK_IO(fwrite(&count, sizeof(count), 1, file));
443 for (list_t* i = l; i; i = i->link) {
444 tri_t* t = (tri_t*)i->val;
445 _CHECK_IO(fwrite(&t->a.v.x, sizeof(float), 1, file));
446 _CHECK_IO(fwrite(&t->a.v.y, sizeof(float), 1, file));
447 _CHECK_IO(fwrite(&t->a.v.z, sizeof(float), 1, file));
448 _CHECK_IO(fwrite(&t->b.v.x, sizeof(float), 1, file));
449 _CHECK_IO(fwrite(&t->b.v.y, sizeof(float), 1, file));
450 _CHECK_IO(fwrite(&t->b.v.z, sizeof(float), 1, file));
451 _CHECK_IO(fwrite(&t->c.v.x, sizeof(float), 1, file));
452 _CHECK_IO(fwrite(&t->c.v.y, sizeof(float), 1, file));
453 _CHECK_IO(fwrite(&t->c.v.z, sizeof(float), 1, file));
454 _CHECK_IO(fwrite(&t->a.n.x, sizeof(float), 1, file));
455 _CHECK_IO(fwrite(&t->a.n.y, sizeof(float), 1, file));
456 _CHECK_IO(fwrite(&t->a.n.z, sizeof(float), 1, file));
457 _CHECK_IO(fwrite(&t->b.n.x, sizeof(float), 1, file));
458 _CHECK_IO(fwrite(&t->b.n.y, sizeof(float), 1, file));
459 _CHECK_IO(fwrite(&t->b.n.z, sizeof(float), 1, file));
460 _CHECK_IO(fwrite(&t->c.n.x, sizeof(float), 1, file));
461 _CHECK_IO(fwrite(&t->c.n.y, sizeof(float), 1, file));
462 _CHECK_IO(fwrite(&t->c.n.z, sizeof(float), 1, file));
463 }
464
465 fail:
466 mem_free(cachename);
467 if (file != NULL) {
468 fclose(file);
469 }
470 }
471
472 #undef _CHECK_IO
473
474
This page took 0.056885 seconds and 4 git commands to generate.