资源描述:
《OpenGL编程轻松入门之菜单管理》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、OpenGL编程轻松入门之菜单管理2006-05-2308:55作者:黄燕出处:天极开发责任编辑:方舟菜单是我们经常使用的工具,很方便,直观。本节讨论在OpenGL中如何进行菜单管理。 例17:本例在蓝色的背景上绘制一个白色的正方形,在窗口内单击鼠标右键,弹出菜单,当选择不同菜单项时命令窗口会显示出是哪个菜单的哪个菜单相被激活。本例一个主菜单,主菜单有3个菜单条目及两个子菜单。两个子菜单都有3个菜单条目。#include#includeintmenu,su
2、bMenu1,subMenu2;/*在蓝色的背景上绘制一个白色的正方形*/voidmyDraw(void){ glClearColor(0.0,0.0,1.0,0.0); glClear(GL_COLOR_BUFFER_BIT
3、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
4、(-0.5,0.5); glVertex2f(0.5,0.5); glVertex2f(0.5,-0.5); glEnd(); glutSwapBuffers();}/*写出当前是哪个菜单*/voidGetCurrentMenu(void){ intnMenu; nMenu=glutGetMenu(); if(nMenu==menu) printf("ThecurrentmenuisMainMenu."); if(nMenu==subMenu1) printf("Thecurrentmen
5、uisSubMenu1."); if(nMenu==subMenu2) printf("ThecurrentmenuisSubMenu2.");}/*子菜单1*/voidSubMenuFunc1(intdata){ GetCurrentMenu(); switch(data) { case1: printf("SubMenu1'sitem1istriggered."); break; case2: printf("SubMenu1'sitem2istriggered.
6、"); break; case3: printf("SubMenu1'sitem3istriggered."); break; }}/*子菜单2*/voidSubMenuFunc2(intdata){ GetCurrentMenu(); switch(data) { case1: printf("SubMenu2'sitem1istriggered."); break; case2: printf("SubMenu2'sitem2istriggered.
7、n"); break; case3: printf("SubMenu2'sitem3istriggered."); break; }}/*主菜单*/voidMenuFunc(intdata){ GetCurrentMenu(); switch(data) { case1: printf("MainMenu'sitem1istriggered."); break; case2: printf("MainMenu'sitem2istriggered.");
8、 break; case3: printf("MainMenu'sitem3istriggered."); break; }}intmain(intargc,char**argv){ /*初始化*/ glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE
9、GLUT_RGB
10、GLUT_DEPTH); glutInitWindowSize(500,400); glutInitWindowPosition(100,100); /*创建窗口
11、*/ glutCreateWindow("MENU"); glutDisplayFunc(myDraw); /*创建子菜单1并加入菜单条目*/ subMenu1=glutCreateMenu(SubMenuFunc1); glutAddMenuEntry("SubMenu1'sitem1",1); glutAddMenuEntry("SubMenu1'sitem2",2); glutAddMenuEntry("SubMenu1'sitem3",3); glutAttachMenu(