#include <GL/glut.h>
void reshape(int w, int h){
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-w/2, w - w/2, -h/2, h - h/2);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void display(){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1, 1, 1);
glPushAttrib(GL_ALL_ATTRIB_BITS);
glColor3f(1, 0, 0);
glPopAttrib();
glRecti(0, 0, 10, 10); // draws white rect
// Commenting the line makes next rect white
// Uncommenting the line makes next rect red
glTranslatef(0, 0, 0);
glPushAttrib(GL_ALL_ATTRIB_BITS);
glColor3f(1, 0, 0);
glPopAttrib();
glRecti(20, 20, 30, 30); // draws white or red rect
glutSwapBuffers();
}
int main (int argc, char * argv[]){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);
glutInitWindowSize(800, 600);
glutCreateWindow("OpenGL lesson 1");
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
The code above is full compilable programm that renders two rectangles. The programm produce different results depending on weather line "glTranslatef(0, 0, 0);" commented or not. Is that a bug, or misusage of OpenGL?
That is just a bug. The spec clearly states that
glColor
will set the current RGBA color value, which will become the vertex's color the next time a vertex is formed. This would happen by the nextglVertex
call inside aglBegin
/glEnd
block.glRect
is specified to be equivalent toglBegin(); glVertex() [4x]; glEnd()
.The current RGBA color value is part of the
GL_CURRENT_BIT
attribute group, and is of course included inGL_ALL_ATTRIB_BITS
.glTranslate
is to only affect the top element of the currenttly selected matrid stack. The correct output for this code are two wihite rectangles, no matter if aglTranslate
is there or not.However, all this stuff is horribly outdated, and deprecated since 2008.