图形学实验报告 OpenGL实现橡皮筋技术解析文档格式.docx
《图形学实验报告 OpenGL实现橡皮筋技术解析文档格式.docx》由会员分享,可在线阅读,更多相关《图形学实验报告 OpenGL实现橡皮筋技术解析文档格式.docx(11页珍藏版)》请在冰豆网上搜索。
五、程序代码1、利用鼠标实现橡皮筋技术#includeintiPointNum=0;
/已确定点的数目intx1=0,x2=0,y1=0,y2=0;
/确定的点坐标intwinWidth=400,winHeight=300;
/窗口的宽度和高度voidInitial(void)glClearColor(1.0f,1.0f,1.0f,1.0f);
voidChangeSize(intw,inth)winWidth=w;
winHeight=h;
glViewport(0,0,w,h);
/指定窗口显示区域glMatrixMode(GL_PROJECTION);
/设置投影参数glLoadIdentity();
gluOrtho2D(0.0,winWidth,0.0,winHeight);
voidDisplay(void)glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f,0.0f,0.0f);
if(iPointNum=1)glBegin(GL_LINES);
/绘制直线段glVertex2i(x1,y1);
glVertex2i(x2,y2);
glEnd();
glutSwapBuffers();
/交换缓冲区voidMousePlot(GLintbutton,GLintaction,GLintxMouse,GLintyMouse)if(button=GLUT_LEFT_BUTTON&
action=GLUT_DOWN)if(iPointNum=0|iPointNum=2)iPointNum=1;
x1=xMouse;
y1=winHeight-yMouse;
elseiPointNum=2;
x2=xMouse;
y2=winHeight-yMouse;
glutPostRedisplay();
/指定窗口重新绘制if(button=GLUT_RIGHT_BUTTON&
action=GLUT_DOWN)iPointNum=0;
voidPassiveMouseMove(GLintxMouse,GLintyMouse)if(iPointNum=1)x2=xMouse;
intmain(intargc,char*argv)glutInit(&
argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
/使用双缓存及RGB模型glutInitWindowSize(400,300);
glutInitWindowPosition(100,100);
glutCreateWindow(橡皮筋技术);
glutDisplayFunc(Display);
glutReshapeFunc(ChangeSize);
/指定窗口在整形回调函数glutMouseFunc(MousePlot);
/指定鼠标响应函数glutPassiveMotionFunc(PassiveMouseMove);
/指定鼠标移动响应函数Initial();
glutMainLoop();
return0;
2、利于键盘实现橡皮筋技术#includeintiPointNum=0;
/交换缓冲区voidPassiveMouseMove(GLintxMouse,GLintyMouse)if(iPointNum=1)x2=xMouse;
voidKey(unsignedcharkey,intx,inty)switch(key)casep:
if(iPointNum=0|iPointNum=2)iPointNum=1;
x1=x;
y1=winHeight-y;
x2=x;
y2=winHeight-y;
break;
default:
/指定窗口在整形回调函数glutKeyboardFunc(Key);
/指定键盘响应函数glutPassiveMotionFunc(PassiveMouseMove);
3、实现拾取操作的例子#include#includestdio.hconstGLintpickSize=32;
intwinWidth=400,winHeight=300;
voidInitial(void)glClearColor(1.0f,1.0f,1.0f,1.0f);
voidDrawRect(GLenummode)if(mode=GL_SELECT)glPushName
(1);
/压入堆栈glColor3f(1.0f,0.0f,0.0f);
glRectf(60.0f,50.0f,150.0f,150.0f);
if(mode=GL_SELECT)glPushName
(2);
/压入堆栈glColor3f(0.0f,1.0f,0.0f);
glRectf(230.0f,50.0f,330.0f,150.0f);
if(mode=GL_SELECT)glPushName(3);
/压入堆栈glColor3f(0.0f,0.0f,1.0f);
glRectf(140.0f,140.0f,240.0f,240.0f);
voidProcessPicks(GLintnPicks,GLuintpickBuffer)GLinti;
GLuintname,*ptr;
printf(选中的数目为%d个n,nPicks);
ptr=pickBuffer;
for(i=0;
inPicks;
i+)name=*ptr;
/选中图元在堆栈中的位置ptr+=3;
/跳过名字和深度信息ptr+=name-1;
/根据位置信息获得选中的图元名字if(*ptr=1)printf(你选择了红色图元n);
if(*ptr=2)printf(你选择了绿色图元n);
if(*ptr=3)printf(你选择了蓝色图元n);
ptr+;
printf(nn);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
DrawRect(GL_RENDER);
glFlush();
voidMousePlot(GLintbutton,GLintaction,GLintxMouse,GLintyMouse)GLuintpickBufferpickSize;
GLintnPicks,vp4;
if(button=GLUT_LEFT_BUTTON&
action=GLUT_DOWN)glSelectBuffer(pickSize,pickBuffer);
/设置选择缓冲区glRenderMode(GL_SELECT);
/激活选择模式glInitNames();
/初始化名字堆栈glMatrixMode(GL_PROJECTION);
glPushMatrix();
glGetIntegerv(GL_VIEWPORT,vp);
/定义一个1010的选择区域gluPickMatrix(GLdouble(xMouse),GLdouble(vp3-yMouse),10.0,10.0,vp);
DrawRect(GL_SELECT);
/恢复投影变换glMatrixMode(GL_PROJECTION);
glPopMatrix();
/获得选择集并输出nPicks=glRenderMode(GL_RENDER);
ProcessPicks(nPicks,pickBuffer);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(400,300);
glutCreateWindow(拾取操作);
glutMouseFunc(MousePlot);
Initial();
6、心得体会通常在实验过程中,可以加入自己喜欢的其他东西,不过要保证原本的基本东西没有错误。