简体   繁体   中英

Project 3D coordinates to screen coordinates

I have world 3D co-ordinates [3D_x, 3D_y, 3D_z] which I want to project onto screen co-ordinates [screen_x, screen_y] using Perspective projection. I also need a camera view which after some research I understand is a LookAt Matrix. I know the following values:

screen = [screen_width, screen_height]
Camera_Coordinates = [cx, cy, cz]
Target_Coordinates = [0,0,0]

So finally I have a 3D coordinate [3D_x, 3D_y, 3D_z] , Perspective Projection Matrix, View Matrix

Am I missing something? If not, in what order should I multiply the above data to obtain final screen coordinates [screen_x, screen_y] ?

Seems like you're missing several steps. In fact you need to calculate a world_to_view matrix, according to your camera rotation and position, then multiply your world_to_view matrix by the projection_matrix to get the transform_matrix, then you have to multiply the in_world coordinates in order to get "in view" coordinates. Then you have to project the obtained coordinates to screen (2D) space using the following formula (that's the one I use) :

onscreen.x = ((camera.zfar / vertex.in_view.z) * vertex.in_view.x - camera.fov) + screen.width / 2.0;
onscreen.y = ((scene.zfar / vertex.in_view.z) * vertex.in_view.y - camera.fov) + screen.height / 2.0;

I know my method is far from perfect (I am looking for a better way to do this) and that I threw away some matrix multiplications in the process, but I found it by myself and it's working. ;-) And also, you don't NEED a lookat_matrix, but it can be very usefull (it can be used for backface culling for instance)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM