下载本文示例代码
菜单是我们经常使用的工具,很方便,直观。本节讨论在OpenGL中如何进行菜单管理。 例17:本例在蓝色的背景上绘制一个白色的正方形,在窗口内单击鼠标右键,弹出菜单,当选择不同菜单项时命令窗口会显示出是哪个菜单的哪个菜单相被激活。本例一个主菜单,主菜单有3个菜单条目及两个子菜单。两个子菜单都有3个菜单条目。
#include
#include int menu,subMenu1,subMenu2;/*在蓝色的背景上绘制一个白色的正方形*/void myDraw(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); 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 == menu) printf("The current menu is Main Menu.\n"); if(nMenu == subMenu1) printf("The current menu is SubMenu1.\n"); if(nMenu == subMenu2) printf("The current menu is SubMenu2.\n");}/*子菜单1*/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; }}/*子菜单2*/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(myDraw); /*创建子菜单1并加入菜单条目*/ subMenu1 = glutCreateMenu(SubMenuFunc1); glutAddMenuEntry("SubMenu1's item1",1); glutAddMenuEntry("SubMenu1's item2",2); glutAddMenuEntry("SubMenu1's item3",3); glutAttachMenu(GLUT_RIGHT_BUTTON); /*创建子菜单2并加入菜单条目*/ subMenu2 = glutCreateMenu(SubMenuFunc2); glutAddMenuEntry("SubMenu2's item1",1); glutAddMenuEntry("SubMenu2's item2",2); glutAddMenuEntry("SubMenu2's item3",3); glutAttachMenu(GLUT_RIGHT_BUTTON); /*创建主菜单并加入菜单条目及子菜单*/ menu = glutCreateMenu(MenuFunc); glutAddMenuEntry("Item1",1); glutAddMenuEntry("Item2",2); glutAddMenuEntry("Item3",3); glutAddSubMenu("SubMenu 1",subMenu1); glutAddSubMenu("SubMenu 2",subMenu2); glutAttachMenu(GLUT_RIGHT_BUTTON); glutMainLoop();//进入GLUT事件处理循环 return 0;} ·int glutCreateMenu(void(*func)(int value))创建一个新的弹出式菜单并返回一个唯一标识此菜单的整型表示符。func指明此菜单的功能。 ·int glutAddMenuEntry(char *name,int value)在当前菜单底部增加一个菜单条目。 name指定显示在新菜单条目上的ASCII码字符串。 value指定当选择该菜单条目时传递到菜单回调函数中的数值。 ·void glutAddSubMenu(char *name,int menu)在当前菜单的底部增加一个子菜单触发条目。 name指定显示在新菜单触发条目上的ASCII码字符串。 meun当选择该子菜单触发条目时弹出的子菜单的标识符。 ·void glutAttachMenu(int button)把当前窗口的一个鼠标按键与当前菜单的标识符联系起来。 button指明鼠标的哪个按键。GLUT_LEFT_BUTTON、GLUT_MIDDLE_BUTTON及GLUT_RIGHT_BUTTON,分别表明鼠标左、中及右键。 ·int glutGetMenu(void)获取当前菜单的标识符,如果没有菜单存在或前一个当前菜单被删除了,glutGetMenu则返回0值。
菜单是我们经常使用的工具,很方便,直观。本节讨论在OpenGL中如何进行菜单管理。 例17:本例在蓝色的背景上绘制一个白色的正方形,在窗口内单击鼠标右键,弹出菜单,当选择不同菜单项时命令窗口会显示出是哪个菜单的哪个菜单相被激活。本例一个主菜单,主菜单有3个菜单条目及两个子菜单。两个子菜单都有3个菜单条目。
#include #include int menu,subMenu1,subMenu2;/*在蓝色的背景上绘制一个白色的正方形*/void myDraw(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); 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 == menu) printf("The current menu is Main Menu.\n"); if(nMenu == subMenu1) printf("The current menu is SubMenu1.\n"); if(nMenu == subMenu2) printf("The current menu is SubMenu2.\n");}/*子菜单1*/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; }}/*子菜单2*/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(myDraw); /*创建子菜单1并加入菜单条目*/ subMenu1 = glutCreateMenu(SubMenuFunc1); glutAddMenuEntry("SubMenu1's item1",1); glutAddMenuEntry("SubMenu1's item2",2); glutAddMenuEntry("SubMenu1's item3",3); glutAttachMenu(GLUT_RIGHT_BUTTON); /*创建子菜单2并加入菜单条目*/ subMenu2 = glutCreateMenu(SubMenuFunc2); glutAddMenuEntry("SubMenu2's item1",1); glutAddMenuEntry("SubMenu2's item2",2); glutAddMenuEntry("SubMenu2's item3",3); glutAttachMenu(GLUT_RIGHT_BUTTON); /*创建主菜单并加入菜单条目及子菜单*/ menu = glutCreateMenu(MenuFunc); glutAddMenuEntry("Item1",1); glutAddMenuEntry("Item2",2); glutAddMenuEntry("Item3",3); glutAddSubMenu("SubMenu 1",subMenu1); glutAddSubMenu("SubMenu 2",subMenu2); glutAttachMenu(GLUT_RIGHT_BUTTON); glutMainLoop();//进入GLUT事件处理循环 return 0;} ·int glutCreateMenu(void(*func)(int value))创建一个新的弹出式菜单并返回一个唯一标识此菜单的整型表示符。func指明此菜单的功能。 ·int glutAddMenuEntry(char *name,int value)在当前菜单底部增加一个菜单条目。 name指定显示在新菜单条目上的ASCII码字符串。 value指定当选择该菜单条目时传递到菜单回调函数中的数值。 ·void glutAddSubMenu(char *name,int menu)在当前菜单的底部增加一个子菜单触发条目。 name指定显示在新菜单触发条目上的ASCII码字符串。 meun当选择该子菜单触发条目时弹出的子菜单的标识符。 ·void glutAttachMenu(int button)把当前窗口的一个鼠标按键与当前菜单的标识符联系起来。 button指明鼠标的哪个按键。GLUT_LEFT_BUTTON、GLUT_MIDDLE_BUTTON及GLUT_RIGHT_BUTTON,分别表明鼠标左、中及右键。 ·int glutGetMenu(void)获取当前菜单的标识符,如果没有菜单存在或前一个当前菜单被删除了,glutGetMenu则返回0值。
下载本文示例代码
OpenGL编程轻松入门之菜单管理OpenGL编程轻松入门之菜单管理OpenGL编程轻松入门之菜单管理OpenGL编程轻松入门之菜单管理OpenGL编程轻松入门之菜单管理OpenGL编程轻松入门之菜单管理OpenGL编程轻松入门之菜单管理OpenGL编程轻松入门之菜单管理OpenGL编程轻松入门之菜单管理OpenGL编程轻松入门之菜单管理OpenGL编程轻松入门之菜单管理OpenGL编程轻松入门之菜单管理OpenGL编程轻松入门之菜单管理OpenGL编程轻松入门之菜单管理OpenGL编程轻松入门之菜单管理
阅读(217) | 评论(0) | 转发(0) |