/* This source-code is (C) 2009 by folkert@vanheusden.com * and was released under GPL version 2. * * This Java program generates a script for Povray. It calculates the * 3D Mandelbrot-fractal as designed by Daniel White. * Daniel's site is at: http://www.skytopia.com/project/fractal/mandelbulb.html * It will calculate a hollow one; if you e.g. cut some of it away (by e.g. * limiting the z-direction, see *1), you'll see it is hollow. * * Please note that while this program hardly uses any memory, it the * povray-renderer does. If you would like to optimize this program, start * by replacing the box { ... } by a mesh { ... }. * * You might want to adapt the first nine final declared variables like * width/height, order, x1/y1 etc. Also the texture might be worth looking at. * * Compile with javac Fractal3DMandDW.java * Invoke with java Fractal3DMandDW > Fractal3DMandDW.pov * After that you can delete the "testdw.bitmap" file as it is entirely * temporarily. * Render the pov-file with e.g.: * povray +W800 +H600 +A0.1 +FN16 +I Fractal3DMandDW.pov * This will procude Fractal3DMandDW.png which should be viewable with all * picture viewers and webbrowsers. */ import java.io.FileOutputStream; import java.io.RandomAccessFile; class Fractal3DMandDW { final static int width = 512, height = 512, depth = 512; final static double cameraX = 512.0, cameraY = 512.0, cameraZ = 512.0; final static double cameraPointAtX = 256.0, cameraPointAtY = 256.0, cameraPointAtZ = 256.0; // the following texture is a glass-like texture. replace by e.g.: // texture { pigment { color <0.5, 1.0, 0.5> } } // to have a solid object final static String texture = "texture { pigment { color red 1.0 green 1.0 blue 1.0 filter 0.95 } finish { ambient 0.0 diffuse 0.0 reflection 0.1 phong 0.3 phong_size 90 } }"; final static int iterations = 256; final static double order = 8.0; final static double x1 = -2.0, x2 = 2.0; final static double y1 = -2.0, y2 = 2.0; final static double z1 = -2.0, z2 = 2.0; // final static double xStep = (x2 - x1) / (double)width; final static double yStep = (y2 - y1) / (double)height; final static double zStep = (z2 - z1) / (double)depth; public static boolean getBit(RandomAccessFile raf, int width, int height, int x, int y, int z) throws Exception { int bit = 7 - (x & 7); int offset = (x / 8) + (y * (width / 8)) + (z * (width / 8) * height); raf.seek(offset); int curByte = raf.readByte(); if ((curByte & (1 << bit)) != 0) return true; return false; } public static void main(String [] args) { System.err.println("Fractal3DMandDW (C) 2009 by folkert@vanheusden.com"); System.err.println(""); try { String fileName = "testdw.bitmap"; FileOutputStream fos = new FileOutputStream(fileName); int bOut = 0, bits = 0; int x, y, z; for(z=0; z= 8.0 || it > iterations) break; double r = Math.sqrt(len); double theta = Math.atan2(Math.sqrt(xck + yck), zc); double phi = Math.atan2(yc, xc); double ro = Math.pow(r, order); double sto = Math.sin(theta * order); double newx = ro * sto * Math.cos(phi*order) + a; double newy = ro * sto * Math.sin(phi*order) + b; double newz = ro * Math.cos(theta*order) + c; xc = newx; yc = newy; zc = newz; } bOut <<= 1; if (it > iterations) bOut |= 1; bits++; if (bits == 8) { fos.write(bOut); bits = bOut = 0; } } } } fos.close(); RandomAccessFile raf = new RandomAccessFile(fileName, "r"); System.out.println("camera { location<" + cameraX + ", " + cameraY + ", " + cameraZ + "> look_at<" + cameraPointAtX + ", " + cameraPointAtY + ", " + cameraPointAtZ + "> }"); System.out.println("light_source { <-3000, -3000, -3000 > color rgb <1.0, 1.0, 1.0> }"); System.out.println("light_source { <-3000, -3000, 3000 > color <1.0, 1.0, 1.0> }"); System.out.println("light_source { <-3000, 3000, -3000 > color <1.0, 1.0, 1.0> }"); System.out.println("light_source { <-3000, 3000, 3000 > color <1.0, 1.0, 1.0> }"); System.out.println("light_source { < 3000, -3000, -3000 > color <1.0, 1.0, 1.0> }"); System.out.println("light_source { < 3000, -3000, 3000 > color <1.0, 1.0, 1.0> }"); System.out.println("light_source { < 3000, 3000, -3000 > color <1.0, 1.0, 1.0> }"); System.out.println("light_source { < 3000, 3000, 3000 > color <1.0, 1.0, 1.0> }"); // for(z=1; z<(depth / 2); z++) // *1 for(z=1; z<(depth - 1); z++) { System.err.println("" + (depth - z)); for(y=1; y<(height - 1); y++) { for(x=1; x<(width - 1); x++) { if (!getBit(raf, width, height, x, y, z)) continue; int cnt = 0; cnt += getBit(raf, width, height, x - 1, y , z ) ? 1 : 0; cnt += getBit(raf, width, height, x , y - 1, z ) ? 1 : 0; cnt += getBit(raf, width, height, x + 1, y , z ) ? 1 : 0; cnt += getBit(raf, width, height, x , y + 1, z ) ? 1 : 0; cnt += getBit(raf, width, height, x , y , z - 1) ? 1 : 0; cnt += getBit(raf, width, height, x , y , z + 1) ? 1 : 0; if (cnt < 6) { System.out.println(" box{ <" + x + ", " + y + ", " + z + "> <" + (x + 1.0) + ", " + (y + 1.0) + ", " + (z + 1.0) + "> " + texture + " }"); } } } } raf.close(); } catch(Exception e) { System.out.println("Exception: " + e); } } }