Volumes Of Fun

Page 1 of 1

Author:  zprg [ Sun Jan 22, 2012 2:41 pm ]
Post subject:  mandelbulb

hey heres a code for mandelbulb
(took from: http://www.treblig.org/3dbrot/3dbrot.c
with maths from http://www.skytopia.com/project/fractal/mandelbulb.html )

const unsigned int size=256;
#define RANGE 1.2
const double xlow=-RANGE;
const double xhigh=RANGE;
const double ylow=-RANGE;
const double yhigh=RANGE;
const double zlow=-RANGE;
const double zhigh=RANGE;

const unsigned int maxiterations=80;
const double mandpow=8.0;
double r;

double valInRange(double low, double high, unsigned int size, unsigned int off)
  return low+((high-low)/(double)size)*(double)off;

unsigned int doPoint(double cx, double cy, double cz)
  // program from http://www.treblig.org/3dbrot/3dbrot.c
  double x,y,z;
  double newx,newy,newz;
  double theta,phi,rpow;
  //double r;
  unsigned int i;

  for(i=0;(i<maxiterations) && ((x*x+y*y+z*z) < 2.0);i++)
   /* These maths from http://www.skytopia.com/project/fractal/mandelbulb.html */
    r = sqrt(x*x + y*y + z*z );
    theta = atan2(sqrt(x*x + y*y) , z);
    phi = atan2(y,x);
    rpow = pow(r,mandpow);
    newx = rpow * sin(theta*mandpow) * cos(phi*mandpow);
    newy = rpow * sin(theta*mandpow) * sin(phi*mandpow);
    newz = rpow * cos(theta*mandpow);
  return i;

void createMandelbulb(PolyVox::SimpleVolume<PolyVox::Material8>& volData)
   PolyVox::Material8 uValue=1;
   //This three-level for loop iterates over every voxel in the volume
   for (int z = 0; z < volData.getWidth(); z++)
      double fz=valInRange(zlow, zhigh, size, z);
      for (int y = 0; y < volData.getHeight(); y++)
         double fy=valInRange(ylow, yhigh, size, y);
         for (int x = 0; x < volData.getDepth(); x++)
            double fx=valInRange(xlow, xhigh, size, x);
            unsigned int val=doPoint(fx,fy,fz);
            //printf("%i ",val);
            if (val>=maxiterations-1)
               uValue=r*12; //for example use r for material
               volData.setVoxelAt(x, y, z, uValue);

Example size 128

Author:  David Williams [ Mon Jan 23, 2012 4:36 pm ]
Post subject:  Re: mandelbulb

Very nice! Reminds me of some work I did testing the Menger Sponge, though I didn't make any screenshots of that. Fractals are great for generating data to use with PolyVox. It would be interesting to have a whole game world built in this way...

Author:  zprg [ Fri Nov 02, 2012 3:25 pm ]
Post subject:  Re: mandelbulb

now i tried mandelbulb instead of cubicsurfaceextractor with surfaceextractor and the smooth look has its own charme:

Author:  milliams [ Fri Nov 02, 2012 4:05 pm ]
Post subject:  Re: mandelbulb

Very nice. This would make a very good stress test of the PolyVox surface extractors for benchmarking.

Author:  David Williams [ Sat Nov 03, 2012 8:10 am ]
Post subject:  Re: mandelbulb

Yep, I like it! I also discovered the Mandelbox recently and thought that would make a good test for just how far we can push PolyVox. I'll try to give it a go once the Gameplay3D integration has come a bit further.

Author:  David Williams [ Sun Mar 17, 2013 8:14 pm ]
Post subject:  Re: mandelbulb

As you might have seen in this thread I've finally made a version of the Mandelbulb myself now. I haven't actually tried loading it into PolyVox/Cubiquity yet but I already have a question. How did you choose what colour each voxel should be?

For each voxel I am storing the number of iterations which were required for the fractal to converge (so it's a greyscale image). I can imagine this could be mapped to a colour gradient which would look great if you cut the volume in half, but I'm not sure it helps if you view the volume from the outside?

I assume you had to choose a threshold for how many iterations represented a solid and how many represented empty space. Let's say the threshold is 100... doesn't that mean that all voxels on the surface will have a value of 100 and so will all be the same colour?

Maybe you used a different approach such as computing the distance from the centre of the fractal to the voxel, and then using this to determine the colour?

Page 1 of 1 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group