Building Worlds With Distance Functions in GLSL (Raymarching)

Article index:

8 – Raymarching: Primitive Blending

Build Worlds With Distance Functions in GLSL, GLSL Hacker
Raymarching: rounded box and torus blended

The blending is performed by the op_blend() function:

vec2 obj_torus(in vec3 p)
{
  vec2 r = vec2(2.4,0.9);
  vec2 q = vec2(length(p.xz)-r.x,p.y);
  float d = length(q)-r.y;
  return vec2(d,1);
}

vec2 obj_round_box(vec3 p)
{
  float d= length(max(abs(p)-vec3(1.0,0.5,2.0),0.0))-0.08;
  return vec2(d,1);
}

vec2 op_blend(vec3 p, vec2 a, vec2 b)
{
 float s = smoothstep(length(p), 0.0, 1.0);
 float d = mix(a.x, b.x, s);
 return vec2(d,1);
}

And the distance_to_obj() function:

vec2 distance_to_obj(in vec3 p)
{
  return obj_union(obj_floor(p), op_blend(p, obj_round_box(p), obj_torus(p)));
}




Article index:

4 thoughts on “Building Worlds With Distance Functions in GLSL (Raymarching)”

  1. GLSLnoob42

    Hey. Just found this. Came across iq’s stuff a while ago, but being a noob I couldn’t do anything with it.

    Then while rediscovering GLSL & iq’s website, I found this awesome resource!

    But here’s the thing, I can’t get it to work. ;o)

    I eventually found 2 errors in the code. Here are the fixes:
    line 97: d.x-distance_to_obj(p-e.yyx).x); // had an extra “)”
    line 98: N = normalize(n); // was missing “;”

    I’m trying to use GLSL Hacker 0.6.0.0 & Blender 2.69 using this tutorial:

    http://en.wikibooks.org/wiki/GLSL_Programming/Blender/Minimal_Shader

    Any help?

    peace & 42

  2. toba

    I too found the errors but cant get any of this to work. There are too few good examples online, it would be nice to get one working

Comments are closed.