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鼠标拾取

http://blog.sina.com.cn/liuzhongtu http://www.cnblogs.com/Yuri/ 本文源自于一篇英文《OpenGL Picking Made Easy》,...
  • sangxiansheng
  • sangxiansheng
  • 2010年04月01日 10:28
  • 7145

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

void OpenGL::pickRects(){ /* GetCursorPos(&mouse); ScreenToClient(hWnd,&mouse); GLuint pickBuff...
  • daojin505
  • daojin505
  • 2017年08月08日 14:53
  • 363

OpenGL选择与拾取GL_SELECT 附源码

讲述在OpenGL下,如何选择和拾取(selecting and picking)三维物体,这里使用3D模型中的obj格式的模型。...
  • Mahabharata_
  • Mahabharata_
  • 2016年12月13日 13:19
  • 2436

opengl中鼠标拾取操作分析

在进行Opengl编程时,通常会进行一些交互式操作,其中的鼠标拾取技术可以实现对目标物体的选取和判断,为开发者和用户带来便利。下面笔者就对这当中的一些核心代码进行总结和分析。      Opengl...
  • hlj184
  • hlj184
  • 2012年07月25日 11:14
  • 1349

OpenGL直线拾取

void WALL::pick(GLfloat xpos,GLfloat ypos) { xpos,ypos; GLint viewport[4]; GLdouble mvmatrix[16],pro...
  • donglei2007
  • donglei2007
  • 2013年05月22日 11:23
  • 700

理解OpenGL拾取模式(OpenGL Picking)

在用OpenGL进行图形编程的时候,通常要用鼠标进行交互操作,比如用鼠标点选择画面中的物体,我们称之为拾取(Picking),在网上看了很多OpenGL拾取的文章,但大多是只是介绍在OpenGL中如何...
  • zsc09_leaf
  • zsc09_leaf
  • 2011年09月17日 17:45
  • 9348

OpenGL 射线 拾取

简介: OpenGL中鼠标射线拾取,就是通过鼠标点击屏幕产生的坐标,通过变换到3D坐标空间,然后从摄像机原点与该3D空间坐标产生一个射线(ray),通过该射线与3D世界中的物体做相交检测,即可实现3...
  • jaccen
  • jaccen
  • 2017年10月25日 19:38
  • 176

OPENGL对象拾取

在用OpenGL进行图形编程的时候,通常要用鼠标进行交互操作,比如用鼠标点选择画面中的物体,我们称之为拾取(Picking),在网 上看了很多OpenGL拾取的文章,但大多是只是介绍在OpenGL中如...
  • jingyu7
  • jingyu7
  • 2015年03月18日 16:56
  • 969

OpenGL鼠标拾取

1.OpenGL自带拾取功能。glSelectBuffer等函数完成。   大每一本OpenGL入门的书都会讲到OpenGL的Select模式,它采用一种方法使得可以获取当前鼠标点选的几何图形。简单...
  • daojin505
  • daojin505
  • 2017年08月07日 10:26
  • 81

OpenGL中鼠标拾取

OpenGL中鼠标拾取的实现方式有很多种: 1、用glPushName,glLoadName,glSelectBuffer和glRenderMode等系列函数通过OpenGL的绘制管线实现; 2、...
  • lcy930822
  • lcy930822
  • 2012年11月24日 10:35
  • 452
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:OpenGL鼠标拾取的实现:终于搞定(包括细节问题)
举报原因:
原因补充:

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