Search code examples
c++graphics3drasterizing

Perspective projection - how to convert coordinates


I'm studying perspective projections and I stumbled upon this concept: enter image description here

Basically it says that if I have a point (x,y,z) I can project it into my perspective screen (camera space) by doing

x' = x/z
y' = y/z
z' = f(z-n) / z(f-n)

I can't understand why x' = x/z or y' = y/z


Solution

  • Geometrically, it is a matter of similar triangles.

    In your diagram, because (x,y,x) is on the same dotted line as (x',y',z'):

    triangle [(0,0,0), (0,0,z), (x,y,z)]
      is similar to
    triangle [(0,0,0), (0,0,z'), (x',y',z')]
    

    This means that the corresponding sides have a fixed ratio. And, further, the original vector is proportional to the projected vector. Finally, note that the notional projection plane is at z' = 1:

    (x,y,z) / z   =  (x',y',z') / z'
    
      -> so, since z' = 1:
           x'/z' = x' = x/z
           y'/z' = y' = y/z
    

    [Warning: note that the z' in my answer is different from its occurrence in the question. The question's z' = f(z-n) / z(f-n) doesn't correspond directly to a physical point: it is a "depth value", which is used to do things like hidden surface removal.]