Display RGB window in linux using OpenGL and Freeglut

648 views Asked by At

I want to create a window covering my whole desktop screen with displaying colors RGB and then VIBGYOR. The color delay should be 1 second. This is the code I have written but output is not as expected. Can anyone tell me where I am wrong?

#include<GL/freeglut.h>
#include<GL/glut.h>
#include<stdio.h>

void main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );

    glutInitWindowSize(1280,1024);
    glutCreateWindow("Displays");
    glutFullScreen();   

    glClearColor (1.0, 0.0, 0.0, 0.0);      //Red
    glClear(GL_COLOR_BUFFER_BIT);
    sleep(1);   

    glClearColor(0,1,0,0);              //Green
    glClear(GL_COLOR_BUFFER_BIT);
    sleep(1);   

    glClearColor(0,0,1,0);
    glClear(GL_COLOR_BUFFER_BIT);           //Blue
    sleep(1);   

    glClearColor(0.933, 0.510, 0.933, 0.0);     //violet
    glClear(GL_COLOR_BUFFER_BIT);
    sleep(1);   

    glClearColor(0.294, 0.000, 0.510, 1);       //Indigo
    glClear(GL_COLOR_BUFFER_BIT);
    sleep(1);   

    glClearColor(0,0,1,1);              //Blue
    glClear(GL_COLOR_BUFFER_BIT);
    sleep(1);   

    glClearColor(0,0.502,0,1);          //Green
    glClear(GL_COLOR_BUFFER_BIT);
    sleep(1);   

    glClearColor(1,1,0,1);              //Yellow
    glClear(GL_COLOR_BUFFER_BIT);
    sleep(1);   

    glClearColor(1,0.647,0,1);          //Orange
    glClear(GL_COLOR_BUFFER_BIT);
    sleep(1);   

    glClearColor(1,0,0,1);              //Red
    glClear(GL_COLOR_BUFFER_BIT);
    sleep(1);   

glutMainLoop();
}

Thanks.

1

There are 1 answers

10
Alex On

You're initializing the display mode with GLUT_DOUBLE (double buffering), you need to add gluSwapBuffers() every time you're finished working with the buffer and want to display it.

...
glClearColor(1,0,0,1);              //Red
glClear(GL_COLOR_BUFFER_BIT);
glutSwapBuffers();
sleep(1);   
...

Difference between single buffered(GLUT_SINGLE) and double buffered drawing(GLUT_DOUBLE)

When using GL_SINGLE, you can picture your code drawing directly to the display.

When using GL_DOUBLE, you can picture having two buffers. One of them is always visible, the other one is not

glut examples

nehe's legacy tutorials

What is the nicest way to close GLUT

void glutLeaveMainLoop ( void );