Commit 614b1ff0 authored by Sylvain Thery's avatar Sylvain Thery

Ajout rendu svg fill de fer (exemple dans tuto1 et tuto5)

parent 08bd3bac
...@@ -22,8 +22,6 @@ ...@@ -22,8 +22,6 @@
* * * *
*******************************************************************************/ *******************************************************************************/
//#define GL3_PROTOTYPES
#include "tuto1.h" #include "tuto1.h"
#include <iostream> #include <iostream>
...@@ -41,6 +39,9 @@ ...@@ -41,6 +39,9 @@
#include <glm/gtc/type_ptr.hpp> #include <glm/gtc/type_ptr.hpp>
#include "Algo/Render/SVG/mapSVGRender.h"
using namespace CGoGN ; using namespace CGoGN ;
/** /**
...@@ -53,6 +54,11 @@ struct PFP: public PFP_STANDARD ...@@ -53,6 +54,11 @@ struct PFP: public PFP_STANDARD
typedef EmbeddedMap2<Map2> MAP; typedef EmbeddedMap2<Map2> MAP;
}; };
// declaration of the map
PFP::MAP myMap;
// and attribute of position
AttributeHandler<PFP::VEC3> position;
void MyQT::cb_initGL() void MyQT::cb_initGL()
{ {
// choose to use GL version 2 // choose to use GL version 2
...@@ -99,12 +105,21 @@ void MyQT::cb_keyPress(int code) ...@@ -99,12 +105,21 @@ void MyQT::cb_keyPress(int code)
if ((code >'0') && (code<='9')) if ((code >'0') && (code<='9'))
CGoGNout << " key num " << code-'0' << "pressed"<< CGoGNendl; CGoGNout << " key num " << code-'0' << "pressed"<< CGoGNendl;
if (code == 's')
{
std::string filename = selectFileSave("Export SVG file ");
CGoGNout << "Exporting "<<filename<<CGoGNendl;
Algo::Render::SVG::SVGOut svg(filename,modelViewMatrix(),projectionMatrix());
svg.renderLinesToSVG<PFP>(myMap,position);
svg.setColor(Geom::Vec3f(0.7f,0.0f,0.4f));
svg.renderFacesToSVG<PFP>(myMap,position,0.8f);
//svg destruction close the file
}
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
// declaration of the map
PFP::MAP myMap;
// creation of 2 new faces: 1 triangle and 1 square // creation of 2 new faces: 1 triangle and 1 square
Dart d1 = myMap.newFace(3); Dart d1 = myMap.newFace(3);
...@@ -115,7 +130,7 @@ int main(int argc, char **argv) ...@@ -115,7 +130,7 @@ int main(int argc, char **argv)
// creation of a new attribute on vertices of type 3D vector // creation of a new attribute on vertices of type 3D vector
// a handler to this attribute is returned // a handler to this attribute is returned
AttributeHandler<PFP::VEC3> position = myMap.addAttribute<PFP::VEC3>(VERTEX_ORBIT, "position"); position = myMap.addAttribute<PFP::VEC3>(VERTEX_ORBIT, "position");
// affect a position to the vertices of the mesh // affect a position to the vertices of the mesh
position[d1] = PFP::VEC3(0, 0, 0); position[d1] = PFP::VEC3(0, 0, 0);
......
...@@ -49,6 +49,7 @@ ...@@ -49,6 +49,7 @@
#include "Utils/shaderVectorPerVertex.h" #include "Utils/shaderVectorPerVertex.h"
#include "Utils/cgognStream.h" #include "Utils/cgognStream.h"
#include "Algo/Render/SVG/mapSVGRender.h"
using namespace CGoGN ; using namespace CGoGN ;
...@@ -232,10 +233,6 @@ void MyQT::cb_mousePress(int button, int x, int y) ...@@ -232,10 +233,6 @@ void MyQT::cb_mousePress(int button, int x, int y)
Dart d = m_render_topo->picking<PFP>(myMap, allDarts, x, getHeight() - y); Dart d = m_render_topo->picking<PFP>(myMap, allDarts, x, getHeight() - y);
if (d != Dart::nil()) if (d != Dart::nil())
{ {
// std::stringstream ss;
// ss << "Dart "<< d << " clicked"<< CGoGNendl;
// statusMsg(ss.str().c_str());
CGoGNout << "Dart "<< d << " clicked" << CGoGNendl; CGoGNout << "Dart "<< d << " clicked" << CGoGNendl;
} }
else else
...@@ -245,6 +242,21 @@ void MyQT::cb_mousePress(int button, int x, int y) ...@@ -245,6 +242,21 @@ void MyQT::cb_mousePress(int button, int x, int y)
} }
} }
void MyQT::cb_keyPress(int code)
{
if (code == 's')
{
std::string filename = selectFileSave("Export SVG file ");
CGoGNout << "Exporting "<<filename<<CGoGNendl;
Algo::Render::SVG::SVGOut svg(filename,modelViewMatrix(),projectionMatrix());
svg.renderLinesToSVG<PFP>(myMap,position);
svg.setColor(Geom::Vec3f(1.,0.,0.));
svg.renderFacesToSVG<PFP>(myMap,position,0.8f);
//svg destruction close the file
}
}
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");
......
...@@ -97,8 +97,7 @@ protected: ...@@ -97,8 +97,7 @@ protected:
void cb_mousePress(int button, int x, int y); void cb_mousePress(int button, int x, int y);
void cb_keyPress(int code) void cb_keyPress(int code);
{}
// slots locaux // slots locaux
public slots: public slots:
......
...@@ -136,12 +136,6 @@ public: ...@@ -136,12 +136,6 @@ public:
template <typename PFP> template <typename PFP>
void initPoints(typename PFP::MAP& map, const FunctorSelect& good,std::vector<GLuint>& tableIndices, unsigned int thread=0) ; void initPoints(typename PFP::MAP& map, const FunctorSelect& good,std::vector<GLuint>& tableIndices, unsigned int thread=0) ;
/**
* creation of VBO for flat faces rendering
*/
// template <typename PFP>
// void initFlatTriangles(typename PFP::MAP& map, const Utils::GL2& vboposition, const FunctorSelect& good=SelectorTrue() , unsigned int thread=0);
/** /**
* initialization of the VBO indices primitives * initialization of the VBO indices primitives
* computed by a traversal of the map * computed by a traversal of the map
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include "Topology/generic/dartmarker.h" #include "Topology/generic/dartmarker.h"
#include "Topology/generic/cellmarker.h" #include "Topology/generic/cellmarker.h"
#include "Utils/vbo.h"
namespace CGoGN namespace CGoGN
{ {
...@@ -210,60 +211,7 @@ void MapRender::initPoints(typename PFP::MAP& map, const FunctorSelect& good, st ...@@ -210,60 +211,7 @@ void MapRender::initPoints(typename PFP::MAP& map, const FunctorSelect& good, st
} }
} }
//template<typename PFP>
//void MapRender::initFlatTriangles( typename PFP::MAP& map, const Utils::GL2& vbopos, const FunctorSelect& good, unsigned int thread)
//{
// std::vector<Geom::Vec3f> tableFlat;
// tableFlat.reserve(3*map.getNbDarts()); // 3 in case of polygonal faces (less chance of realloc, but bigger allocation)
//
// // map VBO of points for vertices positions
// glBindBufferARB(GL_ARRAY_BUFFER, vbopos.id());
// Geom::Vec3f* tablePos = reinterpret_cast<Geom::Vec3f*>(glMapBuffer(GL_ARRAY_BUFFER,GL_READ_ONLY));
//
// m_nbFlatElts=0;
// // traversal of map for creating buffers
// DartMarker m(map,thread);
// for(Dart dd = map.begin(); dd != map.end(); map.next(dd))
// {
// if(!m.isMarked(dd) && good(dd))
// {
// Dart a = dd;
// Dart b = map.phi1(a);
// Dart c = map.phi1(b);
//
// Geom::Vec3f& P = tablePos[map.getEmbedding(a, VERTEX_ORBIT)];
// Geom::Vec3f& Q = tablePos[map.getEmbedding(b, VERTEX_ORBIT)];
// Geom::Vec3f& R = tablePos[map.getEmbedding(c, VERTEX_ORBIT)];
//
// Geom::Vec3f U = Q-P;
// Geom::Vec3f V = R-P;
// Geom::Vec3f N = U^V;
// N.normalize();
//
// // loop to cut a polygon in triangle on the fly (works only with convex faces)
// do
// {
// tableFlat.push_back(tablePos[map.getEmbedding(a, VERTEX_ORBIT)]);
// tableFlat.push_back(N);
// tableFlat.push_back(tablePos[map.getEmbedding(b, VERTEX_ORBIT)]);
// tableFlat.push_back(N);
// tableFlat.push_back(tablePos[map.getEmbedding(c, VERTEX_ORBIT)]);
// tableFlat.push_back(N);
// b = c;
// c = map.phi1(b);
// } while (c != dd);
// m.markOrbit(FACE_ORBIT, dd);
// }
// }
// glUnmapBuffer(GL_ARRAY_BUFFER);
//
// m_nbFlatElts = tableFlat.size()/2;
//
// //creating VBO for flat
// glBindBufferARB(GL_ARRAY_BUFFER, m_VBOBuffers[FLAT_BUFFER]);
// glBufferDataARB(GL_ARRAY_BUFFER, tableFlat.size() * sizeof(Geom::Vec3f), (char*)(&(tableFlat[0])), GL_STREAM_DRAW);
//
//}
template<typename PFP> template<typename PFP>
void MapRender::initPrimitives(typename PFP::MAP& map, const FunctorSelect& good, int prim, bool optimized, unsigned int thread) void MapRender::initPrimitives(typename PFP::MAP& map, const FunctorSelect& good, int prim, bool optimized, unsigned int thread)
......
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps *
* version 0.1 *
* Copyright (C) 2009, IGG Team, LSIIT, University of Strasbourg *
* *
* This library is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your *
* option) any later version. *
* *
* This library is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this library; if not, write to the Free Software Foundation, *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
* *
* Web site: https://iggservis.u-strasbg.fr/CGoGN/ *
* Contact information: cgogn@unistra.fr *
* *
*******************************************************************************/
#ifndef _MAP_SVG_RENDER
#define _MAP_SVG_RENDER
#include <vector>
#include <fstream>
#include <sstream>
#include "Geometry/vector_gen.h"
#include "Topology/generic/functor.h"
#include "Topology/generic/dartmarker.h"
#include "glm/gtc/matrix_transform.hpp"
#include "glm/gtc/type_precision.hpp"
#include "glm/glm.hpp"
#include "glm/gtc/matrix_projection.hpp"
#include "glm/gtc/matrix_transform.hpp"
/**
* A set of functions that allow the creation of rendering
* object using Vertex-Buffer-Object.
* Function are made for dual-2-map and can be used on
* any subset of a dual-N-map which is a 2-map
*/
namespace CGoGN
{
namespace Algo
{
namespace Render
{
namespace SVG
{
class SvgObj
{
protected:
Geom::Vec3f m_color;
public:
virtual void save(std::ofstream& out)=0;
};
class SvgPolyline: public SvgObj
{
protected:
std::vector<Geom::Vec3f> m_vertices;
float m_width;
public:
void addVertex(const Geom::Vec3f& v);
void close();
void setColor(const Geom::Vec3f& c) { m_color=c;}
void setWidth(float w) { m_width=w;}
void save(std::ofstream& out);
};
class Svg_Polygon: public SvgObj
{
protected:
std::vector<Geom::Vec3f> m_vertices;
public:
void addVertex(const Geom::Vec3f& v) {}
void close() {}
void save(std::ofstream& out) {}
};
class SVGOut
{
protected:
std::ofstream* m_out;
const glm::mat4& m_model;
const glm::mat4& m_proj;
glm::i32vec4 m_viewport;
Geom::Vec3f global_color;
float global_width;
std::vector<SvgObj*> m_objs;
public:
/**
* Object that allow the rendering/exporting in svg file
* @param filename file name ended by .svg
* @param model the modelview matrix
* @param proj the projection matrix
*/
SVGOut(const std::string& filename, const glm::mat4& model, const glm::mat4& proj);
/**
* destructor
* flush and close the file
*/
~SVGOut();
void setColor(const Geom::Vec3f& col);
void setWidth(float w);
void closeFile();
template <typename PFP>
void renderLinesToSVG(typename PFP::MAP& map, const typename PFP::TVEC3& position, const FunctorSelect& good = SelectorTrue(), unsigned int thread=0);
template <typename PFP>
void renderFacesToSVG(typename PFP::MAP& map, const typename PFP::TVEC3& position, float shrink, const FunctorSelect& good = SelectorTrue(), unsigned int thread=0);
};
} // namespace SVG
} // namespace Render
} // namespace Algo
} // namespace CGoGN
#include "Algo/Render/SVG/mapSVGRender.hpp"
#endif
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps *
* version 0.1 *
* Copyright (C) 2009, IGG Team, LSIIT, University of Strasbourg *
* *
* This library is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your *
* option) any later version. *
* *
* This library is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this library; if not, write to the Free Software Foundation, *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
* *
* Web site: https://iggservis.u-strasbg.fr/CGoGN/ *
* Contact information: cgogn@unistra.fr *
* *
*******************************************************************************/
#include "GL/glew.h"
#include "Algo/Geometry/centroid.h"
namespace CGoGN
{
namespace Algo
{
namespace Render
{
namespace SVG
{
template <typename PFP>
void SVGOut::renderLinesToSVG(typename PFP::MAP& map, const typename PFP::TVEC3& position, const FunctorSelect& good, unsigned int thread)
{
glm::i32vec4 viewport;
glGetIntegerv(GL_VIEWPORT, &(viewport[0]));
DartMarker m(map, thread);
for(Dart d = map.begin(); d != map.end(); map.next(d))
{
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);
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);
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->setColor(global_color);
pol->setWidth(global_width);
m_objs.push_back(pol);
m.markOrbit(EDGE_ORBIT, d);
}
}
}
template <typename PFP>
void SVGOut::renderFacesToSVG(typename PFP::MAP& map, const typename PFP::TVEC3& position, float shrink, const FunctorSelect& good, unsigned int thread)
{
glm::i32vec4 viewport;
glGetIntegerv(GL_VIEWPORT, &(viewport[0]));
DartMarker m(map, thread);
for(Dart d = map.begin(); d != map.end(); map.next(d))
{
if(!m.isMarked(d) && good(d))
{
typename PFP::VEC3 center = Algo::Geometry::faceCentroid<PFP>(map,d,position);
SvgPolyline* pol = new SvgPolyline();
Dart dd = d;
do
{
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);
pol->addVertex(Geom::Vec3f(Q[0],float(viewport[3])-Q[1],Q[2]));
d = map.phi1(d);
}while (d!=dd);
pol->close();
pol->setColor(global_color);
pol->setWidth(global_width);
m_objs.push_back(pol);
m.markOrbit(FACE_ORBIT, d);
}
}
}
} // namespace SVG
} // namespace Render
} // namespace Algo
} // namespace CGoGN
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#ifndef __FUNCTOR_H__ #ifndef __FUNCTOR_H__
#define __FUNCTOR_H__ #define __FUNCTOR_H__
#include "Topology/generic/dart.h"
#include "Topology/generic/marker.h" #include "Topology/generic/marker.h"
#include <vector> #include <vector>
......
...@@ -192,12 +192,14 @@ public: ...@@ -192,12 +192,14 @@ public:
/** /**
* current modelview matrix * current modelview matrix
*/ */
glm::mat4& modelViewMatrix () { return m_modelView_matrix; } const glm::mat4& modelViewMatrix() const { return m_modelView_matrix; }
glm::mat4& modelViewMatrix() { return m_modelView_matrix; }
/** /**
* current projection matrix * current projection matrix
*/ */
glm::mat4& projectionMatrix () { return m_projection_matrix; } const glm::mat4& projectionMatrix() const { return m_projection_matrix; }
glm::mat4& projectionMatrix() { return m_projection_matrix; }
float* curquat() { return m_curquat; } float* curquat() { return m_curquat; }
...@@ -312,6 +314,15 @@ public: ...@@ -312,6 +314,15 @@ public:
*/ */
std::string selectFile(const std::string& title = "open file", const std::string& dir = ".", const std::string& filters = "all (*.*)"); std::string selectFile(const std::string& title = "open file", const std::string& dir = ".", const std::string& filters = "all (*.*)");
/**
* Open a file selector and return the filename (for saving a file)
* @param title title of window
* @param dir base directory
* @param filters file filters (syntax: "label1 (filter1);; label2 (filter2);; ...")
*/
std::string selectFileSave(const std::string& title = "open file", const std::string& dir = ".", const std::string& filters = "all (*.*)");
public slots: public slots:
virtual void cb_New() { std::cerr << "callback not implemented" << std::endl; } virtual void cb_New() { std::cerr << "callback not implemented" << std::endl; }
virtual void cb_Open() { std::cerr << "callback not implemented" << std::endl; } virtual void cb_Open() { std::cerr << "callback not implemented" << std::endl; }
......
...@@ -56,7 +56,7 @@ protected: ...@@ -56,7 +56,7 @@ protected:
// shaders that ref this vbo // shaders that ref this vbo
std::vector<GLSLShader*> m_refs; std::vector<GLSLShader*> m_refs;
unsigned int m_nbElts; unsigned int m_nbElts;
bool m_lock; mutable bool m_lock;
public: public:
/** /**
...@@ -98,6 +98,9 @@ public: ...@@ -98,6 +98,9 @@ public:
*/ */
void ref(GLSLShader* sh); void ref(GLSLShader* sh);
void sameAllocSameBufferSize(const VBO& vbo);
/** /**
* update data from attribute handler to the vbo * update data from attribute handler to the vbo
*/ */
...@@ -112,7 +115,9 @@ public: ...@@ -112,7 +115,9 @@ public:
void* lockPtr(); void* lockPtr();
void releasePtr(); const void* lockPtr() const;
void releasePtr() const;
unsigned int nbElts() {return m_nbElts;} unsigned int nbElts() {return m_nbElts;}
}; };
......
...@@ -81,14 +81,6 @@ void MapRender::initPrimitives(int prim, std::vector<GLuint>& tableIndices) ...@@ -81,14 +81,6 @@ void MapRender::initPrimitives(int prim, std::vector<GLuint>& tableIndices)
void MapRender::draw(Utils::GLSLShader* sh, int prim) void MapRender::draw(Utils::GLSLShader* sh, int prim)
{ {
sh->enableVertexAttribs(); sh->enableVertexAttribs();
// const std::vector<Utils::GLSLShader::VAStr>& vastr = sh->getVA_VBO_Bindings();
//
// for (std::vector<Utils::GLSLShader::VAStr>::const_iterator it= vastr.begin(); it != vastr.end(); ++it)
// {
// glBindBuffer(GL_ARRAY_BUFFER, it->vbo_ptr->id());
// glEnableVertexAttribArray(it->va_id);
// glVertexAttribPointer(it->va_id ,it->vbo_ptr->dataSize(), GL_FLOAT, false, 0, 0);
// }
switch(prim) switch(prim)
{ {
...@@ -108,10 +100,6 @@ void MapRender::draw(Utils::GLSLShader* sh, int prim) ...@@ -108,10 +100,6 @@ void MapRender::draw(Utils::GLSLShader* sh, int prim)
break; break;
} }