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) ...@@ -112,6 +112,16 @@ void MyQT::slider_text(int x)
updateGL(); 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() void MyQT::storeVerticesInfo()
{ {
...@@ -183,6 +193,11 @@ void MyQT::cb_initGL() ...@@ -183,6 +193,11 @@ void MyQT::cb_initGL()
m_render->initPrimitives<PFP>(myMap, allDarts, Algo::Render::GL2::POINTS); m_render->initPrimitives<PFP>(myMap, allDarts, Algo::Render::GL2::POINTS);
m_render_topo->updateData<PFP>(myMap, allDarts, position, 0.9, 0.9, 0.9); 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() void MyQT::cb_redraw()
...@@ -230,7 +245,7 @@ void MyQT::cb_mousePress(int button, int x, int y) ...@@ -230,7 +245,7 @@ void MyQT::cb_mousePress(int button, int x, int y)
{ {
if (Shift()) 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()) if (d != Dart::nil())
{ {
CGoGNout << "Dart "<< d << " clicked" << CGoGNendl; CGoGNout << "Dart "<< d << " clicked" << CGoGNendl;
...@@ -254,9 +269,17 @@ void MyQT::cb_keyPress(int code) ...@@ -254,9 +269,17 @@ void MyQT::cb_keyPress(int code)
svg.renderFacesToSVG<PFP>(myMap,position,0.8f); svg.renderFacesToSVG<PFP>(myMap,position,0.8f);
//svg destruction close the file //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) int main(int argc, char **argv)
{ {
position = myMap.addAttribute<PFP::VEC3>(VERTEX_ORBIT, "position"); position = myMap.addAttribute<PFP::VEC3>(VERTEX_ORBIT, "position");
...@@ -322,7 +345,8 @@ int main(int argc, char **argv) ...@@ -322,7 +345,8 @@ int main(int argc, char **argv)
CGoGNdbg2.toConsole(&sqt); CGoGNdbg2.toConsole(&sqt);
CGoGNerr.toConsole(&sqt); CGoGNerr.toConsole(&sqt);
CGoGNdbg2 << " TextureSize " << texSize << CGoGNendl; CGoGNdbg2 << " TextureSize " << texSize << CGoGNendl;
CGoGNerr << " ERROR " << 5*7 << CGoGNflush; CGoGNerr << " test ERROR " << 5*7 << CGoGNflush;
// et on attend la fin. // et on attend la fin.
......
...@@ -72,6 +72,8 @@ class MyQT: public Utils::QT::SimpleQT ...@@ -72,6 +72,8 @@ class MyQT: public Utils::QT::SimpleQT
Utils::Strings3D* m_strings; Utils::Strings3D* m_strings;
Utils::PointSprite* m_sprite; Utils::PointSprite* m_sprite;
QTimer *m_timer;
public: public:
MyQT(): MyQT():
render_text(true), render_text(true),
...@@ -85,7 +87,8 @@ public: ...@@ -85,7 +87,8 @@ public:
m_shader(NULL), m_shader(NULL),
m_lines(NULL), m_lines(NULL),
m_strings(NULL), m_strings(NULL),
m_sprite(NULL) m_sprite(NULL),
m_timer(NULL)
{} {}
protected: protected:
...@@ -109,6 +112,8 @@ public slots: ...@@ -109,6 +112,8 @@ public slots:
void slider_balls(int x); void slider_balls(int x);
void slider_vectors(int x); void slider_vectors(int x);
void slider_text(int x); void slider_text(int x);
void animate();
}; };
#endif #endif
...@@ -63,11 +63,14 @@ class SvgObj ...@@ -63,11 +63,14 @@ class SvgObj
{ {
protected: protected:
std::vector<Geom::Vec3f> m_vertices; std::vector<Geom::Vec3f> m_vertices;
std::vector<Geom::Vec3f> m_vertices3D;
Geom::Vec3f m_color; Geom::Vec3f m_color;
float m_width; float m_width;
public: public:
void addVertex(const Geom::Vec3f& v); void addVertex(const Geom::Vec3f& v);
void addVertex3D(const Geom::Vec3f& v);
void setColor(const Geom::Vec3f& c); void setColor(const Geom::Vec3f& c);
void setWidth(float w) { m_width=w;} void setWidth(float w) { m_width=w;}
...@@ -76,9 +79,9 @@ public: ...@@ -76,9 +79,9 @@ public:
virtual void save(std::ofstream& out)=0; 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(); Geom::Vec3f normal();
...@@ -150,17 +153,35 @@ public: ...@@ -150,17 +153,35 @@ public:
template <typename PFP> 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 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 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); 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 } // namespace SVG
......
...@@ -49,15 +49,22 @@ void SVGOut::renderLinesToSVG(typename PFP::MAP& map, const typename PFP::TVEC3& ...@@ -49,15 +49,22 @@ void SVGOut::renderLinesToSVG(typename PFP::MAP& map, const typename PFP::TVEC3&
{ {
if(!m.isMarked(d) && good(d)) if(!m.isMarked(d) && good(d))
{ {
const Geom::Vec3f& P = position[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 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)]; 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 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(); SvgPolyline* pol = new SvgPolyline();
pol->addVertex(Geom::Vec3f(Q[0],float(viewport[3])-Q[1],Q[2])); 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->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->setColor(global_color);
pol->setWidth(global_width); pol->setWidth(global_width);
m_objs.push_back(pol); m_objs.push_back(pol);
...@@ -86,7 +93,9 @@ void SVGOut::renderFacesToSVG(typename PFP::MAP& map, const typename PFP::TVEC3& ...@@ -86,7 +93,9 @@ void SVGOut::renderFacesToSVG(typename PFP::MAP& map, const typename PFP::TVEC3&
Geom::Vec3f P = position[d]; Geom::Vec3f P = position[d];
P = P*shrink + center*(1.0f-shrink); 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 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->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); d = map.phi1(d);
}while (d!=dd); }while (d!=dd);
......
...@@ -150,6 +150,7 @@ protected: ...@@ -150,6 +150,7 @@ protected:
// mouse & matrix // mouse & matrix
glm::mat4 m_projection_matrix; glm::mat4 m_projection_matrix;
glm::mat4 m_modelView_matrix; glm::mat4 m_modelView_matrix;
glm::mat4 m_transfo_matrix;
float m_curquat[4]; float m_curquat[4];
float m_lastquat[4]; float m_lastquat[4];
float m_trans_x; float m_trans_x;
...@@ -162,6 +163,9 @@ protected: ...@@ -162,6 +163,9 @@ protected:
std::string m_helpString; std::string m_helpString;
std::stack<glm::mat4> m_stack_trf;
void keyPressEvent(QKeyEvent *event); void keyPressEvent(QKeyEvent *event);
void keyReleaseEvent(QKeyEvent *e); void keyReleaseEvent(QKeyEvent *e);
...@@ -193,6 +197,9 @@ public: ...@@ -193,6 +197,9 @@ public:
*/ */
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);
const glm::mat4& transfoMatrix() const { return m_transfo_matrix; }
glm::mat4& transfoMatrix() { return m_transfo_matrix; }
/** /**
* current modelview matrix * current modelview matrix
*/ */
...@@ -315,6 +322,33 @@ public: ...@@ -315,6 +322,33 @@ public:
*/ */
void updateGLMatrices(); 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 * Open a file selector and return the filename
* @param title title of window * @param title title of window
......
...@@ -48,6 +48,12 @@ void SvgObj::addVertex(const Geom::Vec3f& v) ...@@ -48,6 +48,12 @@ void SvgObj::addVertex(const Geom::Vec3f& v)
m_vertices.push_back(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) void SvgObj::setColor(const Geom::Vec3f& c)
{ {
...@@ -69,8 +75,8 @@ Geom::Vec3f SvgObj::normal() ...@@ -69,8 +75,8 @@ Geom::Vec3f SvgObj::normal()
return Geom::Vec3f(0.0f,0.0f,0.0f); return Geom::Vec3f(0.0f,0.0f,0.0f);
} }
Geom::Vec3f U = m_vertices[2] - m_vertices[1]; Geom::Vec3f U = m_vertices3D[2] - m_vertices3D[1];
Geom::Vec3f V = m_vertices[0] - m_vertices[1]; Geom::Vec3f V = m_vertices3D[0] - m_vertices3D[1];
Geom::Vec3f N = U^V; Geom::Vec3f N = U^V;
...@@ -80,6 +86,7 @@ Geom::Vec3f SvgObj::normal() ...@@ -80,6 +86,7 @@ Geom::Vec3f SvgObj::normal()
} }
void SvgPolyline::save(std::ofstream& out) void SvgPolyline::save(std::ofstream& out)
{ {
std::stringstream ss; std::stringstream ss;
...@@ -201,11 +208,12 @@ void SVGOut::setWidth(float w) ...@@ -201,11 +208,12 @@ void SVGOut::setWidth(float w)
void SVGOut::closeFile() void SVGOut::closeFile()
{ {
// here do the sort in necessary // here do the sort in necessary
compSvgObj cmp; compNormObj cmp;
// std::sort(m_objs.begin(),m_objs.end(),cmp); std::sort(m_objs.begin(),m_objs.end(),cmp);
bubble_sort(m_objs,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); (*it)->save(*m_out);
} }
...@@ -217,10 +225,11 @@ void SVGOut::closeFile() ...@@ -217,10 +225,11 @@ void SVGOut::closeFile()
// all points behind the plane + // all points behind the plane +1
// all points before the plane - // all points before the plane -1
// all points colinear to the plane 0 // 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(); Geom::Vec3f N = pol_plane->normal();
...@@ -231,9 +240,13 @@ int compSvgObj::points_plane (SvgPolygon* pol_points, SvgPolygon* pol_plane) ...@@ -231,9 +240,13 @@ int compSvgObj::points_plane (SvgPolygon* pol_points, SvgPolygon* pol_plane)
unsigned int nbback=0; unsigned int nbback=0;
unsigned int nbfront=0; unsigned int nbfront=0;
unsigned int nb_col=0; unsigned int nb_col=0;
averageZ=0.0f;
for (unsigned int i=0; i< nb; ++i) 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; float ps = U*N;
if (fabs(ps) < 0.0001f) if (fabs(ps) < 0.0001f)
...@@ -247,29 +260,55 @@ int compSvgObj::points_plane (SvgPolygon* pol_points, SvgPolygon* pol_plane) ...@@ -247,29 +260,55 @@ int compSvgObj::points_plane (SvgPolygon* pol_points, SvgPolygon* pol_plane)
} }
} }
averageZ /= float(nb);
if (nbfront==0) if (nbfront==0)
return 1; return 1;
if (nbback==0) if (nbback==0)
return -1; return -1;
return 0; if (nb_col==nb)
return 0;
return 999;
} }
bool compSvgObj::operator() (SvgObj* a, SvgObj*b) 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_a = dynamic_cast<SvgPolygon*>(a);
SvgPolygon* p_b = dynamic_cast<SvgPolygon*>(b); SvgPolygon* p_b = dynamic_cast<SvgPolygon*>(b);
if ((p_a!= NULL) && (p_b!=NULL)) // first case polygon/polygon 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; return true;
if (points_plane(p_b,p_a)<0)
// all point of b infront of a
if ((t2 == -1) && (t1==999))
return true; return true;
if ((t1 == t2 )&& (t2!=999))
{
return avz_a > avz_b;
}
// all other cases ??
return false; return false;
} }
...@@ -278,31 +317,32 @@ bool compSvgObj::operator() (SvgObj* a, SvgObj*b) ...@@ -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; SvgPolygon* p_a = dynamic_cast<SvgPolygon*>(a);
unsigned int nbswap=1; SvgPolygon* p_b = dynamic_cast<SvgPolygon*>(b);
while (nbswap>0)
if ((p_a!= NULL) && (p_b!=NULL)) // first case polygon/polygon
{ {
nbswap=0; Geom::Vec3f Na = p_a->normal();
for (unsigned int i=0; i<nb;++i) Geom::Vec3f Nb = p_b->normal();
{ return fabs(Na[2]) > fabs(Nb[2]);
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;
} }
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 SVG
} // namespace Render } // namespace Render
} // namespace Algo } // namespace Algo
......
...@@ -100,6 +100,8 @@ m_dock(NULL) ...@@ -100,6 +100,8 @@ m_dock(NULL)
m_dockConsole->setWidget(m_textConsole); m_dockConsole->setWidget(m_textConsole);
m_dockConsole->hide(); m_dockConsole->hide();
m_transfo_matrix = glm::mat4(1.0f);
} }
SimpleQT::~SimpleQT() SimpleQT::~SimpleQT()
...@@ -267,7 +269,6 @@ void SimpleQT::updateGL() ...@@ -267,7 +269,6 @@ void SimpleQT::updateGL()
void SimpleQT::updateGLMatrices() void SimpleQT::updateGLMatrices()
{ {
m_glWidget->modelModified(); m_glWidget->modelModified();
cb_updateMatrix();
m_glWidget->updateGL(); m_glWidget->updateGL();
} }
...@@ -332,13 +333,11 @@ void SimpleQT::init_app_menu() ...@@ -332,13 +333,11 @@ void SimpleQT::init_app_menu()
void SimpleQT::registerShader(GLSLShader* ptr) void SimpleQT::registerShader(GLSLShader* ptr)
{ {
GLSLShader::registerShader(this, ptr) ; GLSLShader::registerShader(this, ptr) ;
// GLSLShader::m_registeredShaders.insert(std::pair<void*,GLSLShader*>(this, ptr));
} }
void SimpleQT::unregisterShader(GLSLShader* ptr) void SimpleQT::unregisterShader(GLSLShader* ptr)