Commit f2a4af2f authored by Sylvain Thery's avatar Sylvain Thery

add picking in qtQGLV

parent d71634ef
......@@ -259,14 +259,14 @@ public:
* @param radius radius on pixel for clicking precision
* @return the distance in modelview world corresponding to radius pixel in screen
*/
// GLfloat getOrthoScreenRay(int x, int y, Geom::Vec3f& rayA, Geom::Vec3f& rayB, int radius=4) { return m_glWidget->getOrthoScreenRay(x,y,rayA,rayB,radius);}
GLfloat getOrthoScreenRay(int x, int y, Geom::Vec3f& rayA, Geom::Vec3f& rayB, int radius=4) { return m_qglWidget->getOrthoScreenRay(x,y,rayA,rayB,radius);}
/**
* transform a pixel distance on screen in distance in world
* @param pixel_width width on pixel on screen
* @param center reference point on world to use (defaut 0,0,0)
*/
// float getWidthInWorld(unsigned int pixel_width, const Geom::Vec3f& center) { return m_glWidget->getWidthInWorld(pixel_width,center);}
float getWidthInWorld(unsigned int pixel_width, const Geom::Vec3f& center) { return m_qglWidget->getWidthInWorld(pixel_width,center);}
const glm::mat4& transfoMatrix() const { return m_transfo_matrix; }
......
......@@ -60,6 +60,7 @@ protected:
int W;
int H;
int m_state_modifier;
int m_current_button;
public:
QGLView(SimpleQGLV* ptr, QGLFormat& format, QWidget *parent = 0);
......@@ -79,7 +80,7 @@ public:
void mouseReleaseEvent(QMouseEvent* event);
void mouseClickEvent(QMouseEvent* event);
// void mouseDoubleClickEvent(QMouseEvent* event);
// void mouseMoveEvent(QMouseEvent* event);
void mouseMoveEvent(QMouseEvent* event);
void keyPressEvent(QKeyEvent* event);
void keyReleaseEvent(QKeyEvent* event);
// void wheelEvent(QWheelEvent* event);
......@@ -89,6 +90,9 @@ public:
int getHeight() const { return H; }
int getWidth() const { return W; }
GLfloat getOrthoScreenRay(int x, int y, Geom::Vec3f& rayA, Geom::Vec3f& rayB, int radius);
float getWidthInWorld(unsigned int pixel_width, const Geom::Vec3f& center);
QSize minimumSizeHint() const;
QSize sizeHint() const;
......
......@@ -162,7 +162,7 @@ void QGLView::mousePressEvent(QMouseEvent* event)
// beginx = event->x();
// beginy = event->y();
// clickPoint = event->pos();
// m_current_button = event->button();
m_current_button = event->button();
if (m_sqgl)
m_sqgl->cb_mousePress(event->button(), event->x(), getHeight() - event->y());
......@@ -173,7 +173,7 @@ void QGLView::mouseReleaseEvent(QMouseEvent* event)
{
if (m_sqgl)
m_sqgl->cb_mouseRelease(event->button(), event->x(), getHeight() - event->y());
m_current_button = 0;
QGLViewer::mouseReleaseEvent(event);
}
......@@ -183,6 +183,14 @@ void QGLView::mouseClickEvent(QMouseEvent* event)
m_sqgl->cb_mouseClick(event->button(), event->x(), getHeight() - event->y());
}
void QGLView::mouseMoveEvent(QMouseEvent* event)
{
if (m_sqgl)
m_sqgl->cb_mouseMove(m_current_button, event->x(), getHeight() - event->y());
QGLViewer::mouseMoveEvent(event);
}
void QGLView::glMousePosition(int& x, int& y)
{
QPoint xy = mapFromGlobal(QCursor::pos());
......@@ -190,6 +198,80 @@ void QGLView::glMousePosition(int& x, int& y)
y = getHeight() - xy.y();
}
GLfloat QGLView::getOrthoScreenRay(int x, int y, Geom::Vec3f& rayA, Geom::Vec3f& rayB, int radius)
{
// get Z from depth buffer
int yy = y;
GLfloat depth_t[25];
glReadPixels(x-2, yy-2, 5, 5, GL_DEPTH_COMPONENT, GL_FLOAT, depth_t);
GLfloat depth=0.0f;
unsigned int nb=0;
for (unsigned int i=0; i< 25; ++i)
{
if (depth_t[i] != 1.0f)
{
depth += depth_t[i];
nb++;
}
}
if (nb>0)
depth /= float(nb);
else
depth = 0.5f;
glm::i32vec4 viewport;
glGetIntegerv(GL_VIEWPORT, &(viewport[0]));
glm::vec3 win(x, yy, 0.0f);
glm::vec3 P = glm::unProject(win, m_sqgl->modelViewMatrix(), m_sqgl->projectionMatrix(), viewport);
rayA[0] = P[0];
rayA[1] = P[1];
rayA[2] = P[2];
win[2] = depth;
P = glm::unProject(win, m_sqgl->modelViewMatrix(), m_sqgl->projectionMatrix(), viewport);
rayB[0] = P[0];
rayB[1] = P[1];
rayB[2] = P[2];
if (depth == 1.0f) // depth vary in [0-1]
win[2] = 0.5f;
win[0] += radius;
P = glm::unProject(win, m_sqgl->modelViewMatrix(), m_sqgl->projectionMatrix(), viewport);
Geom::Vec3f Q;
Q[0] = P[0];
Q[1] = P[1];
Q[2] = P[2];
// compute & return distance
Q -= rayB;
return float(Q.norm());
}
float QGLView::getWidthInWorld(unsigned int pixel_width, const Geom::Vec3f& center)
{
glm::i32vec4 viewport;
glGetIntegerv(GL_VIEWPORT, &(viewport[0]));
glm::vec3 win = glm::project(glm::vec3(center[0],center[1],center[2]), m_sqgl->modelViewMatrix(), m_sqgl->projectionMatrix(), viewport);
win[0]-= pixel_width/2;
glm::vec3 P = glm::unProject(win, m_sqgl->modelViewMatrix(), m_sqgl->projectionMatrix(), viewport);
win[0] += pixel_width;
glm::vec3 Q = glm::unProject(win, m_sqgl->modelViewMatrix(), m_sqgl->projectionMatrix(), viewport);
return glm::distance(P,Q);
}
} // namespace QT
......
......@@ -344,81 +344,6 @@ void SimpleQT::glMousePosition(int& x, int& y)
}
//GLfloat SimpleQT::getOrthoScreenRay(int x, int y, Geom::Vec3f& rayA, Geom::Vec3f& rayB, int radius)
//{
// // get Z from depth buffer
// int yy = y;
// GLfloat depth_t[25];
// glReadPixels(x-2, yy-2, 5, 5, GL_DEPTH_COMPONENT, GL_FLOAT, depth_t);
//
// GLfloat depth=0.0f;
// unsigned int nb=0;
// for (unsigned int i=0; i< 25; ++i)
// {
// if (depth_t[i] != 1.0f)
// {
// depth += depth_t[i];
// nb++;
// }
// }
// if (nb>0)
// depth /= float(nb);
// else
// depth = 0.5f;
//
// glm::i32vec4 viewport;
// glGetIntegerv(GL_VIEWPORT, &(viewport[0]));
//
// glm::vec3 win(x, yy, 0.0f);
//
// glm::vec3 P = glm::unProject(win, m_modelView_matrix, m_projection_matrix, viewport);
//
// rayA[0] = P[0];
// rayA[1] = P[1];
// rayA[2] = P[2];
//
// win[2] = depth;
//
// P = glm::unProject(win, m_modelView_matrix, m_projection_matrix, viewport);
// rayB[0] = P[0];
// rayB[1] = P[1];
// rayB[2] = P[2];
//
// if (depth == 1.0f) // depth vary in [0-1]
// win[2] = 0.5f;
//
// win[0] += radius;
// P = glm::unProject(win, m_modelView_matrix, m_projection_matrix, viewport);
// Geom::Vec3f Q;
// Q[0] = P[0];
// Q[1] = P[1];
// Q[2] = P[2];
//
// // compute & return distance
// Q -= rayB;
// return float(Q.norm());
//}
//
//float SimpleQT::getWidthInWorld(unsigned int pixel_width, const Geom::Vec3f& center)
//{
//
// glm::i32vec4 viewport;
// glGetIntegerv(GL_VIEWPORT, &(viewport[0]));
//
// glm::vec3 win = glm::project(glm::vec3(center[0],center[1],center[2]), m_modelView_matrix, m_projection_matrix, viewport);
//
// win[0]-= pixel_width/2;
//
// glm::vec3 P = glm::unProject(win, m_modelView_matrix, m_projection_matrix, viewport);
//
// win[0] += pixel_width;
//
// glm::vec3 Q = glm::unProject(win, m_modelView_matrix, m_projection_matrix, viewport);
//
// return glm::distance(P,Q);
//}
//
void SimpleQT::synchronize(SimpleQT* sqt)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment