Hi everyone,
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:
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:
- 3D vector to be transformed is created.
- Vector is multiplied by ViewProjection matrix.
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