Viewport

Really simple question:
How can I make a cube fit exactly the viewport?
I want to make a screen saver with a rotating cube.

Many thanks in advance!!! :slight_smile:

I don’t know if I was clear enough:
The cube has pictures in each of it sides, so each cube’s side (or paralelepid actually) would measure the size of the screen, except the top and bottom caps of the cube which would measure width * width of the screen.
So then the cube would rotate through vertical-axis to scroll through the pictures.

Thanks!

Sure you mean the shape is a parallelepiped ? I doubt, cauase then you have parallelograms as sides, and a parallelogram is a generalized rectangle with arbitrary corner angles.

I assume you mean a cuboid , which means all right angles.

If so, an easy setup is to make a unit cube inside a square frustum and position it a distance away from the viewpoint depending on the field of view. The simplest case would be a 45 degree FOV, since the distance is the same as the cube itself basically.

Now with a unit cube, the sides are 2 units (from -1 to 1). So the faces should be positioned 2 units away from the viewpoint to exactly cover the size of the viewport. The center of the cube should therefore be placed at 3 units (2 units for the face, and 1 unit to get to the center of the cube).

Now the near plane. The closest part of a cube is the corner during a 45 degree rotation. In that case, the cube extends from sqrt(2) units towards the viewpoint from the cubes center. So the near plane should be no further away than 3-sqrt(2), so 1.5 will do. Same with the far plane; the cube extends at most 3+sqrt(2), so 4.5 will work.

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, 1, 1.5, 4.5);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotate(angle, 0, 1, 0); // animate your angle variable here
glTranslate(0, 0, 3);

glBegin(GL_QUADS);
    // draw a unit cube here
glEnd();

Should give you a cube that fits exactly to the viewport during a multiple of 90 degree rotations.

A little bugfix:

glTranslatef(0.0f, 0.0f, -3.0f);
glRotatef(angle, 0, 1, 0);

Thanks guys!

Yeah I figured out that bug.
However, I am still not getting a perfect fit.

A portion of my image is still being cut (I change to glTranslatef(0,0,-3.25) to fix this) and when the cube rotates the black screen gets into view.

I tried playing around with the FOV values but couldn’t make it.

Any ideas?

I am using gluPerspective(45, 1, 1.5, 4.5)
and a a unit cube, centered in (0,0,-3.25)

I think the problem is that might need to switch from perspective view to orthogonal view. May this be the case?

Thanks so much in advance!

Originally posted by <Cacho>:
I think the problem is that might need to switch from perspective view to orthogonal view. May this be the case?
not really.

If you don’t want any part of any images to be clipped vertically at any time, and you don’t want any background to show through at any time, then an orthographic projection is the way to go.

But then again, all the user will see is two images sliding across the screen, possibly being shrinked/strectched slightly horizontally as they slide. Will not be any sense of a spinning object, just some sliding pictures.

Sorry… yes! I meant orthographical projection.
Thanks!

Ok I will check it out!!!
Thanks so much guys!!!

Cheers!