initialize window

I know that to initialize openGL window, I have to have :

glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glSwapBuffer();

Then I draw some things in the windows. Now my problem is, how would I clear the window again? How do I make them all back to black again?
I did try that 3 lines of codes again, it only flashed me with black color for maybe .1 sec then go back to the same screen that I already drew things.

You should do -

glClearColor();
glClear(); // clears back-buffer

// draw stuff here (goes in back buffer)

SwapBuffers(); // swaps back buffer to screen

Does that make sense?

[This message has been edited by Rog (edited 07-09-2003).]

yea i’ve tried that, in fact, this is what i tried but the old drawing still there, i don’t know if i missunderstood the concept.

cout << "one
";
glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glutSwapBuffers();
cout << "two
";
glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glutSwapBuffers();
cout << "three
";
glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glutSwapBuffers();

it went through and printed out one,two,three (in the console/dos/whatever u call it); but in the openGL window, the drawing r still there.

Did you initialise glut to use double-buffering?

How about posting more of your code, because that will clear the window.
But you must have somewhere in your code, calling the drawing routine after the clear.

Normaly…

clear screen

draw something

swapbuffers

Originally posted by kentut:
[b]I know that to initialize openGL window, I have to have :

glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glSwapBuffer();

Then I draw some things in the windows. Now my problem is, how would I clear the window again? How do I make them all back to black again?
I did try that 3 lines of codes again, it only flashed me with black color for maybe .1 sec then go back to the same screen that I already drew things.[/b]

Godo idea is to get some code from the various tutorials on the net …

e.g. nehe.gamedev.net

Rob

Yea I use double buffering
Here’s my code :

////////////////////////////

void options()
{
cout << "one
";
glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glutSwapBuffers();
cout << "two
";
glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glutSwapBuffers();
cout << "three
";
glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glutSwapBuffers();
}

///////////////////////////////

void main_option()
{ /* some drawing, and make the value of icon_move to be -6 or -9 so that it goes to the keyboardfunc then keyboardfunc call options func */ }

///////////////////////////////

void display()
{
glEnable(GL_DEPTH_TEST);
glClearColor(0,0,0,1);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,0,5,0,0,0,0,1,0);

main_options();

glutSwapBuffers();
}

////////////////////////////////

void key(unsigned char key, int x, int y)
{
switch(key) {
case 13:
switch(icon_move) {
case -6:
options();
break;
case -9:
exit(0);
break;
}
glutPostRedisplay();
}

I must have drawing routine after clear the window? Just say I have to do something like:

GLfloat black[] = {0,0,0,1};
glMaterialfv(GL_FRONT,GL_AMBIENT,black);
glutSolidCube(1);

like that?

Your code does not make any sense, why are you calling clear screen so many times? also all the swap buffers call’s?

Also with material function, you have to enable lighting for it to work and enable the material function also.
If you want to just have a colored cube, use glColor command.

If you are drawing one object and it is not moving then you don’t need double buffers.
You clear the screen each time your draw because you would be writing on top of the previous image if you don’t clear it.

I must have drawing routine after clear the window? Just say I have to do something like:

Yes, because if it draw before the clear window you drawing will be earsed.

What is it you are trying to do?
Maybe work as suggested with some examples programs to get an understanding of 3D graphics and openGL functions.

Look at some of my glut examples on my website.
www.angelfire.com/linux/nexusone/

Originally posted by kentut:
[b]I must have drawing routine after clear the window? Just say I have to do something like:

GLfloat black = {0,0,0,1};
glMaterialfv(GL_FRONT,GL_AMBIENT,black);
glutSolidCube(1);

like that?[/b]

Originally posted by nexusone:
[b]Your code does not make any sense, why are you calling clear screen so many times? also all the swap buffers call’s?

[/b]

Because I can’t clear my windows. Even after 3 times of clearing it, it still shows me the drawing I have instead of black screen.

Originally posted by nexusone:
[b]Also with material function, you have to enable lighting for it to work and enable the material function also.
If you want to just have a colored cube, use glColor command.

If you are drawing one object and it is not moving then you don’t need double buffers.
You clear the screen each time your draw because you would be writing on top of the previous image if you don’t clear it.

[/b]

No I actually have some lighting and some movement. I just cut some of my code so that the code is not too long and shows only the part that is relevant to clearing the window.

Try clearing to {0,0,0,1} instead of {0,0,0,0}.

It sounds like you want your scene to display for a bit and then a blank screen to display?

If so, do something like so…

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

if (g_bDisplayScene)
{
DisplayScene();
}
glutSwapBuffers();

Then in some timer, or key press, or however you determine that it is time to show a blank screen, you set g_bDisplayScene to false.

Obviously you are doing something wrong with your code, or drawing someplace after the clear screen, but without more code it’s hard to say what. Some thoughts that come to mind are:

  1. You don’t understand how the window messages work and you are drawing something after the clear w/o realizing it.
  2. You have a scissor/viewport region setup in such a way that your glClear is not clearing the whole window.

Originally posted by Jared@ETC:
Try clearing to {0,0,0,1} instead of {0,0,0,0}.

still doesn’t work
I don’t know what I did wrong. I really need to track down my code line by line you think?

Just looked at your code again. It appears you are calling the clear stuff on a keypress? That is only going to clear the scene immediately after the key is pressed, and anything that causes a new WM_PAINT message to occur is going to immediately cause your display() function to draw something. So, if you are using a timer or idle function to call glutPostRedisplay(), it’s going to appear your scene was never cleared. Or if you don’t do that, you’ll likely see the scene cleared briefly, and then shown again when the window is uncovered by another window, or a minimize/maximize/resize of the window is done, or any of the other number of things that can cause a WM_PAINT message to occur.

Instead, you should use the keyboard function to set a state to say that the scene is cleared as in the code example I gave you and call a glutPostRedisplay to make the display() function called. Then do all your drawing in the display function.

Edit: Noticed now that you have a glutPostRedisplay in the key function. So basically what is happening is that you do this when the key is pressed.

clear
clear
clear
post WM_PAINT message
Glut calls Display immediately after the key() function exits due to the WM_PAINT message, so your scene is drawn again

Hope this helps.

[This message has been edited by Deiussum (edited 07-10-2003).]

I didn’t see calling glFinish/Flush() before SwapBuffer(). Must be to complete drawing scene before swapping buffers.

Originally posted by Deiussum:
Just looked at your code again. It appears you are calling the clear stuff on a keypress? That is only going to clear the scene immediately after the key is pressed, and anything that causes a new WM_PAINT message to occur is going to immediately cause your display() function to draw something. So, if you are using a timer or idle function to call glutPostRedisplay(), it’s going to appear your scene was never cleared. Or if you don’t do that, you’ll likely see the scene cleared briefly, and then shown again when the window is uncovered by another window, or a minimize/maximize/resize of the window is done, or any of the other number of things that can cause a WM_PAINT message to occur.

Wow it works! Awesome! Thanks.
I guess I still don’t know how the glutPostRedisplay() works. I was thinking it was my glClear or my glSwapBuffers that doesn’t work.
Can you please briefly explain what it does? I mean which functions the glutPostRedisplay() calls? Does it load everything in the main body, but keep track of the variables’ values?

Originally posted by SnakE:
I didn’t see calling glFinish/Flush() before SwapBuffer(). Must be to complete drawing scene before swapping buffers.

Nope, this isn’t necessary. SwapBuffers will occur after all drawing has been done anyway. Calling glFlush or glFinish will only reduce performance.
It’s only necessary to issue glFlush in single buffered contexts, to get all drawing onto the screen.

Have you went like to my site and look at some function glut program? This will give you a better understanding of glut.

glutPostRedisplay() call’s the routine defined by glutDisplayFunc( display ).

glutPostRedisplay is the refresh display or re-draw routine in glut.

Also the routine defined by glutDisplayFunc will also be called by the routine defined by glutReshapeFunc(reshape) is called.
So every time the window is moved or reshaped the display function will be called to redraw the screen.

This is something people don’t always keep in mind when first writing a glut program. it that you need to have all your drawing code inside of the display routine so that if a resize or window move happens the scene will be redraw correctly.

[This message has been edited by nexusone (edited 07-11-2003).]

Was gone a few days on vacation, so just getting back to reading the forums now.

Nexusone’s description of glutPostRedisplay is ALMOST right. More specifically, glutPostRedisplay puts a WM_PAINT message in the message Q. Then when glut’s internal message pump picks that up, it will call your function defined with glutDisplayFunc. This will happen after the current function finishes.

Just a picky little detail, but understanding of how window messages work will help you out a lot in the future if you are doing much Windows programming.