Debian Codegraph @

vcg/wrap/gl/picking.h

raw symbols SCIP
1 #pragma once
2 /****************************************************************************
3 * VCGLib o o *
4 * Visual and Computer Graphics Library o o *
5 * _ O _ *
6 * Copyright(C) 2004 \/)\/ *
7 * Visual Computing Lab /\/| *
8 * ISTI - Italian National Research Council | *
9 * \ *
10 * All rights reserved. *
11 * *
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
16 * *
17 * This program is distributed in the hope that it will be useful, *
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
20 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
21 * for more details. *
22 * *
23 ****************************************************************************/
24 /****************************************************************************
25 This file contains two function providing the standard way to do picking using opendgl:
26 - using the SELECT mode (first function)
27 - using the depth buffer and gluUnproject (second function)
28
29 History
30 $Log: picking.h,v $
31 Revision 1.6 2007/05/28 07:35:21 fiorin
32 Added std:: prefix to advance call
33
34 Revision 1.5 2007/05/21 13:22:40 cignoni
35 Corrected gcc compiling issues
36
37 Revision 1.4 2006/10/27 08:55:15 fiorin
38 Added type cast (in order to remove warnings)
39
40 Revision 1.3 2006/02/28 13:25:48 ponchio
41 for(ii... -> for(int ii
42
43 Revision 1.2 2006/02/13 13:06:34 cignoni
44 Removed glut. Added ifdef guards and namespace.
45 Added bool return value to the pick function
46
47 Revision 1.1 2005/12/03 09:36:28 ganovelli
48 *** empty log message ***
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 /* Because LoadName() won't work with no names on the stack */
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 //gluPickMatrix(x, viewport[3]-y, 4, 4, viewport);
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 // 10/2/06 Slightly changed the interface.
126 // Return value is used to determine if the picked point was against the far plane
127 // (and therefore nothing was picked)
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 } // end namespace
147
148 #endif