Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am attempting to project a series of 3D points onto the screen using a perspective camera matrix. I do not have world space (or consider it being an identity matrix) and my camera does not have camera space (or consider it an identity matrix), I do have a 4x4 matrix for my object space.

I am taking the object matrix and multiplying it by the camera perspective matrix, generated with the following method:

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 
    );
}

I am then taking my point [x, y, z, 1] and multiplying that by the resulting multiplication of the perspective matrix and object matrix.

The next part is where i'm getting confuzzled, I'm pretty sure that I need to get these points within the ranges of either -1 and 1, or 0 and 1, and, in the case of having the first set of values, I would then multiply the points by the screen width and height for the screen coordinates x and y value respectivly or multiply the values by the screen height/2 and width/2 and add the same values to the respective points.

Any step by step telling me how this might be achieved or where I might be going wrong with any of this would be massivly appreciated!! :D

Best regards everyone!

P.S. In the example of a identity/translation matrix, my matrix format in my model is:

[1, 0, 0, tx,
 0, 1, 0, ty,
 0, 0, 1, tz,
 0, 0, 0, 1 ]
like image 316
Gary Paluk Avatar asked Sep 08 '10 17:09

Gary Paluk


People also ask

What is 3D to 2D projection?

A 3D projection (or graphical projection) is a design technique used to display a three-dimensional (3D) object on a two-dimensional (2D) surface. These projections rely on visual perspective and aspect analysis to project a complex object for viewing capability on a simpler plane.

What is projection matrix of camera?

In computer vision a camera matrix or (camera) projection matrix is a. matrix which describes the mapping of a pinhole camera from 3D points in the world to 2D points in an image.

What is perspective transform matrix?

The homography or perspective transformation is defined by a matrix that defines a mapping of pixel coordinates: (6.4) Here are pixel coordinates in the output image, and are uniform pixel coordinates in the input image. The normal input pixel coordinates are given by. (6.5)


1 Answers

Your problem is that you forget to perform the perspective division.

Perspective division means that you divide x, y and z component of your point by its w component. This is required for transforming your point from Homogeneous 4D Space to Normalized Device Coordinates System (NDCS) in which each component x, y or z falls between -1 and 1, or 0 and 1.

After this transformation, you can do your viewport transformation (multiply points by screen width, heigth etc).

There's a good view of this transformation pipeline in Foley's book (Computer Graphics: Principles and Practice in C), you can see it here:

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

like image 82
Stringer Avatar answered Nov 24 '22 08:11

Stringer