Commit 5e641697 authored by Sylvain Thery's avatar Sylvain Thery
Browse files

Minor modification on qtSimple/qtgl for future plugin system

parent 97d7a2de
...@@ -126,6 +126,8 @@ public: ...@@ -126,6 +126,8 @@ public:
/** /**
* add an entry to popup menu * add an entry to popup menu
* @param label label printed in menu
* @param method qt slot (function name)
*/ */
void add_menu_entry(const std::string label, const char* method); void add_menu_entry(const std::string label, const char* method);
...@@ -207,19 +209,19 @@ public: ...@@ -207,19 +209,19 @@ public:
* get a ray (2 points) from a pick point in GL area * get a ray (2 points) from a pick point in GL area
* @param x mouse position * @param x mouse position
* @param y mouse position * @param y mouse position
* @param rayA first computed point * @param rayA first computed point of ray
* @param rayA second computed point * @param rayA second computed point of ray
* @param radius radius on pixel for clicking precision * @param radius radius on pixel for clicking precision
* @return the distance in modelview world corresponding to radius pixel in screen * @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); GLfloat getOrthoScreenRay(int x, int y, Geom::Vec3f& rayA, Geom::Vec3f& rayB, int radius=4) { return m_glWidget->getOrthoScreenRay(x,y,rayA,rayB,radius);}
/** /**
* transform a pixel distance on screen in distance in world * transform a pixel distance on screen in distance in world
* @param pixel_width width on pixel on screen * @param pixel_width width on pixel on screen
* @param center reference point on world to use (defaut 0,0,0) * @param center reference point on world to use (defaut 0,0,0)
*/ */
float getWidthInWorld(unsigned int pixel_width, const Geom::Vec3f& center=Geom::Vec3f(0.0f,0.0f,0.0f)); float getWidthInWorld(unsigned int pixel_width, const Geom::Vec3f& center) { return m_glWidget->getWidthInWorld(pixel_width,center);}
const glm::mat4& transfoMatrix() const { return m_transfo_matrix; } const glm::mat4& transfoMatrix() const { return m_transfo_matrix; }
glm::mat4& transfoMatrix() { return m_transfo_matrix; } glm::mat4& transfoMatrix() { return m_transfo_matrix; }
...@@ -312,9 +314,9 @@ public: ...@@ -312,9 +314,9 @@ public:
virtual void cb_mouseClick(int button, int x, int y) {} virtual void cb_mouseClick(int button, int x, int y) {}
/** /**
* the mouse has been move (with button still pressed) * the mouse has been move (with buttons still pressed)
*/ */
virtual void cb_mouseMove(int button, int x, int y) {} virtual void cb_mouseMove(int buttons, int x, int y) {}
/** /**
* the mouse has been move (with button still pressed) * the mouse has been move (with button still pressed)
......
...@@ -35,6 +35,7 @@ ...@@ -35,6 +35,7 @@
#include "glm/glm.hpp" #include "glm/glm.hpp"
#include "glm/gtc/matrix_projection.hpp" #include "glm/gtc/matrix_projection.hpp"
#include "glm/gtc/matrix_transform.hpp" #include "glm/gtc/matrix_transform.hpp"
#include "Geometry/vector_gen.h"
namespace CGoGN namespace CGoGN
{ {
...@@ -88,6 +89,9 @@ protected: ...@@ -88,6 +89,9 @@ protected:
int m_state_modifier; int m_state_modifier;
/// stack for transformation matrix
std::stack<glm::mat4> m_stack_trf;
/** /**
* met a jour la matrice modelview * met a jour la matrice modelview
*/ */
...@@ -158,6 +162,56 @@ public: ...@@ -158,6 +162,56 @@ public:
void modelModified() { newModel = 1; } void modelModified() { newModel = 1; }
void glMousePosition(int& x, int& y);
/**
* get a ray (2 points) from a pick point in GL area
* @param x mouse position
* @param y mouse position
* @param rayA first computed point of ray
* @param rayA second computed point of ray
* @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);
/**
* 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=Geom::Vec3f(0.0f,0.0f,0.0f));
/**
* current transfo matrix
*/
const glm::mat4& transfoMatrix() const;
glm::mat4& transfoMatrix();
/**
* current modelview matrix
*/
const glm::mat4& modelViewMatrix() const;
glm::mat4& modelViewMatrix();
/**
* current projection matrix
*/
const glm::mat4& projectionMatrix() const;
glm::mat4& projectionMatrix();
void transfoRotate(float angle, float x, float y, float z);
void transfoTranslate(float tx, float ty, float tz);
void transfoScale(float sx, float sy, float sz);
void pushTransfoMatrix();
bool popTransfoMatrix();
protected: protected:
/** /**
* equivalent to old school glRotate * equivalent to old school glRotate
......
...@@ -309,80 +309,80 @@ void SimpleQT::glMousePosition(int& x, int& y) ...@@ -309,80 +309,80 @@ void SimpleQT::glMousePosition(int& x, int& y)
y = m_glWidget->getHeight() - xy.y(); y = m_glWidget->getHeight() - xy.y();
} }
GLfloat SimpleQT::getOrthoScreenRay(int x, int y, Geom::Vec3f& rayA, Geom::Vec3f& rayB, int radius) //GLfloat SimpleQT::getOrthoScreenRay(int x, int y, Geom::Vec3f& rayA, Geom::Vec3f& rayB, int radius)
{ //{
// get Z from depth buffer // // get Z from depth buffer
int yy = y; // int yy = y;
GLfloat depth_t[25]; // GLfloat depth_t[25];
glReadPixels(x-2, yy-2, 5, 5, GL_DEPTH_COMPONENT, GL_FLOAT, depth_t); // glReadPixels(x-2, yy-2, 5, 5, GL_DEPTH_COMPONENT, GL_FLOAT, depth_t);
//
GLfloat depth=0.0f; // GLfloat depth=0.0f;
unsigned int nb=0; // unsigned int nb=0;
for (unsigned int i=0; i< 25; ++i) // for (unsigned int i=0; i< 25; ++i)
{ // {
if (depth_t[i] != 1.0f) // if (depth_t[i] != 1.0f)
{ // {
depth += depth_t[i]; // depth += depth_t[i];
nb++; // nb++;
} // }
} // }
if (nb>0) // if (nb>0)
depth /= float(nb); // depth /= float(nb);
else // else
depth = 0.5f; // depth = 0.5f;
//
glm::i32vec4 viewport; // glm::i32vec4 viewport;
glGetIntegerv(GL_VIEWPORT, &(viewport[0])); // glGetIntegerv(GL_VIEWPORT, &(viewport[0]));
//
glm::vec3 win(x, yy, 0.0f); // glm::vec3 win(x, yy, 0.0f);
//
glm::vec3 P = glm::unProject(win, m_modelView_matrix, m_projection_matrix, viewport); // glm::vec3 P = glm::unProject(win, m_modelView_matrix, m_projection_matrix, viewport);
//
rayA[0] = P[0]; // rayA[0] = P[0];
rayA[1] = P[1]; // rayA[1] = P[1];
rayA[2] = P[2]; // rayA[2] = P[2];
//
win[2] = depth; // win[2] = depth;
//
P = glm::unProject(win, m_modelView_matrix, m_projection_matrix, viewport); // P = glm::unProject(win, m_modelView_matrix, m_projection_matrix, viewport);
rayB[0] = P[0]; // rayB[0] = P[0];
rayB[1] = P[1]; // rayB[1] = P[1];
rayB[2] = P[2]; // rayB[2] = P[2];
//
if (depth == 1.0f) // depth vary in [0-1] // if (depth == 1.0f) // depth vary in [0-1]
win[2] = 0.5f; // win[2] = 0.5f;
//
win[0] += radius; // win[0] += radius;
P = glm::unProject(win, m_modelView_matrix, m_projection_matrix, viewport); // P = glm::unProject(win, m_modelView_matrix, m_projection_matrix, viewport);
Geom::Vec3f Q; // Geom::Vec3f Q;
Q[0] = P[0]; // Q[0] = P[0];
Q[1] = P[1]; // Q[1] = P[1];
Q[2] = P[2]; // Q[2] = P[2];
//
// compute & return distance // // compute & return distance
Q -= rayB; // Q -= rayB;
return float(Q.norm()); // return float(Q.norm());
} //}
//
float SimpleQT::getWidthInWorld(unsigned int pixel_width, const Geom::Vec3f& center) //float SimpleQT::getWidthInWorld(unsigned int pixel_width, const Geom::Vec3f& center)
{ //{
//
glm::i32vec4 viewport; // glm::i32vec4 viewport;
glGetIntegerv(GL_VIEWPORT, &(viewport[0])); // 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); // 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; // win[0]-= pixel_width/2;
//
glm::vec3 P = glm::unProject(win, m_modelView_matrix, m_projection_matrix, viewport); // glm::vec3 P = glm::unProject(win, m_modelView_matrix, m_projection_matrix, viewport);
//
win[0] += pixel_width; // win[0] += pixel_width;
//
glm::vec3 Q = glm::unProject(win, m_modelView_matrix, m_projection_matrix, viewport); // glm::vec3 Q = glm::unProject(win, m_modelView_matrix, m_projection_matrix, viewport);
//
return glm::distance(P,Q); // return glm::distance(P,Q);
} //}
//
......
...@@ -380,6 +380,15 @@ void GLWidget::keyReleaseEvent(QKeyEvent *event) ...@@ -380,6 +380,15 @@ void GLWidget::keyReleaseEvent(QKeyEvent *event)
m_cbs->cb_keyRelease(k); m_cbs->cb_keyRelease(k);
} }
void GLWidget::glMousePosition(int& x, int& y)
{
QPoint xy = mapFromGlobal(QCursor::pos());
x = xy.x();
y = getHeight() - xy.y();
}
void GLWidget::oglRotate(float angle, float x, float y, float z) void GLWidget::oglRotate(float angle, float x, float y, float z)
{ {
m_cbs->modelViewMatrix() = glm::rotate(m_cbs->modelViewMatrix(), angle, glm::vec3(x,y,z)); m_cbs->modelViewMatrix() = glm::rotate(m_cbs->modelViewMatrix(), angle, glm::vec3(x,y,z));
...@@ -395,6 +404,128 @@ void GLWidget::oglScale(float sx, float sy, float sz) ...@@ -395,6 +404,128 @@ void GLWidget::oglScale(float sx, float sy, float sz)
m_cbs->modelViewMatrix() = glm::scale(m_cbs->modelViewMatrix(), glm::vec3(sx,sy,sz)); m_cbs->modelViewMatrix() = glm::scale(m_cbs->modelViewMatrix(), glm::vec3(sx,sy,sz));
} }
GLfloat GLWidget::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_cbs->modelViewMatrix(), m_cbs->projectionMatrix(), viewport);
rayA[0] = P[0];
rayA[1] = P[1];
rayA[2] = P[2];
win[2] = depth;
P = glm::unProject(win, m_cbs->modelViewMatrix(), m_cbs->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_cbs->modelViewMatrix(), m_cbs->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 GLWidget::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_cbs->modelViewMatrix(), m_cbs->projectionMatrix(), viewport);
win[0]-= pixel_width/2;
glm::vec3 P = glm::unProject(win, m_cbs->modelViewMatrix(), m_cbs->projectionMatrix(), viewport);
win[0] += pixel_width;
glm::vec3 Q = glm::unProject(win, m_cbs->modelViewMatrix(), m_cbs->projectionMatrix(), viewport);
return glm::distance(P,Q);
}
void GLWidget::transfoRotate(float angle, float x, float y, float z)
{
m_cbs->transfoMatrix() = glm::rotate( m_cbs->transfoMatrix(), angle, glm::vec3(x,y,z));
}
void GLWidget::transfoTranslate(float tx, float ty, float tz)
{
m_cbs->transfoMatrix() = glm::translate( m_cbs->transfoMatrix(), glm::vec3(tx,ty,tz));
}
void GLWidget::transfoScale(float sx, float sy, float sz)
{
m_cbs->transfoMatrix() = glm::scale( m_cbs->transfoMatrix(), glm::vec3(sx,sy,sz));
}
void GLWidget::pushTransfoMatrix()
{
m_stack_trf.push( m_cbs->transfoMatrix());
}
bool GLWidget::popTransfoMatrix()
{
if (m_stack_trf.empty())
return false;
m_cbs->transfoMatrix() = m_stack_trf.top();
m_stack_trf.pop();
return true;
}
/**
* current transfo matrix
*/
const glm::mat4& GLWidget::transfoMatrix() const { return m_cbs->transfoMatrix(); }
glm::mat4& GLWidget::transfoMatrix() { return m_cbs->transfoMatrix(); }
/**
* current modelview matrix
*/
const glm::mat4& GLWidget::modelViewMatrix() const { return m_cbs->modelViewMatrix(); }
glm::mat4& GLWidget::modelViewMatrix() { return m_cbs->modelViewMatrix(); }
/**
* current projection matrix
*/
const glm::mat4& GLWidget::projectionMatrix() const { return m_cbs->projectionMatrix(); }
glm::mat4& GLWidget::projectionMatrix() { return m_cbs->projectionMatrix(); }
} // namespace QT } // namespace QT
......
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