Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: how to keep 3D object position/size the same visually while the viewport changes

  1. #1
    Junior Member Newbie
    Join Date
    Feb 2004
    Location
    USA
    Posts
    7

    how to keep 3D object position/size the same visually while the viewport changes

    I draw a 3D object and use glTranslate() to change the Z coord. and make it occupy a particular section of my window (the whole window in this case). When i change screen resolution, the window/viewport becomes bigger/smaller (in pixels) and my 3D object seems to be zoomed in/out and does not occupy the same space anymore (i am still using the same values in glTranslate()). How can i fix this ? Is there a math formula to be used ?

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2000
    Location
    Gastonia, NC, USA
    Posts
    2,068

    Re: how to keep 3D object position/size the same visually while the viewport changes

    When you resize your window, are you telling openGL that you have change viewport with the glViewport command?

  3. #3
    Junior Member Newbie
    Join Date
    Feb 2004
    Location
    USA
    Posts
    7

    Re: how to keep 3D object position/size the same visually while the viewport changes

    I actually change the screen resolution between invocations of my app so yes the viewport is reset each time with glViewport(). It seems like the size of the viewport changes how much of a scene one can see and that i need to change the Z value of glTranslate() or my zoom factor since i always want my object to occupy the whole window up to the edge. I don't know how to compute this correctly for arbitrary viewport sizes...
    May be there is a simple solution that i am missing.

    Originally posted by nexusone:
    When you resize your window, are you telling openGL that you have change viewport with the glViewport command?

  4. #4
    Senior Member OpenGL Guru
    Join Date
    Jun 2000
    Location
    Gastonia, NC, USA
    Posts
    2,068

    Re: how to keep 3D object position/size the same visually while the viewport changes

    Are also at resize are you changeing ortho or prespective view settings?

  5. #5
    Junior Member Newbie
    Join Date
    Feb 2004
    Location
    USA
    Posts
    7

    Re: how to keep 3D object position/size the same visually while the viewport changes

    YES i use gluPerspective(), everything looks good (my cube fits my window perfectly) on 1024 by 768 using a particular Z value for glTranslate() before drawing my cube, then when i switch to say 1600 by 1200 and restart the app (keeping the same Z value) the cube appears bigger than my window..

    Originally posted by nexusone:
    Are also at resize are you changeing ortho or prespective view settings?


  6. #6
    Senior Member OpenGL Guru
    Join Date
    Jun 2000
    Location
    Gastonia, NC, USA
    Posts
    2,068

    Re: how to keep 3D object position/size the same visually while the viewport changes

    Can you post the code you use to set the viewport and perspective view.

    Take a look at some of my example code and how I handle resize. www.angelfire.com/linux/nexusone/


    [This message has been edited by nexusone (edited 02-17-2004).]

  7. #7
    Junior Member Newbie
    Join Date
    Feb 2004
    Location
    USA
    Posts
    7

    Re: how to keep 3D object position/size the same visually while the viewport changes

    here is objective-c snippet (it should be understandable):
    - (void)setViewportRect: (NSRect) _bounds {
    bounds = _bounds;
    glViewport (0.0, 0.0, bounds.size.width, bounds.size.height);

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    gluPerspective( 45.0, bounds.size.width / bounds.size.height, 1.0, 1000.0 );
    }

    i guess what i am trying to find out is a formula between screen size/location and 3D coords. I know where i want my object to appear and how big it should be on the screen so how do i figure out its 3D coords given the Perspective Viewing Volume i am using ?

    Originally posted by nexusone:
    Can you post the code you use to set the viewport and perspective view.

    Take a look at some of my example code and how I handle resize. www.angelfire.com/linux/nexusone/


    [This message has been edited by nexusone (edited 02-17-2004).]

  8. #8
    Senior Member OpenGL Guru
    Join Date
    Jun 2000
    Location
    Gastonia, NC, USA
    Posts
    2,068

    Re: how to keep 3D object position/size the same visually while the viewport changes

    You can make all of settings of gluPerspective fixed.
    This:
    gluPerspective( 45.0, bounds.size.width / bounds.size.height, 1.0, 1000.0 );
    Becomes:
    gluPerspective( 45.0, 1, 1.0, 1000.0 );
    So that no mater how the window is resized all the object stay in there location. Now you will have to add a routine to re-adjust the window to keep that aspect ratio while being dragged.

    The other idea of moving the objects to adjust for the dragging of the window, could become a complex routine.



    [This message has been edited by nexusone (edited 02-18-2004).]

  9. #9
    Junior Member Newbie
    Join Date
    Feb 2004
    Location
    USA
    Posts
    7

    Re: how to keep 3D object position/size the same visually while the viewport changes

    i can't constrain my window to a fixed aspect ratio and i thought that gluPerspective should use the viewport/window's aspect ratio to avoid distortion... To simplify let's say i am using the whole screen as my viewport, when the user changes resolution (from 1024x768 to 1900x1200 for example) the aspect ratio will change but i need to make sure that an object that used to exactly occupy the whole screen (or one of its sides) still does after the resolution change. In my case i want the face of a cube to always fill the screen's whole height regardless of resolution and aspect ratio... how can i compute this ?

    Originally posted by nexusone:
    You can make all of settings of gluPerspective fixed.
    This:
    gluPerspective( 45.0, bounds.size.width / bounds.size.height, 1.0, 1000.0 );
    Becomes:
    gluPerspective( 45.0, 1, 1.0, 1000.0 );
    So that no mater how the window is resized all the object stay in there location. Now you will have to add a routine to re-adjust the window to keep that aspect ratio while being dragged.

    The other idea of moving the objects to adjust for the dragging of the window, could become a complex routine.

    [This message has been edited by nexusone (edited 02-18-2004).]

  10. #10
    Member Contributor
    Join Date
    Jan 2004
    Location
    Hoboken,NJ,USA
    Posts
    60

    Re: how to keep 3D object position/size the same visually while the viewport changes

    I think if you want to place an object so it is bounded by a region on the screen, you have to invert the matrix, effectively pick the corners, and find out the corresponding points in the space. Then you scale the object to reach those points.

    If you don't need to do the full solution, you can just iteratively play around until you've got it right, but if this is a dynamic issue, I think the above is the only way to actually do it robustly.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 2
    Last Post: 10-16-2012, 11:23 AM
  2. MRT with different size,viewport,format
    By vkMarco in forum OpenGL: Basic Coding
    Replies: 8
    Last Post: 05-06-2011, 08:07 PM
  3. Can't Change Viewport Size on XP, OpenGL 1.1?
    By NoelC in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 06-19-2010, 02:03 PM
  4. problems with fbo and viewport size
    By lobbel in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 05-10-2010, 05:56 PM
  5. same object, different size/ viewport
    By in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 03-12-2003, 09:07 AM

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