0
\$\begingroup\$

I have a frustum with l=-2, r=2, n=0.5, 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 x is x=|l|/n*1.5=2/0.5*1.5=6, where |.| 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?

UPDATE

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...

SOLUTION

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.

\$\endgroup\$
  • \$\begingroup\$ In your text you write "exactly centered on the left edge" and in the drawing you show it centered in the left half of the screen. \$\endgroup\$ – Bram Dec 6 '18 at 23:24
  • \$\begingroup\$ @Bram well, the camera is centred at (0,0,0) so (-x,0,0) should be exactly on the left edge of the screen. As you can see the left side of the frustum skims the left of the near plane so x should correspond to the left edge of the screen for z=-1. \$\endgroup\$ – Antinous Dec 7 '18 at 7:52
0
\$\begingroup\$

In your example:

0.5*left / n = x / 1.5
-1 / 0.5 = x / 1.5
-2 = x / 1.5
-3 = x

Or more elaborately, if you want to go via the angle it self:

tan( a ) = 0.5*left / 0.5
a = atan( -2 )
tan( a ) = x / 1.5
tan( atan( -2 ) ) = x / 1.5
-2 = x / 1.5
-3 = x
\$\endgroup\$
  • \$\begingroup\$ But tan a is not equal to 0.5*left/0.5 ... it is equal to l/n... goimg by tan = opposite / adjacent \$\endgroup\$ – Antinous Dec 7 '18 at 7:47
  • \$\begingroup\$ Maybe the drawing was a little confusing, please see update. Nonetheless the x value does not draw at the exact left edge of the screen. I'm sure my maths is right? So there must be some unkown i dont know about. Will take a look again. \$\endgroup\$ – Antinous Dec 7 '18 at 7:59

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.