9

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

Alex
  • 923
  • 9
  • 21
  • Are you setting up your camera's front clipping plane correctly? And then taking it into account when rendering? – ChrisF Jul 25 '10 at 13:34
  • My zNear plane is set to 1, and zFar plane to 1000 units. I've tried changing the values, but problem persists. Any suggestions? – Alex Jul 25 '10 at 13:43
  • It's been a while since I did this sort of thing, but you need to perform an intersection with the front clipping plane *before* projecting the line. – ChrisF Jul 25 '10 at 14:15

1 Answers1

10

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.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
  • 2
    Thanks. Is it OK if I do that in clip space, after perspective transform, prior to perspective division (using homogeneous coordinates and w)? – Alex Jul 27 '10 at 05:06
  • @Alex Yes. I am fairly sure you can do this in clip space or even NDC – Samie Bencherif Aug 02 '21 at 20:35