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 csg_roughness(oa, 0.3);
43 ob = csg_sphere(0.3, 0.7, 0.7, 0.7);
44 csg_color(ob, 0.2, 0.3, 1);
45 csg_roughness(ob, 0.3);
46 oc = csg_subtraction(oa, ob);
50 ob = csg_sphere(-0.9, -0.1, 0.7, 0.5);
51 csg_color(ob, 1, 0.9, 0.2);
52 csg_roughness(ob, 0.3);
54 oc = csg_subtraction(oa, ob);
58 obj = csg_plane(0, -1, 0, 0, 1, 0);
59 csg_color(obj, 0.4, 0.7, 0.4);
62 obj = csg_null(-4, 10, 10);
63 csg_emission(obj, 80, 80, 80);
66 csg_render_image(pixels, width, height);
67 save_image(out_fname, pixels, width, height);
73 static int save_image(const char *fname, float *pix, int xsz, int ysz)
78 if(!(fp = fopen(fname, "wb"))) {
79 fprintf(stderr, "failed to open %s for writing: %s\n", fname, strerror(errno));
83 fprintf(fp, "P6\n%d %d\n255\n", xsz, ysz);
85 for(i=0; i<xsz * ysz; i++) {
86 unsigned int r = pow(*pix++, inv_gamma) * 255.0f;
87 unsigned int g = pow(*pix++, inv_gamma) * 255.0f;
88 unsigned int b = pow(*pix++, inv_gamma) * 255.0f;
102 static void print_usage(const char *argv0)
104 printf("Usage: %s [options] <csg file>\n", argv0);
105 printf("Options:\n");
106 printf(" -s <WxH> output image resolution (default: %dx%d)\n", DFL_WIDTH, DFL_HEIGHT);
107 printf(" -g <gamma> set output gamma (default: %g)\n", DFL_GAMMA);
108 printf(" -o <file> output image file (default: %s)\n", DFL_OUTFILE);
109 printf(" -h print usage information and exit\n");
112 static int parse_opt(int argc, char **argv)
116 for(i=1; i<argc; i++) {
117 if(argv[i][0] == '-') {
118 if(argv[i][2] == 0) {
121 if(sscanf(argv[++i], "%dx%d", &width, &height) != 2) {
122 fprintf(stderr, "-s must be followed by WIDTHxHEIGHT\n");
128 if((inv_gamma = atof(argv[++i])) == 0.0f) {
129 fprintf(stderr, "-g must be followed by a non-zero gamma value\n");
132 inv_gamma = 1.0f / inv_gamma;
136 out_fname = argv[++i];
140 print_usage(argv[0]);
144 fprintf(stderr, "invalid option: %s\n", argv[i]);
148 fprintf(stderr, "invalid option: %s\n", argv[i]);
152 fprintf(stderr, "unexpected argument: %s\n", argv[i]);