So, I am writing my own 3D transformation pipeline to better understand how it all works. I can get all the images on the screen correctly, and now I'm going to go back and see the cropping.
In my opinion, I should trim the vertex if the value of x or y after breaking the perspective goes beyond [-1, 1], and in my case, if the value of z goes beyond [0, 1].
When I implement this, my z value is always -1.xxxxxxxxxxx, where xxxxxxx is a very small number.
It's a little longer, and I apologize, but I wanted to make sure I gave all the information I could.
First agreements:
I use the left system, where the matrix looks like this:
[m00, m01, m02, m03] [m10, m11, m12, m13] [m20, m21, m22, m23] [m30, m31, m32, m33]
And my vectors are columns looking like this:
[x] [y] [z] [w]
My camera is configured with
Vertical FOV in PI / 4 radians.
Aspect Ratio 1. (Square view port)
Close clip value 1.
The far clip value is 1000.
Starting world position x 0.
The starting position of the world y.
The initial position of the world is -500.
The camera is looking down along the Z axis (0, 0, 1)
Given the top, the pipeline works as follows:
Step 1: Multiply the vertex by the camera matrix.
Step 2: Multiply the vertex by the projection matrix.
Projection Matrix:
[2.41421, 0, 0, 0] [0 2.41421, 0, 0] [0, 0, 1.001001, 1] [0, 0, -1.001001, 0]
Step 3: Multiply the components x, y and z by 1 / w.
Step 4: [This is where the problem is] Fix the top if there is an outer border.
Step 5: Convert to screen coordinates.
The approximate peak that I have,
(-100, -100, 0, 1)
After multiplying by the camera matrix, I get:
(-100, -100, 500, 1)
Which makes sense, since with respect to the camera this vertex is 100 units to the left and down and 500 units ahead. It is also between close clip 1 and far clip 1000. W is still 1.
After multiplying by the projection matrix we get:
(-241.42135, -241.42135, 601.600600, -600.600600)
This I'm not sure if that makes sense. X and y seem correct, but I'm not sure about z and w, since the next step of diversity exploration is odd.
After breaking the perspective, I get:
(0.401966, 0.401966, -1.001665, 1)
Again, x and y make sense, they are both within [-1, 1]. But the value of z clearly goes beyond the limits, although I believe that it should still be inside the frust. W returns to 1, which again makes sense.
Again, I apologize for the romance, but I hope someone can help me figure out what I'm doing wrong.
Thanks!