Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: how does glClearColor function works?

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2012
    Posts
    4

    how does glClearColor function works?

    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,

    and also please explain me how the glClearColor function works

    here is my code

    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

  2. #2
    Super Moderator OpenGL Guru
    Join Date
    Feb 2000
    Location
    Montreal, Canada
    Posts
    4,256

    Re: how does glClearColor function works?

    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.
    ------------------------------
    Sig: http://glhlib.sourceforge.net
    an open source GLU replacement library. Much more modern than GLU.
    float matrix[16], inverse_matrix[16];
    glhLoadIdentityf2(matrix);
    glhTranslatef2(matrix, 0.0, 0.0, 5.0);
    glhRotateAboutXf2(matrix, angleInRadians);
    glhScalef2(matrix, 1.0, 1.0, -1.0);
    glhQuickInvertMatrixf2(matrix, inverse_matrix);
    glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
    glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •