# Howto clear opengl window

This topic is 4311 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm quite new to this, so please be patient;-) I'm modifying the example from lesson 2, what I would like to achieve is clearing the window completely OR _adding_ some more objects to the window when a key is pressed. To determine which key is it I use: if (XLookupKeysym(&event.xkey, 0) == XK_Home) { printf( "HOME's pressed" ); /* Add another object here */ } in main(). In other words, when I press a key (HOME) for instance I want EITHER the window to be cleared and add a figure or just add a figure, leaving the rest as it is. How do I modify the source to do so? Thank you for any help;-)

##### Share on other sites
to clear the screen just call the gl(clear) function simple. For adding the key presses, there is plenty of examples in the tutorials to show you this, no books here at work for any examples at the moment sorry. but look at those for a starting place.

##### Share on other sites
Quote:
 Original post by inmysightsto clear the screen just call the gl(clear) function simple. For adding the key presses, there is plenty of examples in the tutorials to show you this, no books here at work for any examples at the moment sorry. but look at those for a starting place.

Presses are ok, no problem with that, I'm trying to do something different - adding an object to an existing scene (with a key press, or after a delay, whatever).

Thank you so far.

##### Share on other sites
Quote:

To clear the screen you can use the glClear function. If you pass in the GL_COLOR_BUFFER_BIT function, then the screen's color buffer is cleared. So it looks like this: glClear(GL_COLOR_BUFFER_BIT);

You should note though, you can clear the screen, but if you continue to draw the objects, then you have not accompished anything. If you want to not draw anything anymore, you will probabally want to use classes for your objects to allow you to tell them not to draw anymore.

For example:
class Object{private:	bool visible;public:        void SetVisibility(bool var)        {                visible = var;        }	void Draw()	{		// If we are not showing the object, don't draw it                if(!visible)			return;		glBegin(GL_TRIANGLES); // Drawing Using Triangles		glVertex3f( 0.0f, 1.0f, 0.0f);				// Top		glVertex3f(-1.0f,-1.0f, 0.0f);				// Bottom Left		glVertex3f( 1.0f,-1.0f, 0.0f);				// Bottom Right		glEnd();		}};

So with that quick little example, after you make your Object's, you can add in the code so that if the user presses a specific key, then you set the visibility to false, thus not drawing the object anymore. There is a lot more you can do, but that should give you a basic idea of one way to go about it. Good luck!

##### Share on other sites
oops, that was suppose to be glclear() must have fat fingered it. my bad!

##### Share on other sites
Quote:
Original post by Drew_Benton
Quote:

To clear the screen you can use the glClear function. If you pass in the GL_COLOR_BUFFER_BIT function, then the screen's color buffer is cleared. So it looks like this: glClear(GL_COLOR_BUFFER_BIT);

Yes, that's it. But as I'm using double buffering I need to swap buffers every time I use the function. Not really handy.

Quote:
 You should note though, you can clear the screen, but if you continue to draw the objects, then you have not accompished anything. If you want to not draw anything anymore, you will probabally want to use classes for your objects to allow you to tell them not to draw anymore.For example:*** Source Snippet Removed ***So with that quick little example, after you make your Object's, you can add in the code so that if the user presses a specific key, then you set the visibility to false, thus not drawing the object anymore. There is a lot more you can do, but that should give you a basic idea of one way to go about it. Good luck!

Hm, that looks interesting. I will give it a try soon. Thank you.

##### Share on other sites
Quote:
 Original post by raMMzesYes, that's it. But as I'm using double buffering I need to swap buffers every time I use the function. Not really handy.

What is the problem with swapping buffers? You need to swap them anyway to update the screen.

##### Share on other sites
Clearing the screen is not a problem with double buffers. What you do is run glClear() at the beginning of your draw function, and then you run glSwapBuffers() at the end of your drawing function. Oh yeah, and don't forget to glFlush() the pipline before you return from the drawing function.

##### Share on other sites
Ok, I think I've sorted that out, thanks;-)

Don't know whether it should be posted in a separate thread, but, here it goes:
I'm looking for a logical way to place objects at given coordinates such as:

moveto(x, y, z);
draw();
moveto(x,y,z);
draw();

or:
drawAtPosition(x,y,z);

So far, I used:
glTranslated(x,y,z);

Can someone point me to a resource or post a solution draft?
Thanks.

##### Share on other sites
Quote:
 Original post by raMMzesOk, I think I've sorted that out, thanks;-)Don't know whether it should be posted in a separate thread, but, here it goes:I'm looking for a logical way to place objects at given coordinates such as:moveto(x, y, z);draw();moveto(x,y,z);draw();or:drawAtPosition(x,y,z);So far, I used: glTranslated(x,y,z);Can someone point me to a resource or post a solution draft?Thanks.

Actually, if you look at my post, you will just add a few more things to make it work as you want it to. For example:
class Object{private:	bool visible;        float X, Y, Z;public:        void MoveTo(float x, float y, float z)        {           X = x;           Y = y;           Z = z;        }        void SetVisibility(bool var)        {                visible = var;        }	void Draw()	{		// If we are not showing the object, don't draw it                if(!visible)			return;                glPushMatrix();                // might need a glLoadIdentity here                glTranslatef(X, Y, Z);		glBegin(GL_TRIANGLES); // Drawing Using Triangles		glVertex3f( 0.0f, 1.0f, 0.0f);				// Top		glVertex3f(-1.0f,-1.0f, 0.0f);				// Bottom Left		glVertex3f( 1.0f,-1.0f, 0.0f);				// Bottom Right		glEnd();	                glPopMatrix();	}};

Of course there are several variations you can take, but that is up to you as to how you want your final design to be. That is the basic idea though, you can use a class and just do stuff like that [wink] Of course, you might want to read up some more on classes and such if you are not familiar with them. Good luck!

##### Share on other sites
Sure, I understand what you mean, but I'm curious what's the *right* way of drawing objects at given (x,y,z) in opengl itselt not looking at the actual implementation, yet.

Using the function I mentioned (glTranslated) I cannot pass the coordinates but a translation vector, which is not an absolute position of the object whatsoever.

##### Share on other sites
glTranslate() is the right way to position objects. You can use glPushMatrix() and glPopMatrix() to save and restore the current transformation matrix.

Lets say you want to draw obj1 and obj2, both relative to the current transformation (instead of relative to each other) you would do something along these lines:
glPushMatrix();glTranslate(obj1.x, obj1.y, obj1.z);draw(obj1);glPopMatrix();glPushMatrix();glTranslate(obj2.x, obj2.y, obj2.z);draw(obj2);glPopMatrix();

You might want to take a look at the NeHe tutorials. They will get you started.