• Create Account

## Ray marching thread.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

3 replies to this topic

### #1MrOMGWTF  Members

444
Like
0Likes
Like

Posted 27 August 2012 - 01:08 AM

Can somebody link me to a good ray marching tutorial/article/whatever?
I can't find anything good.

@edit: let this thread die, don't respond. I found nice tutorial about ray marching with distance fields.

Edited by MrOMGWTF, 27 August 2012 - 07:38 AM.

### #2MrOMGWTF  Members

444
Like
0Likes
Like

Posted 27 August 2012 - 02:43 AM

Ok, I tried to implement ray marching with distance fields.
This is my code:
#ifdef GL_ES
precision mediump float;
#endif

uniform float time;
uniform vec2 resolution;

//rp - ray position sp - sphere position radius - radius
float sphere(in vec3 rp, in vec3 sp, in float radius)
{
return length(rp - sp) - radius;
}

float getDistance(vec3 p)
{
float s1 = sphere(p, vec3(0.0, 0.0, 0.0), 2.0);

return s1;
}

void main( void ) {

vec2 uv = ( gl_FragCoord.xy / resolution.xy );
vec3 rp = vec3(0.0, 5.0, 10.0); // ray position
vec3 rd = normalize( vec3( -1.0 + 2.0*uv* vec2(resolution.x/resolution.y, 1.0), -1.0 ) ); // ray direction
const int maxIter = 10; //max iterations

float t = 0.0;

bool hit = false;

for(int i = 0; i < maxIter; i++)
{
float cD = getDistance(rp + rd * t);
if(cD < 0.001)
{
hit = true;
break;
}
t += cD;
}

if(hit == true)
{
gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
}
else
{
gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
}
}

It's just 50 lines of glsl code, really simple. It should display an unlit white sphere. But it displays nothing :|
You can test this code and edit in realtime here: http://glsl.heroku.com/e#3699.1
What's wrong with this code? I don't see anything wrong.

@edit: I think it's bug in this glsl sandbox.. i'll write it in my own opengl app.
@edit2: Yeah.. it was a bug. I rewrote this in my own app and it works.

Edited by MrOMGWTF, 27 August 2012 - 05:02 AM.

### #3Álvaro  Members

20266
Like
0Likes
Like

Posted 27 August 2012 - 11:58 AM

@edit: let this thread die, don't respond. I found nice tutorial about ray marching with distance fields.

You can make this thread much more useful if you post a link to that tutorial.

### #4jbadams  Senior Staff

24740
Like
0Likes
Like

Posted 29 August 2012 - 02:12 AM

We don't remove topics at user request -- we prefer to leave them so that they might help others.