简体   繁体   中英

Computing pixel coordinate of a 3d point

I'm trying to compute the pixel coordinates of a 3d point (call this p) in world space. I understand I have to transform p from world space into camera space by multiplying it by the inverse of the camera-to-world matrix. Then find the coords of the point on the canvas using perspective projection, then convert this to pixel coordinates. I think this is correct?

I am currently stuck on finding the 4x4 matrix that would allow me to convert p from world space to camera space. If I have the vectors of my camera position and direction, how can I calculate this 4x4 matrix?

vec3 cameraPos = vec3(0.f, 0.f, -5.f);
vec3 cameraDir = vec3(0.f, 0.f, 1.f);

Do I also need a camera up vector and what does this represent?

I am using glm.

I am currently stuck on finding the 4x4 matrix that would allow me to convert p from world space to camera space.

That matrix is called the view matrix. The view matrix is the Inverse matrix of that matrix, which is defined by the camera position and orientation (point of view).
The position is 3D point ( cameraPos ). The orientation of the camera is defined by the line of sight ( cameraDir ) and the up-vector. You have to define the up-vector, eg (0, 1, 0).

The OpenGL Mathematics (GLM) library provides a handy function ( glm::lookAt ), to compute the view matrix by the position, target and up-vector:

#include <glm/gtc/matrix_transform.hpp>
glm::vec3 cameraPos = glm::vec3(0.f, 0.f, -5.f);
glm::vec3 cameraDir = glm::vec3(0.f, 0.f, 1.f);
glm::vec3 cameraUp = glm::vec3(0.f, 1.f, 0.f);

glm::mat4 viewMatrix = glm::lookAt(cameraPos, cameraPos+cameraDir, cameraUp);

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