Jump to content


Ray Tracing with GLSL - Some Doubts

opengl raytracing graphics gpu

4 replies to this topic

#1 Abhishek Bansal

    New Member

  • Members
  • Pip
  • 5 posts

Posted 20 December 2011 - 12:43 PM

Hello All,

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

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 ?

Thank You
Thanks and Regards
Abhishek Bansal

#2 jari

    New Member

  • Members
  • PipPip
  • 18 posts

Posted 20 December 2011 - 03:43 PM

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

I recommend to use ShaderToy (http://www.iquilezle...apps/shadertoy/), or GLSL Sandbox (http://glsl.heroku.com/e). Quick and simple. :)

#3 Vilem Otte

    Valued Member

  • Members
  • PipPipPipPip
  • 345 posts

Posted 20 December 2011 - 09:39 PM

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).
My blog about game development (and not just game development) - http://gameprogramme...y.blogspot.com/

If you don't know how to speed up application, go "roarrrrrr!", hit the compiler with the club and use -O3 :D

#4 Abhishek Bansal

    New Member

  • Members
  • Pip
  • 5 posts

Posted 22 December 2011 - 04:15 PM

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.
Thanks and Regards
Abhishek Bansal

#5 jari

    New Member

  • Members
  • PipPip
  • 18 posts

Posted 27 December 2011 - 03:33 PM

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





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users