I am trying to make a renderer for a programming project, and yet I am having trouble projecting the points onto the screen (the way it works so far, the camera can't look down on a face because the face gets stretched out horizontally).

Imagine the area around the camera as a sphere, the camera projecting what it is facing onto the screen. The $yaw$ is the angle of the ray whose endpoint is the camera, on the same $xz$ plane as the camera that intersects a surrounding point. The $pitch$ is the angle of a similar ray from the $xz$ plane to a surrounding point. I am looking for formulas to convert the $pitch$ and $yaw$ into $x$ and $y$ coordinates I can show on-screen.

The linked image shows the type of projection I want. The $yaw$ goes around the parallels, and wraps around the poles, so the projection continues above and below for points that are more than more than 90 or less than negative 90.

This means that any formula you find should have a solution "above" and "below" if a surrounding point is behind the player.

Thank you in advance.

Projection from inside sphere

Projection from inside sphere


Your projection as shown in the picture is not from the center of the sphere. The projection from the center of the sphere would put the north and south poles at $\pm\infty$ on the $z$-axis of the projected image. These are apparently at finite values. Therefore I will make the assumption that the projection is from the point $(-1, 0, 0)$ on the unit sphere, that the projection is of points on the unit sphere onto the plane $x = R$ for some fixed value $R$.

You have not adequately defined pitch and yaw, so I will try to give them a complete definition. Consider the ray from the origin through the point being projected. Then

  • pitch is the directed angle between the vector and the $xy$-plane ($z = 0$), positive if the vector lies above the plane ($z > 0$) and negative below the plane.
  • yaw is the directed angle between the plane containing the vector and the $z$-axis, and the $xz$-plane ($y = 0$), positive when $y > 0$ and negative when $y < 0$.

If we let $\theta$ be the yaw angle, and $\phi$ be the pitch angle, and if $(x_0, y_0, z_0)$ is the point on the unit sphere, then note that $r^2 + z_0^2 = 1$, where $r^2 = x_0^2 + y_0^2$. $r$ is the radius of the circle of constant pitch, and $x_0 = r\cos\theta, y_0 = r\sin\theta$. $r$ and $z_0$ form coordinates for the great circle of constant yaw. So $r = \cos\phi, z_0 = \sin\phi$. Putting these together: $$(x_0, y_0, z_0) = (\cos\phi\cos\theta, \cos\phi\sin\theta, \sin\phi)$$

Now to find the projected point, we find the intersection of the line through $(-1, 0, 0)$ and $(x_0, y_0, z_0)$, whose equation is $$(x, y, z) = (-1, 0, 0)(1 - t) + t(x_0, y_0, z_0) = (-1 + t(1 + \cos\phi\cos\theta), t\cos\phi\sin\theta, t\sin\phi)$$

Setting the $x$ value to $R$: $$-1 + t(1 + \cos\phi\cos\theta) = R$$$$t = \frac{R + 1}{1 + \cos\phi\cos\theta}$$

Hence the $y$ and $z$ values of the projection are: $$y = \frac{(R + 1)\cos\phi\sin\theta}{1 + \cos\phi\cos\theta}$$ $$z = \frac{(R + 1)\sin\phi}{1 + \cos\phi\cos\theta}$$ If instead of $(-1, 0, 0)$, we put the camera at $(a, 0, 0)$, these formulas become $$y = \frac{(a - R)\cos\phi\sin\theta}{a - \cos\phi\cos\theta}$$ $$z = \frac{(a - R)\sin\phi}{a - \cos\phi\cos\theta}$$


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy