I have a frustum with
f=10, corresponding to left, right, near, far respectively. I also define top and bottom too.
I've set the camera eye up at
(0, 0, -2.5), looking directly at
(0, 0, 0) with up
(0, 1, 0).
Suppose I want to position an object centred at
z=-1. I want to find the
x coordinate so that under this frustum it appears exactly centred on the left edge of the screen.
I set up the following diagram to help me:
+z | \ | / -x<_____\_________|_________/______>x \____x___|-1 / \ | / \ | / \_|l|_| / \ | / |near dist=n=0.5 \ | / | \ | / | \a| / | \|/ | |-2.5 | -z
So to find
x I can use simple trig. Since
tan a = |l|/n and
tan a=x/1.5 then the formula for
|.| is the absolute value.
But when I use this
x value to draw an object left of the centre, it does not appear centred at the edge of the screen. I can see the object but it's not on the edge. Moreover, if I increase
n then the gap between the object and the left edge increases. What am I doing wrong?
Empirically I have discovered that if I compute
x=|l|/n*2.5 then my object is correctly centred on the left edge of the device screen, and works for any
n value I choose. Not sure why 2.5 works...
Not really worthy of an answer, but I found in some legacy code that the renderer was set up to transalte the z forward by 1 unit, so this was undoing my
-1 z value, hence why multiplying by 2.5 worked and not 1.5.
Despite this annoying fact, I'm now confident I fully understand how the frustum fits into OpenGL.