title: How to transform an image based on the position of camera
body: <p>I'm trying to create a perspective projection of an image based on the look direction. I'm unexperienced on this field and can't manage to do that myself, however. Will you help me, please?</p>
  <p>There is an image and an observer (camera). If camera can be considered an object on an invisible sphere and the image a plane going through the middle of the sphere, then camera position can be expressed as:</p>
  <blockquote>
  <p>x = d cos(θ) cos(φ)</p>
  <p>y = d sin(θ)</p>
  <p>z = d sin(φ) cos(θ)</p>
  </blockquote>
  <p>Where θ is latitude, φ is longitude and d is the distance (radius) from the middle of the sphere where the middle of the image is.</p>
  <p>I found these formulae somwhere, but I'm not sure about the coordinates (I don't know but it looks to me that x should be z but I guess it depends on the coordinate system).</p>
  <p>Now, what I need to do is make a proper transformation of my image so it looks as if viewed from the camera (in a proper perspective). Would you be so kind to tell me a few words how this could be done? What steps should I take?</p>
  <p>I'm developing an iOS app and I thought I could use the following method from the QuartzCore. But I have no idea what angle I should pass to this method and how to derive the new x, y, z coordinates from the camera position.</p>
  <pre><code>CATransform3D CATransform3DRotate (CATransform3D t, CGFloat angle,
      CGFloat x, CGFloat y, CGFloat z)
  </code></pre>
  <p>So far I have successfully created a simple viewing perspective by:</p>
  <blockquote>
  <ol>
  <li>using an identity matrix (as the CATransform3D parameter) with .m34 set to 1/-1000,</li>
  <li>rotating my image by the angle of φ with the (0, 1, 0) vector,</li>
  <li>concatenating the result with a rotation by θ and the (1, 0, 0) vector,</li>
  <li>scaling based on the d is ignored (I scale the image based on some other criteria).</li>
  </ol>
  </blockquote>
  <p>But the result I got was not what I wanted (which was obvious) :-/. The perspective looks realistic as long as one of these two angles is close to 0. Therefore I thought there could be a way to calculate somehow a proper angle and the x, y and z coordinates to achieve a proper transformation (which might be wrong because it's just my guess).</p>