|
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实现的拾取操作的例子- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">#include <gl/glut.h></span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">#include "stdio.h"</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">const GLint pickSize = 32;</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">int winWidth = 400, winHeight = 300;</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">void Initial(void)</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">{</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glClearColor(1.0f, 1.0f, 1.0f, 1.0f); </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">}</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">void DrawRect(GLenum mode)</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">{</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">if(mode == GL_SELECT) glPushName(1); //压入堆栈</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glColor3f(1.0f,0.0f,0.0f);</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glRectf(60.0f,50.0f,150.0f,150.0f);</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">if(mode == GL_SELECT) glPushName(2); //压入堆栈</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glColor3f(0.0f,1.0f,0.0f);</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glRectf(230.0f,50.0f,330.0f,150.0f);</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">if(mode == GL_SELECT) glPushName(3); //压入堆栈</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glColor3f(0.0f,0.0f,1.0f);</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glRectf(140.0f,140.0f,240.0f,240.0f);</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">}</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">void ProcessPicks(GLint nPicks, GLuint pickBuffer[])</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">{</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">GLint i;</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">GLuint name, *ptr;</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">printf("选中的数目为%d个\n",nPicks);</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">ptr=pickBuffer;</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">for(i=0;i<nPicks; i++){</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">name=*ptr; //选中图元在堆栈中的位置</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">ptr+=3; //跳过名字和深度信息</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">ptr+=name-1; //根据位置信息获得选中的图元名字</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">if(*ptr==1) printf("你选择了红色图元\n");</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">if(*ptr==2) printf("你选择了绿色图元\n");</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">if(*ptr==3) printf("你选择了蓝色图元\n");</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">ptr++;</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">}</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">printf("\n\n");</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">}</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">void ChangeSize(int w, int h)</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">{</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">winWidth = w;</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">winHeight = h;</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glViewport(0, 0, w, h);</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glMatrixMode(GL_PROJECTION); </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glLoadIdentity();</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">gluOrtho2D(0.0,winWidth,0.0,winHeight);</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">}</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">void Display(void)</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">{</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glClear(GL_COLOR_BUFFER_BIT);</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">DrawRect(GL_RENDER);</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glFlush();</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">}</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">void MousePlot(GLint button, GLint action, GLint xMouse, GLint yMouse)</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">{</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">GLuint pickBuffer[pickSize];</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">GLint nPicks, vp[4];</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">if(button == GLUT_LEFT_BUTTON && action == GLUT_DOWN){</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glSelectBuffer(pickSize,pickBuffer); //设置选择缓冲区</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glRenderMode(GL_SELECT); //激活选择模式</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glInitNames(); //初始化名字堆栈</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glMatrixMode(GL_PROJECTION);</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glPushMatrix();</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glLoadIdentity();</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glGetIntegerv(GL_VIEWPORT, vp);</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">//定义一个10×10的选择区域</span>
- <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>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">gluOrtho2D(0.0,winWidth,0.0,winHeight);</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">DrawRect(GL_SELECT);</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">//恢复投影变换</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glMatrixMode(GL_PROJECTION);</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glPopMatrix();</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glFlush();</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">//获得选择集并输出</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">nPicks = glRenderMode(GL_RENDER);</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">ProcessPicks(nPicks, pickBuffer);</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glutPostRedisplay();</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">}</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">}</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">int main(int argc, char* argv[])</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">{</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glutInit(&argc, argv);</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glutInitWindowSize(400,300); </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glutInitWindowPosition(100,100); </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glutCreateWindow("拾取操作"); </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glutDisplayFunc(Display);</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glutReshapeFunc(ChangeSize);</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glutMouseFunc(MousePlot);</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">Initial(); </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">glutMainLoop(); </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">return 0;</span>
复制代码 |
|