10 #define DFL_GAMMA 2.2f
11 #define DFL_OUTFILE "output.ppm"
13 static int save_image(const char *fname, float *pix, int xsz, int ysz);
14 static int parse_opt(int argc, char **argv);
16 static int width = DFL_WIDTH, height = DFL_HEIGHT;
17 static float inv_gamma = 1.0f / DFL_GAMMA;
18 static const char *out_fname = DFL_OUTFILE;
20 int main(int argc, char **argv)
22 csg_object *oa, *ob, *oc, *obj;
25 if(parse_opt(argc, argv) == -1) {
29 if(csg_init() == -1) {
33 if(!(pixels = malloc(width * height * 3 * sizeof *pixels))) {
34 perror("failed to allocate framebuffer");
38 csg_view(0, 0, 5, 0, 0, 0);
40 oa = csg_sphere(0, 0, 0, 1);
41 csg_color(oa, 1, 0.1, 0.05);
42 ob = csg_sphere(0.3, 0.7, 0.7, 0.7);
43 csg_color(ob, 0.2, 0.3, 1);
44 oc = csg_subtraction(oa, ob);
47 obj = csg_plane(0, -1, 0, 0, 1, 0);
48 csg_color(obj, 0.4, 0.7, 0.4);
51 obj = csg_null(-4, 10, 10);
52 csg_emission(obj, 80, 80, 80);
55 csg_render_image(pixels, width, height);
56 save_image(out_fname, pixels, width, height);
62 static int save_image(const char *fname, float *pix, int xsz, int ysz)
67 if(!(fp = fopen(fname, "wb"))) {
68 fprintf(stderr, "failed to open %s for writing: %s\n", fname, strerror(errno));
72 fprintf(fp, "P6\n%d %d\n255\n", xsz, ysz);
74 for(i=0; i<xsz * ysz; i++) {
75 unsigned int r = pow(*pix++, inv_gamma) * 255.0f;
76 unsigned int g = pow(*pix++, inv_gamma) * 255.0f;
77 unsigned int b = pow(*pix++, inv_gamma) * 255.0f;
91 static void print_usage(const char *argv0)
93 printf("Usage: %s [options] <csg file>\n", argv0);
95 printf(" -s <WxH> output image resolution (default: %dx%d)\n", DFL_WIDTH, DFL_HEIGHT);
96 printf(" -g <gamma> set output gamma (default: %g)\n", DFL_GAMMA);
97 printf(" -o <file> output image file (default: %s)\n", DFL_OUTFILE);
98 printf(" -h print usage information and exit\n");
101 static int parse_opt(int argc, char **argv)
105 for(i=1; i<argc; i++) {
106 if(argv[i][0] == '-') {
107 if(argv[i][2] == 0) {
110 if(sscanf(argv[++i], "%dx%d", &width, &height) != 2) {
111 fprintf(stderr, "-s must be followed by WIDTHxHEIGHT\n");
117 if((inv_gamma = atof(argv[++i])) == 0.0f) {
118 fprintf(stderr, "-g must be followed by a non-zero gamma value\n");
121 inv_gamma = 1.0f / inv_gamma;
125 out_fname = argv[++i];
129 print_usage(argv[0]);
133 fprintf(stderr, "invalid option: %s\n", argv[i]);
137 fprintf(stderr, "invalid option: %s\n", argv[i]);
141 fprintf(stderr, "unexpected argument: %s\n", argv[i]);