PDA

View Full Version : how does glClearColor function works?



gl_siddarth
01-10-2012, 12:35 PM
Hi,
I am newbie to the OpenGL programming,
actually i have written a code for displaying a polygon,

and i want to clear the background color from black to red,(using glClearColor and glClear functions) but it is not happening

I am unable to figure out where i have gone wrong, :confused:

and also please explain me how the glClearColor function works

here is my code



#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
void display(void)
{
int i;

glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 1.0);

glBegin(GL_POLYGON);
glVertex3f(1.1, 1.1, 0.1);
glVertex3f(2.7, 2.1, 0.5);
glVertex3f(2.7, 2.7, 0.1);
glVertex3f(1.1, 2.7, 0.5);
glEnd();

glFlush();
}
void init(void)
{
glClearColor(1.0,0.0,0.0,0.0);


//glMatrixMode(GL_PROJECTION);
//glLoadIdentity();
glOrtho(0.0, 3.0, 0.0, 3.0, -1.0, 1.0);

}

int main(int argc,char **argv)
{
glutInit(&amp;argc,argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | GLUT_SINGLE);
//glutInitWindowSize(1600, 1600);
//glutInitWindowPosition(100, 100);


glutCreateWindow("hello");
glClearColor(1.0f,0.0f,0.0f,0.0f);
glClear(GL_COLOR_BUFFER_BIT);
init();
glutDisplayFunc(display);
//glClear(GL_COLOR_BUFFER_BIT);
//glutReshapeFunc(resize(100,200));
glutMainLoop();
return 0;

}




Thanks in advance
gl_siddarth

V-man
01-10-2012, 12:48 PM
I tested it and i got a red background with a blue polygon on it.

How glClearColor works? It just sets the current clear color but you have to call glClear(GL_COLOR_BUFFER_BIT) for something to happen.