I'm writing a program that allows the user to draw different shapes through the menu options, the shapes after being drawn need to be on the same screen, but the problem is that after choosing another option in the menu to draw another shape, the previous shape disappears. How can I fix this? This is my program:
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
if (shape == 1)
{
draw_rectangle();
}
if (shape == 2)
{
draw_circle();
}
glFlush();
}
void menu(int choice)
{
switch (choice)
{
case 1:
shape = 1;
break;
case 2:
shape = 2;
break;
}
glutPostRedisplay();
}
The glClear(GL_COLOR_BUFFER_BIT)
is only in display()
.
You have to use a separate Boolean state variables for each shape (rectangle_shape
, circle_shape
), instead of one integral variable that indicates the shape (shape
):
bool rectangle_shape = false;
bool circle_shape = false;
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
if (rectangle_shape)
{
draw_rectangle();
}
if (circle_shape)
{
draw_circle();
}
glFlush();
}
void menu(int choice)
{
switch (choice)
{
case 1:
rectangle_shape = !rectangle_shape;
break;
case 2:
circle_shape = !circle_shape;
break;
}
glutPostRedisplay();
}