# Ray marching thread.

This topic is 2300 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
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

##### Share on other sites

@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.

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

1. 1
2. 2
Rutin
20
3. 3
A4L
15
4. 4
5. 5
khawk
14

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633738
• Total Posts
3013617
×