• Advertisement
Sign in to follow this  

Ray marching thread.

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement