Ray tracing on the GPU

Document Sample
Ray tracing on the GPU Powered By Docstoc
					Ray tracing on the GPU

László Szécsi, TU Budapest
Ray tracing on the GPU
                                 Ray tracing:
                                 for each ray do
                                      t = infinity
                                      for each triangle do
                                          tnew = Intersect(triangle, ray)
                                          if (tnew < t) t = tnew
                                      endfor
                                     hit[ray] = ray.o + ray.dir * t
                                 endfor
Problems:
                                                   Input stream of
• two loops - all elements with all elements
                                                   geometry
• t is a global variable
                                                  Textures
                           z-buffer
    Ray engine
                                            Output texture: hits



                             Input stream: triangles


Input texture: rays
                                  pixels


       Combination:                        a triangle is a full screen quad,
       a triangle with each ray            pixel shader intersects a
       as many outputs as rays             triangle with a ray
 Ray engine                                 Intersection
                                            between one
                                            triangle and
                                            a ray
             Ray texture ids
                                                       Hit
                                             frag       t     depth
CPU            Vertex    Rasterization      shader           composit
prog           shader    Interpolation
                                 Triangles
       “Triangles”               as many times
       as full screen            as pixels
       quads                     the quad
                                 has         Rays in           First
                                             Texture           Hits
                                               maps
  CPU: triangles as full screen
  quads
Triangle triang[ntriangles];
void Display( ) {
   ...
   glBegin( GL_QUADS );
   for(int i = 0; i < ntriangles, i++) {
       glMultiTexCoord2fARB(GL_TEXTURE1_ARB,    // TEXCOORD1
              triang[i].v1.x, triang[i].v1.y, triang[i].v1.z);
       glMultiTexCoord2fARB(GL_TEXTURE2_ARB,    // TEXCOORD2
              triang[i].v2.x, triang[i].v2.y, triang[i].v2.z);
       glMultiTexCoord2fARB(GL_TEXTURE3_ARB,    // TEXCOORD3
              triang[i].v3.x, triang[i].v3.y, triang[i].v3.z);
       glTexCoord2f(0,0);   glVertex4f(-1,-1,   0,   1);
       glTexCoord2f(0,1);   glVertex4f(-1, 1,   0,   1);
       glTexCoord2f(1,1);   glVertex4f( 1, 1,   0,   1);
       glTexCoord2f(1,0);   glVertex4f( 1,-1,   0,   1);
    }
    glEnd();
}
Vertex shader is just
copying
void main( in   float4   position   :   POSITION,
           in   float2   rayuv      :   TEXCOORD0,
           in   float3   r1         :   TEXCOORD1,
           in   float3   r2         :   TEXCOORD2,
           in   float3   r3         :   TEXCOORD3,

          out   float3   hposition      :   POSITION,
          out   float2   orayuv         :   TEXCOORD0,
          out   float3   or1            :   TEXCOORD1,
          out   float3   or2            :   TEXCOORD2,
          out   float3   or3            :   TEXCOORD3
) {
   or1 = r1; or2 = r2; or3 = r3;
   orayuv = rayuv;
   hposition = position; // already in clipping space
}
Triangle-ray intersection
                                                      r3
                                              p



  r1                                 r1                      r2

1. Plane intersection: p = rayo + raydir · t,    t>0
                      (p - r1) ·n = 0,
                                                  (r1 – rayo) · n
   normal: n = (r2 - r1) x (r3 - r1)          t=    raydir · n

2. Is the intersection inside the triangle?
    ((r2 - r1) x (p - r1)) ·n > 0
    ((r3 - r2) x (p - r2)) ·n > 0
    ((r1 - r3) x (p - r3)) ·n > 0
Pixel shader: ray-triangle
intersection
void main(in   float2   rayuv   :   TEXCOORD0,   //   ray index
          in   float3   r1      :   TEXCOORD1,   //   vertex 1
          in   float3   r2      :   TEXCOORD2,   //   vertex 2
          in   float3   r3      :   TEXCOORD3,   //   vertex 3 of triang
          out float3 p : COLOR,            // hit point to texture
          out float t : DEPTH,             // z buffer finds the min
          uniform sampler2D rayorgs,    // array of rays
          uniform sampler2D raydirs,
          uniform float     maxdepth) {
   float3 rayo = tex2D(rayorgs, rayuv);            // ray pars
   float3 raydir = tex2d(raydirs, rayuv);
   float3 normal = cross(r2 – r1, r3 – r1);
   t = dot(p1 – rayo, normal) / dot(raydir, normal);
   p = rayo + raydir * t;
   if (t < 0 ||
       dot(cross(r2-r1, p-r1), normal) < 0 ||
       dot(cross(r3-r2, p-r2), normal) < 0 ||
       dot(cross(r1-r3, p-r3), normal) < 0) t = 2; // ignore
   else t /= maxdepth;
Ray tracing results

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:4
posted:5/2/2012
language:English
pages:9