資源描述:
《OpenGL編程輕松入門(mén)之菜單管理》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在工程資料-天天文庫(kù)。
1、OpenGL編程輕松入門(mén)之菜單管理2006-05-2308:55作者:黃燕出處:天極開(kāi)發(fā)責(zé)任編輯:方舟菜單是我們經(jīng)常使用的工具,很方便,直觀。本節(jié)討論在OpenGL中如何進(jìn)行菜單管理?! ±?7:本例在藍(lán)色的背景上繪制一個(gè)白色的正方形,在窗口內(nèi)單擊鼠標(biāo)右鍵,彈出菜單,當(dāng)選擇不同菜單項(xiàng)時(shí)命令窗口會(huì)顯示出是哪個(gè)菜單的哪個(gè)菜單相被激活。本例一個(gè)主菜單,主菜單有3個(gè)菜單條目及兩個(gè)子菜單。兩個(gè)子菜單都有3個(gè)菜單條目。#include#includeintmenu,su
2、bMenu1,subMenu2;/*在藍(lán)色的背景上繪制一個(gè)白色的正方形*/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();}/*寫(xiě)出當(dāng)前是哪個(gè)菜單*/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); /*創(chuàng)建窗口
11、*/ glutCreateWindow("MENU"); glutDisplayFunc(myDraw); /*創(chuàng)建子菜單1并加入菜單條目*/ subMenu1=glutCreateMenu(SubMenuFunc1); glutAddMenuEntry("SubMenu1'sitem1",1); glutAddMenuEntry("SubMenu1'sitem2",2); glutAddMenuEntry("SubMenu1'sitem3",3); glutAttachMenu(