**Article index**:

- 1 – Raymarching, Distance functions
- 2 – GLSL framework for Raymarching
- 3 – Raymarching: Floor + Sphere
- 4 – Raymarching: Floor + Torus
- 5 – Raymarching: Floor + Rounded Box
- 6 – Raymarching: Floor + Union of Rounded Box and Sphere
- 7 – Raymarching: Floor + Rounded Box minus Sphere
- 8 – Raymarching: Primitive Blending
- 9 – Raymarching: Primitive Repetition

## 3 – Raymarching: Floor + Sphere

Raymarching: floor + sphere

The new functions to generate this scene: `obj_union()` and `obj_sphere()`:

vec2 obj_union(in vec2 obj0, in vec2 obj1) { if (obj0.x < obj1.x) return obj0; else return obj1; }

The obj_union() function will be used with all other primitives to combine the floor and a primitive. This function can be seen as the **depth test**.

Now the function to generate a sphere. This function returns the distance between a position p and a sphere with a radius of 1.9. The vec2 used to return the result holds the distance in x and the material index in y. If material == 0, we have the floor. If material == 1 we have a primitive.

vec2 obj_sphere(in vec3 p) { float d = length(p)-1.9; return vec2(d,1); }

Now let’s see where to use both functions in our GLSL framework: we just need to modify the `distance_to_obj()` function like this:

vec2 distance_to_obj(in vec3 p) { return obj_union(obj_floor(p), obj_sphere(p)); }

**Article index**:

- 1 – Raymarching, Distance functions
- 2 – GLSL framework for Raymarching
- 3 – Raymarching: Floor + Sphere
- 4 – Raymarching: Floor + Torus
- 5 – Raymarching: Floor + Rounded Box
- 6 – Raymarching: Floor + Union of Rounded Box and Sphere
- 7 – Raymarching: Floor + Rounded Box minus Sphere
- 8 – Raymarching: Primitive Blending
- 9 – Raymarching: Primitive Repetition

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

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

I’m fixing the errors and I upload the code samples asap!

Code samples have been updated for the latest DEV version (0.6.3.11) and a new demo about menger sponge has been added:

http://www.geeks3d.com/20140201/glsl-menger-sponge-raymarching-code-samples-updated/