I am trying to make a rectangle of any color other than white or black, but seem to be failing miserably as it is always white no matter what I put in the code. I don't know what I'm doing wrong (if anything is wrong with my code even, as far as I can tell it is no different from some examples I have seen even). Here is the code I have that I think should be making a red rectangle but only makes a white one:
#include <gl/glut.h>
void mydisplay ()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f); //sets color
glBegin(GL_QUADS);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, -0.5);
glEnd();
//glutSwapBuffers();
//glutSolidTeapot(1);
}
int main (int argc, char** argv)
{
glutCreateWindow("simple");
glutDisplayFunc(mydisplay);
glutMainLoop();
}
Ok, what I have found is that I was missing one line of code. I needed glFlush(); instead of glutSwapBuffers(); Also, I could blend colors by assigning colors to each vertex. Here is what I have now, which will give a multicolored square:
#include <gl/glut.h>
void mydisplay ()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//sets color
glBegin(GL_QUADS);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(-0.5, -0.5);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(-0.5, 0.5);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex2f(0.5, 0.5);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex2f(0.5, -0.5);
glEnd();
glFlush();
//glutSwapBuffers();
//glutSolidTeapot(1);
}
int main (int argc, char** argv)
{
glutCreateWindow("simple");
glutDisplayFunc(mydisplay);
glutMainLoop();
}
The other code given in the previous answer works as well. Hence I am voting it up, but accepting my answer (the glFlush() works for my original code as well, and is a simpler fix).