#include #include "images/images.h" #include "marcher.h" #define SCENE_MOD 2 /* Infinite circle scene object This circle repeats every SCENE_MOD units in all directions It has some basic shading, calculation the hit angle and making shallower impacts darker */ double circle_dist(Point x, SceneObject *self) { double r = self->args[0]; return pt_dist(pt_mod(x, SCENE_MOD), self->location) - r; } Color circle_color(Point hit, Point direction, SceneObject *self) { Point obj_direction = self->location; pt_sub(&obj_direction, pt_mod(hit, SCENE_MOD)); double angle = pt_angle(direction, obj_direction) / M_PI * 180; Color color = self->color; if (angle > 90) angle = 180 - angle ; // clamp angle to 0-90 color = color_mix(color, color_new(0,0,0), 1 - (angle / (double) 120)); return color; } // constructs the scene object SceneObject circle_new(Point loc, double radius) { SceneObject so; so.location = loc; so.args = malloc(sizeof(double) * 2); so.args[0] = radius; so.distance = circle_dist; so.get_color = circle_color; so.color = color_new(255,255,255); return so; } /* Mandelbulb scene object Currently cannot be set at a specific location, always resides at origin (0,0,0) Color function is just a flat shader, detail is displayed with ambient occlusion */ double mandelbulb_dist(Point pt, SceneObject *self) { int iters = self->args[0]; double power = self->args[1]; Point z = pt; float dr = 1.0; float r = 0.0; for (int i = 0; i < iters ; i++) { r = pt_length(z); if (r>2) { break; } // convert to polar coordinates float theta = acos(z.z/r); float phi = atan2(z.y,z.x); dr = pow(r, power-1.0)*power*dr + 1.0; // scale and rotate the point float zr = pow(r, power); theta = theta*power; phi = phi*power; // convert back to cartesian coordinates z = pt_mult(pt_new(sin(theta)*cos(phi), sin(phi)*sin(theta), cos(theta)), zr); pt_add(&z, pt); } return 0.5*log(r)*r/dr; } Color mandelbulb_color(Point hit, Point direction, SceneObject *self) { return self->color; } // constructs the scene object SceneObject mandelbulb_new(Point location, int iters, double power) { SceneObject so; so.location = location; so.args = malloc(sizeof(double) * 3); so.args[0] = iters; // iterations so.args[1] = power; // power so.args[2] = -1; // reserved for color calculations so.distance = mandelbulb_dist; so.get_color = mandelbulb_color; so.color = color_new(255,255,255); return so; } int main(int argc, char* argv[]) { float dpi = 800; int threads = 32; int size = dpi * 27.56f; // 400dpi by 70cm size float pow = 3; float cam_position = 1.15; int steps = 1000; int iters = 500; float threshold = 0.001; char path[80]; // get params from cli if (argc < 5) { return 1; } pow = atof(argv[1]); steps = atoi(argv[2]); iters = atoi(argv[3]); threshold = atof(argv[4]); sprintf(path, "tries/pow%.2f-%ist-%iit-%.5fth-%.0fdpi.bmp", pow, steps, iters, threshold, dpi); printf("Rendering to %s\n", path); Camera cam; cam.fov = 90; camera_set_looking_at(&cam, pt_new(cam_position, cam_position, cam_position), pt_new(0,0,0)); // create basic scene with up to 10 objects Scene scene = scene_new(size, size, 1); scene.perf_opts.max_steps = steps; scene.perf_opts.threshold = threshold; scene.perf_opts.speed_cutoff = 10; scene.background = color_new(0,0,0); //scene_add_obj(&scene, circle_new(pt_new(SCENE_MOD / 2.0, SCENE_MOD/ 2.0, SCENE_MOD / 2.0), .2)); scene_add_obj(&scene, mandelbulb_new(pt_new(0,0,0), iters, pow)); Image *img = render_scene(&scene, &cam, threads); image_save_bmp(*img, path); image_destroy_shared(*img); scene_destroy(scene); return 0; }