]> Dogcows Code - chaz/rasterize/blob - main.c
add opengl support
[chaz/rasterize] / main.c
1
2 /*
3 * CS5600 University of Utah
4 * Charles McGarvey
5 * mcgarvey@eng.utah.edu
6 */
7
8 #define _POSIX_C_SOURCE 2
9 #include <errno.h>
10 #include <unistd.h>
11
12 #include "raster.h"
13 #include "scene.h"
14
15
16 /*
17 * Load a scene from a file.
18 */
19 static int load(scene_t** scene, const char* filename)
20 {
21 FILE* file = fopen(filename, "r");
22 if (file == NULL) {
23 fprintf(stderr, "Cannot read %s: %s\n", filename, strerror(errno));
24 return -1;
25 }
26
27 *scene = scene_alloc(file);
28 if (*scene == NULL) {
29 fclose(file);
30 return -1;
31 }
32
33 fclose(file);
34 return 0;
35 }
36
37 /*
38 * Load a scene from standard input.
39 */
40 static int load_from_stdin(scene_t** scene)
41 {
42 *scene = scene_alloc(stdin);
43 if (*scene == NULL) {
44 return -1;
45 }
46 return 0;
47 }
48
49 /*
50 * Load a scene file, render it, and export it.
51 */
52 static void draw(const char* filename)
53 {
54 scene_t* scene;
55 TRY_DO("Loading %s", load(&scene, filename), filename);
56
57 #if OPENGL
58 scene_render_gl(scene);
59 scene_destroy(scene);
60 #else
61 raster_t* raster = scene_render(scene);
62 scene_destroy(scene);
63 raster_printstats(raster);
64
65 char* u3d = mem_strdup(filename);
66 strcut(u3d, '.');
67 #if EXPORT_PPM
68 char* ppm = mem_strcat(u3d, ".ppm");
69 TRY_DO("Exporting to %s", raster_export_ppm(raster, ppm), ppm);
70 mem_free(ppm);
71 #endif
72 #if EXPORT_BMP
73 char* bmp = mem_strcat(u3d, ".bmp");
74 TRY_DO("Exporting to %s", raster_export_bmp(raster, bmp), bmp);
75 mem_free(bmp);
76 #endif
77
78 mem_free(u3d);
79 raster_destroy(raster);
80 #endif // OPENGL
81 }
82
83 /*
84 * Render a scene that is read from standard input, and export to BMP.
85 */
86 static void draw_from_stdin(const char* filename)
87 {
88 scene_t* scene;
89 TRY_DO("Loading from stdin", load_from_stdin(&scene));
90
91 #if OPENGL
92 scene_render_gl(scene);
93 scene_destroy(scene);
94 #else
95 raster_t* raster = scene_render(scene);
96 scene_destroy(scene);
97 raster_printstats(raster);
98
99 TRY_DO("Exporting to %s", raster_export_bmp(raster, filename), filename);
100
101 raster_destroy(raster);
102 #endif // OPENGL
103 }
104
105 /*
106 * Render one or more scenes from 3D scene files.
107 */
108 int main(int argc, char* argv[])
109 {
110 int out = 0;
111 int c;
112 while ((c = getopt(argc, argv, "o:")) != -1) {
113 switch (c) {
114 case 'o':
115 ++out;
116 draw_from_stdin(optarg);
117 }
118 }
119
120 if (out == 0 && argc <= 1) {
121 draw("scene.u3d");
122 }
123 else {
124 for (int i = optind; i < argc; ++i) {
125 draw(argv[i]);
126 }
127 }
128
129 #if !NDEBUG
130 int _blocks = mem_blocks();
131 if (_blocks != 0) {
132 fprintf(stderr, " *** Leaked %d blocks of memory! ***\n", _blocks);
133 return EXIT_FAILURE;
134 }
135 #endif
136 return EXIT_SUCCESS;
137 }
138
This page took 0.033863 seconds and 4 git commands to generate.