glFrustum Frustrations

Hi all,

I don’t know how far the away the screen is.

I want to move my camera so that an object will perfectly align to the top of the screen but I don’t understand what to set my z to in a lookAt call to make it line up right given a certain frustrum.

For example (in Java OpenGL ES),

// Creating the perspective
float ratio = (float)width/(float)height;
gl.glFrustumf(-ratio,ratio,-1,1,4,28);

// The front face of my rectangular object is on the x,y plane
// and it’s top edge is at y=4.
// I want the top edge of it to align to the top
// of the screen. Since the near top is 1 and my object top
// is 4, I figured that I need my camera 4 times as far away
// as near to perfectly align. But…

// This doesn’t work because the camera is too far away
glu.GLUlookAt(0,0,16, 0,0,0, 0,1,0);

// This is real close, but I don’t know why this Z works nor
// do I understand how to derive it.
glu.GLUlookAt(0,0,10.44f, 0,0,0, 0,1,0);

Any help would be appreciated.

Regards,
Seed

It’s 4 units away, down the negative Z axis, in eye-space (see your “4” above).

That is, you’re looking from 0,0,0 in eye space, at a screen on the Z=-4 plane.

It’s 4 units away, down the negative Z axis, in eye-space (see your “4” above).

That is, you’re looking from 0,0,0 in eye space, at a screen on the Z=-4 plane.
[/QUOTE]

OK, thanks. I get that the near clipping plane is 4 units away. But that is not really what I am asking. I guess my terminology is all messed up.

I have an object on the xy plane in object space that has it’s top edge on y=4. How far away do I have to be away in a lookat call to make the top edge of my object line up to the top edge of the screen given the frustum I gave - and why?

My experiment says that my eye needs to be right around 10.44f, but I don’t understand why. I would think that z=16 is right.

in the example code you posted, what are the values of width and height? these are being used to determine the size of the near plane.

if you want your object which is at the far plane to fill the screen it just has to be the same size as the far plane I believe. when calling glFrustum you specify the size of the near plane and you position it relative to the camera. The near plane size and distance determine the field of view angle. Given that angle, you could find where the far plane intersects it and this would give you the size of your far plane.


height_of_far_plane = tan(fov/2) * dist_from_eye_to_far_plane

They are the width and height of the screen. I am just using them to get a ratio of width to height.

My understanding is that the far plane is the far clipping plane. I want my object between the two planes but, since it extends backwards, I can’t put it at the far plane or it will get clipped.

Right. OK. Calculating FOV from glFrustom is easy. It is:

fov = atan(top/near)*2
fov = atan(1/4)*2
fov = 0.48996
Now, your calculation leads to:

height_of_plane = tan(fov/2) * dist_from_plane
dist_from_plane = height_of_plane / tan(fov/2)
dist_from_plane = 4 / tan(0.48996/2)
dist_from_plane = 16

Argh. Just a fancy way of doing the ratio of near to z. And wrong because 10.44 works and 16 is too far away.

Thus the frustration.

Oh, crap.

Nevermind. I know exactly what I am doing and am doing it exactly right. There was some friggin model translation hidden somewhere in the code that I didn’t see that was screwing things all up.

I appologize.

If anyone read this that cared, they probably learned something.

Thanks for the help.

my point was not that you had to render it at the far plane, just that at the far plane the plane size would be mapped to the view size, so figuring out the plane size at any point would be key to ensuring that something at a given depth fills the screen.

sounds like you got it working though :slight_smile: