diff --git a/Apps/Tuto/tuto5.cpp b/Apps/Tuto/tuto5.cpp index 260e9345d25452a22efd6ac847973972b0cefe84..7039a04350621f46382c87fb255b12b373ad0e37 100644 --- a/Apps/Tuto/tuto5.cpp +++ b/Apps/Tuto/tuto5.cpp @@ -249,8 +249,11 @@ void MyQT::cb_keyPress(int code) svg.setWidth(1.0f); svg.setColor(Geom::Vec3f(0.0f,0.0f,0.5f)); Algo::Render::SVG::renderEdges(svg,myMap,position); + svg.setColor(Geom::Vec3f(0.0f,0.8f,0.0f)); svg.setWidth(5.0f); Algo::Render::SVG::renderVertices(svg,myMap,position); + svg.setColor(Geom::Vec3f(1.0f,0.0f,0.0f)); + m_strings->toSVG(svg); //svg destruction close the file } if (code == 't') diff --git a/include/Utils/svg.h b/include/Utils/svg.h index 451fa125808eec2e7b8a680d12f0e33c4ef41e4d..3ced3668d7b4a99404a12d4bfac66d03e4832c91 100644 --- a/include/Utils/svg.h +++ b/include/Utils/svg.h @@ -28,6 +28,7 @@ #include #include #include +#include #include "Geometry/vector_gen.h" @@ -65,6 +66,7 @@ class SvgObj protected: std::vector m_vertices; std::vector m_colors; + std::vector m_strings; std::vector m_vertices3D; Geom::Vec3f m_color; float m_width; @@ -79,6 +81,10 @@ public: void addVertex3D(const Geom::Vec3f& v, const Geom::Vec3f& C); + void addString(const Geom::Vec3f& v, const std::string& str); + + void addString(const Geom::Vec3f& v, const std::string& str, const Geom::Vec3f& C); + void setColor(const Geom::Vec3f& c); void setWidth(float w); @@ -87,7 +93,7 @@ public: virtual void save(std::ofstream& out) const = 0; - virtual void saveOne(std::ofstream& out, unsigned int i) const = 0; + virtual void saveOne(std::ofstream& out, unsigned int i, unsigned int bbl = 0) const = 0; unsigned int nbv() const; @@ -107,7 +113,7 @@ class SvgPoints: public SvgObj { public: void save(std::ofstream& out) const; - void saveOne(std::ofstream& out, unsigned int i) const; + void saveOne(std::ofstream& out, unsigned int i, unsigned int bbl = 0) const; unsigned int nbPrimtives() const; void fillDS(std::vector& vds, unsigned int idObj) const; }; @@ -116,7 +122,20 @@ class SvgLines: public SvgObj { public: void save(std::ofstream& out) const; - void saveOne(std::ofstream& out, unsigned int i) const; + void saveOne(std::ofstream& out, unsigned int i, unsigned int bbl = 0) const; + unsigned int nbPrimtives() const; + void fillDS(std::vector& vds, unsigned int idObj) const; +}; + + +class SvgStrings: public SvgObj +{ +protected: + float m_sf; +public: + SvgStrings(float scalefactor = 1.0f) : m_sf(scalefactor) {} + void save(std::ofstream& out) const; + void saveOne(std::ofstream& out, unsigned int i, unsigned int bbl = 0) const; unsigned int nbPrimtives() const; void fillDS(std::vector& vds, unsigned int idObj) const; }; @@ -154,6 +173,14 @@ protected: std::vector m_objs; SvgObj* m_current; + + unsigned int m_bbX0; + unsigned int m_bbY0; + unsigned int m_bbX1; + unsigned int m_bbY1; + + + protected: void computeBB(unsigned int& a, unsigned int& b, unsigned int& c, unsigned& d); @@ -190,6 +217,13 @@ public: void addLine(const Geom::Vec3f& P, const Geom::Vec3f& P2); void addLine(const Geom::Vec3f& P, const Geom::Vec3f& P2, const Geom::Vec3f& C); + + void beginStrings(float scalefactor = 1.0f); + void endStrings(); + void addString(const Geom::Vec3f& P, const std::string& str); + void addString(const Geom::Vec3f& P, const Geom::Vec3f& Q, const std::string& str); + void addString(const Geom::Vec3f& P, const std::string& str, const Geom::Vec3f& C); + void sortSimpleDepth( std::vector& vds); }; diff --git a/include/Utils/text3d.h b/include/Utils/text3d.h index a59abbedd4a03671c5c98a4e58dc28394a4919b4..681765d8971c3bf2d059dde42111049fda00765f 100644 --- a/include/Utils/text3d.h +++ b/include/Utils/text3d.h @@ -28,10 +28,10 @@ #include "Utils/GLSLShader.h" #include "Geometry/vector_gen.h" +namespace CGoGN { namespace Utils { namespace SVG { class SVGOut ; } } } namespace CGoGN { - namespace Utils { @@ -67,6 +67,8 @@ protected: Utils::VBO* m_vbo1; + float m_scale; + unsigned int sendOneStringToVBO(const std::string& str, float **buffer); GLuint m_uniform_texture; @@ -137,6 +139,8 @@ public: */ void setScale(float scale); + + void toSVG(Utils::SVG::SVGOut& svg); }; } // namespace Utils diff --git a/src/Utils/svg.cpp b/src/Utils/svg.cpp index 5a611345c48f4195724d262c6bcd8fd40d2301c1..9a05031ac8c7387e6e168d750477e84691dd5099 100644 --- a/src/Utils/svg.cpp +++ b/src/Utils/svg.cpp @@ -70,6 +70,21 @@ void SvgObj::addVertex3D(const Geom::Vec3f& v, const Geom::Vec3f& c) } +void SvgObj::addString(const Geom::Vec3f& v, const std::string& str) +{ + m_vertices.push_back(v); + m_strings.push_back(str); + m_colors.push_back(m_color); +} + +void SvgObj::addString(const Geom::Vec3f& v, const std::string& str, const Geom::Vec3f& c) +{ + m_vertices.push_back(v); + m_strings.push_back(str); + m_colors.push_back(c); +} + + void SvgObj::setWidth(float w) { m_width=w; @@ -146,7 +161,7 @@ void SvgPoints::save(std::ofstream& out) const } -void SvgPoints::saveOne(std::ofstream& out, unsigned int i) const +void SvgPoints::saveOne(std::ofstream& out, unsigned int i, unsigned int bbl) const { std::stringstream ss; @@ -212,7 +227,7 @@ void SvgLines::save(std::ofstream& out) const } } -void SvgLines::saveOne(std::ofstream& out, unsigned int i) const +void SvgLines::saveOne(std::ofstream& out, unsigned int i, unsigned int bbl) const { std::stringstream ss; @@ -249,6 +264,59 @@ void SvgLines::fillDS(std::vector& vds, unsigned int idObj) const } + + +void SvgStrings::save(std::ofstream& out) const +{ + unsigned int nb = m_vertices.size(); + for (unsigned int i=0; i"; + out << ""<< m_strings[i]<< ""<< std::endl; +} + + +unsigned int SvgStrings::nbPrimtives() const +{ + return m_vertices.size(); +} + +void SvgStrings::fillDS(std::vector& vds, unsigned int idObj) const +{ + for (unsigned int i = 0; i< m_vertices.size(); ++i) + { + vds.push_back(DepthSort(idObj,i,m_vertices[i][2])); + } +} + + + //void SvgPolyline::save(std::ofstream& out) //{ // std::stringstream ss; @@ -368,17 +436,12 @@ void SVGOut::setWidth(float w) void SVGOut::closeFile() { - unsigned int a = 0; - unsigned int b = 0; - unsigned int c = 1024; - unsigned int d = 1024; - computeBB(a,b,c,d); + computeBB(m_bbX0, m_bbY0, m_bbX1, m_bbY1); *m_out << ""<< std::endl; *m_out << ""<< std::endl; - *m_out << "viewBox=\""<< a <<" "<< b <<" "<< c-a << " " << d-b <<"\">"<< std::endl; + *m_out << "viewBox=\""<< m_bbX0 <<" "<< m_bbY0 <<" "<< m_bbX1-m_bbX0 << " " << m_bbY1-m_bbY0 <<"\">"<< std::endl; *m_out << "test"<< std::endl; *m_out << ""<< std::endl; *m_out << "Rendered from CGoGN"<< std::endl; @@ -395,7 +458,7 @@ void SVGOut::closeFile() for (std::vector::iterator it = vds.begin(); it != vds.end(); ++it) { - m_objs[it->obj]->saveOne(*m_out,it->id); + m_objs[it->obj]->saveOne(*m_out,it->id, m_bbX1-m_bbX0); } // for (std::vector::iterator it = m_objs.begin(); it != m_objs.end(); ++it) @@ -446,9 +509,6 @@ void SVGOut::computeBB(unsigned int& a, unsigned int& b, unsigned int& c, unsign void SVGOut::beginPoints() { -// glm::i32vec4 viewport; -// glGetIntegerv(GL_VIEWPORT, &(viewport[0])); - m_current = new SvgPoints(); m_current->setColor(global_color); m_current->setWidth(global_width); @@ -528,6 +588,33 @@ void SVGOut::addLine(const Geom::Vec3f& P, const Geom::Vec3f& P2, const Geom::Ve } +void SVGOut::beginStrings(float scalefactor) +{ + m_current = new SvgStrings(scalefactor); + m_current->setColor(global_color); + m_current->setWidth(global_width); +} + +void SVGOut::endStrings() +{ + m_objs.push_back(m_current); + m_current = NULL; +} + +void SVGOut::addString(const Geom::Vec3f& P, const std::string& str) +{ + glm::vec3 Q = glm::project(glm::vec3(P[0],P[1],P[2]),m_model,m_proj,m_viewport); + m_current->addString(Geom::Vec3f(float(Q[0]),float(m_viewport[3])-float(Q[1]),float(Q[2])), str); +} + + +void SVGOut::addString(const Geom::Vec3f& P, const std::string& str, const Geom::Vec3f& C) +{ + glm::vec3 Q = glm::project(glm::vec3(P[0],P[1],P[2]),m_model,m_proj,m_viewport); + m_current->addString(Geom::Vec3f(float(Q[0]),float(m_viewport[3])-float(Q[1]),float(Q[2])), str, C); +} + + void SVGOut::sortSimpleDepth(std::vector& vds) { unsigned int nb=0; diff --git a/src/Utils/text3d.cpp b/src/Utils/text3d.cpp index bf4aea2fc7237b87b37711a5b1fcce96c7029ccc..390eb44c1c0e56a4a14c3140bba225b71d182bc8 100644 --- a/src/Utils/text3d.cpp +++ b/src/Utils/text3d.cpp @@ -23,8 +23,8 @@ *******************************************************************************/ #include "Utils/text3d.h" - #include "Utils/vbo.h" +#include "Utils/svg.h" namespace CGoGN { @@ -43,7 +43,7 @@ std::string Strings3D::fragmentShaderText2 = GLuint Strings3D::m_idTexture = 0xffffffff; -Strings3D::Strings3D(bool withBackground, const Geom::Vec3f& bgc) : m_nbChars(0) +Strings3D::Strings3D(bool withBackground, const Geom::Vec3f& bgc) : m_nbChars(0),m_scale(1.0f) { if (m_idTexture == 0xffffffff) { @@ -101,6 +101,7 @@ void Strings3D::setScale(float scale) { bind(); glUniform1f(m_uniform_scale, scale); + m_scale = scale; unbind(); } @@ -246,6 +247,15 @@ void Strings3D::drawAll(const Geom::Vec3f& color) postdraw(); } +void Strings3D::toSVG(Utils::SVG::SVGOut& svg) +{ + svg.beginStrings(m_scale); + unsigned int nb = m_strings.size(); + for(unsigned int i=0; i