Read: http://mathworld.wolfram.com/Tangent.html -- look for the big triangle. Remember SOHCAHTOA.

tan(theta), where theta = pi/8, gives you the rise over run. When the camera is 1 unit away from the origin, the run is 1. The rise goes on to give you half of the image plane width.

That helps because I didn't know what the tangent was for.

I've analyzed the code and simplified it. Below is the final modified version. Let's look at it line by line.

First useless line.

const float pi = 4.0f*atanf(1.0f);

All this does is give us the value of PI. math.h has a #define you can use so this is unnecessary. As you'll see in the finial code, many things were unnecessary.

Next line.

const float aspect = (float)(m_renderbufferWidth) / (float)(m_renderbufferHeight);

This give us the aspect ratio of the screen. Not sure why this is needed for the x coordinate and not the y. I think it's magic.

Next Two lines.

const float fx = 2.0f * ((float)(x) / (float)(m_renderbufferWidth - 1)) - 1.0f; const float fy = 2.0f * ((float)(y) / (float)(m_renderbufferHeight - 1)) - 1.0f;

This converts screen coordinates from a top/left orientation to a center screen coordinates but there's a little more to this then that.

1) Subtracting the width and height by one. The reason you do this is because resolution sizes are usually all even numbers. For example, a resolution of 1280x720 there is no center pixel or a pixel at 0. The other reason this could be is that you need a value of base 0, as if this was an array and w/h represents the total number of elements.

2) x/y divied by w/h. This gives you a normalize value of the point on your screen. In other words, a value from 0 to 1.

3) 2.0f *; not entirely sure what this is doing and why it is needed.

4) The last - 1.0f flips the sign.

Second useless line

const float y_fov = pi/4; // pi/4 radians = 45 degrees

This takes PI and divides it by 4 to give us 45 degrees in radians. This is unnecessary.

Modified line.

const float tangent = tan(y_fov / 2.0f);

This divides the 45 degrees by 2 to give use 22.5 degrees in radians. This can be simplified by plopping a number in tan.

I know what the rest is doing, I just don't understand why it works.

/************************************************************************ * desc: Convert 2d screen coordinates to 3D perspective space ************************************************************************/ void Convert2Dto3D( float & destX, float & destY, float x, float y, float width, float height ) { const float aspect = width / height; // Convert upper left 0,0 to center screen 0,0 coordinates const float fx = (2.0f * (x / (width - 1))) - 1.0f; const float fy = (2.0f * (y / (height - 1))) - 1.0f; // (pi/4 radians = 45 degrees / 2) or ((pi/4) / 2)) const float tangent = tan( 0.392699082 ); // Project x,y to a z plane of 1 destX = aspect * tangent* fx; destY = -tangent * fy; } // Convert2Dto3D