Convert 3d position to 2d screen position

I would like to convert the 3d position to the 2d position of the screen. I considered a similar question: Projecting a 3D point onto the coordinate of a 2D screen , but I do not understand it completely. I thought that to calculate the 2nd position I would need a projection matrix, but I don’t see how it is used, except for converting the point to the coordinate space of the location. Also, is cam.FieldOfView equal to farZ in OpenGL?

Can someone please help me fulfill this function. Are the parameters sufficient to calculate the 2nd position? Pos is already a vector relative to the position of the camera.

Vector2* convert(Vector3& pos, Matrix4& projectionMatrix, int screenWidth, int screenHeight) { float ratio = screenWidth / screenHeight; ... screenX = screenWidth * ( 1.0f - screenX); screenY = screenHeight * ( 1.0f - screenY); return new Vector2(screenX, screenY); } 
+4
source share
3 answers

It seems to be like this:

  Vector2 Convert(Vector3 pos, const Matrix& viewMatrix, const Matrix& projectionMatrix, int screenWidth, int screenHeight) { pos = Vector3::Transform(pos, viewMatrix); pos = Vector3::Transform(pos, projectionMatrix); pos.X = screenWidth*(pos.X + 1.0)/2.0; pos.Y = screenHeight * (1.0 - ((pos.Y + 1.0) / 2.0)); return Vector2(pos.X, pos.Y); } 

What we are doing here is simply to convey the Vector, although there are two transformation matrices: view, and then projection. After projection, you get a vector with Y and X between -1 and 1. We do the corresponding transformation to get the real coordinates of the pixels and return a new vector2. Note that the z-component "pos" also stores the depth of a point in the screen space at the end of the function.

You need a β€œview” matrix because it determines where the camera is located and rotates. A projection only defines how the 3D space is β€œflattened” on the 2D space.

The field of view is not farZ. The projection matrix has some parameters, among which:

  • field of view, FOV, that is, the horizontal angle of view, in radians;
  • far plane or farZ: this determines the maximum distance that a point can be from the camera;
  • nearest plane, near Z: the minimum distance that can be from the camera point.

Besides the math problem, you can directly use Vector2 instead of allocating a heap (returning a pointer). Vector2 is a lightweight structure, and pointers are likely to cause headaches in this context (where are you going to remove it, etc.). Also note that I used const links, since we do not modify them except the vector. This requires a local copy, so it is not a link at all.

+10
source

The previous code only works if you do not perform any turns (for example, GL.Rotate(rotation_x, 1.0, 0.0, 0.0) ). But if you are here, then the code:

 private Vector2 Convert(Vector3 pos, Matrix4 viewMatrix, Matrix4 projectionMatrix, int screenWidth, int screenHeight) { pos = Vector3.Transform(pos, viewMatrix); pos = Vector3.Transform(pos, projectionMatrix); pos.X /= pos.Z; pos.Y /= pos.Z; pos.X = (pos.X + 1) * screenWidth / 2; pos.Y = (pos.Y + 1) * screenHeight / 2; return new Vector2(pos.X, pos.Y); } 
+5
source

I think you are looking for a replacement for gluLookAt . Given the position and orientation, it converts the scene geometry into screen coordinates for rendering. As the article says, it relies on a number of legacy OpenGL functions, but it provides sample code that you can implement using your vector / matrix library. More information on projection matrices can be obtained from here .

Once you have the projection matrix, you simply apply it to your vectors (after multiplying the scene vectors by the projection matrix), and then simply discard the Z component of the resulting vector ... that is, just use the X and Y components of the resulting vectors.

0
source

All Articles