Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perspective projection - how do I project points which are behind 'camera'?

I'm writing my own software rasterizer in Java, and I ran into some trouble with it... take a look at a sample image, please:

Image

This sample just draw simple square grid on a plane. Everything works fine until I move camera close enough for some points to move behind it. After that, they're no longer projected correctly, as you can see (vertical lines - points that should be behind camera are projected on top of the screen).

My transformation matrices and vectors are same ones DirectX is using (PerspectiveFovLH for projection, LookAtLH for camera).

I'm using following transformation method to project 3D point:

  1. 3D vector to be transformed is created.
  2. Vector is multiplied by ViewProjection matrix.
  3. After that, point is transformed to screen using following method:

    // 'vector' is input vector in projection space
    // projection to screen
    double vX = vector.x / vector.z;        
    double vY = vector.y / vector.z;
    
    //translate
    //surfaceW is width and surfaceH is height of the rendering window.
    vX = (( vX + 1.0f) / 2.0f) * surfaceW;
    vY = ((-vY + 1.0f) / 2.0f) * surfaceH;
    
    return new Vector3(vX, vY, vector.z);
    

As I said earlier, it works fine until point moves behind camera. The fact is, I can figure out when the point is behind camera (by testing it's Z value after final transform), but since I'm drawing lines and other line based objects, I can't just skip that point.

Then I tried setting my transformation pipeline according to The Direct3D Transformation Pipeline article on MSDN.

Unfortunately, I haven't had any luck with that as well (same results), so any help would be highly appreciated, since I'm a bit stuck on this one.

Thank you.

Best Regards, Alex

like image 491
Alex Avatar asked Jul 25 '10 13:07

Alex


1 Answers

You need to intersect the line with the front clipping plane in 3d space and truncate the line so you only draw the line segment that's visible:

             |
             |
             |
x------------+-----------o
             |
             |
             |   * - camera
             |
             |
             |
       clipping plane

You've got a line xo where x in front of the clipping plane and o behind it. Intersect this line with the clipping plane to generate the point +. You know which of x and o is visible so draw the line from x to +.

This way you're not projecting points which are behind the camera.

like image 67
ChrisF Avatar answered Oct 25 '22 03:10

ChrisF