83878bf5e70e0dc29dddbfc93460d96312c20f3e
[chaz/rasterize] / main.c
1
2 /*
3 * CS5600 University of Utah
4 * Charles McGarvey
5 * mcgarvey@eng.utah.edu
6 */
7
8 #include <stdio.h>
9
10 #include "raster.h"
11 #include "scene.h"
12
13
14 #ifndef EXPORT_PPM
15 #define EXPORT_PPM 1
16 #endif
17 #ifndef EXPORT_BMP
18 #define EXPORT_BMP 1
19 #endif
20
21
22 /*
23 * Load a scene from a file.
24 */
25 static int load(scene_t** scene, const char* filename)
26 {
27 *scene = scene_alloc(filename);
28 if (*scene == NULL) {
29 return 1;
30 }
31 return 0;
32 }
33
34 /*
35 * Load a scene file, render it, and export it to PPM and BMP formats.
36 */
37 static void draw(const char* filename)
38 {
39 char* u3d;
40 if (strcmp(filename, "-") == 0) {
41 u3d = mem_strdup("stdin");
42 }
43 else {
44 u3d = mem_strdup(filename);
45 }
46
47 scene_t* scene;
48 TRY_DO("Loading %s", load(&scene, filename), u3d);
49
50 raster_t* raster = scene_render(scene);
51 scene_destroy(scene);
52 raster_printstats(raster);
53
54 strcut(u3d, '.');
55 #if EXPORT_PPM
56 char* ppm = mem_strcat(u3d, ".ppm");
57 TRY_DO("Exporting to %s", raster_export_ppm(raster, ppm), ppm);
58 mem_free(ppm);
59 #endif
60 #if EXPORT_BMP
61 char* bmp = mem_strcat(u3d, ".bmp");
62 TRY_DO("Exporting to %s", raster_export_bmp(raster, bmp), bmp);
63 mem_free(bmp);
64 #endif
65
66 mem_free(u3d);
67 raster_destroy(raster);
68 }
69
70 /*
71 * Render one or more scenes from 3D scene files.
72 */
73 int main(int argc, char* argv[])
74 {
75 if (argc <= 1) {
76 draw("scene.u3d");
77 }
78 else {
79 for (int i = 1; i < argc; ++i) {
80 draw(argv[i]);
81 }
82 }
83
84 #if !NDEBUG
85 int _blocks = mem_blocks();
86 if (_blocks != 0) {
87 fprintf(stderr, " *** Leaked %d blocks of memory! ***\n", _blocks);
88 return EXIT_FAILURE;
89 }
90 #endif
91 return EXIT_SUCCESS;
92 }
93
This page took 0.04173 seconds and 3 git commands to generate.