Welcome to Fractal Forums

Fractal Software => Programming => Topic started by: fractalnoob on June 08, 2013, 10:47:14 AM




Title: Raymarching misses fractal?
Post by: fractalnoob on June 08, 2013, 10:47:14 AM
Hi

I hope this is the right place to ask.
I'd like to reproduce the fractal from this blog post: http://blog.hvidtfeldts.net/index.php/2011/08/distance-estimated-3d-fractals-iii-folding-space/comment-page-1/#comment-20302 section "A Real Fractal". The post uses Raymarchiing and a distance function.
The problem is, that my ray misses the fractal. I don't know what sensible values for "Iteration" and "scale" to set and I guess, that may ray construction is also a little bit off...

Here is my code:

Constants:
Code:
const float Iterations = 4;
const float Scale = 5.0;

Fractal DE:
Code:
float distanceFromPoint(vec3 z) {
        vec3 a1 = vec3(1,1,1);
vec3 a2 = vec3(-1,-1,1);
vec3 a3 = vec3(1,-1,-1);
vec3 a4 = vec3(-1,1,-1);
vec3 c;
int n = 0;
float dist, d;
while (n < Iterations) {
c = a1; dist = length(z-a1);
       d = length(z-a2); if (d < dist) { c = a2; dist=d; }
d = length(z-a3); if (d < dist) { c = a3; dist=d; }
d = length(z-a4); if (d < dist) { c = a4; dist=d; }
z = Scale*z-c*(Scale-1.0);
n++;
}

  return = (length(z) ) * pow(Scale, -float(n));

Construction of rays
Code:
  vec2 q = gl_FragCoord.xy / u_ws.xy;
  q = -1.0 + 2.0 * q;
  q.x *= u_aspect;
  vec3 planePoint = vec3(q.xy, 0.0);
  vec3 ro = vec3(0.0, 0.0, 1.3); //eye-position
  vec3 rd = normalize(planePoint - ro);

  vec4 debugColor = vec4(vec3(0.1).xyz, 1.0);
  vec4 color = vec4(trace(ro, rd));

I hope someone can give me a hint.



Title: Re: Raymarching misses fractal?
Post by: AndyAlias on June 21, 2013, 06:35:43 AM
I found that scale 2.0 produces the Sierpinski Pyramid.

Also, length(z) on the last line of the distance function is the distance to a point in space.

Unless you have a very high iteration count you wont see anything as most rays wont get close enough to any points.

Use (length(z)-radius) to get the distance to a sphere instead (radius 1.5 seems to be about optimal for this one).