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 6 of 6

Thread: 2D grid problem

  1. #1
    Advanced Member Frequent Contributor Ehsan Kamrani's Avatar
    Join Date
    May 2005
    Location
    Iran
    Posts
    547

    2D grid problem

    I'm drawing some 2D grids like 3DS Max or maya. The problem is that the viewport is not square and my code converts the square grids to the rectangle grids!
    here's my code:
    //Draw to the upper left window
    glViewport( 0, m_height / 2 , m_width / 2 , m_height / 2 );
    glMatrixMode(GL_PROJECTION); // Sets the projection matrix.
    glLoadIdentity(); // Reset the modelview matrix.

    // calculate the aspect ratio of the window.
    gluPerspective( 45.f, m_width / m_height , 0.1f, 10000.0f );
    //or
    //gluOrtho2D( -200. , 200. , -200. , 200. );

    glMatrixMode(GL_MODELVIEW); // Sets the projection matrix.
    glLoadIdentity(); // Reset the modelview matrix.
    gluLookAt( 0.0, 0.0, 500.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f );
    glColor3f( 0.7f, 0.7f, 0.7f );
    glLineWidth( 1.0f );
    glBegin( GL_LINES );
    for( CInt index = -10; index <= 10; index++ )
    {
    glVertex3f( 0.0f + (CFloat)index * 20.0f, -200.0f, 0.0f );
    glVertex3f( 0.0f + (CFloat)index * 20.0f, 200.0f, 0.0f );

    glVertex3f( -200.0f ,(CFloat)index * 20.0f, 0.0f );
    glVertex3f( 200.0f,(CFloat)index * 20.0f,0.0f );
    }
    glEnd();


    What's the problem?
    -Ehsan-
    <span style="color: #006600">-Ehsan-</span>

  2. #2
    Super Moderator OpenGL Lord
    Join Date
    Dec 2003
    Location
    Grenoble - France
    Posts
    5,655

    Re: 2D grid problem

    According to the settings in gluPerspective, the glViewport params are wrong. Try with :

    glViewport( 0, 0, m_width, m_height);

    gluPerspective( 45.f, m_width / m_height , 0.1f, 10000.0f );

  3. #3
    Advanced Member Frequent Contributor Ehsan Kamrani's Avatar
    Join Date
    May 2005
    Location
    Iran
    Posts
    547

    Re: 2D grid problem

    Hi ZBuffer
    Yes, My gluPerspective() code had some problems ...
    But your code also draws to the whole window and I still have the same problem. My window is not square and so the grids aren't square
    <span style="color: #006600">-Ehsan-</span>

  4. #4
    Advanced Member Frequent Contributor Ehsan Kamrani's Avatar
    Join Date
    May 2005
    Location
    Iran
    Posts
    547

    Re: 2D grid problem

    I added "double" keyword and my problem solved
    gluPerspective( 45.f, double( m_width / 2 ) / double( m_height / 2 ), 1., 10000. );

    Thank you ZBuffer for suggestions about the m_width and m_height variables inside gluPerspective().

    <span style="color: #006600">-Ehsan-</span>

  5. #5
    Super Moderator OpenGL Lord
    Join Date
    Dec 2003
    Location
    Grenoble - France
    Posts
    5,655

    Re: 2D grid problem

    Ah, good old integer divide ...

  6. #6
    Intern Newbie
    Join Date
    Jan 2010
    Location
    Romania
    Posts
    46

    Re: 2D grid problem

    so the solution would have been to actually read the warnings the compiler spits up and only after that decide wether to ignore them or not

Posting Permissions

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