--- fnintern.cpp	2004-08-02 19:11:35.000000000 -0400
+++ /home/abez/Desktop/povray-3.6.1/source/fnintern.cpp	2009-11-12 16:58:25.419047811 -0500
@@ -157,6 +157,7 @@
 DBL f_noise3d(DBL *ptr, unsigned int fn); // 76
 DBL f_pattern(DBL *ptr, unsigned int fn); // 77
 DBL f_noise_generator(DBL *ptr, unsigned int fn); // 78
+DBL f_mandelbulb(DBL *ptr, unsigned int fn); // 79
 
 void f_pigment(DBL *ptr, unsigned int fn, unsigned int sp); // 0
 void f_transform(DBL *ptr, unsigned int fn, unsigned int sp); // 1
@@ -248,6 +249,7 @@
 	{ f_noise3d,                 0 + 3 }, // 76
 	{ f_pattern,                 0 + 3 }, // 77
 	{ f_noise_generator,         1 + 3 }, // 78
+	{ f_mandelbulb     ,         2 + 3 }, // 79
 	{ NULL, 0 }
 };
 
@@ -259,7 +261,7 @@
 	{ NULL, 0 }
 };
 
-const unsigned int POVFPU_TrapTableSize = 79;
+const unsigned int POVFPU_TrapTableSize = 80;
 const unsigned int POVFPU_TrapSTableSize = 3;
 
 
@@ -1214,6 +1216,44 @@
 	return Noise(Vec, &TPat);
 }
 
+DBL f_mandelbulb(DBL *ptr, unsigned int) // 79
+{
+        // PARAM_X  .. PARAM_Z  - x,y,z function parameters
+        // PARAM(1) - power of n
+        // PARAM(2) - iterations
+  DBL x,y,z,newx, newy, newz, r, theta, phi, rn, thetan, phin, sinthetan, cx,cy,cz;
+  int n,cnt,iterations;
+  n = (int)PARAM(0);
+  iterations = PARAM(1);
+  cnt = iterations;
+  cx = PARAM_X;
+  cy = PARAM_Y; 
+  cz = PARAM_Z;
+  x = 0.0;
+  y = 0.0;
+  z = 0.0;
+  while(cnt > 0 && x*x + y*y + z*z < 2.0) {
+    r = sqrt(x*x + y*y + z*z);
+    theta = atan2(sqrt(x*x + y*y), z);
+    phi = atan2(y,x);
+    rn = pow(r,n);
+    thetan = theta * n;
+    phin = phi * n;
+    sinthetan = sin(thetan);
+    newx = cx + rn * sinthetan * (cos(phin));
+    newy = cy + rn * sinthetan * (sin(phin));
+    newz = cz + rn * (cos(thetan));
+    // now we reassign them
+    x = newx;
+    y = newy;
+    z = newz;
+    cnt--;
+  }
+  if (cnt <= 0) { return -1.0; } else {return 0.0; }
+  /* return (1.0*cnt) / iterations; */
+}
+
+
 void f_pigment(DBL *ptr, unsigned int fn, unsigned int sp) // 0
 {
 	VECTOR Vec = { PARAM_N_X(5), PARAM_N_Y(5), PARAM_N_Z(5) };

