2005-12-03 10:36:28 +01:00
|
|
|
#pragma once
|
|
|
|
/****************************************************************************
|
|
|
|
* VCGLib o o *
|
|
|
|
* Visual and Computer Graphics Library o o *
|
|
|
|
* _ O _ *
|
|
|
|
* Copyright(C) 2004 \/)\/ *
|
|
|
|
* Visual Computing Lab /\/| *
|
|
|
|
* ISTI - Italian National Research Council | *
|
|
|
|
* \ *
|
|
|
|
* All rights reserved. *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
|
|
|
|
* for more details. *
|
|
|
|
* *
|
|
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
|
|
|
This file contains two function providing the standard way to do picking using opendgl:
|
|
|
|
- using the SELECT mode (first function)
|
|
|
|
- using the depth buffer and gluUnproject (second function)
|
|
|
|
|
|
|
|
History
|
|
|
|
$Log: not supported by cvs2svn $
|
2007-05-28 09:35:21 +02:00
|
|
|
Revision 1.5 2007/05/21 13:22:40 cignoni
|
|
|
|
Corrected gcc compiling issues
|
|
|
|
|
2007-05-21 15:22:40 +02:00
|
|
|
Revision 1.4 2006/10/27 08:55:15 fiorin
|
|
|
|
Added type cast (in order to remove warnings)
|
|
|
|
|
2006-10-27 10:55:15 +02:00
|
|
|
Revision 1.3 2006/02/28 13:25:48 ponchio
|
|
|
|
for(ii... -> for(int ii
|
|
|
|
|
2006-02-28 14:25:48 +01:00
|
|
|
Revision 1.2 2006/02/13 13:06:34 cignoni
|
|
|
|
Removed glut. Added ifdef guards and namespace.
|
|
|
|
Added bool return value to the pick function
|
|
|
|
|
2006-02-13 14:06:34 +01:00
|
|
|
Revision 1.1 2005/12/03 09:36:28 ganovelli
|
|
|
|
*** empty log message ***
|
|
|
|
|
2005-12-03 10:36:28 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2006-02-13 14:06:34 +01:00
|
|
|
#ifndef WRAP_GL_PICKING_H
|
|
|
|
#define WRAP_GL_PICKING_H
|
2005-12-03 10:36:28 +01:00
|
|
|
|
|
|
|
#include <algorithm>
|
2006-02-13 14:06:34 +01:00
|
|
|
namespace vcg
|
|
|
|
{
|
2005-12-03 10:36:28 +01:00
|
|
|
|
|
|
|
template <class TO_PICK_CONT_TYPE>
|
|
|
|
int Pick( const int & x, const int &y,
|
|
|
|
TO_PICK_CONT_TYPE &m,
|
|
|
|
std::vector<typename TO_PICK_CONT_TYPE::value_type*> &result,
|
|
|
|
void (draw_func)(typename TO_PICK_CONT_TYPE::value_type &),
|
|
|
|
int width=4,
|
|
|
|
int height=4)
|
|
|
|
{
|
|
|
|
result.clear();
|
|
|
|
long hits;
|
2006-10-27 10:55:15 +02:00
|
|
|
int sz = int(m.size())*5;
|
2007-05-21 15:22:40 +02:00
|
|
|
GLuint *selectBuf =new GLuint[sz];
|
2005-12-03 10:36:28 +01:00
|
|
|
glSelectBuffer(sz, selectBuf);
|
|
|
|
glRenderMode(GL_SELECT);
|
|
|
|
glInitNames();
|
|
|
|
|
|
|
|
/* Because LoadName() won't work with no names on the stack */
|
|
|
|
glPushName(-1);
|
|
|
|
double mp[16];
|
|
|
|
|
2007-05-21 15:22:40 +02:00
|
|
|
GLint viewport[4];
|
2005-12-03 10:36:28 +01:00
|
|
|
glGetIntegerv(GL_VIEWPORT,viewport);
|
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glGetDoublev(GL_PROJECTION_MATRIX ,mp);
|
|
|
|
glPushMatrix();
|
|
|
|
glLoadIdentity();
|
|
|
|
//gluPickMatrix(x, viewport[3]-y, 4, 4, viewport);
|
|
|
|
gluPickMatrix(x, y, width, height, viewport);
|
|
|
|
glMultMatrixd(mp);
|
|
|
|
|
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
glPushMatrix();
|
|
|
|
int cnt=0;
|
|
|
|
typename TO_PICK_CONT_TYPE::iterator ei;
|
|
|
|
for(ei=m.begin();ei!=m.end();++ei)
|
|
|
|
{
|
|
|
|
|
|
|
|
glLoadName(cnt);
|
|
|
|
draw_func(*ei);
|
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
|
|
|
|
glPopMatrix();
|
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glPopMatrix();
|
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
hits = glRenderMode(GL_RENDER);
|
|
|
|
|
|
|
|
if (hits <= 0) return 0;
|
|
|
|
std::vector< std::pair<double,unsigned int> > H;
|
|
|
|
for(int ii=0;ii<hits;ii++){
|
|
|
|
H.push_back( std::pair<double,unsigned int>(selectBuf[ii*4+1]/4294967295.0,selectBuf[ii*4+3]));
|
|
|
|
}
|
|
|
|
std::sort(H.begin(),H.end());
|
|
|
|
|
|
|
|
result.resize(H.size());
|
2006-02-28 14:25:48 +01:00
|
|
|
for(int ii=0;ii<hits;ii++){
|
2005-12-03 10:36:28 +01:00
|
|
|
typename TO_PICK_CONT_TYPE::iterator ei=m.begin();
|
2007-05-28 09:35:21 +02:00
|
|
|
std::advance(ei ,H[ii].second);
|
2005-12-03 10:36:28 +01:00
|
|
|
result[ii]=&*ei;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete [] selectBuf;
|
2006-10-27 10:55:15 +02:00
|
|
|
return int(result.size());
|
2005-12-03 10:36:28 +01:00
|
|
|
}
|
|
|
|
|
2006-02-13 14:06:34 +01:00
|
|
|
// 10/2/06 Slightly changed the interface.
|
|
|
|
// Return value is used to determine if the picked point was against the far plane
|
|
|
|
// (and therefore nothing was picked)
|
2005-12-03 10:36:28 +01:00
|
|
|
template <class PointType>
|
2006-02-13 14:06:34 +01:00
|
|
|
bool Pick(const int & x, const int &y, PointType &pp){
|
2005-12-03 10:36:28 +01:00
|
|
|
double res[3];
|
|
|
|
GLdouble mm[16],pm[16]; GLint vp[4];
|
|
|
|
glGetDoublev(GL_MODELVIEW_MATRIX,mm);
|
|
|
|
glGetDoublev(GL_PROJECTION_MATRIX,pm);
|
|
|
|
glGetIntegerv(GL_VIEWPORT,vp);
|
|
|
|
|
|
|
|
float pix;
|
|
|
|
glReadPixels(x,y,1,1,GL_DEPTH_COMPONENT,GL_FLOAT,&pix);
|
2006-02-13 14:06:34 +01:00
|
|
|
GLfloat depthrange[2]={0,0};
|
|
|
|
glGetFloatv(GL_DEPTH_RANGE,depthrange);
|
|
|
|
if(pix==depthrange[1]) return false;
|
2005-12-03 10:36:28 +01:00
|
|
|
gluUnProject(x,y,pix,mm,pm,vp,&res[0],&res[1],&res[2]);
|
2006-02-13 14:06:34 +01:00
|
|
|
pp=PointType (res[0],res[1],res[2]);
|
|
|
|
return true;
|
2005-12-03 10:36:28 +01:00
|
|
|
}
|
|
|
|
|
2006-02-13 14:06:34 +01:00
|
|
|
} // end namespace
|
|
|
|
|
|
|
|
#endif
|