World space to camera space

2019-04-28 05:35发布

I am confused on how to convert world space coordinates to camera coordinates.

My current understanding is that I would need to calculate the camera space vector where

n = eyepoint - lookat

u = up(0,1,0) X n(normalized)

v = n X u

Then once I have < U, V, N > would I simply multiply each point by ?

标签: math matrix 3d
2条回答
家丑人穷心不美
2楼-- · 2019-04-28 05:57

Lets assume:

  • Eye position is E=(e_x, e_y, e_z),
  • Viewing direction is D=(d_x, d_y, d_z)
  • Up-Vector is UP=(up_x, up_y, up_z)

Now first construct an orthonormal frame:

  • R = D X UP
  • U = R X D
  • Now normalize D,R,U and you have an orthonormal frame for the camera (D,R,U)

In order to transform the global coord frame into the cam-coord frame you can apply the following matrix M_R:

  • | R_x, R_y, R_z, 0 |
  • | U_x, U_y, U_z, 0 |
  • | -D_x, -D_y, -D_z, 0|
  • | 0.0, 0.0, 0.0, 1.0|

If your cam is not positioned at global origin you also have to apply a translation M_T:

  • | 1, 0, 0, -e_x |
  • | 0, 1, 0, -e_y |
  • | 0, 0, 1, -e_z|
  • | 0, 0, 0, 1|

In the end your complete transformation matrix from global to cam-coords is:

  • M = M_R * M_T
  • | R_x, R_y, R_z, (R dot -E) |
  • | U_x, U_y, U_z, (U dot -E) |
  • | -D_x, -D_y, -D_z, (D dot E)|
  • | 0.0, 0.0, 0.0, 1.0|
查看更多
做个烂人
3楼-- · 2019-04-28 05:59

I think there is an error in previous post

this matrix

| R_x, R_y, R_z, (R dot -E) |
| U_x, U_y, U_z, (U dot -E) |
| -D_x, -D_y, D_z, (D dot E)|
| 0.0, 0.0, 0.0, 1.0|

should be(I test it in openGL, this one is right)

| R_x, R_y, R_z, (R dot -E) |
| U_x, U_y, U_z, (U dot -E) |
| -D_x, -D_y, -D_z, (D dot E)|
| 0.0, 0.0, 0.0, 1.0|
查看更多
登录 后发表回答