Search code examples
pythonmathmatrix3dcoordinate-systems

How convert 3d world coordinates to 3d camera coordinates


I have an input 3D vector points in world coordinate system. Can anyone describe or provide a link to a resource that will help me understand and implement the required transformation and matrix mapping to convert into camera coordinates? Image for this http://www.mathworks.in/help/matlab/visualize/chview3.gif I know the viewpoint coordinates in this image in world coordinates and them convert into camera coordinates


Solution

  • You have to apply two math operations:

    1. Shift all coordinates by the coordinates of the view point - but in opposite direction. After this, the viewpoint is the new origin and all coordinates are expressed according to it.

    Example (the '|' just denotes the vector parentheses)

    |x'|   |x| - |x_vp|
    |y'| = |y| - |y_vp|
    |z'|   |z| - |z_vp|
    
    1. Rotate the coordinates. e.g. first around Z, then Y. This is done via two rotation matrices:

    Z:

    |x''|   | cos a    -sin a    0 |   |x'|
    |y''| = | sin a     cos a    0 | * |y'|
    |z''|   |  0         0       1 |   |z'|
    

    Y:

    |x'''|   | cos b    0    -sin b |   |x''|
    |y'''| = |  0       1      0    | * |y''|
    |z'''|   | sin b    0     cos b |   |z''|
    

    For example, if your VP is at (1, 1, 1), you first shift it so that the old origin now is at (-1, -1, -1) . The camera is still looking into positive x direction, so you would rotate it by 225 degree around z (now spotting on the old z axis) and then by 45 degree around y to spot directly on the old origin.

    However, you do not rotate the camera, but the whole space around the camera, so you have to multiply the angles by -1.

    You can find more infos at http://en.wikipedia.org/wiki/Rotation_matrix

    If you don't know matrix multiplication, the first chapter there shows it.