EaBIM一直以来积极响应国家“十二五”推进建筑业信息化的号召,对建筑领域的信息技术开展深入技术交流和探讨!致力于打造“BIM-建筑师-生态技术”三位一体综合资源交流共享平台,希望为BIM与可持续设计理念及技术的普及做出微小的贡献!!!

EaBIM

 找回密码
 注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

搜索
查看: 517|回复: 0
打印 上一主题 下一主题

[计算机图形学] 拾取操作的实现

[复制链接]

1514

主题

7465

帖子

1万

积分

admin

Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10

积分
12406

社区QQ达人

跳转到指定楼层
楼主
发表于 2014-1-9 13:36:16 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
OpenGL中采用一种比较复杂的方式实现了拾取操作,即选择模式。选择模式是一种绘制模式,它基本思想是在一次拾取操作时,系统根据拾取操作的参数(如鼠标位置)生成一个特定视景体,然后由系统重新绘制场景中的所有图元,但这些图元并不会绘制到颜色缓存中,系统跟踪有哪些图元绘制到了这个特定的视景体中,并将这些对象的标识符保存到拾取缓冲区数组中。
    在OpenGL中实现拾取操作主要包括以下步骤。
    1.设置拾取缓冲区
    拾取时,在特定的视景体中绘制每个对象都会产生一个命中消息,命中消息将存放在一个名字堆栈中,这个名字堆栈就是拾取缓冲区。函数:
        void glSelectBuffer(GLsizei n, GLunint *buff);
指定了一个具有n个元素的整形数组buffer作为拾取缓冲区。对于每个命中消息,都会在拾取缓冲区数组中添加一条记录,每条记录包含了以下的信息:
    (1)命中发生时堆栈中的名称序号;
    (2)拾取图元所有顶点的最大和最小窗口z坐标。这两个值的范围都位于[0,1]内,他们都乘以232-1,然后四舍五入为最接近的无符号整数。
    (3)命中发生时堆栈中的内容,最下面的名称排在最前面。
    2.进入选择模式
    在定义了拾取缓冲区后,需要激活选择模式。选择模式的指定采用函数:
        GLint glRenderMode(GLenum mode);
其中,参数mode值可以为GL_RENDER(默认值)、GL_SELECT或GL_FEEDBACK,分别指定应用程序处于渲染模式、选择模式和反馈模式。应用程序一直处于当前模式下,直到调用本函数改变为其他模式为止。
    3.名字堆栈操作
    在选择模式下,需要对名字堆栈进行一系列操作,包括初始化、压栈、弹栈以及栈顶元素操作等。
        void glInitNames();//初始化名字堆栈,其初始状态为空
        void glPushName(GLuint name);//将一个名字压入堆栈,其中name是标识图元的一个无符号整数值
        void glLoad Name(GLuint name);//将名字堆栈的栈顶元素替换为name
        void glPopName();//将栈顶元素弹出
    4.设置合适的变换过程
    拾取操作可以通过矩形拾取窗口来实现,我们可以用下面的函数调用:
        gluPickMatrix(xPick, yPick, widthPick, heightPick, *vp);
其中参数xPick和yPick指定相对于显示区域左下角的拾取窗口中心的双精度浮点屏幕坐标值。当使用鼠标进行选择操作时,xPick和yPick由鼠标位置确定,但要注意y坐标的反转。参数widthPick和heightPick指定拾取窗口的双精度浮点宽高值。参数vp指定了一个包含当前显示区域的坐标位置和尺寸等参数的整型数组,该参数可以通过函数glGetIntegerv来获得。这个函数可以设置一个用于拾取操作的观察空间。
    5.为每个图元分配名字并绘制
    为了标识图元,在图元绘制过程中需要用一个整型值指定图元的名称,并在选择模式下,将这个名字压入到名字堆栈中。为了节省名字堆栈的空间,应该在图元绘制完成后,将其名字从堆栈中弹出。
    6.切换回渲染模式
    在选择模式下,所有的图元绘制完成后,应该再次调用函数glRenderMode选择渲染模式,在帧缓冲存储器中绘制图元,并返回被选中图元的个数。
    7.分析选择缓冲区中的数据
    拾取操作完成之后,可以根据选择缓冲区中的内容进行分析,以确定拾取的图元。
程序3-3 OpenGL实现的拾取操作的例子
  1. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">#include <gl/glut.h></span>
  2. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">#include "stdio.h"</span>
  3. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">const GLint pickSize = 32;</span>
  4. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">int winWidth = 400, winHeight = 300;</span>
  5. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">void Initial(void)</span>
  6. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">{</span>
  7. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glClearColor(1.0f, 1.0f, 1.0f, 1.0f);         </span>
  8. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">}</span>
  9. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">void DrawRect(GLenum mode)</span>
  10. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">{</span>
  11. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">if(mode == GL_SELECT) glPushName(1); //压入堆栈</span>
  12. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glColor3f(1.0f,0.0f,0.0f);</span>
  13. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glRectf(60.0f,50.0f,150.0f,150.0f);</span>

  14. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">if(mode == GL_SELECT) glPushName(2); //压入堆栈</span>
  15. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glColor3f(0.0f,1.0f,0.0f);</span>
  16. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glRectf(230.0f,50.0f,330.0f,150.0f);</span>
  17. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">if(mode == GL_SELECT) glPushName(3); //压入堆栈</span>
  18. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glColor3f(0.0f,0.0f,1.0f);</span>
  19. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glRectf(140.0f,140.0f,240.0f,240.0f);</span>
  20. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">}</span>
  21. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">void ProcessPicks(GLint nPicks, GLuint pickBuffer[])</span>
  22. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">{</span>
  23. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">GLint i;</span>
  24. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">GLuint name, *ptr;</span>
  25. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">printf("选中的数目为%d个\n",nPicks);</span>
  26. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">ptr=pickBuffer;</span>
  27. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">for(i=0;i<nPicks; i++){</span>
  28. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">name=*ptr;    //选中图元在堆栈中的位置</span>
  29. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">ptr+=3;       //跳过名字和深度信息</span>
  30. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">ptr+=name-1;  //根据位置信息获得选中的图元名字</span>
  31. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">if(*ptr==1) printf("你选择了红色图元\n");</span>
  32. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">if(*ptr==2) printf("你选择了绿色图元\n");</span>
  33. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">if(*ptr==3) printf("你选择了蓝色图元\n");</span>
  34. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">ptr++;</span>
  35. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">}</span>
  36. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">printf("\n\n");</span>
  37. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">}</span>
  38. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">void ChangeSize(int w, int h)</span>
  39. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">{</span>
  40. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">winWidth = w;</span>
  41. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">winHeight = h;</span>
  42. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glViewport(0, 0, w, h);</span>
  43. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glMatrixMode(GL_PROJECTION); </span>
  44. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glLoadIdentity();</span>
  45. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">gluOrtho2D(0.0,winWidth,0.0,winHeight);</span>
  46. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">}</span>
  47. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">void Display(void)</span>
  48. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">{</span>
  49. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glClear(GL_COLOR_BUFFER_BIT);</span>
  50. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">DrawRect(GL_RENDER);</span>
  51. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glFlush();</span>
  52. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">}</span>
  53. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">void MousePlot(GLint button, GLint action, GLint xMouse, GLint yMouse)</span>
  54. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">{</span>
  55. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">GLuint pickBuffer[pickSize];</span>
  56. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">GLint nPicks, vp[4];</span>
  57. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">if(button == GLUT_LEFT_BUTTON && action == GLUT_DOWN){</span>
  58. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glSelectBuffer(pickSize,pickBuffer); //设置选择缓冲区</span>
  59. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glRenderMode(GL_SELECT); //激活选择模式</span>
  60. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glInitNames();   //初始化名字堆栈</span>
  61. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glMatrixMode(GL_PROJECTION);</span>
  62. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glPushMatrix();</span>
  63. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glLoadIdentity();</span>
  64. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glGetIntegerv(GL_VIEWPORT, vp);</span>
  65. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">//定义一个10×10的选择区域</span>
  66. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">gluPickMatrix(GLdouble(xMouse), GLdouble(vp[3]-yMouse),10.0,10.0,vp);</span>
  67. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">gluOrtho2D(0.0,winWidth,0.0,winHeight);</span>
  68. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">DrawRect(GL_SELECT);</span>
  69. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">//恢复投影变换</span>
  70. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glMatrixMode(GL_PROJECTION);</span>
  71. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glPopMatrix();</span>
  72. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glFlush();</span>
  73. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">//获得选择集并输出</span>
  74. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">nPicks = glRenderMode(GL_RENDER);</span>
  75. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">ProcessPicks(nPicks, pickBuffer);</span>
  76. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glutPostRedisplay();</span>
  77. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">}</span>
  78. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">}</span>
  79. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">int main(int argc, char* argv[])</span>
  80. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">{</span>
  81. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glutInit(&argc, argv);</span>
  82. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);  </span>
  83. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glutInitWindowSize(400,300);                  </span>
  84. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glutInitWindowPosition(100,100);              </span>
  85. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glutCreateWindow("拾取操作");                   </span>
  86. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glutDisplayFunc(Display);</span>
  87. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glutReshapeFunc(ChangeSize);</span>
  88. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glutMouseFunc(MousePlot);</span>
  89. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">Initial();                                    </span>
  90. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glutMainLoop();                               </span>
  91. <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">return 0;</span>
复制代码
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
收藏收藏 转播转播 分享分享 分享淘帖 支持支持 反对反对
工作时间:工作日的9:00-12:00/13:30-18:00,节假日不在线,请勿留言
*滑块验证:
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|EaBIM网 ( 苏ICP备2020058923号-1  苏公网安备32011502011255号

GMT+8, 2024-11-27 03:54

Powered by Discuz! X3.2 Licensed

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表