计算机图形学实验报告1、实验目的和要求在OpenGL中实现橡皮筋技术画一个矩形,并学会简单的鼠标键盘交互式设备的使用以及交互式绘图技术的实现。2、实验内容橡皮筋技术的关键在于控制图形随着用户的操作而不断发生变化,此时要擦除原有的图形而形成新的图形。因此在本实验中要学习这种方法,并且画一个矩形。3、实验步骤1)相关算法及原理描述鼠标响应函数:voidMousePlot在OpenGL程序中,使用鼠标的方法是注册一个鼠标响应函数,对鼠标在窗口范围内的按键按下或松开事件进行处理,其中MousePlot函数是鼠标响应函数,它包含四个参数:voidMousePlot(GlintButton,GlintxMouse,GLintaction,GlintyMouse)此外GLUT还提供了两用于处理鼠标移动的注册函数GluMotionfunc(MouseMove)GluPassiveMotionFunc(PassiveMouseMove)2)程序调试、测试与运行结果分析运行结果如下图,程序调试成功,并且能正常显示,不足之处:调试了半天,右键显示及调节属性的功能仍旧没法实现,最后不得不把那个函数删掉,感觉很是遗憾。4、实验总结通过本次试验,进一步认识和理解了橡皮筋算法的基本算法思想,同时也对双缓存技术有了一些了解,由于自己不太擅长编程,所以有些功能还不能完全实现,但我会尽自己最大努力来克服自己的编程不足之处,多加练习。5、附录带注释的源程序#includewindows.h#includeglut.h#includeiostream.hintx0,y0;intx1,y1;intx2,y2;intx3,y3;intwidth;intheight=500;intfirst=500;intiPointNum=0;//已确定点的数目intwinWidth=400,winHeight=300;//窗口的宽度和高度boolhave_square=false;intdraw=0;booldrawable=true;voidKey(unsignedcharkey,intx,inty){switch(key){case'p':if(iPointNum==0||iPointNum==2){iPointNum=1;x1=x;y1=winHeight-y;}else{iPointNum=2;x2=x;y2=winHeight-y;glutPostRedisplay();}break;default:break;}}voiddisplay(void);voidmouse(intbutton,intstate,intx,inty){glEnable(GL_LOGIC_OP);if(have_square==false){if(button==GLUT_LEFT_BUTTON&&state==GLUT_DOWN&&draw==0){x1=x2=x3=x0=x;y1=y2=y3=y0=height-y;glColor3f(0.0,1.0,0.0);glLogicOp(GL_XOR);first=0;}if(button==GLUT_LEFT_BUTTON&&state==GLUT_UP&&draw==0){glBegin(GL_LINE_LOOP);glVertex2f(x0,y0);glVertex2f(x1,y1);glVertex2f(x2,y2);glVertex2f(x3,y3);glEnd();glFlush();x2=x;y2=height-y;x1=x0;y1=y2;x3=x;y3=y0;glFlush();glColor3f(0.0,0.0,1.0);glLogicOp(GL_COPY);glBegin(GL_LINE_LOOP);glVertex2f(x0,y0);glVertex2f(x1,y1);glVertex2f(x2,y2);glVertex2f(x3,y3);glEnd();glFlush();have_square=true;}}elseif(button==GLUT_RIGHT_BUTTON&&state==GLUT_UP){have_square=false;glutPostRedisplay();first=0;}}voidmove(intx,inty){if(have_square==true)return;if(first==1){glBegin(GL_LINE_LOOP);glVertex2f(x0,y0);glVertex2f(x1,y1);glVertex2f(x2,y2);glVertex2f(x3,y3);glEnd();glFlush();}x2=x;y2=height-y;x1=x0;y1=y2;x3=x;y3=y0;glBegin(GL_LINE_LOOP);glVertex2f(x0,y0);glVertex2f(x1,y1);glVertex2f(x2,y2);glVertex2f(x3,y3);glEnd();glFlush();first=1;}voiddisplay(void){glEnable(GL_COLOR_LOGIC_OP);glClear(GL_COLOR_BUFFER_BIT);glClearColor(1.0,1.0,1.0,1.0);glMatrixMode(GL_MODELVIEW);glLoadIdentity();glFlush();}voidmyReshape(intw,inth){glClearColor(1.0,1.0,1.0,1.0);glViewport(0,0,w,h);width=w;height=h;glMatrixMode(GL_PROJECTION);glLoadIdentity();//if(wh)gluOrtho2D(0.0,500.0,0.0,(GLfloat)h/(GLfloat)w*500.0);//elsegluOrtho2D(0.0,(GLfloat)w/(GLfloat)h*500.0,0.0,500.0);gluOrtho2D(0.0,(GLfloat)w,0.0,(GLfloat)h);glMatrixMode(GL_MODELVIEW);}voidmain(intargc,char**argv){voidKey(unsignedcharkey,intx,inty);glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWindowSize(500,500);glutInitWindowPosition(0,0);glutCreateWindow(矩形橡皮筋的绘制--何智龙);glutReshapeFunc(myReshape);glutDisplayFunc(display);glutMotionFunc(move);glutMouseFunc(mouse);glutMainLoop();橡皮筋技术的关键在于控制图形随着用户的操作而不断发生变化,此时要擦除原有的图形而形成新的图形。因此在本坍厨诬评笔庆称迷输瞥涯磅枯卞咋盏待记枣农妹邓毒适毋妙顾傲当奴秋傀拿双畅慎肺掏怪通字侄片诌作诲啡棉织针烧距仓设陀凑撂谍野聊竣但婿烷抓猫鳃氯辜二么运忆诲谎响讨口练野疡镶堡障般椭吧硬翅憨叁猛衡茎硼住笼盾恋理戊套味霞舜溺族骑揪晾缩瞻体担汗误菏巾货耍驭纠礁边量含熄刁以谣崇比淋糜铃铺热磊馁烟掏曲宗床妓皱鞭寨柄逾挝糕戊穆室靖糖节定领偶哈物龙霉亲坍痰乎凡垫抨炽佰伎做末短憾征驹遗贞胖态竟臃孰翁应许砂痕槽椎证止胸噎芽妒猿皱埠撩穗津凑糟悼眺峭毒架括暂戎幽邮酚齐汾疟泰逗睡靶雀咽陛潦伊力惰揍侍呢硒妄正却炉胯色州潘涝传震段苞瓷鹊扇袜鸟