拾取之选择模式

Picking Tutorial
拾取教程

The Selection Mode
选择模式

So far the OpenGL naming scheme has been presented. This section will show you how to enter the selection mode for picking purposes. The first step is to tell OpenGL where to store thehit records. This is accomplished with the following function:

至此,OpenGL的命名方案已经展现了.这部分将展示为了选择的目的怎样进入选择模式.第一步是告诉OpenGL存储命中记录的位置.这由下面的函数完成:



void glSelectBuffer(GLsizei size, GLuint *buffer);

Parameters:
buffer : An array of unsigned integers. This array is where OpenGL will store the hit records.
size : The size of the array.


You are required to call this function before entering theselection mode. Next you enter theselection modewith a call to

在进入选择模式前必须调用这个函数.接下来通过调用下面的函数进入选择模式



void glRenderMode(GLenum mode);

Parameters:
mode - use GL_SELECT to enter the rendering mode and GL_RENDER to return to normal rendering. This later value is the default.


Now comes the tricky part. The application must redefine the viewing volume so that it renders only a small area around the place where the mouse was clicked. In order to do that it is necessary to set the matrix mode to GL_PROJECTION. Afterwards, the application should push the current matrix to save the normal rendering mode settings. Next initialise the matrix. The following snippet of code shows how to do this:

现在到了要小心的地方.应用程序必须重新定义视口区域,这样它只绘制鼠标点击区域的一小块区域.为了这样做,必须设置GL_PROJECTION的矩阵.应用程序应该将当前矩阵压入栈来保存正常绘制模式的设置.接下来初始化矩阵.下面的代码展示怎样做:



    
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();



OK so now you have a clean projection matrix. All that is required now is to define the viewing volume so that rendering is done only in a small area around the cursor. This can be accomplished using the following function:

这样你就有了一个干净的投影矩阵.现在需要的是定义视口区域,这样绘制只在围绕光标一个小区域内.使用下面的函数实现:



void gluPickMatrix(GLdouble x, GLdouble y, GLdouble witdth, GLdouble height, GLint viewport[4]);

Parameters:
x,y : These two values represent the cursor location. They define the centre of the picking area. This value is specified in window coordinates. However note that window coordinates in OpenGL have the origin at the bottom left corner of the viewport, whereas for the Operating system is the upper left corner.
width, height : The size of the picking region, too small and you may be hard pressed to pick small objects, too large and you may end up with too many hits.
viewport : The current viewport


Before you call the above function you have to get the current viewport. This can be done querying OpenGL for the state variable GL_VIEWPORT (use the function glGetIntegerv). Then you call gluPickMatrix, and finally set your projection (perspective or orthogonal) just as you did for the normal rendering mode. Finally get back to the modelview matrix mode, and initialise theName Stackto start rendering. The following code shows the sequence of function calls to do just that using a perspective projection.

在你调用下面的函数之前,你必须得到当前的视口,可以通过查询OpenGL的状态变量GL_VIEWPORT(使用glGetIntegerv函数)得到.然后调用gluPickMatrix,最终设置你的投影矩阵(锥形投影或是正交投影),就像在普通绘制模式下一样.最终返回模型视图矩阵模式,初始化名字栈,然后开始绘制.下面的一系列函数就是做这些事的.



    
glGetIntegerv(GL_VIEWPORT,viewport);
gluPickMatrix(cursorX,viewport[3]-cursorY,
		5,5,viewport);
gluPerspective(45,ratio,0.1,1000);
glMatrixMode(GL_MODELVIEW);
glInitNames();



Note the second parameter of gluPickMatrix. As mentioned before, OpenGL has a different origin for its window coordinates than the operation system. The second parameter provides for the conversion between the two systems, i.e. it transforms the origin from the upper left corner, as provided by GLUT for example, into the bottom left corner.

注意gluPicMatrix的第二个参数.像以前提到的, OpenGL有一个与操作系统不同的坐标原点.第二个参数提供在两种系统之间的转换,例如,它将由GLUT提供的坐标原点从右上角转换到左下角.

The picking region in this case is a 5x5 window. You may find that it is not appropriate for your application. Do some tests to find an appropriate value if you're finding it hard to pick the right objects.

在这个例子中,选择区域是一个5*5的窗口.你可能发现它不适合你的应用程序.如果你发现很难选择正确的对象,多做一些测试来找到一个合适的值.

The following function does all operations required to enter the selection mode and start picking, assuming thatcursorXandcursorYare the operating system window coordinates for the location of the mouse click.

下面的函数实现从进入选择模式到开始选择的所有操作,假设cursorX cursorY是鼠标点击时的位置在操作系统中的坐标.



    
#define BUFSIZE 512
GLuint selectBuf[BUFSIZE]

...
 
void startPicking(int cursorX, int cursorY


) {

	GLint viewport[4];

	glSelectBuffer(BUFSIZE,selectBuf);
	glRenderMode(GL_SELECT);

	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();

	glGetIntegerv(GL_VIEWPORT,viewport);
	gluPickMatrix(cursorX,viewport[3]-cursorY,
			5,5,viewport);
	gluPerspective(45,ratio,0.1,1000);
	glMatrixMode(GL_MODELVIEW);
	glInitNames();
}


You may copy and paste this function to your application, with the appropriate modifications regarding your projection. If you do that then just call this function when entering the rendering mode and before rendering any graphic primitives.

你可以拷贝和粘贴这个函数到你的应用程序并根据你的工程作一些合适的修改.之后当进入绘制模式时,并且在绘制任何图元之前调用这个函数.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值