拾取之颜色编码

Picking Tutorial
拾取教程


Color Coding
颜色编码


The Red Book describes an interesting approach to picking based on color coding. This is a very simple approach that can be used instead of using the OpenGL API described in the previous sections.

红宝书描述了一种基本颜色编码来拾取的有趣方法。它是一个非常简单的方法,它能被用来代替前面章节描述的使用OpenGL API的方法。

The color coding scheme does not require any perspective changes and therefore it is simpler in theory. Just define a rendering function where the relevant objects (pickable and occluders) are assigned each a different color. When the user clicks the mouse over the scene, render the scene on the back buffer, read back the selected pixel from the back buffer and check its color. The process is completely transparent to the user because the buffers are not swapped, so the color coding rendering is never seen.

颜色编码方法不要求任何视角变体,因此在理论上也非常简单。仅定义一个绘制函数,它给每个相关的对象(可选择和遮挡板)赋于一个不同的颜色。当用户在场景上点击鼠标时,绘制场景到后缓冲区,从后缓冲区读回选择像素的颜色然后检查它。这个处理过程对用户完全透明,因为缓冲区是不会交换的,所以颜色编码绘制是永不会见到的。

For instance, consider the following scene with 4 snowmen. What you're seeing is the result of the normal rendering function.

例如,考虑下面有4个雪人的场景,你所见到的是正常绘制函数的结果。

The next figure shows the result produced in the back buffer by the color coding rendering function. As you can see each snowman has a different color.

下图展示通过颜色编码绘制函数绘制到后缓冲区中的结果。如你所见到的,每个雪人有一个不同的颜色。

The required steps when the mouse is clicked are:

当鼠标点击时需的步骤:

  • 1. Call the color coding rendering function
  • 1. 调用颜色编码绘制函数
  • 2. Read the pixel where the mouse was clicked from the back buffer
  • 2. 从背后缓冲区中读取鼠标点击位置的像素
  • 3. Process the color information to find out which item was clicked
  • 3. 处理颜色信息,找到哪一项被点击
  • 4. Do NOT swap buffers, otherwise the user will see the color coding
  • 4。 不要交换缓冲区,否则用户将会看到颜色编码

Bellow the normal rendering function is presented. It contains the code to render both the ground as well as the snowmen (contained in the display list). 

下面是一个正常绘制函数。它包含绘制大地和雪人的代码(包含在显示列表中)。



    
void draw() {

// Draw ground
	glColor3f(0.9f, 0.9f, 0.9f);
	glBegin(GL_QUADS);
		glVertex3f(-100.0f, 0.0f, -100.0f);
		glVertex3f(-100.0f, 0.0f,  100.0f);
		glVertex3f( 100.0f, 0.0f,  100.0f);
		glVertex3f( 100.0f, 0.0f, -100.0f);
	glEnd();

// Draw 4 Snowmen

	glColor3f(1.0f, 1.0f, 1.0f);

	for(int i = 0; i < 2; i++)
		for(int j = 0; j < 2; j++) {
			glPushMatrix();
			glTranslatef(i*3.0,0,-j * 3.0);
			glColor3f(1.0f, 1.0f, 1.0f);
			glCallList(snowman_display_list);
			glPopMatrix();
		}

}



The following function is for color coding rendering. As you can see the ground was left out since it is neither a pickable object, nor an occluding one. Also the code for the snowmen was altered 

下在的函数是颜色编码绘制。如你所见,大地被忽略掉了,因为它即不是可选择对象,也不是一个遮挡板。同时,绘制雪人的代码被修改了。



    
void drawPickingMode() {

// Draw 4 SnowMen


	glDisable(GL_DITHER);
	for(int i = 0; i < 2; i++)
           for(int j = 0; j < 2; j++) {
	 	glPushMatrix();
		
// A different color for each snowman

		switch (i*2+j) {	
			case 0: glColor3ub(255,0,0);break;
			case 1: glColor3ub(0,255,0);break;
			case 2: glColor3ub(0,0,255);break;
			case 3: glColor3ub(250,0,250);break;
		}

		glTranslatef(i*3.0,0,-j * 3.0);
		glCallList(snowman_display_list);
		glPopMatrix();
	   }
	glEnable(GL_DITHER);
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值