Reputation: 1451
I'm starting out on OpenGL. I'm not sure how gluOrtho2d
and glLoadIdentity
play out together. In particular, I have the following code, which is supposed to draw a 2x2 square in the center of a 10x10 display.
int main() {
glutCreateWindow("Draw A Square");
glutDisplayFunc(mydisplay);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-5.0, 5.0, -5.0, 5.0);
glutMainLoop();
}
void mydisplay() {
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
drawAUnitSquare();
glFlush();
}
void drawAUnitSquare() {
// glLoadIdentity();
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(-1, -1);
glVertex2f(1, -1);
glVertex2f(1, 1);
glVertex2f(-1, 1);
glEnd();
}
The above code works. However, if I uncomment the glLoadIdentity();
in drawAUnitSquare()
, the square then fills the entire viewport. What is happening here?
Upvotes: 1
Views: 629
Reputation: 2953
You forgot to switch the matrix mode back to GL_MODELVIEW
.
Just add glMatrixMode(GL_MODELVIEW);
after gluOrtho2D(-5.0, 5.0, -5.0, 5.0);
.
Upvotes: 1