picking.h
Go to the documentation of this file.
00001 #pragma once
00002 /****************************************************************************
00003 * VCGLib                                                            o o     *
00004 * Visual and Computer Graphics Library                            o     o   *
00005 *                                                                _   O  _   *
00006 * Copyright(C) 2004                                                \/)\/    *
00007 * Visual Computing Lab                                            /\/|      *
00008 * ISTI - Italian National Research Council                           |      *
00009 *                                                                    \      *
00010 * All rights reserved.                                                      *
00011 *                                                                           *
00012 * This program is free software; you can redistribute it and/or modify      *
00013 * it under the terms of the GNU General Public License as published by      *
00014 * the Free Software Foundation; either version 2 of the License, or         *
00015 * (at your option) any later version.                                       *
00016 *                                                                           *
00017 * This program is distributed in the hope that it will be useful,           *
00018 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
00019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
00020 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
00021 * for more details.                                                         *
00022 *                                                                           *
00023 ****************************************************************************/
00024 /****************************************************************************
00025 This file contains two function providing the standard way to do picking using opendgl:
00026 - using the SELECT mode (first function)
00027 - using the depth buffer and gluUnproject (second function)
00028 
00029   History
00030 $Log: not supported by cvs2svn $
00031 Revision 1.5  2007/05/21 13:22:40  cignoni
00032 Corrected gcc compiling issues
00033 
00034 Revision 1.4  2006/10/27 08:55:15  fiorin
00035 Added type cast (in order to remove warnings)
00036 
00037 Revision 1.3  2006/02/28 13:25:48  ponchio
00038 for(ii... -> for(int ii
00039 
00040 Revision 1.2  2006/02/13 13:06:34  cignoni
00041 Removed glut. Added ifdef guards and namespace.
00042 Added bool return value to the pick function
00043 
00044 Revision 1.1  2005/12/03 09:36:28  ganovelli
00045 *** empty log message ***
00046 
00047 ****************************************************************************/
00048 
00049 #ifndef WRAP_GL_PICKING_H
00050 #define WRAP_GL_PICKING_H
00051 
00052 #include <algorithm>
00053 
00054 #ifndef GLU_VERSIONS
00055 #ifdef __APPLE__
00056 #include <OpenGL/glu.h>
00057 #else
00058 #ifdef _WIN32
00059   #include <windows.h>
00060 #endif
00061 #include <GL/glu.h>
00062 #endif
00063 #endif
00064 
00065 namespace vcg
00066 {
00067 
00068 template <class TO_PICK_CONT_TYPE>
00069 int Pick(       const int & x, const int &y,
00070                     TO_PICK_CONT_TYPE &m,
00071                     std::vector<typename TO_PICK_CONT_TYPE::value_type*> &result,
00072                     void (draw_func)(typename TO_PICK_CONT_TYPE::value_type &),
00073                     int width=4,
00074                     int height=4)
00075     {
00076         result.clear();
00077         long hits;
00078         int sz = int(m.size())*5;
00079         GLuint *selectBuf =new GLuint[sz];
00080         glSelectBuffer(sz, selectBuf);
00081         glRenderMode(GL_SELECT);
00082         glInitNames();
00083 
00084         /* Because LoadName() won't work with no names on the stack */
00085         glPushName(-1);
00086         double mp[16];
00087 
00088         GLint viewport[4];
00089         glGetIntegerv(GL_VIEWPORT,viewport);
00090         glPushAttrib(GL_TRANSFORM_BIT);
00091         glMatrixMode(GL_PROJECTION);
00092         glGetDoublev(GL_PROJECTION_MATRIX ,mp);
00093         glPushMatrix();
00094         glLoadIdentity();
00095         //gluPickMatrix(x, viewport[3]-y, 4, 4, viewport);
00096         gluPickMatrix(x, y, width, height, viewport);
00097         glMultMatrixd(mp);
00098 
00099         glMatrixMode(GL_MODELVIEW);
00100         glPushMatrix();
00101         int cnt=0;
00102         typename TO_PICK_CONT_TYPE::iterator  ei;
00103         for(ei=m.begin();ei!=m.end();++ei)
00104         {
00105 
00106                 glLoadName(cnt);
00107                 draw_func(*ei);
00108                 cnt++;
00109         }
00110 
00111         glPopMatrix();
00112         glMatrixMode(GL_PROJECTION);
00113         glPopMatrix();
00114         glMatrixMode(GL_MODELVIEW);
00115         hits = glRenderMode(GL_RENDER);
00116 
00117         if (hits <= 0)     return 0;
00118         std::vector< std::pair<double,unsigned int> > H;
00119         for(int ii=0;ii<hits;ii++){
00120             H.push_back( std::pair<double,unsigned int>(selectBuf[ii*4+1]/4294967295.0,selectBuf[ii*4+3]));
00121         }
00122         std::sort(H.begin(),H.end());
00123 
00124         result.resize(H.size());
00125         for(int ii=0;ii<hits;ii++){
00126             typename TO_PICK_CONT_TYPE::iterator ei=m.begin();
00127             std::advance(ei ,H[ii].second);
00128             result[ii]=&*ei;
00129         }
00130         glPopAttrib();
00131         delete [] selectBuf;
00132         return int(result.size());
00133     }
00134 
00135 // 10/2/06 Slightly changed the interface.
00136 // Return value is used to determine if the picked point was against the far plane
00137 // (and therefore nothing was picked)
00138 template <class PointType>
00139 bool Pick(const int & x, const int &y, PointType &pp){
00140     GLdouble res[3];
00141     GLdouble mm[16],pm[16]; GLint vp[4];
00142     glGetDoublev(GL_MODELVIEW_MATRIX,mm);
00143     glGetDoublev(GL_PROJECTION_MATRIX,pm);
00144     glGetIntegerv(GL_VIEWPORT,vp);
00145 
00146     GLfloat   pix;
00147     glReadPixels(x,y,1,1,GL_DEPTH_COMPONENT,GL_FLOAT,&pix);
00148   GLfloat depthrange[2]={0,0};
00149   glGetFloatv(GL_DEPTH_RANGE,depthrange);
00150   if(pix==depthrange[1]) return false;
00151     gluUnProject(x,y,pix,mm,pm,vp,&res[0],&res[1],&res[2]);
00152     pp=PointType (res[0],res[1],res[2]);
00153   return true;
00154     }
00155 
00156 } // end namespace
00157 
00158 #endif


shape_reconstruction
Author(s): Roberto Martín-Martín
autogenerated on Sat Jun 8 2019 18:34:10