Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 10 of 10

Thread: Coordinate -> Viewport scaling

  1. #1
    Junior Member Newbie
    Join Date
    Nov 2005
    Location
    Rvk
    Posts
    6

    Coordinate -> Viewport scaling

    Hello, i'm having a little difficulty here. I've got an application where i'm doing some very simple drawing but having proplems with scaling.

    How can i scale the vertex2f input
    parameters into the viewport,that is
    (vertex2f coordinates->viewport coordinates).

    Drawing a dot at (0.0, 0.0) draws a dot into the middle of the screen, now drawing again at (1.0, 1.0) and i'm drawing way outside the viewport. How can i scale this so that i'm drawing, lets say (50,50) before its drawing outside my viewport.

    I've been trying to work around this for a while but see nothing to my aid.

    Any suggestions on solving this issue ?

  2. #2
    Intern Contributor
    Join Date
    Apr 2005
    Location
    Prague, Czech Republic
    Posts
    87

    Re: Coordinate -> Viewport scaling

    You have to set a correct projection..take a look for a functions glOrho , gluOrtho2D, glFrustum and gluPerspective and choose one of them to setup your projection matrix

  3. #3
    Junior Member Newbie
    Join Date
    Nov 2005
    Location
    Rvk
    Posts
    6

    Re: Coordinate -> Viewport scaling

    i've tried both glOrtho and gluPerspective already, still messing around with them to get it right

  4. #4
    Senior Member OpenGL Pro
    Join Date
    May 2000
    Location
    Naarn, Austria
    Posts
    1,102

    Re: Coordinate -> Viewport scaling

    The easiest function is glOrtho. You just give it the coordinates that the viewport borders should have.

    For example, if you want the cube from -50 to 50 along every axis to be just inside the viewport, you call:
    glOrtho(-50, 50, -50, 50, -50, 50);

  5. #5
    Junior Member Newbie
    Join Date
    Nov 2005
    Location
    Rvk
    Posts
    6

    Re: Coordinate -> Viewport scaling

    thanks overmind for your answer but i'm afraid it didnt solve my proplem. I tried puting the values you gave me into my Ortho function but i still have to draw in coordinates about -0.95f to 1.0f on x and y axis if i dont want to draw outside the screen.

    Is there no way to scale those numbers so i can draw from lets say +-X before drawing outside the viewport. Where as X is some chosen number?

  6. #6
    Senior Member OpenGL Pro
    Join Date
    May 2000
    Location
    Naarn, Austria
    Posts
    1,102

    Re: Coordinate -> Viewport scaling

    The glOrtho call should do exactly this. Perhaps you have called it in the wrong place?

    This is the complete code:
    Code :
    glViewport(0, 0, x, y);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-50, 50, -50, 50, -50, 50);
    glMatrixMode(GL_MODELVIEW);
    x and y are the dimensions of your window. This should be called once at program start and whenever the size of the window changes. If you use glut, the reshape function does exactly this. You can also put it at the beginning of your drawing code, but that would be slower...

    Make sure that glOrtho really gets called. Perhaps put a debug output around it.

  7. #7
    Junior Member Newbie
    Join Date
    Nov 2004
    Posts
    10

    Re: Coordinate -> Viewport scaling

    No mather what projection you're using, the center of the screen is always (0, 0), because the gl camera is located by default at (0,0,0) and looking at the negative z-axis. You'll have to make a translation of the coordinate system before you draw anything.

  8. #8
    Junior Member Newbie
    Join Date
    Nov 2005
    Location
    Rvk
    Posts
    6

    Re: Coordinate -> Viewport scaling

    fWidth = m_rWindowSize.width;
    fHeight = m_rWindowSize.height;

    glViewport(0, 0, 480, 480);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    glOrtho(-50, 50, -50, 50, -50, 50);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    Unfortunately my code looks exacly like you described it should be, no i'm not using glut for window creation, i dont find it flexible enough.

    I suspect there is a proplem somewhere with the initialisation of the window. So i'm currently doing some extensive testing and debugging trying to find it out. Thnx for your replies, if anyone knows the solution to my proplem a post would be appreciated

  9. #9
    Junior Member Newbie
    Join Date
    Nov 2005
    Location
    Rvk
    Posts
    6

    Re: Coordinate -> Viewport scaling

    oh, thnx for you post Hernan, i'll look into that, hopefully it will solve itself

  10. #10
    Junior Member Newbie
    Join Date
    Nov 2005
    Location
    Rvk
    Posts
    6

    Re: Coordinate -> Viewport scaling

    I found out what my proplem was, forgot to specify depth testing type (bangs head into wall).

    Thnx for your posts everyone , esp you hernan, your post set me on path and gave me a clue about what the proplem was. Found the dots "dissapearing" when i zoomed in or out on the Z so i figured it was the depth testing.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •