2004-09-23 15:13:09 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* VCGLib o o *
|
|
|
|
* Visual and Computer Graphics Library o o *
|
|
|
|
* _ O _ *
|
2016-06-13 07:29:25 +02:00
|
|
|
* Copyright(C) 2004-2016 \/)\/ *
|
2004-09-23 15:13:09 +02:00
|
|
|
* Visual Computing Lab /\/| *
|
|
|
|
* ISTI - Italian National Research Council | *
|
|
|
|
* \ *
|
|
|
|
* All rights reserved. *
|
|
|
|
* *
|
2014-04-07 14:32:21 +02:00
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
2004-09-23 15:13:09 +02:00
|
|
|
* 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. *
|
|
|
|
* *
|
|
|
|
****************************************************************************/
|
2006-12-07 01:39:22 +01:00
|
|
|
|
2005-10-27 16:46:26 +02:00
|
|
|
#ifndef __PICK______H
|
|
|
|
#define __PICK______H
|
2004-09-23 15:13:09 +02:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <algorithm>
|
2016-07-31 11:50:24 +02:00
|
|
|
#include "gl_type_name.h"
|
|
|
|
|
2004-09-23 15:13:09 +02:00
|
|
|
namespace vcg{
|
|
|
|
|
2005-09-15 17:44:52 +02:00
|
|
|
template <class MESH_TYPE>
|
|
|
|
class GLPickTri
|
|
|
|
{
|
2014-08-09 12:00:09 +02:00
|
|
|
typedef typename MESH_TYPE::ScalarType ScalarType;
|
|
|
|
typedef typename MESH_TYPE::CoordType CoordType;
|
2014-04-08 11:37:44 +02:00
|
|
|
typedef typename MESH_TYPE::FaceIterator FaceIterator;
|
2010-02-26 17:20:15 +01:00
|
|
|
typedef typename MESH_TYPE::VertexIterator VertexIterator;
|
|
|
|
typedef typename MESH_TYPE::FacePointer FacePointer;
|
|
|
|
typedef typename MESH_TYPE::VertexPointer VertexPointer;
|
|
|
|
typedef typename MESH_TYPE::VertexType VertexType;
|
2005-09-15 17:44:52 +02:00
|
|
|
|
2014-04-08 11:37:44 +02:00
|
|
|
private:
|
|
|
|
|
2014-08-09 12:00:09 +02:00
|
|
|
static CoordType Proj(const Eigen::Matrix<ScalarType,4,4> &M, const ScalarType * viewport, const CoordType &p)
|
2014-04-08 11:37:44 +02:00
|
|
|
{
|
2014-08-09 12:00:09 +02:00
|
|
|
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<ScalarType,4,1> vp(p[0],p[1],p[2],ScalarType(1.0));
|
|
|
|
Eigen::Matrix<ScalarType,4,1> vpp = M*vp;
|
|
|
|
Eigen::Matrix<ScalarType,4,1> ndc = vpp/vpp[3];
|
|
|
|
|
|
|
|
CoordType sc(
|
2014-04-08 11:37:44 +02:00
|
|
|
vw2*ndc[0] + vx+vw2,
|
|
|
|
vh2*ndc[1] + vy+vh2,
|
|
|
|
ndc[2]
|
|
|
|
);
|
|
|
|
|
|
|
|
return sc;
|
|
|
|
}
|
|
|
|
|
2014-08-09 12:00:09 +02:00
|
|
|
static void FillProjectedVector(MESH_TYPE &m, std::vector<CoordType> &pVec, const Eigen::Matrix<ScalarType,4,4> &M, const ScalarType * viewportF)
|
2014-04-08 11:37:44 +02:00
|
|
|
{
|
|
|
|
pVec.resize(m.vert.size());
|
|
|
|
for(size_t i=0;i<m.vert.size();++i) if(!m.vert[i].IsD())
|
|
|
|
{
|
2014-08-09 12:00:09 +02:00
|
|
|
pVec[i] = Proj(M, viewportF,CoordType::Construct(m.vert[i].P()));
|
2014-04-08 11:37:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-09 12:00:09 +02:00
|
|
|
static void glGetMatrixAndViewport(Eigen::Matrix<ScalarType,4,4> &M, ScalarType *viewportF)
|
2014-04-08 11:37:44 +02:00
|
|
|
{
|
|
|
|
Eigen::Matrix4d mp,mm;
|
|
|
|
|
|
|
|
GLint viewport[4];
|
|
|
|
glGetIntegerv(GL_VIEWPORT,viewport);
|
|
|
|
for(int i=0;i<4;++i) viewportF[i]=viewport[i];
|
|
|
|
|
|
|
|
glGetDoublev(GL_PROJECTION_MATRIX, mp.data());
|
|
|
|
glGetDoublev(GL_MODELVIEW_MATRIX, mm.data());
|
|
|
|
|
2014-08-09 12:00:09 +02:00
|
|
|
M = (mp*mm).cast<ScalarType>();
|
2014-04-08 11:37:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// compute a bbox in Device Coordinate (with the z without the near far normalization and ranged in -1 1)
|
2014-08-09 12:00:09 +02:00
|
|
|
static Box3<ScalarType> ComputeDCBox(int x, int y, int width, int height)
|
2014-04-08 11:37:44 +02:00
|
|
|
{
|
2014-08-09 12:00:09 +02:00
|
|
|
Box3<ScalarType> bb;
|
2014-04-08 11:37:44 +02:00
|
|
|
bb.SetNull();
|
2014-08-09 12:00:09 +02:00
|
|
|
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)));
|
2014-04-08 11:37:44 +02:00
|
|
|
return bb;
|
|
|
|
}
|
|
|
|
|
2005-09-15 17:44:52 +02:00
|
|
|
public:
|
|
|
|
|
2014-04-08 11:37:44 +02:00
|
|
|
static bool PickClosestFace(int x, int y, MESH_TYPE &m, FacePointer &fp,int width=4, int height=4)
|
2014-04-07 14:32:21 +02:00
|
|
|
{
|
2014-08-09 12:00:09 +02:00
|
|
|
Eigen::Matrix<ScalarType,4,4> M;
|
|
|
|
ScalarType viewportF[4];
|
2014-04-08 11:37:44 +02:00
|
|
|
glGetMatrixAndViewport(M,viewportF);
|
2014-08-09 12:00:09 +02:00
|
|
|
Box3<ScalarType> reg=ComputeDCBox(x,y,width,height);
|
2014-04-08 11:37:44 +02:00
|
|
|
|
2014-08-09 12:00:09 +02:00
|
|
|
ScalarType bzmin = std::numeric_limits<ScalarType>::max();
|
2014-04-08 11:37:44 +02:00
|
|
|
fp=0;
|
|
|
|
for(size_t i=0;i<m.face.size();++i) if(!m.face[i].IsD())
|
2014-04-07 14:32:21 +02:00
|
|
|
{
|
2014-08-09 12:00:09 +02:00
|
|
|
CoordType bary = vcg::Barycenter(m.face[i]);
|
|
|
|
CoordType bz = Proj(M, viewportF,bary);
|
2014-04-08 11:37:44 +02:00
|
|
|
|
|
|
|
if(bz[2]<bzmin && reg.IsIn(bz))
|
|
|
|
{
|
|
|
|
bzmin=bz[2];
|
|
|
|
fp = &m.face[i];
|
|
|
|
}
|
2014-04-07 14:32:21 +02:00
|
|
|
}
|
2014-04-08 11:37:44 +02:00
|
|
|
return fp!=0;
|
2014-04-07 14:32:21 +02:00
|
|
|
}
|
|
|
|
|
2014-04-08 11:37:44 +02:00
|
|
|
static bool PickClosestVert(int x, int y, MESH_TYPE &m, VertexPointer &vp,int width=4, int height=4)
|
|
|
|
{
|
2014-08-09 12:00:09 +02:00
|
|
|
Eigen::Matrix<ScalarType,4,4> M;
|
|
|
|
ScalarType viewportF[4];
|
2014-04-08 11:37:44 +02:00
|
|
|
glGetMatrixAndViewport(M,viewportF);
|
2014-08-09 12:00:09 +02:00
|
|
|
ScalarType bzmin = std::numeric_limits<ScalarType>::max();
|
2014-04-08 11:37:44 +02:00
|
|
|
vp=0;
|
2014-04-07 16:49:07 +02:00
|
|
|
|
2014-08-09 12:00:09 +02:00
|
|
|
Box3<ScalarType> reg=ComputeDCBox(x,y,width,height);
|
2014-04-07 14:32:21 +02:00
|
|
|
|
2014-04-08 11:37:44 +02:00
|
|
|
for(size_t i=0;i<m.vert.size();++i) if(!m.vert[i].IsD())
|
|
|
|
{
|
2014-08-09 12:00:09 +02:00
|
|
|
CoordType bz = Proj(M, viewportF,m.vert[i].P());
|
2014-04-08 11:37:44 +02:00
|
|
|
if(bz[2]<bzmin && reg.IsIn(bz))
|
|
|
|
{
|
|
|
|
bzmin=bz[2];
|
|
|
|
vp = &m.vert[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return vp!=0;
|
|
|
|
}
|
2014-04-07 16:49:07 +02:00
|
|
|
|
2014-04-08 11:37:44 +02:00
|
|
|
static int PickVert(int x, int y, MESH_TYPE &m, std::vector<VertexPointer> &result, int width=4, int height=4)
|
2014-04-07 14:32:21 +02:00
|
|
|
{
|
|
|
|
result.clear();
|
2014-08-09 12:00:09 +02:00
|
|
|
static Eigen::Matrix<ScalarType,4,4> lastM;
|
2014-04-07 16:49:07 +02:00
|
|
|
static MESH_TYPE *lastm=0;
|
2014-08-09 12:00:09 +02:00
|
|
|
static std::vector<CoordType> pVec;
|
2014-04-07 16:49:07 +02:00
|
|
|
|
2014-08-09 12:00:09 +02:00
|
|
|
Eigen::Matrix<ScalarType,4,4> M;
|
|
|
|
ScalarType viewportF[4];
|
2014-04-08 11:37:44 +02:00
|
|
|
glGetMatrixAndViewport(M,viewportF);
|
2014-04-07 14:32:21 +02:00
|
|
|
|
2014-08-09 12:00:09 +02:00
|
|
|
Box3<ScalarType> reg =ComputeDCBox(x,y,width,height);
|
2014-04-07 16:49:07 +02:00
|
|
|
|
2016-11-21 12:00:56 +01:00
|
|
|
if ((M != lastM) || (&m != lastm) || (pVec.size() != m.VN()))
|
2014-04-07 14:32:21 +02:00
|
|
|
{
|
2014-04-07 16:49:07 +02:00
|
|
|
FillProjectedVector(m,pVec,M,viewportF);
|
|
|
|
lastM = M;
|
|
|
|
lastm = &m;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(size_t i=0;i<m.vert.size();++i) if(!m.vert[i].IsD())
|
|
|
|
{
|
2014-04-08 11:37:44 +02:00
|
|
|
if(reg.IsIn(pVec[i]))
|
|
|
|
result.push_back(&m.vert[i]);
|
2014-04-07 14:32:21 +02:00
|
|
|
}
|
|
|
|
return result.size();
|
|
|
|
}
|
|
|
|
|
2014-04-08 11:37:44 +02:00
|
|
|
static int PickFace(int x, int y, MESH_TYPE &m, std::vector<FacePointer> &result, int width=4, int height=4)
|
2010-02-26 17:20:15 +01:00
|
|
|
{
|
2014-08-09 12:00:09 +02:00
|
|
|
static Eigen::Matrix<ScalarType,4,4> lastM;
|
2014-04-07 16:49:07 +02:00
|
|
|
static MESH_TYPE *lastm=0;
|
2014-08-09 12:00:09 +02:00
|
|
|
static std::vector<CoordType> pVec;
|
2014-04-07 16:49:07 +02:00
|
|
|
|
2014-08-09 12:00:09 +02:00
|
|
|
ScalarType viewportF[4];
|
|
|
|
Eigen::Matrix<ScalarType,4,4> M;
|
2014-04-08 11:37:44 +02:00
|
|
|
glGetMatrixAndViewport(M,viewportF);
|
|
|
|
result.clear();
|
2014-08-09 12:00:09 +02:00
|
|
|
Box3<ScalarType> 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)));
|
2014-04-07 16:49:07 +02:00
|
|
|
|
2016-11-21 12:00:56 +01:00
|
|
|
if((M!=lastM) || (&m != lastm) || (pVec.size() != m.VN()))
|
2014-04-07 16:49:07 +02:00
|
|
|
{
|
|
|
|
FillProjectedVector(m,pVec,M,viewportF);
|
|
|
|
lastM = M;
|
|
|
|
lastm = &m;
|
|
|
|
}
|
|
|
|
|
2016-11-21 12:00:56 +01:00
|
|
|
for (size_t i = 0; i < m.face.size(); ++i)
|
|
|
|
{
|
|
|
|
if (!m.face[i].IsD())
|
|
|
|
{
|
|
|
|
const CoordType &p0 = pVec[tri::Index(m, m.face[i].V(0))];
|
|
|
|
const CoordType &p1 = pVec[tri::Index(m, m.face[i].V(1))];
|
|
|
|
const CoordType &p2 = pVec[tri::Index(m, m.face[i].V(2))];
|
|
|
|
if ((p0[2] > -1.0f) && (p1[2] > -1.0f) && (p2[2] > -1.0f) && IntersectionTriangleBox(reg, p0, p1, p2))
|
|
|
|
result.push_back(&m.face[i]);
|
|
|
|
}
|
|
|
|
}
|
2014-04-07 16:49:07 +02:00
|
|
|
return result.size();
|
|
|
|
}
|
2014-04-07 14:32:21 +02:00
|
|
|
|
2014-04-08 11:37:44 +02:00
|
|
|
// Same of above but it also assumes that you want only visible faces.
|
|
|
|
// Visibility is computed according to the current depth buffer.
|
|
|
|
static int PickVisibleFace(int x, int y, MESH_TYPE &m, std::vector<FacePointer> &resultZ, int width=4, int height=4)
|
|
|
|
{
|
2014-08-09 12:00:09 +02:00
|
|
|
ScalarType vp[4];
|
|
|
|
Eigen::Matrix<ScalarType,4,4> M;
|
2014-04-08 11:37:44 +02:00
|
|
|
glGetMatrixAndViewport(M,vp);
|
2014-04-07 14:32:21 +02:00
|
|
|
|
2014-04-08 11:37:44 +02:00
|
|
|
int screenW = (int)(vp[2]-vp[0]);
|
|
|
|
int screenH = (int)(vp[3]-vp[1]);
|
2014-04-07 14:32:21 +02:00
|
|
|
|
2014-08-28 10:59:24 +02:00
|
|
|
GL_TYPE_NM<Scalarm>::ScalarType *buffer = new GL_TYPE_NM<Scalarm>::ScalarType[screenW*screenH];
|
2014-11-12 01:22:24 +01:00
|
|
|
|
2014-08-09 12:00:09 +02:00
|
|
|
//I'm not sure glReadPixels can accept GL_DOUBLE tag
|
|
|
|
//GLenum err = glGetError();
|
2014-08-28 10:59:24 +02:00
|
|
|
glReadPixels(vp[0],vp[1],vp[2],vp[3],GL_DEPTH_COMPONENT,GL_TYPE_NM<Scalarm>::SCALAR(),buffer);
|
2014-08-09 12:00:09 +02:00
|
|
|
//err = glGetError();
|
2014-04-08 11:37:44 +02:00
|
|
|
std::vector<FacePointer> result;
|
|
|
|
PickFace(x,y,m,result,width,height);
|
2014-08-09 12:00:09 +02:00
|
|
|
ScalarType LocalEpsilon(0.001);
|
2014-04-08 11:37:44 +02:00
|
|
|
for(size_t i =0;i<result.size();++i)
|
|
|
|
{
|
2014-08-09 12:00:09 +02:00
|
|
|
CoordType p = Proj(M,vp,CoordType::Construct(Barycenter(*(result[i])))) ;
|
2014-04-08 11:37:44 +02:00
|
|
|
if(p[0] >=0 && p[0]<screenW && p[1] >=0 && p[1]<screenH)
|
|
|
|
{
|
2014-08-09 12:00:09 +02:00
|
|
|
ScalarType bufZ(buffer[int(p[0])+int(p[1])*screenW]);
|
2014-04-08 11:37:44 +02:00
|
|
|
//qDebug("face %i txyz (%f %f %f) bufz %f",i,tx,ty,tz,bufZ);
|
2014-08-09 12:00:09 +02:00
|
|
|
if(bufZ + LocalEpsilon >= ScalarType(p[2]+1.0)/2.0)
|
2014-04-08 11:37:44 +02:00
|
|
|
resultZ.push_back(result[i]);
|
|
|
|
}
|
2014-04-07 14:32:21 +02:00
|
|
|
}
|
2005-09-15 17:44:52 +02:00
|
|
|
|
2014-04-08 11:37:44 +02:00
|
|
|
delete [] buffer;
|
|
|
|
return resultZ.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-04-01 16:51:49 +02:00
|
|
|
// Same of above but it also assumes that you want only visible faces.
|
2014-04-07 14:32:21 +02:00
|
|
|
// Visibility is computed according to the current depth buffer.
|
2014-04-08 11:37:44 +02:00
|
|
|
static int OldPickFaceVisible(int x, int y, MESH_TYPE &m, std::vector<FacePointer> &resultZ, int width=4, int height=4, bool sorted=true)
|
2014-04-07 14:32:21 +02:00
|
|
|
{
|
|
|
|
// First step
|
|
|
|
|
|
|
|
double mm[16];
|
|
|
|
double mp[16];
|
|
|
|
GLint vp[4];
|
|
|
|
glGetIntegerv(GL_VIEWPORT,vp);
|
|
|
|
glGetDoublev(GL_MODELVIEW_MATRIX ,mm);
|
|
|
|
glGetDoublev(GL_PROJECTION_MATRIX ,mp);
|
|
|
|
int screenW = vp[2]-vp[0];
|
|
|
|
int screenH = vp[3]-vp[1];
|
|
|
|
|
2014-08-09 12:00:09 +02:00
|
|
|
|
2014-11-12 01:22:24 +01:00
|
|
|
GL_TYPE_NM<Scalarm>::ScalarType *buffer = new GL_TYPE_NM<Scalarm>::ScalarType[screenW*screenH];
|
2014-08-09 12:00:09 +02:00
|
|
|
|
|
|
|
//I'm not sure glReadPixels can accept GL_DOUBLE tag
|
|
|
|
//GLenum err = glGetError();
|
2014-11-12 01:22:24 +01:00
|
|
|
glReadPixels(vp[0],vp[1],vp[2],vp[3],GL_DEPTH_COMPONENT,GL_TYPE_NM<Scalarm>::SCALAR(),buffer);
|
2014-08-09 12:00:09 +02:00
|
|
|
//err = glGetError();
|
2014-04-07 14:32:21 +02:00
|
|
|
|
|
|
|
std::vector<FacePointer> result;
|
2014-04-08 11:37:44 +02:00
|
|
|
OldPickFace(x,y,m,result,width,height,sorted);
|
2014-08-09 12:00:09 +02:00
|
|
|
ScalarType LocalEpsilon(0.001);
|
|
|
|
for(size_t i =0;i<result.size();++i)
|
2014-04-07 14:32:21 +02:00
|
|
|
{
|
2014-08-09 12:00:09 +02:00
|
|
|
CoordType v=Barycenter(*(result[i]));
|
2014-04-07 14:32:21 +02:00
|
|
|
GLdouble tx,ty,tz;
|
|
|
|
gluProject(v.X(),v.Y(),v.Z(), mm,mp,vp, &tx,&ty,&tz);
|
|
|
|
if(tx >=0 && tx<screenW && ty >=0 && ty<screenH)
|
|
|
|
{
|
2014-08-09 12:00:09 +02:00
|
|
|
ScalarType bufZ(buffer[int(tx)+int(ty)*screenW]);
|
2014-04-07 14:32:21 +02:00
|
|
|
//qDebug("face %i txyz (%f %f %f) bufz %f",i,tx,ty,tz,bufZ);
|
|
|
|
if(bufZ + LocalEpsilon >= tz)
|
|
|
|
resultZ.push_back(result[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete [] buffer;
|
|
|
|
return resultZ.size();
|
|
|
|
}
|
2005-09-15 17:44:52 +02:00
|
|
|
|
|
|
|
|
2014-04-08 11:37:44 +02:00
|
|
|
static int OldPickFace(int x, int y, MESH_TYPE &m, std::vector<FacePointer> &result, int width=4, int height=4,bool sorted=true)
|
|
|
|
{
|
|
|
|
result.clear();
|
|
|
|
if(width==0 ||height==0) return 0;
|
|
|
|
long hits;
|
|
|
|
int sz=m.face.size()*5;
|
|
|
|
GLuint *selectBuf =new GLuint[sz];
|
|
|
|
// static unsigned int selectBuf[16384];
|
|
|
|
glSelectBuffer(sz, selectBuf);
|
|
|
|
glRenderMode(GL_SELECT);
|
|
|
|
glInitNames();
|
|
|
|
|
|
|
|
/* Because LoadName() won't work with no names on the stack */
|
|
|
|
glPushName(-1);
|
|
|
|
double mp[16];
|
|
|
|
|
|
|
|
GLint viewport[4];
|
|
|
|
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 fcnt=0;
|
|
|
|
FaceIterator fi;
|
|
|
|
for(fi=m.face.begin();fi!=m.face.end();++fi)
|
|
|
|
{
|
|
|
|
if(!(*fi).IsD())
|
|
|
|
{
|
|
|
|
glLoadName(fcnt);
|
|
|
|
glBegin(GL_TRIANGLES);
|
|
|
|
glVertex( (*fi).V(0)->P() );
|
|
|
|
glVertex( (*fi).V(1)->P() );
|
|
|
|
glVertex( (*fi).V(2)->P() );
|
|
|
|
glEnd();
|
|
|
|
}
|
|
|
|
fcnt++; // the counter should advance even for deleted faces!
|
|
|
|
}
|
|
|
|
|
|
|
|
glPopMatrix();
|
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glPopMatrix();
|
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
hits = glRenderMode(GL_RENDER);
|
|
|
|
//xstring buf;
|
|
|
|
//if (hits <= 0) return 0;
|
|
|
|
std::vector< std::pair<double,unsigned int> > H;
|
|
|
|
for(long ii=0;ii<hits;ii++){
|
|
|
|
//TRACE("%ui %ui %ui %ui\n",selectBuf[ii*4],selectBuf[ii*4+1],selectBuf[ii*4+2],selectBuf[ii*4+3]);
|
|
|
|
H.push_back( std::pair<double,unsigned int>(selectBuf[ii*4+1]/4294967295.0,selectBuf[ii*4+3]));
|
|
|
|
}
|
|
|
|
if(sorted)
|
|
|
|
std::sort(H.begin(),H.end());
|
|
|
|
// if(H.size()>0) TRACE("\n Closest is %i\n",H[0].second);
|
|
|
|
result.resize(H.size());
|
|
|
|
for(long ii=0;ii<hits;ii++){
|
|
|
|
FaceIterator fi=m.face.begin();
|
|
|
|
advance(fi ,H[ii].second);
|
|
|
|
result[ii]=&*fi;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete [] selectBuf;
|
|
|
|
return result.size();
|
|
|
|
}
|
2005-09-15 17:44:52 +02:00
|
|
|
|
2014-04-08 11:37:44 +02:00
|
|
|
static int OldPickVert(int x, int y, MESH_TYPE &m, std::vector<VertexPointer> &result, int width=4, int height=4,bool sorted=true)
|
|
|
|
{
|
|
|
|
result.clear();
|
|
|
|
if(width==0 ||height==0) return 0;
|
|
|
|
long hits;
|
|
|
|
int sz=m.vert.size()*5;
|
|
|
|
GLuint *selectBuf =new GLuint[sz];
|
|
|
|
glSelectBuffer(sz, selectBuf);
|
|
|
|
glRenderMode(GL_SELECT);
|
|
|
|
glInitNames();
|
2004-09-23 15:13:09 +02:00
|
|
|
|
2014-04-08 11:37:44 +02:00
|
|
|
/* Because LoadName() won't work with no names on the stack */
|
|
|
|
glPushName(-1);
|
|
|
|
double mp[16];
|
2006-12-07 01:39:22 +01:00
|
|
|
|
2014-04-08 11:37:44 +02:00
|
|
|
GLint viewport[4];
|
|
|
|
glGetIntegerv(GL_VIEWPORT,viewport);
|
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glGetDoublev(GL_PROJECTION_MATRIX ,mp);
|
|
|
|
glPushMatrix();
|
|
|
|
glLoadIdentity();
|
|
|
|
gluPickMatrix(x, y, width, height, viewport);
|
|
|
|
glMultMatrixd(mp);
|
2006-12-07 01:39:22 +01:00
|
|
|
|
2014-04-08 11:37:44 +02:00
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
glPushMatrix();
|
|
|
|
int vcnt=0;
|
|
|
|
VertexIterator vi;
|
|
|
|
for(vi=m.vert.begin();vi!=m.vert.end();++vi)
|
2014-04-07 14:32:21 +02:00
|
|
|
{
|
2014-04-08 11:37:44 +02:00
|
|
|
if(!(*vi).IsD())
|
|
|
|
{
|
|
|
|
glLoadName(vcnt);
|
|
|
|
glBegin(GL_POINTS);
|
|
|
|
glVertex( (*vi).P() );
|
2014-04-07 14:32:21 +02:00
|
|
|
glEnd();
|
2014-04-08 11:37:44 +02:00
|
|
|
}
|
|
|
|
vcnt++; // the counter should advance even for deleted faces!
|
2014-04-07 14:32:21 +02:00
|
|
|
}
|
|
|
|
|
2014-04-08 11:37:44 +02:00
|
|
|
glPopMatrix();
|
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glPopMatrix();
|
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
hits = glRenderMode(GL_RENDER);
|
|
|
|
std::vector< std::pair<double,unsigned int> > H;
|
|
|
|
for(long ii=0;ii<hits;ii++){
|
|
|
|
H.push_back( std::pair<double,unsigned int>(selectBuf[ii*4+1]/4294967295.0,selectBuf[ii*4+3]));
|
|
|
|
}
|
|
|
|
if(sorted)
|
|
|
|
std::sort(H.begin(),H.end());
|
|
|
|
result.resize(H.size());
|
|
|
|
for(long ii=0;ii<hits;ii++){
|
|
|
|
VertexIterator vi=m.vert.begin();
|
|
|
|
advance(vi ,H[ii].second);
|
|
|
|
result[ii]=&*vi;
|
2014-04-07 14:32:21 +02:00
|
|
|
}
|
2004-10-06 18:23:59 +02:00
|
|
|
|
2014-04-08 11:37:44 +02:00
|
|
|
delete [] selectBuf;
|
|
|
|
return result.size();
|
2014-04-07 14:32:21 +02:00
|
|
|
}
|
|
|
|
|
2004-09-23 15:13:09 +02:00
|
|
|
};
|
2005-09-15 17:44:52 +02:00
|
|
|
|
2004-09-23 15:13:09 +02:00
|
|
|
}
|
2005-10-27 16:46:26 +02:00
|
|
|
|
|
|
|
#endif
|