Saturday, May 14, 2011

glClearColor example c c++ objc

[glClearColor example] Following code fragment shows a glClearColor example. glClearColor() specifies the color to fill the GL surface when the surface is cleared by glClear() function call. [glClearColor example]

 #include <GL/glut.h>                  //Allows access to the glut library

void init()
{
    glClearColor (0.0, 0.0, 0.0, 0.0);        //will discuss this late in course
    glShadeModel (GL_FLAT);                    //will discuss this late in course
}

void display()
{
    glClear (GL_COLOR_BUFFER_BIT);
    glColor3f (1.0, 0.0, 0.0);            //This sets the color of the polygon - RGB format (red, green, blue) (covered next chapter)
    glBegin(GL_POLYGON);           ///polygon - many sided figure
        glVertex2f(200.0,200.0);     //f means floating point or those with decimals  - setting the vertices of the polygon
        glVertex2f(400.0,200.0);
        glVertex2f(400.0, 400.0);
    glEnd();
    glFlush ();    //forces previously issued commands to execute
}

void reshape (int w, int h)       //Covered later
{
    glViewport (0, 0, (GLsizei) w, (GLsizei) h);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h);
}

int main(int argc, char** argv)    //Because of glut, parameters are necessary
{
    glutInit(&argc, argv);              //calling various methods above
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);    // | means or
    glutInitWindowSize (600, 600);
    glutInitWindowPosition (100,100);
    glutCreateWindow (argv[0]);
    init ();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();
    return 0;
}