I am trying to develop a basic Ray Tracer. So far i have calculated
intersection with a plane and blinn-phong shading.i am working on a
500*500 window and my primary ray generation code is as follows
vec3 rayDirection = vec3( gl_FragCoord.x-250.0,gl_FragCoord.y-250.0 , 10.0);
Now i doubt that above method is right or wrong. Please give me some
I am also having doubt that do we need to construct geometry in OpenGL
code while rayTracing in GLSL. for example if i am trying to raytrace a
plane do i need to construct plane in OpenGL code using glVertex2f ?
Please log in or register to post a reply.
The direction vector need to be normalized.
When i did raytracing in glsl it was something like this:
vec2 uv = gl_FragCoord.xy / resolution.xy;
vec3 r0 = vec3(0.0, 1.0, 4.0); //the position of view
vec3 rd = vec3((-1.0 + 2.0 * uv) * vec2(1.78, 1.0), -1.0); //the look direction, note the -Z!, the multiplication with vec2(1.78, 1.0) is just a correction for the aspect.
The resolution is a 2d vec, the screen width and height (in window
coords, for example: 800*600).
Yes, you need a full screen quad, and run the (fragment) shader code on
I recommend to use ShaderToy
or GLSL Sandbox (http://glsl.heroku.com/e).
Quick and simple. :)
Note. that using GLSL for ray tracing isn’t as good as it seem to be.
Use OpenCL rather, as you’ll more likely get better code, it will give
you more options (as you’re coding in language very similar to C, GLSL
is also similar, but not that much).
Anyway, try to pass ray directions through vertex attribute - as it will
save you a lot of computations (when working with GLSL).
first of all thank you for your reply..
@Jari Your referred tools are very very useful thanx again for that…!!
My code is working on that tool but not on my PC. Is this the problem of
vertex shader or OpenGL code settings. My current code only finds rays
intersection with a sphere. Which i can clearly seen on GLSL Sandbox but
not in my project.
in vertex shader i m just using ftransform and nothing else.
and can u please tell me what is logic of converting those coordinates
in the range of [-0.5,0.5]. I dont get it..!!
@Villem Otte Thank you very much for your advice but right now my
objective is to just construct a basic raytracer in GLSL with phong,
shadows, reflection and may be refraction. After that i will concentrate
on Optimizations and look for OpenCL or CUDA.
You shoot the rays in [-1.0, 1.0] range, both in vertical and in
horizontal directions (of cource you can modify this, eg. on the x axis
to correct the aspect).
You get the current fragment position (gl_FragCoord) in window
coordinate space (eg. [0, 800], [0, 600]), so to convert it to [-1.0,
1.0] first divide with window (viewport) size and you get a value in
range [0.0, 1.0]. To convert this to [-1.0, 1.0] first multiply this
value with 2.0, and then subtract -1.0 from it.
What these tools do is draw a fullscreen quad (two triangles), and
execute fragment shaders on it. The vertex shaders just bypass the
vertices (if you send them in normalized device coordinates, you dont
need to transform).