diff --git a/wrap/gl/pick.h b/wrap/gl/pick.h index f1b5f28c..8d39b8b5 100644 --- a/wrap/gl/pick.h +++ b/wrap/gl/pick.h @@ -31,6 +31,8 @@ namespace vcg{ template class GLPickTri { + typedef typename MESH_TYPE::ScalarType ScalarType; + typedef typename MESH_TYPE::CoordType CoordType; typedef typename MESH_TYPE::FaceIterator FaceIterator; typedef typename MESH_TYPE::VertexIterator VertexIterator; typedef typename MESH_TYPE::FacePointer FacePointer; @@ -39,17 +41,17 @@ class GLPickTri private: - static Point3f Proj(const Eigen::Matrix4f &M, const float * viewport, const Point3f &p) + static CoordType Proj(const Eigen::Matrix &M, const ScalarType * viewport, const CoordType &p) { - const float vx=viewport[0]; - const float vy=viewport[1]; - const float vw2=viewport[2]/2.0f; - const float vh2=viewport[3]/2.0f; - Eigen::Vector4f vp(p[0],p[1],p[2],1.0f); - Eigen::Vector4f vpp = M*vp; - Eigen::Vector4f ndc = vpp/vpp[3]; + const ScalarType vx=viewport[0]; + const ScalarType vy=viewport[1]; + const ScalarType vw2=viewport[2]/ScalarType(2.0); + const ScalarType vh2=viewport[3]/ScalarType(2.0); + Eigen::Matrix vp(p[0],p[1],p[2],ScalarType(1.0)); + Eigen::Matrix vpp = M*vp; + Eigen::Matrix ndc = vpp/vpp[3]; - Point3f sc( + CoordType sc( vw2*ndc[0] + vx+vw2, vh2*ndc[1] + vy+vh2, ndc[2] @@ -58,16 +60,16 @@ private: return sc; } - static void FillProjectedVector(MESH_TYPE &m, std::vector &pVec, const Eigen::Matrix4f &M, const float * viewportF) + static void FillProjectedVector(MESH_TYPE &m, std::vector &pVec, const Eigen::Matrix &M, const ScalarType * viewportF) { pVec.resize(m.vert.size()); for(size_t i=0;i &M, ScalarType *viewportF) { Eigen::Matrix4d mp,mm; @@ -78,16 +80,16 @@ private: glGetDoublev(GL_PROJECTION_MATRIX, mp.data()); glGetDoublev(GL_MODELVIEW_MATRIX, mm.data()); - M = (mp*mm).cast(); + M = (mp*mm).cast(); } // compute a bbox in Device Coordinate (with the z without the near far normalization and ranged in -1 1) - static Box3f ComputeDCBox(int x, int y, int width, int height) + static Box3 ComputeDCBox(int x, int y, int width, int height) { - Box3f bb; + Box3 bb; bb.SetNull(); - bb.Add(Point3f(x-width/2.0f,y-height/2.0f,-1.0f)); - bb.Add(Point3f(x+width/2.0f,y+height/2.0f, 1.0f)); + bb.Add(CoordType(x-width/ScalarType(2.0),y-height/ScalarType(2.0),ScalarType(-1.0))); + bb.Add(CoordType(x+width/ScalarType(2.0),y+height/ScalarType(2.0), ScalarType(1.0))); return bb; } @@ -95,17 +97,17 @@ public: static bool PickClosestFace(int x, int y, MESH_TYPE &m, FacePointer &fp,int width=4, int height=4) { - Eigen::Matrix4f M; - float viewportF[4]; + Eigen::Matrix M; + ScalarType viewportF[4]; glGetMatrixAndViewport(M,viewportF); - Box3f reg=ComputeDCBox(x,y,width,height); + Box3 reg=ComputeDCBox(x,y,width,height); - float bzmin = std::numeric_limits::max(); + ScalarType bzmin = std::numeric_limits::max(); fp=0; for(size_t i=0;i M; + ScalarType viewportF[4]; glGetMatrixAndViewport(M,viewportF); - float bzmin = std::numeric_limits::max(); + ScalarType bzmin = std::numeric_limits::max(); vp=0; - Box3f reg=ComputeDCBox(x,y,width,height); + Box3 reg=ComputeDCBox(x,y,width,height); for(size_t i=0;i &result, int width=4, int height=4) { result.clear(); - static Eigen::Matrix4f lastM; + static Eigen::Matrix lastM; static MESH_TYPE *lastm=0; - static std::vector pVec; + static std::vector pVec; - Eigen::Matrix4f M; - float viewportF[4]; + Eigen::Matrix M; + ScalarType viewportF[4]; glGetMatrixAndViewport(M,viewportF); - Box3f reg =ComputeDCBox(x,y,width,height); + Box3 reg =ComputeDCBox(x,y,width,height); if(M!=lastM || &m != lastm) { @@ -168,17 +170,17 @@ public: static int PickFace(int x, int y, MESH_TYPE &m, std::vector &result, int width=4, int height=4) { - static Eigen::Matrix4f lastM; + static Eigen::Matrix lastM; static MESH_TYPE *lastm=0; - static std::vector pVec; + static std::vector pVec; - float viewportF[4]; - Eigen::Matrix4f M; + ScalarType viewportF[4]; + Eigen::Matrix M; glGetMatrixAndViewport(M,viewportF); result.clear(); - Box3f reg; - reg.Add(Point3f(x-width/2.0f,y-height/2.0f,-1.0f)); - reg.Add(Point3f(x+width/2.0f,y+height/2.0f,1.0f)); + Box3 reg; + reg.Add(CoordType(x-width/ScalarType(2.0),y-height/ScalarType(2.0),ScalarType(-1.0))); + reg.Add(CoordType(x+width/ScalarType(2.0),y+height/ScalarType(2.0),ScalarType(1.0))); if(M!=lastM || &m != lastm) { @@ -189,9 +191,9 @@ public: for(size_t i=0;i-1.0f) && (p1[2]>-1.0f) && (p2[2]>-1.0f) && IntersectionTriangleBox(reg,p0,p1,p2)) result.push_back(&m.face[i]); } @@ -202,27 +204,30 @@ public: // Visibility is computed according to the current depth buffer. static int PickVisibleFace(int x, int y, MESH_TYPE &m, std::vector &resultZ, int width=4, int height=4) { - float vp[4]; - Eigen::Matrix4f M; + ScalarType vp[4]; + Eigen::Matrix M; glGetMatrixAndViewport(M,vp); int screenW = (int)(vp[2]-vp[0]); int screenH = (int)(vp[3]-vp[1]); - GLfloat *buffer = new GLfloat[screenW*screenH]; - glReadPixels(vp[0],vp[1],vp[2],vp[3],GL_DEPTH_COMPONENT,GL_FLOAT,buffer); - + GL_TYPE::ScalarType *buffer = new GL_TYPE::ScalarType[screenW*screenH]; + + //I'm not sure glReadPixels can accept GL_DOUBLE tag + //GLenum err = glGetError(); + glReadPixels(vp[0],vp[1],vp[2],vp[3],GL_DEPTH_COMPONENT,GL_TYPE::SCALAR(),buffer); + //err = glGetError(); std::vector result; PickFace(x,y,m,result,width,height); - float LocalEpsilon = 0.001f; + ScalarType LocalEpsilon(0.001); for(size_t i =0;i=0 && p[0]=0 && p[1]= (p[2]+1.0)/2.0f) + if(bufZ + LocalEpsilon >= ScalarType(p[2]+1.0)/2.0) resultZ.push_back(result[i]); } } @@ -248,20 +253,25 @@ public: int screenW = vp[2]-vp[0]; int screenH = vp[3]-vp[1]; - GLfloat *buffer = new GLfloat[screenW*screenH]; - glReadPixels(vp[0],vp[1],vp[2],vp[3],GL_DEPTH_COMPONENT,GL_FLOAT,buffer); + + GL_TYPE::ScalarType *buffer = new GL_TYPE::ScalarType[screenW*screenH]; + + //I'm not sure glReadPixels can accept GL_DOUBLE tag + //GLenum err = glGetError(); + glReadPixels(vp[0],vp[1],vp[2],vp[3],GL_DEPTH_COMPONENT,GL_TYPE::SCALAR(),buffer); + //err = glGetError(); std::vector result; OldPickFace(x,y,m,result,width,height,sorted); - float LocalEpsilon = 0.001f; - for(size_t i =0;i=0 && tx=0 && ty= tz) resultZ.push_back(result[i]);