Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ray tracing on the GPU László Szécsi, TU Budapest.

Similar presentations


Presentation on theme: "Ray tracing on the GPU László Szécsi, TU Budapest."— Presentation transcript:

1 Ray tracing on the GPU László Szécsi, TU Budapest

2 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: two loops - all elements with all elements t is a global variable Input stream of geometry Textures z-buffer

3 Ray engine Input texture: rays Combination: a triangle with each ray as many outputs as rays pixels a triangle is a full screen quad, pixel shader intersects a triangle with a ray Input stream: triangles Output texture: hits

4 Ray engine CPU prog Vertex shader frag shader “Triangles” as full screen quads Ray texture ids Rasterization Interpolation Rays in Texture maps Intersection between one triangle and a ray Triangles as many times as pixels the quad has depth composit Hit t First Hits

5 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(); }

6 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 }

7 Triangle-ray intersection 1.Plane intersection: p = rayo + raydir · t, t > 0 (p - r1) ·n = 0, normal: n = (r2 - r1) x (r3 - r1) 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 r1 r2 p r3 (r1 – rayo) · n raydir · n t =

8 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; }

9 Ray tracing results


Download ppt "Ray tracing on the GPU László Szécsi, TU Budapest."

Similar presentations


Ads by Google