Previous points drawing is deleted when glutMenu pops up

Hi, I was trying to create a program so that when I select an item, which is a type of shape, from the glutMenu (first right click on the screen to open the menu), the next 2 left clicks are going to set the boundary for this shape. After that, the program draws it depends on the type of shape it is. The drawing part is going well with me. After I draw a shape (as a set of points using Midpoint algorithm with glBegin(GL_POINTS) and glEnd()), the shape is displayed with no problem.

However, after I open the menu again with a right click, my previous shape just disappears, I do not know what might be the cause of this, is it a glutMenu thing?

My mouse action handling function:

static void mouseActionHandler(int button, int state, int x, int y)
		switch (button) {
			if (state == GLUT_UP)
				if (remainingClicks == 0)
			if (programState == DRAWING && state == GLUT_DOWN) {
				vertices.push_back(Point(x, y));
				if (remainingClicks == 0) {

You shouldn’t be drawing anything in the mouse callback. All drawing code should go in the display callback. The mouse handler should just update variables then call glutPostRedisplay.

1 Like

So what I should be doing is that I keep a list of shapes that need to be drawn, and every time a new shape is added using the mouse, I should add the new shape to that list?

And in the display callback, I will have codes to draw the whole list of shapes?

Thank you so much for your help, it works now! Sorry for making such an elementary mistake, but I did not find any guide about this, or may be have not found yet because I’m just starting to learn OpenGL.

However, may I have an additional question? I also want to color shape using Boundary Fill Algorithm by letting user click on the shape (like in MS Paint). Should the coloring part also be inside the display callback? Because Boundary Fill is slow and I do not think it should be called every time the screen re-renders.

Flood fill? That isn’t really appropriate for OpenGL (or anything that revolves around the GPU). The usual way to fill non-convex shapes is to either

  1. Tessellate them into triangles; the GLU tessellator can be used for this, although the API is designed for use with legacy OpenGL (glBegin/glEnd).
  2. Use a winding test. Draw the boundary as a triangle fan (the common vertex can be anything, but it may as well be an existing vertex) using either stencilling or glLogicOp(GL_XOR). Search for “opengl concave polygon stencil”. XOR is less work but only works with a solid background; you can’t use it to render a non-convex polygon on top of other graphics.
1 Like

Thank you for such detailed advice. The reason I have to use Flood Fill is because it is required in a course I’m taking about Computer Graphics. We are learning about many algorithms in Computer Graphics, so I have to implement the algorithms (in this case, Flood Fill) by hand with minimal help from OpenGL functions.

If you were in my position, where would you think is the best place (ie., performance wise) to put my Flood Fill function call, in the mouse function, or in the display callback?

Thank you again for your time.

Perform the flood fill in response to a mouse event. That will give you a bitmap, which you can optionally upload to a texture. The display callback either renders a textured quad or renders the bitmap data directly with glDrawPixels.

Flood fill is expensive enough that you’d want to save the result rather than performing it again for each redraw.

1 Like

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.