I was trying to understand the gluOrtho2D function. I have drawn 4 lines originating from the center reaching up to 4 corners of the screen.

You can follow the below code. osize is a variable which is used to set the parameters of gluOrtho2D. It will create a window of size 2*osize.

If works fine when osize is 1. Lines reach the corners. But as I increase the value of osize, the length of the lines decreases (cross becomes smaller and does not cover the whole screen). But I think it should reach the corner.

void display()
    glClear( GL_COLOR_BUFFER_BIT );

    //glViewport(0, 0, 100, 100);

    glMatrixMode (GL_PROJECTION);
    float osize = 1.2;
    //glOrtho(-osize*1.0, osize*1.0, osize*1.0, -osize*1.0, -1.0, 1.0);
    gluOrtho2D(-osize*1.0, osize*1.0, osize*1.0, -osize*1.0);
    glMatrixMode (GL_MODELVIEW);

       glColor3f(0.0, 0.0, 1.0);
       glVertex2f(0.0, 0.0);
       glVertex2f(-osize*1.0, -osize*1.0);
       glVertex2f(0.0, 0.0);
       glVertex2f(-osize*1.0, osize*1.0);       
       glVertex2f(0.0, 0.0);
       glVertex2f(osize*1.0, -osize*1.0);
       glVertex2f(0.0, 0.0);
       glVertex2f(osize*1.0, osize*1.0);

    glutSwapBuffers();  //includes glFlush();

What is the problem?

  • 4
    \$\begingroup\$ Are you resetting the projection matrix to the identity anywhere? If not, all the gluOrtho2D() calls will stack on top of each other... \$\endgroup\$ – Nathan Reed Aug 20 '13 at 20:52
  • \$\begingroup\$ Ok got it. After setting the Projection matrix to Identity, it works as expected. Thank you. \$\endgroup\$ – Shashwat Aug 21 '13 at 18:56

Maybe you inverted bottom with top? Ortho matrix will scale Y by 2 / (top - bottom), in your case a negative number.

  • \$\begingroup\$ In fact it only alters the order which lines will be drawn, as we normally think of Y increasing up. \$\endgroup\$ – felipe Aug 21 '13 at 19:04
  • \$\begingroup\$ The difference between the both is just that it will invert the y-axis. \$\endgroup\$ – Shashwat Aug 21 '13 at 19:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.