I want to convert 3d coordinates (of specific points such as vertices of a cube) to the x y position of a pixel on a screen which using the resolution of the screen, the fov of the camera (x, y separated) and the pitch and yaw of the camera. I've tried some kind of raycasting to attempt this but it was really slow and I couldn't get the pitch and yaw to work at the same time. I'm programming in c++ so if there are any libraries or headers that automatically do this without any speed issues it would also be good
Asked
Active
Viewed 83 times
0
-
5Look for a computer graphics tutorial online. It is not a specific C++ problem, more of a math one. For example you can look for 3d graphics libraries. There is even a computer graphics stack exchange with similar questions [here](https://computergraphics.stackexchange.com/questions/6012/im-trying-to-get-a-2d-screen-position-so-i-unproject-a-point-from-3d-into-2d-sc). Are you familiar with vector and matrix calculations? Because they are most useful for this. – Pepijn Kramer Aug 07 '23 at 15:40
-
see [Mathematically compute a simple graphics pipeline](https://stackoverflow.com/a/21100338/2521214) and here [Understanding 4x4 homogenous transform matrices](https://stackoverflow.com/a/28084380/2521214) is also simple C++ example of rotating cube with perspective using 2D moveto/lineto GDI commands... – Spektre Aug 09 '23 at 07:31
2 Answers
0
If we have a 3D space (coordinates x,y,z), and project objects on the 2D subspace (plane) z=0 from a source S (the camera) of coordinates (x_S,y_S,z_S),
the projection of the point P (x_P,y_P,z_P) on the plane is the point Q of coordinates (x,y) given par this formula
x = (x_P*z_S - x_S*z_P)/(z_S - z_P)
y = (y_P*z_S - y_S*z_P)/(z_S - z_P)
Matrixes are the general tool for all linear (more precisely, affine) 2D transformations, but projection from 3D-space to a 2D-space is a projective transformation (non linear), and the calculations are a bit more complex.
In this case, I just made the calculations with wxMaxima (a free and powerful computer algegra system). The calculations consist of solving the equations who express the fact that the the 3 points S, P, and Q(x,y,0) are aligned.

toyota Supra
- 3,181
- 4
- 15
- 19