🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Howto clear opengl window

Started by
10 comments, last by nefthy 18 years, 3 months ago
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;-)
Advertisement
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.
Quote: Original post by inmysights
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.


gl(clear)? Sorry but I don't quite know about this...

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.
Quote: Original post by raMMzes
gl(clear)? Sorry but I don't quite know about this...


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!
oops, that was suppose to be glclear() must have fat fingered it. my bad!
Quote: Original post by Drew_Benton
Quote: Original post by raMMzes
gl(clear)? Sorry but I don't quite know about this...


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.
Quote: Original post by raMMzes
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.


What is the problem with swapping buffers? You need to swap them anyway to update the screen.
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.
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.
Quote: Original post by raMMzes
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.


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!

This topic is closed to new replies.

Advertisement