Jump to content
  • Advertisement
Sign in to follow this  
raMMzes

Howto clear opengl window

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
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 this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!