PDA

View Full Version : glOrtho in display function not working



Yang Zhang
04-04-2010, 06:22 PM
Hi, I have the following simple hello world program from the OpenGL red book:



#include <GL/glut.h>

void display() {
glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT);

// glOrtho(0,1,0,1,-1,1);

glColor3f(1,1,1);
glBegin(GL_POLYGON);
glVertex3f(.25,.25,0);
glVertex3f(.75,.25,0);
glVertex3f(.75,.75,0);
glVertex3f(.25,.75,0);
glEnd();

glFlush();
}

int main(int argc, char **argv) {
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400, 400);

glutCreateWindow("blah");
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,1,0,1,-1,1);

glutDisplayFunc(display);
glutMainLoop();
return 0;
}


This works fine, but if I add glOrtho() to the display function, the window sometimes shows the square in the right position, sometimes shows it offset, and sometimes doesn't show the square at all. Any hints on what's going on here?

strattonbrazil
04-04-2010, 09:01 PM
Hi, I have the following simple hello world program from the OpenGL red book:



glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,1,0,1,-1,1);




Look at this code. Load identity replaces whatever is there with the identity. glOrtho just multiplies that call with what's there. You need to load the identity before the glOrtho call or it'll multiply against itself every screen reload. I bet it works fine when the program starts, but on resizing the square disappears, right?

Yang Zhang
04-05-2010, 12:11 PM
Thanks!