PDA

View Full Version : Perspective projection



ukopenglnewbie
12-26-2008, 06:33 AM
Please help me understand this:
I am working on an application that uses perspective projection using:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fFOV, fAspect , fNearPlane, fFarPlane);
with the fFOV = 45 degrees. fNearPlane = 0.1 and fFarPlane = 500.

I can place an object (rectangle) and see it rendered on screen. What I am trying to do is place it somewhere along the Z-axis so that all the four corners fit the four corners of the screen.
What is the math required for this ? I know the size of the viewport as well.

ZbuffeR
12-26-2008, 06:44 AM
gluPerspective fFOV is the vertical fov.
So you have fFOV/2 above and below the view center.
Lets put the rectangle at distance zdist from the camera.
Then its height should be :

tan(fFOV/2)*zdist = h/2
h = tan(fFOV/2)*zdist/2

and width should be :
w = h*viewportw/viewporth

ukopenglnewbie
12-26-2008, 07:11 AM
It works beuatifully. Thank you.

Height = tan((fFOV * M_PI/ 180)/2.0) * (mTranslateZ) * 2 ;
Width = Height * viewport[2] / viewport[3] ;

Viewport is obtained by:

GLint viewport[4];
glGetIntegerv(GL_VIEWPORT,viewport);


Thank you for your help.

funkynesh
01-18-2009, 04:47 PM
Hello,

I have a similar problem where I wish to draw a one by one rectangle in the top left corner of the screen at a given depth.

Computing the width / height at a given depth gives the top-left max visible point of that plane.

y = Height / 2.0f; // positive
x = Width / 2.0f; // negative since 0, 0 is the center of the screen.

However drawing from that point doesn't show anything on the screen.

I assume this point is visible since it would be on the border of the clipping planes and I draw toward the center of the screen, am I correct? The resulting drawing should be inside the clipping volume.

Any ideas? (I am using glRect to draw)

Rosario Leonardi
01-21-2009, 04:03 PM
a) be sure that your model view matrix is an identity
b) your point should be these
xCorner =-width * .5f;
yCorner = height * .5f;
xRect = xCorner + delta;
yRect = yCorner - delta;

And if you really want to use glRect (that is deprecated) you should call it like this in order to have the normal facing the camera.
glRect(xCorner, yRect, xRect, yCorner);

funkynesh
01-30-2009, 02:52 PM
Hello,

Thank you for your response.

Two things:
a) I am not sure to understand why my matrix should be an identity. You were right it wasn't. I translated the modelview matrix 20 unity forward.

I computed the height and width when the translation was done. Shouldn't it work?

I mean the volume doesn't change if I am 20 unit forward in the volume, right? I assumed when the drawing is over that I look from the identity matrix.

Maybe I am using the modelview matrix incorrectly. I thought that once the volume is built thanks to the projection matrix, I could position my object inside it and always view them.

The idea behind my example was to draw a rectangle of a fixed size, let say 5 x 10 and position it at a certain depth at the top left corner. Depending on the depth level I could then add others rectangles so I could fill the screen, but always using the initial value of the rectangle 5 x 10.
i.e. at a translation of n units forward z I could draw 10 rectangles on the same row and at a translation of 2n units forward z I could draw 20 rectangles on the same row.

b) I am using a book (OpenGL SuperBible), and it is using glRect (in the first chapters for now), what is the new alternative? :)