Sign in to follow this  

Intersect ray with depth map

This topic is 3589 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

I'm trying to intersect a ray with a depth map. What I do is find the start and end point of the 3D ray. I then project these endpoints using the MVP matrix associated that rendered the depth map, here I use orthographic projection. Then I rasterize the projected ray segment, looking for depth map intersections. To check for intersections, I take the current and last uv coords, on the rasterized segment and reconstruct a 3D segment, that I can intersect with the 3D ray segment. Like so: //unproject the current and last pixel float4 segStartWorld4 = mul(imposters[i].invMVP, float4(lastTexC.xy, lastZ * lastw, lastw)); float4 segEndWorld4 = mul(imposters[i].invMVP, float4(texC.xy, z * w, w)); float3 segStartWorld3 = segStartWorld4.xyz / segStartWorld4.w; float3 segEndWorld3 = segEndWorld4.xyz / segEndWorld4.w; float4 isect = Intersect3DSegments(segStartWorld3 , segEndWorld3 , startPoint, endPoint); Where: z = tex2D(imposters[i].depthTex, texC.xy).r; lastZ = the z in the last loop iteration. startPoint and endPoint are the start and end of the 3D ray segment. I have implemented this technique in a software rasterizer and do get correct intersections. But transferring it to hardware has proved troublesome. Here is my shader code pertaining to the intersection. I'm using CG with the VP40 and FP40 profiles.
float4 PSMain( float4 posW	: TEXCOORD0,
               float3 normalW	: TEXCOORD1,

	       uniform float3 eyePosW,

   	       uniform Imposter imposters[10],

	       uniform samplerCUBE envMap) : COLOR0
{
     //find vector from pos to camera
     float3 toEyeW = posW.xyz - eyePosW;

     //normalize
     normalW = normalize(normalW);
     toEyeW = normalize(toEyeW);

     //reflect the incident vector across the normal
     float4 R = float4(0, 0, 0, 0);
     R.xyz = reflect(toEyeW, normalW);

     float4 finalColor = texCUBE(envMap, R);
     float4 projStart, projEnd;
     float4 startPoint, endPoint;
     float2 texC;

     //calculate start and end points. R is the direction of the Ray.
     // 5 & 50 are temporary values until I can find the real start and
     // end points. But for my scene these are garunteed to be before and
     // after the object we got the depth map from.
     startPoint = posW + (R * 5);
     endPoint = posW + (R * 50);

     //project the points using the impostor mvp
     projStart = mul(imposters[i].mvp, startPoint);
     projEnd = mul(imposters[i].mvp, endPoint);

     //transform to clip space, texture space
     projStart.xy = projStart.xy / projStart.w * .5 + .5;
     projEnd.xy = projEnd.xy / projEnd.w * .5 + .5;


     //calculate the steps for rasterizing the line
     float dxf = abs(projStart.x - projEnd.x);
     float dyf = abs(projStart.y - projEnd.y);
     int stepsN;

     if (dxf < dyf)
     {
         stepsN = ceil(dyf);
     }
     else
     {
         stepsN = ceil(dxf);
     }

     stepsN++;
     float dx1 = (projStart.x - projEnd.x) / (float)stepsN;
     float dy1 = (projStart.y - projEnd.y) / (float)stepsN;
     float dw = (projEnd.w - projStart.w) / (float)stepsN;

     float lastw = projStart.w;
     float2 lastTexC = projStart.xy;
     float lastZ = tex2D(imposters[i].depthTex, lastTexC.xy).r;
     float z;
     float w;

     //walk along the rasterized line and find the intersection with the depth map
     for(int n = 1; n < stepsN; n++)
     {
          texC.x = projStart.x + dx1 * n;
          texC.y = projStart.y + dy1 * n;

          w = projStart.w + dw * n;
          z = tex2D(imposters[i].depthTex, texC.xy).r;

          //unproject the current and last pixel
          float4 segStartWorld4 = mul(imposters[i].invMVP, float4(lastTexC.xy, lastZ * lastw, lastw));
          float4 segEndWorld4 = mul(imposters[i].invMVP, float4(texC.xy, z * w, w));

          float3 segStartWorld3 = segStartWorld4.xyz / segStartWorld4.w;
          float3 segEndWorld3 = segEndWorld4.xyz / segEndWorld4.w;
          float4 isect = Intersect3DSegments(segStartWorld3 , segEndWorld3 , startPoint, endPoint);

          //we found an intersection. w is set to 1 in Intersect3DSegments
          //if there is an intersection
          if(isect.w > 0)
          {
               isect = mul(imposters[i].mvp, float4(isect.xyz, 1));
		
               float2 tex = (isect.xy / isect.w) *.5 + .5;

               if(tex.x < 0 || tex.y < 0)
		     continue;

               if(tex.x > 1 || tex.y > 1)
                     continue;

               return tex2D(imposters[i].tex, tex);
          }

          lastTexC = texC;
          lastZ = z;
          lastw = w;
     }
}




I have a simple scene setup with a quad facing down the +Z axis and a teapot centered at [0, 0, 25]. The 3D ray is computed by taking vector from the current world position of the quad to the eye and reflecting it across the normal; i.e. R = reflect(toEyeW, normalW); I have made sure that R and the depth map are correct. And if I use set R to [0, 0, 1] I get correct intersections. I suppose I'm not unprojecting correctly, or that I'm interpolating the w component of the rasterized ray incorrectly. Any help is much appreciated.

Share this post


Link to post
Share on other sites
With parallax occlusion mapping, they have access to the normal, binormal, and tangent. They're intersecting the object they're rendering. I'm rendering a reflector, whose reflected rays are intersecting another objects depth map. So I won't have that information about the object I'm intersecting. So I'm not sure, how I would transform the ray to the tangent space of the object.

Share this post


Link to post
Share on other sites

This topic is 3589 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this