Projecting a 3D point onto a 2D screen space using a perspective camera matrix

I am trying to project a series of three-dimensional points onto a screen using a perspective camera matrix. I don’t have world space (or consider it to be a single matrix) and my camera does not have space for the camera (or consider it an identity matrix), I have a 4x4 matrix for my object space.

I take the object matrix and multiply it by the camera perspective matrix created in the following way:

Matrix4 createPerspectiveMatrix( Float fov, Float aspect, Float near, Float far ) { Float fov2 = (fov/2) * (Math.PI/180); Float tan = Math.tan(fov2); Float f = 1 / tan; return new Matrix4 ( f/aspect, 0, 0, 0, 0, f, 0, 0, 0, 0, -((near+far)/(near-far)), (2*far*near)/(near-far), 0, 0, 1, 0 ); } 

Then I take my point [x, y, z, 1] and multiplying it by the result of multiplying the perspective matrix and the object matrix.

In the next part, I get confuzzled, I am sure that I need to get these points within the ranges -1 or 1, or 0 and 1, and, if there was a first set of values, I would then multiply the points by the width and height of the screen for the coordinate x and y of the screen in the same way or multiplied the values ​​by the screen height / 2 and width / 2 and added the same values ​​to the corresponding points.

Any step by step telling me how this can be achieved, or where I could go wrong with any of this, would be very helpful !: D

Best wishes!

PS In the example of the identity / translation matrix, my matrix format is in my model:

 [1, 0, 0, tx, 0, 1, 0, ty, 0, 0, 1, tz, 0, 0, 0, 1 ] 
+6
matrix perspective 3d camera projection
source share
2 answers

Your problem is that you forget to split the perspective.

Perspective division means that you separate the x, y, and z components of your point into your w-component. This is necessary to convert your point from a homogeneous 4D space to a normalized device coordinate system (NDCS), in which each component x, y or z is between -1 and 1, or 0 and 1.

After this conversion, you can perform the conversion as a viewport (multiply points by screen width, height, etc.).

A good overview of this transformation pipeline in Foley's book (Computer Graphics: Principles and Practice in C) can be seen here:

http://www.ugrad.cs.ubc.ca/~cs314/notes/pipeline.html

+5
source share

For issues like this, I would suggest excellent resources: http://scratchapixel.com/lessons/3d-advanced-lessons/perspective-and-orthographic-projection-matrix/ This really goes into the details of the projection matrix. These are more lessons on the subject of the camera, constructing camera rays, etc. You will need to do digging.

+3
source share

All Articles