10 return rand() / (RAND_MAX + 1.0);
13 typedef void (*mapfunc_t) (double x_in, double y_in, double *x_out, double *y_out, double *z_out);
14 typedef void (*colorfunc_t) (double x, double y, double z, double *r, double *g, double *b);
16 void color_test(double x, double y, double z, double *r, double *g, double *b)
18 // put in a nice function here
24 double mandelbrot_iter(double zx, double zy, double cx, double cy, int maxiter)
33 for(i = 1; i < maxiter; ++i)
36 zx = zx * zx - zy * zy + cx;
37 zy = 2 * tmp * zy + cy;
39 f = zx * zx + zy * zy;
48 // f: the greater, the more in 0 direction
49 // the smaller, the more in 1 direction
51 // the greater, the more in 0 direction
52 return i + 1 / (f - 4 + 1); // f = 16: + 0, f = 4: + 1
54 // i.e. 0 for i=1, 1 for i=maxiter
57 double mandelbrot_range(double zx, double zy, double cx, double cy, int maxiter, double offset)
59 double i = mandelbrot_iter(zx, zy, cx, cy, maxiter);
60 // map linearily 1/(offset + iter) so that:
64 // f(iter) = A/(offset + iter) + B
66 // f(0) = A/offset + B = 0
67 // f(maxiter) = A/(offset + maxiter) + B = 1
69 // 1/(1/(offset + maxiter) - 1/offset) = A
70 // B = 1 + offset / maxiter
71 // A = -offset (1 + offset / maxiter)
73 // f(iter) = -offset (1 + offset / maxiter) / (offset + iter) + 1 + offset / maxiter
74 // = -offset (1 + offset / maxiter) / (offset + iter) + 1 + offset / maxiter
75 // = iter (offset + maxiter) / maxiter (offset + iter)
76 return (i * (offset + maxiter)) / ((i + offset) * maxiter);
79 double color_mandelbrot_parms[13];
80 double mandelbrot_miniter = -1;
83 double iter_mandelbrot_raw(double x, double y, double z)
85 z -= color_mandelbrot_parms[6];
90 return mandelbrot_range(color_mandelbrot_parms[4], color_mandelbrot_parms[5], color_mandelbrot_parms[0] + x * color_mandelbrot_parms[2], color_mandelbrot_parms[1] + y * color_mandelbrot_parms[3], MAXITER, color_mandelbrot_parms[9]);
95 void iter_mandelbrot_raw_initialize_min()
97 if(mandelbrot_miniter >= 0)
99 // randomly sample 256 points
101 // set that as miniter
104 mandelbrot_miniter = MAXITER;
105 for(i = 0; i < 8192; ++i)
110 double f = sqrt(x*x + y*y + z*z);
114 double a = (z - color_mandelbrot_parms[6]) / (color_mandelbrot_parms[7] - color_mandelbrot_parms[6]);
115 a = (a - color_mandelbrot_parms[8]) / (1 - color_mandelbrot_parms[8]);
118 double iterations = iter_mandelbrot_raw(x, y, z);
121 if(iterations < mandelbrot_miniter)
122 mandelbrot_miniter = iterations;
126 void color_mandelbrot(double x, double y, double z, double *r, double *g, double *b)
128 iter_mandelbrot_raw_initialize_min();
130 double iterations = iter_mandelbrot_raw(x, y, z);
131 //printf("iter = %f\n", iterations);
132 double a = (z - color_mandelbrot_parms[6]) / (color_mandelbrot_parms[7] - color_mandelbrot_parms[6]);
133 a = (a - color_mandelbrot_parms[8]) / (1 - color_mandelbrot_parms[8]);
138 iterations = iterations * a + mandelbrot_miniter * (1-a);
139 *r = pow(iterations, color_mandelbrot_parms[10]);
140 *g = pow(iterations, color_mandelbrot_parms[11]);
141 *b = pow(iterations, color_mandelbrot_parms[12]);
151 color_starfield_parms;
152 void color_starfield(double x, double y, double z, double *r, double *g, double *b)
164 fprintf(stderr, "Initializing starfield...\n");
165 starfield = malloc(sizeof(*starfield) * color_starfield_parms.n);
166 for(i = 0; i < color_starfield_parms.n; ++i)
171 starfield[i].x = rnd() * 2 - 1;
172 starfield[i].y = rnd() * 2 - 1;
173 starfield[i].z = rnd() * 2 - 1;
174 r = starfield[i].x * starfield[i].x
175 + starfield[i].y * starfield[i].y
176 + starfield[i].z * starfield[i].z;
184 starfield[i].e = color_starfield_parms.density * pow(rnd(), -color_starfield_parms.power);
186 starfield[i].R = rnd();
187 starfield[i].G = rnd();
188 starfield[i].B = rnd();
189 f = starfield[i].R * 0.299 + starfield[i].G * 0.587 + starfield[i].B * 0.114;
193 starfield[i].A = rnd();
195 fprintf(stderr, "Done.\n");
199 for(i = 0; i < color_starfield_parms.n; ++i)
201 double dot = x * starfield[i].x + y * starfield[i].y + z * starfield[i].z;
204 double f = pow(dot, starfield[i].e) * starfield[i].A;
205 *r += starfield[i].R * f;
206 *g += starfield[i].G * f;
207 *b += starfield[i].B * f;
210 *r = *r / (color_starfield_parms.darkness + *r);
211 *g = *g / (color_starfield_parms.darkness + *g);
212 *b = *b / (color_starfield_parms.darkness + *b);
215 void map_back(double x_in, double y_in, double *x_out, double *y_out, double *z_out)
217 *x_out = 2 * x_in - 1;
219 *z_out = 1 - 2 * y_in;
222 void map_right(double x_in, double y_in, double *x_out, double *y_out, double *z_out)
225 *y_out = 1 - 2 * x_in;
226 *z_out = 1 - 2 * y_in;
229 void map_front(double x_in, double y_in, double *x_out, double *y_out, double *z_out)
231 *x_out = 1 - 2 * x_in;
233 *z_out = 1 - 2 * y_in;
236 void map_left(double x_in, double y_in, double *x_out, double *y_out, double *z_out)
239 *y_out = 2 * x_in - 1;
240 *z_out = 1 - 2 * y_in;
243 void map_up(double x_in, double y_in, double *x_out, double *y_out, double *z_out)
245 *x_out = 2 * y_in - 1;
246 *y_out = 1 - 2 * x_in;
250 void map_down(double x_in, double y_in, double *x_out, double *y_out, double *z_out)
252 *x_out = 1 - 2 * y_in;
253 *y_out = 1 - 2 * x_in;
257 void writepic(colorfunc_t f, mapfunc_t m, const char *fn, int width, int height)
263 FILE *file = fopen(fn, "wb");
265 err(1, "fopen %s", fn);
267 memset(tga, 0, sizeof(tga));
268 tga[2] = 2; // uncompressed type
269 tga[12] = (width >> 0) & 0xFF;
270 tga[13] = (width >> 8) & 0xFF;
271 tga[14] = (height >> 0) & 0xFF;
272 tga[15] = (height >> 8) & 0xFF;
273 tga[16] = 24; // pixel size
275 fwrite(&tga, sizeof(tga), 1, file);
277 for(y = height-1; y >= 0; --y)
279 for(x = 0; x < width; ++x)
284 double xxx, yyy, zzz;
286 xx = (x + 0.5) / width;
287 yy = (y + 0.5) / height;
288 m(xx, yy, &xxx, &yyy, &zzz);
289 r = sqrt(xxx*xxx + yyy*yyy + zzz*zzz);
293 f(xxx, yyy, zzz, &rr, &gg, &bb);
294 rgb[2] = floor(rnd() + rr * 255);
295 rgb[1] = floor(rnd() + gg * 255);
296 rgb[0] = floor(rnd() + bb * 255);
297 fwrite(rgb, sizeof(rgb), 1, file);
299 p = (100 * (height - y)) / height;
303 fprintf(stderr, "%d%%\r", percent);
306 fprintf(stderr, "\n");
311 void map_all(const char *fn, colorfunc_t f, int width, int height)
314 fprintf(stderr, "%s_bk.tga\n", fn);
315 snprintf(buf, sizeof(buf), "%s_bk.tga", fn); buf[sizeof(buf) - 1] = 0; writepic(f, map_back, buf, width, height);
316 fprintf(stderr, "%s_ft.tga\n", fn);
317 snprintf(buf, sizeof(buf), "%s_ft.tga", fn); buf[sizeof(buf) - 1] = 0; writepic(f, map_front, buf, width, height);
318 fprintf(stderr, "%s_rt.tga\n", fn);
319 snprintf(buf, sizeof(buf), "%s_rt.tga", fn); buf[sizeof(buf) - 1] = 0; writepic(f, map_right, buf, width, height);
320 fprintf(stderr, "%s_lf.tga\n", fn);
321 snprintf(buf, sizeof(buf), "%s_lf.tga", fn); buf[sizeof(buf) - 1] = 0; writepic(f, map_left, buf, width, height);
322 fprintf(stderr, "%s_up.tga\n", fn);
323 snprintf(buf, sizeof(buf), "%s_up.tga", fn); buf[sizeof(buf) - 1] = 0; writepic(f, map_up, buf, width, height);
324 fprintf(stderr, "%s_dn.tga\n", fn);
325 snprintf(buf, sizeof(buf), "%s_dn.tga", fn); buf[sizeof(buf) - 1] = 0; writepic(f, map_down, buf, width, height);
328 int main(int argc, char **argv)
332 errx(1, "usage: %s filename res func parms...", *argv);
333 int res = atoi(argv[2]);
334 if(!strcmp(argv[3], "mandel"))
336 f = color_mandelbrot;
337 color_mandelbrot_parms[0] = argc<= 4 ? -0.740 : atof(argv[4]); // shift xy
338 color_mandelbrot_parms[1] = argc<= 5 ? -0.314 : atof(argv[5]);
339 color_mandelbrot_parms[2] = argc<= 6 ? -0.003 : atof(argv[6]); // mul xy
340 color_mandelbrot_parms[3] = argc<= 7 ? -0.003 : atof(argv[7]);
341 color_mandelbrot_parms[4] = argc<= 8 ? 0.420 : atof(argv[8]); // shift z
342 color_mandelbrot_parms[5] = argc<= 9 ? 0.000 : atof(argv[9]);
343 color_mandelbrot_parms[6] = argc<=10 ? -0.8 : atof(argv[10]); // horizon
344 color_mandelbrot_parms[7] = argc<=11 ? -0.7 : atof(argv[11]);
345 color_mandelbrot_parms[8] = argc<=12 ? 0.5 : atof(argv[12]);
346 color_mandelbrot_parms[9] = argc<=13 ? 400 : atof(argv[13]); // coloring
347 color_mandelbrot_parms[10] = argc<=14 ? 0.6 : atof(argv[14]);
348 color_mandelbrot_parms[11] = argc<=15 ? 0.5 : atof(argv[15]);
349 color_mandelbrot_parms[12] = argc<=16 ? 0.2 : atof(argv[16]);
351 else if(!strcmp(argv[3], "starfield"))
354 color_starfield_parms.n = argc<= 4 ? 8192 : atoi(argv[4]);
355 color_starfield_parms.darkness = argc<= 5 ? 0.4 : atof(argv[5]);
356 color_starfield_parms.power = argc<= 6 ? 2.5 : atof(argv[6]);
357 color_starfield_parms.density = argc<= 7 ? 60000 : atof(argv[7]);
363 map_all(argv[1], f, res, res);