OpenGL鼠标拾取的实现:终于搞定(包括细节问题)

转载 2012年06月08日 21:50:59

点击打开链接

void OpenGL::pickRects(){
/*	GetCursorPos(&mouse);
	ScreenToClient(hWnd,&mouse);
	GLuint pickBuffer [32];
    GLint nPicks,vpArray[4];
	glRenderMode(GL_SELECT);
		glSelectBuffer(32,pickBuffer);
		glInitNames();
		//glPushName(-1);
		glMatrixMode(GL_PROJECTION);
		glPushMatrix();		   
			//glLoadIdentity();
			glGetIntegerv(GL_VIEWPORT,vpArray);
			gluPickMatrix(GLdouble(mouse.x),GLdouble(vpArray[3]-mouse.y),5.0,5.0,vpArray);
		//	glOrtho(0,1,0,1,0,1);
			//激活拾取区域。		  	
		//	gluOrtho2D(0,0,Width,Height);
		
		rects(GL_SELECT);
		glMatrixMode(GL_PROJECTION);
		int j;
		glGetIntegerv(GL_NAME_STACK_DEPTH,&j);
		cout<<j<<endl;
		glPopMatrix();
		glFlush();
	    nPicks=glRenderMode(GL_RENDER);
	    processPicks(nPicks,pickBuffer);
        //processPicks(nPicks,pickBuffer);
		*/
   GetCursorPos(&mouse);
   ScreenToClient(hWnd,&mouse);
   GLuint selectBuf[32];
   GLint hits;
   GLint viewport[4];
  
   glGetIntegerv(GL_VIEWPORT, viewport);

   glSelectBuffer(32, selectBuf);
   (void) glRenderMode(GL_SELECT);
   glInitNames();
   glPushName(0);
   glMatrixMode(GL_PROJECTION);

//下面这段代码可有可无,只是为了确保点击测试和实际绘图用的是同一个投影阵

 glLoadIdentity(); // 重置当前指定的矩阵为单位矩阵
   gluPerspective // 设置透视图
( 53.0f, // 透视角设置为 90 度
  (GLfloat)Width/(GLfloat)Height,// 窗口的宽与高比
  0.1f, // 视野透视深度:近点1.0f
  3000.0f // 视野透视深度:始点0.1f远点1000.0f
);

  
glPushMatrix();  

   glPushMatrix();  
//下面的代码是最关键的地方,,注意:据测试:gluPickMatrix()意思并不是仅仅绘制一个拾取窗口。同时,  

//下面的代码是最关键的地方,,注意:据测试:gluPickMatrix()意思并不是仅仅绘制一个拾取窗口。同时,Cpp代码  
//它也是一个投影矩阵变换!!~~~~如果使用第二个LoadIdentity()那么:前面的拾取矩阵就会失效!!  

//它也是一个投影矩阵变换!!~~~~如果使用第二个LoadIdentity()那么:前面的拾取矩阵就会失效!!Cpp代码  
  

 glLoadIdentity();   
  create 5x5 pixel picking region near cursor location */   
 gluPickMatrix((GLdouble) mouse.x, (GLdouble) (viewport[3] - mouse.y),   
               5.0, 5.0, viewport);  

   glLoadIdentity();
/*  create 5x5 pixel picking region near cursor location */
   gluPickMatrix((GLdouble) mouse.x, (GLdouble) (viewport[3] - mouse.y),
                 5.0, 5.0, viewport);Cpp代码  
//这个会促使上面那个PickMatrix失效!!切记~~~~
glLoadIdentity(); // 重置当前指定的矩阵为单位矩阵   
   gluPerspective                           // 设置透视图   
        ( 53.0f,                            // 透视角设置为 90 度   
          (GLfloat)Width/(GLfloat)Height,   // 窗口的宽与高比   
          0.1f,                             // 视野透视深度:近点1.0f   
          3000.0f                           // 视野透视深度:始点0.1f远点1000.0f   
        );</SPAN>   
   rects(GL_SELECT);   
   glPopMatrix();   
   glFlush();   
   hits = glRenderMode(GL_RENDER);   
   processPicks(hits, selectBuf);   
   int j;   
   glGetIntegerv(GL_NAME_STACK_DEPTH,&j);   
   int m;   
}   
void OpenGL::rects(GLenum mode)   
{   
if (mode == GL_SELECT)   
    glLoadName(30);   
    glPushMatrix();   
    glTranslatef(-2,-1,-15);   
    glColor3f(0.6f,0.2f,0.9f);   
//  glRecti(0,0,100,100);   
    Font->c3dtext("离开游戏",hFont,0.25f);   
    glPopMatrix();   
  /* glBegin(GL_QUADS);  
   glColor3f(1.0, 1.0, 0.0);  
   glVertex3i(2, 0, 0);  
   glVertex3i(2, 6, 0);  
   glVertex3i(6, 6, 0);  
   glVertex3i(6, 0, 0);  
   glEnd();  
   */  
   if (mode == GL_SELECT)   
      glLoadName(20);   
   glPushMatrix();   
      glTranslatef(-2,1,-15);   
      glColor3f(0.6f,0.2f,0.9f);   
      Font->c3dtext("开始游戏",hFont,0.25f);   
   glPopMatrix();   
      /*glBegin(GL_QUADS);  
   glColor3f(0.0, 1.0, 1.0);  
   glVertex3i(3, 2, -1);  
   glVertex3i(3, 8, -1);  
   glVertex3i(8, 8, -1);  
   glVertex3i(8, 2, -1);  
   glEnd();  
   */  
   /*  
   if (mode == GL_SELECT)  
      glLoadName(3);  
   glBegin(GL_QUADS);  
   glColor3f(1.0, 0.0, 1.0);  
   glVertex3i(0, 2, -2);  
   glVertex3i(0, 7, -2);  
   glVertex3i(5, 7, -2);  
   glVertex3i(5, 2, -2);  
   glEnd();  
    /*  
   if(mode==GL_SELECT)  
    glPushName(1);  
    glPushMatrix();  
//  glTranslatef(0,0,-Height);  
    glColor3f(0.0,1.0,0.0);  
//  glPushName(20);   
    glTranslatef(-2,-1,-15);  
//  glColor3f(0.6f,0.2f,0.9f);  
//  glRecti(0,0,100,100);  
    Font->c3dtext("离开游戏",hFont,0.25f);  
    /*glBegin(GL_QUADS);  
        glTexCoord2f(0.0f, 0.0f); glVertex3f(-Width/2, -Height/2,  -0.0f);// 前  
        glTexCoord2f(1.0f, 0.0f); glVertex3f( Width/2,-Height/2,  -0.0f);  
        glTexCoord2f(1.0f, 1.0f); glVertex3f( Width/2,  Height/2,  -0.0f);  
        glTexCoord2f(0.0f, 1.0f); glVertex3f(-Width/2,  Height/2,  -0.0f);  
    glEnd();  
 
 //   glColor3f(1.0,0.0,0.0);  
//  glTranslatef(-2,1,-15);  
    glColor3f(0.6f,0.2f,0.9f);  
//  Font->c3dtext("开始游戏",hFont,0.25f);  
    glPopMatrix();        
   if(mode==GL_SELECT)  
    glPushName(2);  
    glPushMatrix();  
   // glTranslatef(0,0,-Height);  
//  glColor3f(0.0,1.0,0.0);  
//  glPushName(20);   
    glTranslatef(-2,-1,-15);  
    glColor3f(0.6f,0.2f,0.9f);  
//  glRecti(0,0,100,100);  
    Font->c3dtext("离开游戏",hFont,0.25f);  
 
    glBegin(GL_QUADS);  
        glTexCoord2f(0.0f, 0.0f); glVertex3f(-Width/2, -Height/2,  -0.0f);// 前  
        glTexCoord2f(1.0f, 0.0f); glVertex3f( Width/2,-Height/2,  -0.0f);  
        glTexCoord2f(1.0f, 1.0f); glVertex3f( Width/2,  Height/2,  -0.0f);  
        glTexCoord2f(0.0f, 1.0f); glVertex3f(-Width/2,  Height/2,  -0.0f);  
    glEnd();  
 
    glPopMatrix();  
    */  
}   
void OpenGL::processPicks(GLint hits,GLuint buffer[]){   
    rects(GL_RENDER);   
    cout<<hits<<endl;   
 unsigned int i, j;   
   GLuint names, *ptr;   
   printf("hits = %d\n", hits);   
   ptr = (GLuint *) buffer;   
   for (i = 0; i < hits; i++) {     
      names = *ptr;   
      printf(" number of names for hit = %d\n", names); ptr++;   
      printf("  z1 is %g;", (float) *ptr/0x7fffffff); ptr++;   
      printf(" z2 is %g\n", (float) *ptr/0x7fffffff); ptr++;   
      printf("   the name is ");   
      for (j = 0; j < names; j++) {     
         printf("%d ", *ptr); ptr++;   
      }   
      printf("\n");   
   }   
}  

//这个会促使上面那个PickMatrix失效!!切记~~~~
   //glLoadIdentity();						// 重置当前指定的矩阵为单位矩阵
   gluPerspective							// 设置透视图
		( 53.0f,							// 透视角设置为 90 度
		  (GLfloat)Width/(GLfloat)Height,	// 窗口的宽与高比
		  0.1f,								// 视野透视深度:近点1.0f
		  3000.0f							// 视野透视深度:始点0.1f远点1000.0f
		);
   rects(GL_SELECT);
   glPopMatrix();
   glFlush();
   hits = glRenderMode(GL_RENDER);
   processPicks(hits, selectBuf);
   int j;
   glGetIntegerv(GL_NAME_STACK_DEPTH,&j);
   int m;
}
void OpenGL::rects(GLenum mode)
{
if (mode == GL_SELECT)
    glLoadName(30);
    glPushMatrix();
	glTranslatef(-2,-1,-15);
	glColor3f(0.6f,0.2f,0.9f);
//	glRecti(0,0,100,100);
	Font->c3dtext("离开游戏",hFont,0.25f);
	glPopMatrix();
  /* glBegin(GL_QUADS);
   glColor3f(1.0, 1.0, 0.0);
   glVertex3i(2, 0, 0);
   glVertex3i(2, 6, 0);
   glVertex3i(6, 6, 0);
   glVertex3i(6, 0, 0);
   glEnd();
   */
   if (mode == GL_SELECT)
      glLoadName(20);
   glPushMatrix();
   	  glTranslatef(-2,1,-15);
	  glColor3f(0.6f,0.2f,0.9f);
	  Font->c3dtext("开始游戏",hFont,0.25f);
   glPopMatrix();
	  /*glBegin(GL_QUADS);
   glColor3f(0.0, 1.0, 1.0);
   glVertex3i(3, 2, -1);
   glVertex3i(3, 8, -1);
   glVertex3i(8, 8, -1);
   glVertex3i(8, 2, -1);
   glEnd();
   */
   /*
   if (mode == GL_SELECT)
      glLoadName(3);
   glBegin(GL_QUADS);
   glColor3f(1.0, 0.0, 1.0);
   glVertex3i(0, 2, -2);
   glVertex3i(0, 7, -2);
   glVertex3i(5, 7, -2);
   glVertex3i(5, 2, -2);
   glEnd();
	/*
   if(mode==GL_SELECT)
	glPushName(1);
  	glPushMatrix();
//	glTranslatef(0,0,-Height);
    glColor3f(0.0,1.0,0.0);
//	glPushName(20); 
	glTranslatef(-2,-1,-15);
//	glColor3f(0.6f,0.2f,0.9f);
//	glRecti(0,0,100,100);
	Font->c3dtext("离开游戏",hFont,0.25f);
	/*glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-Width/2, -Height/2,  -0.0f);// 前
		glTexCoord2f(1.0f, 0.0f); glVertex3f( Width/2,-Height/2,  -0.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f( Width/2,  Height/2,  -0.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-Width/2,  Height/2,  -0.0f);
    glEnd();

 //   glColor3f(1.0,0.0,0.0);
//	glTranslatef(-2,1,-15);
	glColor3f(0.6f,0.2f,0.9f);
//	Font->c3dtext("开始游戏",hFont,0.25f);
	glPopMatrix();		
   if(mode==GL_SELECT)
	glPushName(2);
   	glPushMatrix();
   // glTranslatef(0,0,-Height);
//	glColor3f(0.0,1.0,0.0);
//	glPushName(20); 
	glTranslatef(-2,-1,-15);
	glColor3f(0.6f,0.2f,0.9f);
//	glRecti(0,0,100,100);
	Font->c3dtext("离开游戏",hFont,0.25f);

	glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-Width/2, -Height/2,  -0.0f);// 前
		glTexCoord2f(1.0f, 0.0f); glVertex3f( Width/2,-Height/2,  -0.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f( Width/2,  Height/2,  -0.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-Width/2,  Height/2,  -0.0f);
    glEnd();

	glPopMatrix();
	*/
}
void OpenGL::processPicks(GLint hits,GLuint buffer[]){
	rects(GL_RENDER);
	cout<<hits<<endl;
 unsigned int i, j;
   GLuint names, *ptr;
   printf("hits = %d\n", hits);
   ptr = (GLuint *) buffer;
   for (i = 0; i < hits; i++) {  
      names = *ptr;
      printf(" number of names for hit = %d\n", names); ptr++;
      printf("  z1 is %g;", (float) *ptr/0x7fffffff); ptr++;
      printf(" z2 is %g\n", (float) *ptr/0x7fffffff); ptr++;
      printf("   the name is ");
      for (j = 0; j < names; j++) {  
         printf("%d ", *ptr); ptr++;
      }
      printf("\n");
   }
} 


相关文章推荐

opengl 键盘和鼠标

opengl 键盘和鼠标
  • cloud95
  • cloud95
  • 2014年07月18日 11:07
  • 2352

话说,终于搞定了在一套ubuntu里既能编译px4又能编译apm的问题

这一篇算是对之前环境准备的总结,因为我发现即使是很熟练的开发者在选择开发环境的时候其实也蛮苦恼的…所以把现阶段部署开发环境的一些新的写出来,至少在这个时间点,现在大约是17年近中旬吧,按照我说的做,一...

奋战一周,终于基本搞定了跨平台内联汇编的选择和其他各种问题.分享一下

其实本来我的需求挺简单的,我甚至不能说自己是个程序员,各种类库,API什么的我也只是简单了解过boost,ZThread之类常用的,连怎么在windows里用MFC画个窗体都不知道....我主要是个搞...
  • menzi11
  • menzi11
  • 2012年03月19日 05:30
  • 1461

终于搞定被客户投诉的问题

之前在用户有新需求的情况下,改了不少代码,最后升级上去,一个月后,进行月度检查时,发现有一半部分数据并没有插入数据库。说道这里你可能有点晕了,我解释解释。 整个结构分为总循环控制程序,出账程序,包括...

主机+虚拟机+开发板互相ping通(折腾很久的问题。。终于搞定了。。) .

以下是我的IP设置: 本机本地连接 IP: 192.168.1.23 子网掩码:255.255.255.0 默认网关:192.168.1.1 首选DNS:202.96.128.86 开发板地...

终于搞定Linux的NAT即时生效问题

引:超长的前言Linux的NAT不能及时生效,因为它是基于ip_conntrack的,如果在NAT的iptables规则添加之前,此流的数据包已经绑定了一个ip_conntrack,那么该NAT规则就...
  • dog250
  • dog250
  • 2013年12月29日 15:04
  • 9363

韩顺平_轻松搞定网页设计(html+css+javascript)_第30讲_类和对象细节_创建对象的几种方式_js对象内存分析_学习笔记_源代码图解_PPT文档整理

文西马龙:http://blog.csdn.net/wenximalong/ 对象——function特别说明 1.在js中一切都是对象 2.类(原型对象)其实也是对象,它实际上是Functio...

OpenGL入门学习之三——绘制几何图形的一些细节问题

1、关于点 点的大小默认为1个像素,但也可以改变之。改变的命令为glPointSize,其函数原型如下: void glPointSize(GLfloat size); size必须大于0.0f...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:OpenGL鼠标拾取的实现:终于搞定(包括细节问题)
举报原因:
原因补充:

(最多只允许输入30个字)