0

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.

Dr. Essen
  • 603
  • 2
  • 9
  • 25

1 Answers1

1

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 );

Community
  • 1
  • 1
Alex
  • 3,264
  • 1
  • 25
  • 40
  • Thanks, That worked. But I wanted to know the difference between the GLUT_DOUBLE and GLUT_SINGLE Macro, because in both cases it worked. Also, I want to write some Text before the appearance of 1st color display in the same window. I went through this link to get understand it but it didn't work well. http://stackoverflow.com/questions/538661/how-do-i-draw-text-with-glut-opengl-in-c – Dr. Essen Dec 24 '15 at 10:20
  • @Ac3_DeXt3R I've added some link, about the difference has been already answered, I also added some glut example which should help you about the text (and glut in general) and a reference to some easy GL tutorial (old but still good for learning purpose) – Alex Dec 24 '15 at 10:46
  • Thanks a lot, that is very helpful. Can you please tell me how to **Exit** the window after displaying everything? – Dr. Essen Dec 24 '15 at 11:04
  • @Ac3_DeXt3R take a look at glut examples, [abgr.c](https://www.opengl.org/archives/resources/code/samples/glut_examples/examples/abgr.c) for instance, it use `glutKeyboardFunc(Key)` to register a key handler, in the `Key` handler check for the `ESC` key, `case 27` – Alex Dec 24 '15 at 11:16
  • The original `glut` implementation has some limitation, one of them was the *main loop* never returning, `freeglut` implement `glutLeaveMainLoop` which should be used instead of `exit`. [glut-and-freeglut](http://www.lighthouse3d.com/cg-topics/glut-and-freeglut/) – Alex Dec 24 '15 at 11:27
  • Thanks, quite helpful... :) – Dr. Essen Dec 24 '15 at 11:33
  • @Ac3_DeXt3R in your case just remove `glutMainLoop();` and the application will exit. – Alex Dec 24 '15 at 11:33
  • Well, I tried that, but after removing, a window pops up and disappears very quickly. – Dr. Essen Dec 24 '15 at 11:36
  • Weird, it work for me, the sleeps keep the window opened till the color sequence is finished. You need a *MainLoop* if you need to handle user input, keys, mouse or timer events. – Alex Dec 24 '15 at 11:39
  • I will recheck my program once. – Dr. Essen Dec 24 '15 at 11:40