OpenGL since 1.0 -- GLUT menu

Let's now learn how to use GLUT to add a popup menu, and look directly at the example:

#include <Gl/glut.h>
#include <stdio.h>
const GLint pickBuffsize = 32;
GLsizei winWidth = 400, winHeight = 400;
GLfloat red = 1.0, green = 1.0, blue = 1.0;
GLenum fillMode = GL_SMOOTH;
void init()
{
    glClearColor(0.6, 0.,0.6, 1.0);
    glMatrixMode(GL_PROJECTION);
    gluOrtho2D(0.0, 300.0, 0.0, 300.0);
}
void fillOption(GLint selectedOption)//菜单消息响应函数
{
    switch (selectedOption)
    {
    case 1:
        fillMode = GL_FLAT;//同色填充
        break;
    case 2:
        fillMode = GL_SMOOTH;//顶点颜色插值
        break;
    default:
        break;
    }
    glutPostRedisplay();//消息响应后必须被重绘
}
void displayTriangle()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glShadeModel(fillMode);//多边形填充方式
    glColor3f(red, green, blue);
    glBegin(GL_TRIANGLES);//两个白点
    glVertex2i(280, 20);
    glVertex2i(160, 280);
    glColor3f(red, 0.0, 0.0);//一个红点
    glVertex2i(20, 100);
    glEnd();
    glFlush();
}

void winReshapeFcn(GLint newWidth, GLint newHeight)
{
    glViewport(0, 0, newWidth, newHeight);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0, GLdouble(newWidth), 0.0, GLdouble(newHeight));
    winWidth = newWidth;
    winHeight = newHeight;
}

void main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(winWidth, winHeight);
    glutCreateWindow("An Example OpenGL Program");
    init();
    glutDisplayFunc(displayTriangle);
    glutCreateMenu(fillOption);//创建菜单并绑定回调函数
    glutAddMenuEntry("Solid-Color Fill", 1);//第一个菜单项
    glutAddMenuEntry("Color-Interpolation Fill", 2);//第二个菜单
    glutAttachMenu(GLUT_RIGHT_BUTTON);//指定鼠标右键来弹出菜单项
    glutReshapeFunc(winReshapeFcn);

    glutMainLoop();//启动主循环,等待消息
}

write picture description here
The program displays an interpolated triangle by default. When we click the right mouse button, a pop-up menu appears at the cursor position to change the interpolation method. When we select the first option, the triangle turns red. Next, we will explain the corresponding menu functions.
The following statement creates a popup menu;

glutCreateMenu(menuFcn);

Its parameter is a callback function:

void menuFcn(GLint menuItemNumber)

The integer parameter of the function corresponds to the position of the selected item. We use the following functions to set the name and position of each menu item:

glutAddMenuEntry("First Menu Item",1);
glutAddMenuEntry("Second Menu Item",2);

Then we have to select the mouse button of the menu item by specifying the following function

glutAttachMenu(button);

button is the symbolic constant of the mouse button.
Very simply we can also add submenus to menu items.

#include <Gl/glut.h>
#include <stdio.h>
const GLint pickBuffsize = 32;
GLsizei winWidth = 400, winHeight = 400;
GLfloat red = 1.0, green = 1.0, blue = 1.0;
GLenum fillMode = GL_SMOOTH;
void init()
{
    glClearColor(0.6, 0.,0.6, 1.0);
    glMatrixMode(GL_PROJECTION);
    gluOrtho2D(0.0, 300.0, 0.0, 300.0);
}
void mainMenu(GLint selectedOption)
{
    switch (selectedOption)
    {
    case 1:
        fillMode = GL_FLAT;
        break;
    case 2:
        fillMode = GL_SMOOTH;
        break;
    default:
        break;
    }
    glutPostRedisplay();
}
void colorSubMenu(GLint colorOption)
{
    switch (colorOption)
    {
    case  1:
        red = 0.0, green = 0.0, blue = 1.0;
        break;
    case  2:
        red = 0.0, green = 1.0, blue =0.0;
        break;
    case  3:
        red = 1.0, green = 0.0, blue = 0.0;
        break;
    default:
        break;
    }
    glutPostRedisplay();
}
void displayTriangle()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glShadeModel(fillMode);
    glColor3f(red, green, blue);
    glBegin(GL_TRIANGLES);
    glVertex2i(280, 20);
    glVertex2i(160, 280);
    glColor3f(red, 0.0, 0.0);
    glVertex2i(20, 100);
    glEnd();
    glFlush();
}
void winReshapeFcn(GLint newWidth, GLint newHeight)
{
    glViewport(0, 0, newWidth, newHeight);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0, GLdouble(newWidth), 0.0, GLdouble(newHeight));
    winWidth = newWidth;
    winHeight = newHeight;
}
void main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(winWidth, winHeight);
    glutCreateWindow("An Example OpenGL Program");
    init();
    glutDisplayFunc(displayTriangle);
    GLint subMenu = glutCreateMenu(colorSubMenu);//子菜单项和普通菜单项创建一样
    glutAddMenuEntry("Blue", 1);
    glutAddMenuEntry("Green", 2);
    glutAddMenuEntry("Red", 3);
    glutCreateMenu(mainMenu);//主菜单项
    glutAddMenuEntry("Solid-Color Fill", 1);
    glutAddMenuEntry("Color-Interpolation Fill", 2);
    glutAddSubMenu("color", subMenu);//给主菜单添加子菜单项
    glutAttachMenu(GLUT_RIGHT_BUTTON);
    glutReshapeFunc(winReshapeFcn);
    glutMainLoop();//启动主循环,等待消息
}

write picture description here
You can see that the whole process is very simple, just use glutAddSubMenu to add a menu item to another menu item as a submenu item.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325525838&siteId=291194637