1、3、实现拾取操作的例子结果:五、程序代码1、利用鼠标实现橡皮筋技术#include int iPointNum = 0; /已确定点的数目int x1=0,x2=0,y1=0,y2=0; /确定的点坐标int winWidth = 400, winHeight = 300; /窗口的宽度和高度void Initial(void) glClearColor(1.0f, 1.0f, 1.0f, 1.0f);void ChangeSize(int w, int h) winWidth = w; winHeight = h; glViewport(0, 0, w, h); /指定窗口显示区域 glMa
2、trixMode(GL_PROJECTION); /设置投影参数 glLoadIdentity(); gluOrtho2D(0.0,winWidth,0.0,winHeight);void Display(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(); /交换缓冲区void MousePlot(G
3、Lint button, GLint action, GLint xMouse, GLint yMouse) if(button = GLUT_LEFT_BUTTON & action = GLUT_DOWN) if(iPointNum = 0 | iPointNum = 2) iPointNum = 1; x1 = xMouse; y1 = winHeight - yMouse; else iPointNum = 2; x2 = xMouse; y2 = winHeight - yMouse; glutPostRedisplay(); /指定窗口重新绘制 if(button=GLUT_RIG
4、HT_BUTTON&action = GLUT_DOWN) iPointNum = 0; glutPostRedisplay();void PassiveMouseMove (GLint xMouse, GLint yMouse) if(iPointNum = 1) x2 = xMouse; int main(int argc, char* argv) glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); /使用双缓存及RGB模型 glutInitWindowSize(400,300); glutInitWind
5、owPosition(100,100); glutCreateWindow(橡皮筋技术); glutDisplayFunc(Display); glutReshapeFunc(ChangeSize); /指定窗口在整形回调函数 glutMouseFunc(MousePlot); /指定鼠标响应函数 glutPassiveMotionFunc(PassiveMouseMove); /指定鼠标移动响应函数 Initial(); glutMainLoop(); return 0;2、利于键盘实现橡皮筋技术void Key(unsigned char key, int x, int y) switch
6、(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; glutKeyboardFunc(Key); /指定键盘响应函数3、实现拾取操作的例子#include stdio.hconst GLint pickSize = 32;void DrawRect(GLenum mode) if(mo
7、de = 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);void P
8、rocessPicks(GLint nPicks, GLuint pickBuffer) GLint i; GLuint name, *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+; pri
9、ntf(nnwinWidth = w;winHeight = h;glViewport(0, 0, w, h);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(0.0,winWidth,0.0,winHeight);glClear(GL_COLOR_BUFFER_BIT);DrawRect(GL_RENDER);glFlush();GLuint pickBufferpickSize;GLint nPicks, vp4;if(button = GLUT_LEFT_BUTTON & action = GLUT_DOWN) glSele
10、ctBuffer(pickSize,pickBuffer); /设置选择缓冲区glRenderMode(GL_SELECT); /激活选择模式glInitNames(); /初始化名字堆栈glPushMatrix();glGetIntegerv(GL_VIEWPORT, vp);/定义一个1010的选择区域gluPickMatrix(GLdouble(xMouse), GLdouble(vp3-yMouse),10.0,10.0,vp);DrawRect(GL_SELECT);/恢复投影变换glPopMatrix();/获得选择集并输出nPicks = glRenderMode(GL_REND
11、ER);ProcessPicks(nPicks, pickBuffer);glutPostRedisplay();glutInit(&glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);glutInitWindowSize(400,300);glutInitWindowPosition(100,100);glutCreateWindow(拾取操作glutDisplayFunc(Display);glutReshapeFunc(ChangeSize);glutMouseFunc(MousePlot);Initial();glutMainLoop();return 0;6、心得体会 通常在实验过程中,可以加入自己喜欢的其他东西,不过要保证原本的基本东西没有错误。
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1