Results 1 to 8 of 8

Thread: Viewport

  1. #1
    Guest

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

  2. #2
    Guest

    Re: Viewport

    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!

  3. #3
    Senior Member OpenGL Guru
    Join Date
    Feb 2000
    Location
    Sweden
    Posts
    2,978

    Re: Viewport

    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.

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

  4. #4
    Senior Member OpenGL Pro k_szczech's Avatar
    Join Date
    Feb 2006
    Location
    Poland
    Posts
    1,107

    Re: Viewport

    A little bugfix:
    Code :
    glTranslatef(0.0f, 0.0f, -3.0f);
    glRotatef(angle, 0, 1, 0);

  5. #5
    Guest

    Re: Viewport

    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!

  6. #6
    Guest

    Re: Viewport

    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.

  7. #7
    Senior Member OpenGL Guru
    Join Date
    Feb 2000
    Location
    Sweden
    Posts
    2,978

    Re: Viewport

    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.

  8. #8
    Guest

    Re: Viewport

    Sorry... yes! I meant orthographical projection.
    Thanks!

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

    Cheers!

Similar Threads

  1. FBO and viewport
    By Swoop in forum OpenGL: Advanced Coding
    Replies: 2
    Last Post: 07-17-2013, 12:34 PM
  2. 4 viewport
    By cwc36 in forum OpenGL: Basic Coding
    Replies: 11
    Last Post: 09-25-2002, 05:34 PM
  3. Replies: 2
    Last Post: 05-27-2002, 02:59 AM
  4. Viewport or multiple viewport in a window!
    By Boubou in forum OpenGL: Basic Coding
    Replies: 9
    Last Post: 11-30-2000, 05:45 AM
  5. Viewport
    By in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 09-15-2000, 05:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean