Search code examples
c++openglglut

How to only draw the rectangle when choosing from the menu?


I am writing a program that draws a rectangle with the mouse when and only when the user selects it in the menu, if the user does not select it will not draw. Up to now, I have successfully drawn the rectangle with the mouse, now how can I create a menu so that the user can choose to draw the rectangle? This is my program:

struct Position
{
    Position() : x(0), y(0) {}
    float x, y;
};
Position start, finish;

void mouse(int button, int state, int x, int y)
{
    if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
    {
        start.x = finish.x = x;
        start.y = finish.y = y;
    }
    if (button == GLUT_LEFT_BUTTON && state == GLUT_UP)
    {
        finish.x = x;
        finish.y = y;
    }
    glutPostRedisplay();
}

void motion(int x, int y)
{
    finish.x = x;
    finish.y = y;
    glutPostRedisplay();
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    double w = glutGet(GLUT_WINDOW_WIDTH);
    double h = glutGet(GLUT_WINDOW_HEIGHT);
    glOrtho(0, w, h, 0, -1, 1);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glPushMatrix();
    glBegin(GL_LINE_LOOP);
    glVertex2f(start.x, start.y);
    glVertex2f(finish.x, start.y);
    glVertex2f(finish.x, finish.y);
    glVertex2f(start.x, finish.y);
    glEnd();
    glPopMatrix();

    glutSwapBuffers();
}

void menu(int choice)
{
    switch (choice)
    {
    case 1:
        // What to write in here?
        break;
    }
    glutPostRedisplay();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(640, 480);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("");
    
    glutCreateMenu(menu);
    glutAddMenuEntry("Rectangle", 1);
    glutAttachMenu(GLUT_RIGHT_BUTTON);

    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);

    glutMainLoop();
}

Solution

  • Add a variable that indicates which shape has to be drawn:

    int shape = 0;
    

    Set the variable in menu:

    void menu(int choice)
    {
        switch (choice)
        {
        case 1:
            shape = 1
            break;
        }
        glutPostRedisplay();
    }
    

    Draw the scene dependent on shape:

    void display()
    {
        glClear(GL_COLOR_BUFFER_BIT);
    
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        double w = glutGet(GLUT_WINDOW_WIDTH);
        double h = glutGet(GLUT_WINDOW_HEIGHT);
        glOrtho(0, w, h, 0, -1, 1);
    
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    
        if (shape == 1)
        {
            glPushMatrix();
            glBegin(GL_LINE_LOOP);
            glVertex2f(start.x, start.y);
            glVertex2f(finish.x, start.y);
            glVertex2f(finish.x, finish.y);
            glVertex2f(start.x, finish.y);
            glEnd();
            glPopMatrix();
        }
    
        glutSwapBuffers();
    }