Commit c789b3e2 authored by Sylvain Thery's avatar Sylvain Thery

Ajout de la matrice de transformation, qui permet d'appliquer ses

propre transformation sur l'objet affiche
Exemple dans tuto5 avec un timer pour faire un "animate"
parent 803f13ce
......@@ -112,6 +112,16 @@ void MyQT::slider_text(int x)
updateGL();
}
void MyQT::animate()
{
// transfoMatrix() = glm::rotate(transfoMatrix(), 0.5f, glm::vec3(0.5773f,0.5773f,0.5773f));
transfoRotate( 0.5f, 0.5773f,0.5773f,0.5773f);
updateGLMatrices();
}
void MyQT::storeVerticesInfo()
{
......@@ -183,6 +193,11 @@ void MyQT::cb_initGL()
m_render->initPrimitives<PFP>(myMap, allDarts, Algo::Render::GL2::POINTS);
m_render_topo->updateData<PFP>(myMap, allDarts, position, 0.9, 0.9, 0.9);
// timer example for animation
m_timer = new QTimer( m_glWidget );
connect( m_timer, SIGNAL(timeout()), SLOT(animate()) );
}
void MyQT::cb_redraw()
......@@ -230,7 +245,7 @@ void MyQT::cb_mousePress(int button, int x, int y)
{
if (Shift())
{
Dart d = m_render_topo->picking<PFP>(myMap, allDarts, x, getHeight() - y);
Dart d = m_render_topo->picking<PFP>(myMap, allDarts, x,y);
if (d != Dart::nil())
{
CGoGNout << "Dart "<< d << " clicked" << CGoGNendl;
......@@ -254,9 +269,17 @@ void MyQT::cb_keyPress(int code)
svg.renderFacesToSVG<PFP>(myMap,position,0.8f);
//svg destruction close the file
}
if (code == 't')
{
if (m_timer->isActive())
m_timer->stop();
else
m_timer->start(1000/30); // 30 fps
}
}
int main(int argc, char **argv)
{
position = myMap.addAttribute<PFP::VEC3>(VERTEX_ORBIT, "position");
......@@ -322,7 +345,8 @@ int main(int argc, char **argv)
CGoGNdbg2.toConsole(&sqt);
CGoGNerr.toConsole(&sqt);
CGoGNdbg2 << " TextureSize " << texSize << CGoGNendl;
CGoGNerr << " ERROR " << 5*7 << CGoGNflush;
CGoGNerr << " test ERROR " << 5*7 << CGoGNflush;
// et on attend la fin.
......
......@@ -72,6 +72,8 @@ class MyQT: public Utils::QT::SimpleQT
Utils::Strings3D* m_strings;
Utils::PointSprite* m_sprite;
QTimer *m_timer;
public:
MyQT():
render_text(true),
......@@ -85,7 +87,8 @@ public:
m_shader(NULL),
m_lines(NULL),
m_strings(NULL),
m_sprite(NULL)
m_sprite(NULL),
m_timer(NULL)
{}
protected:
......@@ -109,6 +112,8 @@ public slots:
void slider_balls(int x);
void slider_vectors(int x);
void slider_text(int x);
void animate();
};
#endif
......@@ -63,11 +63,14 @@ class SvgObj
{
protected:
std::vector<Geom::Vec3f> m_vertices;
std::vector<Geom::Vec3f> m_vertices3D;
Geom::Vec3f m_color;
float m_width;
public:
void addVertex(const Geom::Vec3f& v);
void addVertex3D(const Geom::Vec3f& v);
void setColor(const Geom::Vec3f& c);
void setWidth(float w) { m_width=w;}
......@@ -76,9 +79,9 @@ public:
virtual void save(std::ofstream& out)=0;
unsigned int nbv() const { return m_vertices.size();}
unsigned int nbv() const { return m_vertices3D.size();}
const Geom::Vec3f& P(unsigned int i) const { return m_vertices[i];}
const Geom::Vec3f& P(unsigned int i) const { return m_vertices3D[i];}
Geom::Vec3f normal();
......@@ -150,17 +153,35 @@ public:
template <typename PFP>
void renderFacesToSVG(typename PFP::MAP& map, const typename PFP::TVEC3& position, float shrink, const FunctorSelect& good = SelectorTrue(), unsigned int thread=0);
void orderPrimitives(std::list<SvgObj*>& primitives);
};
struct compSvgObj
{
int points_plane (SvgPolygon* pol_points, SvgPolygon* pol_plane);
int points_plane (SvgPolygon* pol_points, SvgPolygon* pol_plane, float& averageZ);
bool operator() (SvgObj* a, SvgObj*b);
};
struct compNormObj
{
// int points_plane (SvgPolygon* pol_points, SvgPolygon* pol_plane);
bool operator() (SvgObj* a, SvgObj*b);
};
//
//class BSP_SVG
//{
//protected:
// std::list<BSP_SVG*> m_front;
// std::list<BSP_SVG*> m_back;
// std::list<BSP_SVG*> m_intersect;
// SvgObj* m_obj;
//
//public:
// void insert(SvgObj*)
//};
void bubble_sort(std::vector<SvgObj*>& table, compSvgObj& cmp);
} // namespace SVG
......
......@@ -49,15 +49,22 @@ void SVGOut::renderLinesToSVG(typename PFP::MAP& map, const typename PFP::TVEC3&
{
if(!m.isMarked(d) && good(d))
{
const Geom::Vec3f& P = position[d];
glm::vec3 Q = glm::project(glm::vec3(P[0],P[1],P[2]),m_model,m_proj,viewport);
glm::vec3 R = glm::project(glm::vec3(P[0],P[1],P[2]),m_model,glm::mat4(1.0),viewport);
const Geom::Vec3f& P2 = position[map.phi1(d)];
glm::vec3 Q2 = glm::project(glm::vec3(P2[0],P2[1],P2[2]),m_model,m_proj,viewport);
glm::vec3 R2 = glm::project(glm::vec3(P2[0],P2[1],P2[2]),m_model,glm::mat4(1.0),viewport);
SvgPolyline* pol = new SvgPolyline();
pol->addVertex(Geom::Vec3f(Q[0],float(viewport[3])-Q[1],Q[2]));
pol->addVertex(Geom::Vec3f(Q2[0],float(viewport[3])-Q2[1],Q2[2]));
pol->addVertex3D(Geom::Vec3f(R[0],float(viewport[3])-R[1],R[2]));
pol->addVertex3D(Geom::Vec3f(R2[0],float(viewport[3])-R2[1],R2[2]));
pol->setColor(global_color);
pol->setWidth(global_width);
m_objs.push_back(pol);
......@@ -86,7 +93,9 @@ void SVGOut::renderFacesToSVG(typename PFP::MAP& map, const typename PFP::TVEC3&
Geom::Vec3f P = position[d];
P = P*shrink + center*(1.0f-shrink);
glm::vec3 Q = glm::project(glm::vec3(P[0],P[1],P[2]),m_model,m_proj,viewport);
glm::vec3 R = glm::project(glm::vec3(P[0],P[1],P[2]),m_model,glm::mat4(1.0),viewport);
pol->addVertex(Geom::Vec3f(Q[0],float(viewport[3])-Q[1],Q[2]));
pol->addVertex3D(Geom::Vec3f(R[0],R[1],R[2]));
d = map.phi1(d);
}while (d!=dd);
......
......@@ -150,6 +150,7 @@ protected:
// mouse & matrix
glm::mat4 m_projection_matrix;
glm::mat4 m_modelView_matrix;
glm::mat4 m_transfo_matrix;
float m_curquat[4];
float m_lastquat[4];
float m_trans_x;
......@@ -162,6 +163,9 @@ protected:
std::string m_helpString;
std::stack<glm::mat4> m_stack_trf;
void keyPressEvent(QKeyEvent *event);
void keyReleaseEvent(QKeyEvent *e);
......@@ -193,6 +197,9 @@ public:
*/
GLfloat getOrthoScreenRay(int x, int y, Geom::Vec3f& rayA, Geom::Vec3f& rayB, int radius = 4);
const glm::mat4& transfoMatrix() const { return m_transfo_matrix; }
glm::mat4& transfoMatrix() { return m_transfo_matrix; }
/**
* current modelview matrix
*/
......@@ -315,6 +322,33 @@ public:
*/
void updateGLMatrices();
/**
* apply rotation to transformation matrix
*/
void transfoRotate(float angle, float x, float y, float z);
/**
* apply translation to transformation matrix
*/
void transfoTranslate(float tx, float ty, float tz);
/**
* apply scale to transformation matrix
*/
void transfoScale(float sx, float sy, float sz);
/**
* push the transfo matrix on stack
*/
void pushTransfoMatrix();
/**
* pop the transfo matrix from stack
*/
bool popTransfoMatrix();
/**
* Open a file selector and return the filename
* @param title title of window
......
......@@ -48,6 +48,12 @@ void SvgObj::addVertex(const Geom::Vec3f& v)
m_vertices.push_back(v);
}
void SvgObj::addVertex3D(const Geom::Vec3f& v)
{
m_vertices3D.push_back(v);
}
void SvgObj::setColor(const Geom::Vec3f& c)
{
......@@ -69,8 +75,8 @@ Geom::Vec3f SvgObj::normal()
return Geom::Vec3f(0.0f,0.0f,0.0f);
}
Geom::Vec3f U = m_vertices[2] - m_vertices[1];
Geom::Vec3f V = m_vertices[0] - m_vertices[1];
Geom::Vec3f U = m_vertices3D[2] - m_vertices3D[1];
Geom::Vec3f V = m_vertices3D[0] - m_vertices3D[1];
Geom::Vec3f N = U^V;
......@@ -80,6 +86,7 @@ Geom::Vec3f SvgObj::normal()
}
void SvgPolyline::save(std::ofstream& out)
{
std::stringstream ss;
......@@ -201,11 +208,12 @@ void SVGOut::setWidth(float w)
void SVGOut::closeFile()
{
// here do the sort in necessary
compSvgObj cmp;
// std::sort(m_objs.begin(),m_objs.end(),cmp);
bubble_sort(m_objs,cmp);
compNormObj cmp;
std::sort(m_objs.begin(),m_objs.end(),cmp);
for (std::vector<SvgObj*>::iterator it = m_objs.begin(); it != m_objs.end(); ++it)
std::list<SvgObj*> primitives;
for (std::list<SvgObj*>::iterator it = primitives.begin(); it != primitives.end(); ++it)
{
(*it)->save(*m_out);
}
......@@ -217,10 +225,11 @@ void SVGOut::closeFile()
// all points behind the plane +
// all points before the plane -
// all points behind the plane +1
// all points before the plane -1
// all points colinear to the plane 0
int compSvgObj::points_plane (SvgPolygon* pol_points, SvgPolygon* pol_plane)
// undefined 999
int compSvgObj::points_plane (SvgPolygon* pol_points, SvgPolygon* pol_plane, float& averageZ)
{
Geom::Vec3f N = pol_plane->normal();
......@@ -231,9 +240,13 @@ int compSvgObj::points_plane (SvgPolygon* pol_points, SvgPolygon* pol_plane)
unsigned int nbback=0;
unsigned int nbfront=0;
unsigned int nb_col=0;
averageZ=0.0f;
for (unsigned int i=0; i< nb; ++i)
{
Geom::Vec3f U = pol_points->P(i) - pol_plane->P(0);
const Geom::Vec3f& Q = pol_points->P(i);
averageZ += Q[2];
Geom::Vec3f U = Q - pol_plane->P(0);
float ps = U*N;
if (fabs(ps) < 0.0001f)
......@@ -247,29 +260,55 @@ int compSvgObj::points_plane (SvgPolygon* pol_points, SvgPolygon* pol_plane)
}
}
averageZ /= float(nb);
if (nbfront==0)
return 1;
if (nbback==0)
return -1;
return 0;
if (nb_col==nb)
return 0;
return 999;
}
bool compSvgObj::operator() (SvgObj* a, SvgObj*b)
{
// std::cout << typeid(*a).name()<< " / "<< typeid(*b).name()<< std::endl;
SvgPolygon* p_a = dynamic_cast<SvgPolygon*>(a);
SvgPolygon* p_b = dynamic_cast<SvgPolygon*>(b);
if ((p_a!= NULL) && (p_b!=NULL)) // first case polygon/polygon
{
if (points_plane(p_a,p_b)>0)
float avz_a;
int t1 = points_plane(p_a,p_b,avz_a);
if (t1==0) // colinear choose farthest
{
float za = p_a->P(0)[2];
float zb = p_b->P(0)[2];
return za > zb;
}
float avz_b;
int t2 = points_plane(p_b,p_a,avz_b);
// all point of a behind b
if ((t1 == 1)&&(t2==999))
return true;
if (points_plane(p_b,p_a)<0)
// all point of b infront of a
if ((t2 == -1) && (t1==999))
return true;
if ((t1 == t2 )&& (t2!=999))
{
return avz_a > avz_b;
}
// all other cases ??
return false;
}
......@@ -278,31 +317,32 @@ bool compSvgObj::operator() (SvgObj* a, SvgObj*b)
}
void bubble_sort(std::vector<SvgObj*>& table, compSvgObj& cmp)
bool compNormObj::operator() (SvgObj* a, SvgObj*b)
{
unsigned int nb = table.size()-1;
unsigned int nbswap=1;
while (nbswap>0)
SvgPolygon* p_a = dynamic_cast<SvgPolygon*>(a);
SvgPolygon* p_b = dynamic_cast<SvgPolygon*>(b);
if ((p_a!= NULL) && (p_b!=NULL)) // first case polygon/polygon
{
nbswap=0;
for (unsigned int i=0; i<nb;++i)
{
if (cmp(table[i+1], table[i]))
{
SvgObj* tempo = table[i];
table[i] = table[i+1];
table[i+1] = tempo;
nbswap++;
std::cout << "SWAP "<< i << " / "<< i+1 << std::endl;
}
}
std::cout << "NB SWAP = "<< nbswap<< std::endl;
Geom::Vec3f Na = p_a->normal();
Geom::Vec3f Nb = p_b->normal();
return fabs(Na[2]) > fabs(Nb[2]);
}
if ((p_a!= NULL)) // second case polygon/other
{
return true; // all polygon before segments.
}
std::cout << "Cas non traite !!"<< std::endl;
return false;
}
} // namespace SVG
} // namespace Render
} // namespace Algo
......
......@@ -100,6 +100,8 @@ m_dock(NULL)
m_dockConsole->setWidget(m_textConsole);
m_dockConsole->hide();
m_transfo_matrix = glm::mat4(1.0f);
}
SimpleQT::~SimpleQT()
......@@ -267,7 +269,6 @@ void SimpleQT::updateGL()
void SimpleQT::updateGLMatrices()
{
m_glWidget->modelModified();
cb_updateMatrix();
m_glWidget->updateGL();
}
......@@ -332,13 +333,11 @@ void SimpleQT::init_app_menu()
void SimpleQT::registerShader(GLSLShader* ptr)
{
GLSLShader::registerShader(this, ptr) ;
// GLSLShader::m_registeredShaders.insert(std::pair<void*,GLSLShader*>(this, ptr));
}
void SimpleQT::unregisterShader(GLSLShader* ptr)
{
GLSLShader::unregisterShader(this, ptr) ;
// GLSLShader::m_registeredShaders.erase(std::pair<void*,GLSLShader*>(this, ptr));
}
GLfloat SimpleQT::getOrthoScreenRay(int x, int y, Geom::Vec3f& rayA, Geom::Vec3f& rayB, int radius)
......@@ -383,6 +382,37 @@ GLfloat SimpleQT::getOrthoScreenRay(int x, int y, Geom::Vec3f& rayA, Geom::Vec3f
return float(Q.norm());
}
void SimpleQT::transfoRotate(float angle, float x, float y, float z)
{
transfoMatrix() = glm::rotate(transfoMatrix(), angle, glm::vec3(x,y,z));
}
void SimpleQT::transfoTranslate(float tx, float ty, float tz)
{
transfoMatrix() = glm::translate(transfoMatrix(), glm::vec3(tx,ty,tz));
}
void SimpleQT::transfoScale(float sx, float sy, float sz)
{
transfoMatrix() = glm::scale(transfoMatrix(), glm::vec3(sx,sy,sz));
}
void SimpleQT::pushTransfoMatrix()
{
m_stack_trf.push(transfoMatrix());
}
bool SimpleQT::popTransfoMatrix()
{
if (m_stack_trf.empty())
return false;
transfoMatrix() = m_stack_trf.top();
m_stack_trf.pop();
return true;
}
} // namespace QT
} // namespace Utils
......
......@@ -98,8 +98,8 @@ void GLWidget::recalcModelView()
{
glm::mat4 m;
oglPopModelViewMatrix();
oglPushModelViewMatrix();
// positionne l'objet / mvt souris
oglTranslate(m_cbs->trans_x(), m_cbs->trans_y(), m_cbs->trans_z());
......@@ -112,10 +112,15 @@ void GLWidget::recalcModelView()
oglScale(m_obj_sc, m_obj_sc, m_obj_sc);
oglTranslate(m_obj_pos[0], m_obj_pos[1], m_obj_pos[2]);
// ajout transformation in screen
m_cbs->modelViewMatrix()*= m_cbs->transfoMatrix();
newModel = 0;
if (m_cbs)
m_cbs->cb_updateMatrix();
oglPopModelViewMatrix();
}
void GLWidget::initializeGL()
......@@ -176,7 +181,8 @@ void GLWidget::mouseClickEvent(QMouseEvent* event)
void GLWidget::mouseMoveEvent(QMouseEvent* event)
{
if(m_state_modifier == Qt::NoModifier)
// move object only if no special keys pressed
if (!(m_state_modifier & ( Qt::ShiftModifier|Qt::ControlModifier|Qt::AltModifier| Qt::MetaModifier)))
{
int x = event->x();
int y = event->y();
......
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