#include <gl/glut.h>
#include <stdio.h>
int mainMenu,subMenu1,subMenu2;
void myDisplay(void)
{
glClearColor(0.0,0.0,1.0,0.0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f(1.0,1.0,1.0);
glOrtho(-1.0,1.0,-1.0,1.0,-1.0,1.0);//void glOrtho(GLdouble left,GLdouble right,GLdouble bottom,GLdouble top,GLdouble near,GLdouble far)
//创建一个正交平行的视景体。 一般用于物体不会因为离屏幕的远近而产生大小的变换的情况
glBegin(GL_POLYGON);
glVertex2f(-0.5,-0.5);
glVertex2f(-0.5,0.5);
glVertex2f(0.5,0.5);
glVertex2f(0.5,-0.5);
glEnd();
glutSwapBuffers();
}
void GetCurrentMenu(void)
{
int nMenu;
nMenu=glutGetMenu();
if(nMenu==mainMenu)
printf("The current menu is mainMenu./n");
if(nMenu==subMenu1)
printf("The current menu is subMenu1./n");
if(nMenu==subMenu2)
printf("The current menu is subMenu2./n");
}
void SubMenuFunc1(int data)
{
GetCurrentMenu();
switch(data)
{
case 1:
printf("subMenu1's item 1 is triggered./n");
break;
case 2:
printf("subMenu1's item 2 is triggered./n");
break;
case 3:
printf("subMenu1's item 3 is triggered./n");
break;
}
}
void SubMenuFunc2(int data)
{
GetCurrentMenu();
switch(data)
{
case 1:
printf("subMenu2's item 1 is triggered./n");
break;
case 2:
printf("subMenu2's item 2 is triggered./n");
break;
case 3:
printf("subMenu2's item 3 is triggered./n");
break;
}
}
void MenuFunc(int data)
{
GetCurrentMenu();
switch(data)
{
case 1:
printf("mainMenu's item 1 is triggered./n");
break;
case 2:
printf("mainMenu's item 2 is triggered./n");
break;
case 3:
printf("mainMenu's item 3 is triggered./n");
break;
}
}
int main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(500,400);
glutInitWindowPosition(100,100);
glutCreateWindow("Menu");
glutDisplayFunc(myDisplay);
subMenu1=glutCreateMenu(SubMenuFunc1);
glutAddMenuEntry("subMenu1's item",1);
glutAddMenuEntry("subMenu1's item",2);
glutAddMenuEntry("subMenu1's item",3);
glutAttachMenu(GLUT_RIGHT_BUTTON);
subMenu2=glutCreateMenu(SubMenuFunc2);
glutAddMenuEntry("subMenu2's item1",1);
glutAddMenuEntry("subMenu2's item2",2);
glutAddMenuEntry("subMenu2's item3",3);
glutAttachMenu(GLUT_RIGHT_BUTTON);
mainMenu=glutCreateMenu(MenuFunc);
glutAddSubMenu("subMenu1",subMenu1);
glutAddSubMenu("subMenu2",subMenu2);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutAddMenuEntry("Item1",1);
glutAddMenuEntry("Item2",2);
glutAddMenuEntry("Item3",3);
glutMainLoop();
return 0;
}
右键菜单
最新推荐文章于 2020-03-22 10:53:33 发布