I'm getting a very strange bug in my OpenGL program:
Code:
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
void draw(void)
{
GLenum e;
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0, 0, -5);
/* no problem without call to scale */
if (1) glScalef(1, 1, 1);
glColor3f(1, 1, 1);
glBegin(GL_QUADS);
glNormal3f(0, 0, 1);
glVertex2f(-.5, .5);
glVertex2f(-.5, -.5);
glVertex2f(.5, -.5);
glVertex2f(.5, .5);
glEnd();
glFinish();
e = glGetError();
if (e != GL_NO_ERROR) {
fprintf(stderr, "OpenGL: %s\n", gluErrorString(e));
exit(1);
}
}
#define W 400
#define H 400
void setup_gl(void)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, W, H);
gluPerspective(40.0, (GLfloat)W/(GLfloat)H, 0.1, 1000.0);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glShadeModel(GL_FLAT);
}
/* toolkit specific */
static void display(void)
{
draw();
glutSwapBuffers();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(W, H);
glutCreateWindow ("gl window");
glutDisplayFunc(&display);
setup_gl();
glutMainLoop();
return 0;
}
It should draw a square in the middle of the screen (expected-quad.png), and it does when glScalef(1,1,1) (values other than 1 also cause this, with 1 it's obviously a noop) isn't called, or when lighting is not enabled, but with both of those the square looks rotated and stretched (broken-quad.png).
I don't know where the problem is: my program, opengl implementation, graphics card (lspci gives: Intel Corporation 82815 Chipset Graphics Controller)? I've found the same problem using glx and sdl, so I know it's not the toolkit. Any insights are appreciated.