| 1 |
#pragma once |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
#ifndef WRAP_GL_PICKING_H |
| 53 |
#define WRAP_GL_PICKING_H |
| 54 |
|
| 55 |
#include <algorithm> |
| 56 |
namespace vcg |
| 57 |
{ |
| 58 |
|
| 59 |
template <class TO_PICK_CONT_TYPE> |
| 60 |
int Pick( const int & x, const int &y, |
| 61 |
TO_PICK_CONT_TYPE &m, |
| 62 |
std::vector<typename TO_PICK_CONT_TYPE::value_type*> &result, |
| 63 |
void (draw_func)(typename TO_PICK_CONT_TYPE::value_type &), |
| 64 |
int width=4, |
| 65 |
int height=4) |
| 66 |
{ |
| 67 |
result.clear(); |
| 68 |
long hits; |
| 69 |
int sz = int(m.size())*5; |
| 70 |
GLuint *selectBuf =new GLuint[sz]; |
| 71 |
glSelectBuffer(sz, selectBuf); |
| 72 |
glRenderMode(GL_SELECT); |
| 73 |
glInitNames(); |
| 74 |
|
| 75 |
|
| 76 |
glPushName(-1); |
| 77 |
double mp[16]; |
| 78 |
|
| 79 |
GLint viewport[4]; |
| 80 |
glGetIntegerv(GL_VIEWPORT,viewport); |
| 81 |
glMatrixMode(GL_PROJECTION); |
| 82 |
glGetDoublev(GL_PROJECTION_MATRIX ,mp); |
| 83 |
glPushMatrix(); |
| 84 |
glLoadIdentity(); |
| 85 |
|
| 86 |
gluPickMatrix(x, y, width, height, viewport); |
| 87 |
glMultMatrixd(mp); |
| 88 |
|
| 89 |
glMatrixMode(GL_MODELVIEW); |
| 90 |
glPushMatrix(); |
| 91 |
int cnt=0; |
| 92 |
typename TO_PICK_CONT_TYPE::iterator ei; |
| 93 |
for(ei=m.begin();ei!=m.end();++ei) |
| 94 |
{ |
| 95 |
|
| 96 |
glLoadName(cnt); |
| 97 |
draw_func(*ei); |
| 98 |
cnt++; |
| 99 |
} |
| 100 |
|
| 101 |
glPopMatrix(); |
| 102 |
glMatrixMode(GL_PROJECTION); |
| 103 |
glPopMatrix(); |
| 104 |
glMatrixMode(GL_MODELVIEW); |
| 105 |
hits = glRenderMode(GL_RENDER); |
| 106 |
|
| 107 |
if (hits <= 0) return 0; |
| 108 |
std::vector< std::pair<double,unsigned int> > H; |
| 109 |
for(int ii=0;ii<hits;ii++){ |
| 110 |
H.push_back( std::pair<double,unsigned int>(selectBuf[ii*4+1]/4294967295.0,selectBuf[ii*4+3])); |
| 111 |
} |
| 112 |
std::sort(H.begin(),H.end()); |
| 113 |
|
| 114 |
result.resize(H.size()); |
| 115 |
for(int ii=0;ii<hits;ii++){ |
| 116 |
typename TO_PICK_CONT_TYPE::iterator ei=m.begin(); |
| 117 |
std::advance(ei ,H[ii].second); |
| 118 |
result[ii]=&*ei; |
| 119 |
} |
| 120 |
|
| 121 |
delete [] selectBuf; |
| 122 |
return int(result.size()); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
template <class PointType> |
| 129 |
bool Pick(const int & x, const int &y, PointType &pp){ |
| 130 |
double res[3]; |
| 131 |
GLdouble mm[16],pm[16]; GLint vp[4]; |
| 132 |
glGetDoublev(GL_MODELVIEW_MATRIX,mm); |
| 133 |
glGetDoublev(GL_PROJECTION_MATRIX,pm); |
| 134 |
glGetIntegerv(GL_VIEWPORT,vp); |
| 135 |
|
| 136 |
float pix; |
| 137 |
glReadPixels(x,y,1,1,GL_DEPTH_COMPONENT,GL_FLOAT,&pix); |
| 138 |
GLfloat depthrange[2]={0,0}; |
| 139 |
glGetFloatv(GL_DEPTH_RANGE,depthrange); |
| 140 |
if(pix==depthrange[1]) return false; |
| 141 |
gluUnProject(x,y,pix,mm,pm,vp,&res[0],&res[1],&res[2]); |
| 142 |
pp=PointType (res[0],res[1],res[2]); |
| 143 |
return true; |
| 144 |
} |
| 145 |
|
| 146 |
} |
| 147 |
|
| 148 |
#endif |