计算机图形学computer graph(1)

上传人:资****亨 文档编号:480715889 上传时间:2024-05-07 格式:PPT 页数:20 大小:993.50KB
返回 下载 相关 举报
计算机图形学computer graph(1)_第1页
第1页 / 共20页
计算机图形学computer graph(1)_第2页
第2页 / 共20页
计算机图形学computer graph(1)_第3页
第3页 / 共20页
计算机图形学computer graph(1)_第4页
第4页 / 共20页
计算机图形学computer graph(1)_第5页
第5页 / 共20页
点击查看更多>>
资源描述

《计算机图形学computer graph(1)》由会员分享,可在线阅读,更多相关《计算机图形学computer graph(1)(20页珍藏版)》请在金锄头文库上搜索。

1、Working with CallbacksYuanfeng ZhouShandong University1精选pptObjectivesLearn to build interactive programs using GLUT callbacksMouseKeyboardReshapeIntroduce menus in GLUT2精选pptThe mouse callbackglutMouseFunc(mymouse)void mymouse(GLint button,GLint state,GLint x,GLint y)Returns which button(GLUT_LEFT_

2、BUTTON,GLUT_MIDDLE_BUTTON,GLUT_RIGHT_BUTTON)caused event state of that button(GLUT_UP,GLUT_DOWN)Position in window3精选pptPositioningThe position in the screen window is usually measured in pixels with the origin at the topleft cornerConsequence of refresh done from top to bottomOpenGL uses a world co

3、ordinate system with origin at the bottom leftMust invert y coordinate returned by callback by height of windowy=h y;(0,0)hw4精选pptObtaining the window sizeTo invert the y position we need the window heightHeight can change during program executionTrack with a global variableNew height returned to re

4、shape callback that we will look at in detail soonCan also use query functions glGetIntvglGetFloatvto obtain any value that is part of the state5精选pptTerminating a programIn our original programs,there was no way to terminate them through OpenGLWe can use the simple mouse callbackvoid mouse(int btn,

5、int state,int x,int y)if(btn=GLUT_RIGHT_BUTTON&state=GLUT_DOWN)exit(0);6精选pptUsing the mouse positionIn the next example,we draw a small square at the location of the mouse each time the left mouse button is clickedThis example does not use the display callback but one is required by GLUT;We can use

6、 the empty display callback functionmydisplay()7精选pptDrawing squares at cursor locationvoid mymouse(int btn,int state,int x,int y)if(btn=GLUT_RIGHT_BUTTON&state=GLUT_DOWN)exit(0);if(btn=GLUT_LEFT_BUTTON&state=GLUT_DOWN)drawSquare(x,y);void drawSquare(int x,int y)y=w-y;/*invert y position*/glColor3ub

7、(char)rand()%256,(char)rand)%256,(char)rand()%256);/*a random color*/glBegin(GL_POLYGON);glVertex2f(x+size,y+size);glVertex2f(x-size,y+size);glVertex2f(x-size,y-size);glVertex2f(x+size,y-size);glEnd();8精选pptUsing the motion callbackWe can draw squares(or anything else)continuously as long as a mouse

8、 button is depressed by using the motion callbackglutMotionFunc(drawSquare)We can draw squares without depressing a button using the passive motion callbackglutPassiveMotionFunc(drawSquare)9精选pptUsing the keyboardglutKeyboardFunc(mykey)void mykey(unsigned char key,int x,int y)Returns ASCII code of k

9、ey depressed and mouse locationvoid mykey()if(key=Q|key=q)exit(0);10精选pptSpecial and Modifier KeysGLUT defines the special keys in glut.hFunction key 1:GLUT_KEY_F1Up arrow key:GLUT_KEY_UPif(key=GLUT_KEY_F1 Can also check of one of the modifiersGLUT_ACTIVE_SHIFTGLUT_ACTIVE_CTRLGLUT_ACTIVE_ALTis depre

10、ssed byglutGetModifiers()Allows emulation of threebutton mouse with one or twobutton mice11精选pptReshaping the windowWe can reshape and resize the OpenGL display window by pulling the corner of the windowWhat happens to the display?Must redraw from applicationTwo possibilitiesDisplay part of worldDis

11、play whole world but force to fit in new windowCan alter aspect ratio12精选pptReshape possiblitiesoriginalreshaped13精选pptThe Reshape callbackglutReshapeFunc(myreshape)void myreshape(int w,int h)Returns width and height of new window(in pixels)A redisplay is posted automatically at end of execution of

12、the callbackGLUT has a default reshape callback but you probably want to define your ownThe reshape callback is good place to put viewing functions because it is invoked when the window is first opened14精选pptExample ReshapeThis reshape preserves shapes by making the viewport and world window have th

13、e same aspect ratiovoid myReshape(int w,int h)glViewport(0,0,w,h);glMatrixMode(GL_PROJECTION);/*switch matrix mode*/glLoadIdentity();if(w=h)gluOrtho2D(-2.0,2.0,-2.0*(GLfloat)h/(GLfloat)w,2.0*(GLfloat)h/(GLfloat)w);else gluOrtho2D(-2.0*(GLfloat)w/(GLfloat)h,2.0*(GLfloat)w/(GLfloat)h,-2.0,2.0);glMatri

14、xMode(GL_MODELVIEW);/*return to modelview mode*/15精选pptToolkits and WidgetsMost window systems provide a toolkit or library of functions for building user interfaces that use special types of windows called widgetsWidget sets include tools such asMenusSlidebarsDialsInput boxesBut toolkits tend to be

15、 platform dependentGLUT provides a few widgets including menus16精选pptMenusGLUT supports popup menusA menu can have submenusThree stepsDefine entries for the menuDefine action for each menu itemAction carried out if entry selectedAttach menu to a mouse button17精选pptDefining a simple menuIn main.cmenu

16、_id=glutCreateMenu(mymenu);glutAddmenuEntry(“clear Screen,1);gluAddMenuEntry(“exit,2);glutAttachMenu(GLUT_RIGHT_BUTTON);entries that appear whenright button depressedidentifiersclear screenexit18精选pptMenu actionsMenu callbackNote each menu has an id that is returned when it is createdAdd submenus byglutAddSubMenu(char*submenu_name,submenu id)void mymenu(int id)if(id=1)glClear();if(id=2)exit(0);entry in parent menu19精选pptOther functions in GLUTDynamic WindowsCreate and destroy during executionSub

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 医学/心理学 > 基础医学

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号