Add color writing for OFF format. Fixes Vertex_color display.

This commit is contained in:
Maxime Gimeno 2017-06-26 16:57:01 +02:00
parent e362b83c00
commit 0111db6173
3 changed files with 73 additions and 6 deletions

View File

@ -556,13 +556,15 @@ void Scene_surface_mesh_item_priv::initializeBuffers(CGAL::Three::Viewer_interfa
item->buffers[Scene_surface_mesh_item_priv::Smooth_normals].release();
if(has_vcolors)
{
item->buffers[VColors].bind();
item->buffers[VColors].allocate(v_colors.data(),
item->buffers[Scene_surface_mesh_item_priv::VColors].bind();
item->buffers[Scene_surface_mesh_item_priv::VColors].allocate(v_colors.data(),
static_cast<int>(v_colors.size()*sizeof(cgal_gl_data)));
//program->enableAttributeArray("colors");
program->enableAttributeArray("colors");
program->setAttributeBuffer("colors",CGAL_GL_DATA,0,3);
item->buffers[VColors].release();
item->buffers[Scene_surface_mesh_item_priv::VColors].release();
}
else
program->disableAttributeArray("colors");
item->vaos[Scene_surface_mesh_item_priv::Smooth_facets]->release();
program->release();

View File

@ -39,6 +39,7 @@
#include <CGAL/Surface_mesh/Surface_mesh.h>
#include <CGAL/Surface_mesh/Properties.h>
#include <CGAL/Kernel_traits.h>
#include <CGAL/boost/graph/graph_traits_Surface_mesh.h>
//=============================================================================
@ -446,7 +447,50 @@ bool write_mesh(const Surface_mesh<K>& mesh, const std::string& filename)
/// group io
/// @}
template <class P, class Writer>
void
generic_print_surface_mesh( std::ostream& out,
const Surface_mesh<P>& M,
Writer& writer) {
// writes M to `out' in the format provided by `writer'.
typedef typename boost::graph_traits<Surface_mesh<P> >::vertex_iterator VCI;
typedef typename boost::graph_traits<Surface_mesh<P> >::face_iterator FCI;
typedef typename Surface_mesh<P>::Halfedge_around_face_circulator HFCC;
typedef typename boost::property_map<Surface_mesh<P>,CGAL::vertex_point_t>::type VPmap;
VPmap map = get(CGAL::vertex_point, M);
// Print header.
writer.write_header( out,
num_vertices(M),
num_halfedges(M),
num_faces(M));
std::map<typename Surface_mesh<P>::vertex_index, std::size_t> index_map;
typename std::map<typename Surface_mesh<P>::vertex_index, std::size_t>::iterator hint = index_map.begin();
std::size_t id = 0;
for( VCI vi = vertices(M).begin(); vi != vertices(M).end(); ++vi) {
writer.write_vertex( ::CGAL::to_double( get(map, *vi).x()),
::CGAL::to_double( get(map, *vi).y()),
::CGAL::to_double( get(map, *vi).z()));
hint = index_map.insert(hint, std::make_pair(*vi, id++));
}
writer.write_facet_header();
for( FCI fi = faces(M).begin(); fi != faces(M).end(); ++fi) {
HFCC hc(halfedge(*fi, M), M);
HFCC hc_end = hc;
std::size_t n = circulator_size( hc);
CGAL_assertion( n >= 3);
writer.write_facet_begin( n);
do {
writer.write_facet_vertex_index(index_map[target(*hc, M)]);
++hc;
} while( hc != hc_end);
writer.write_facet_end();
}
writer.write_footer();
}
} // CGAL

View File

@ -1969,12 +1969,28 @@ private: //------------------------------------------------------- private data
typedef typename Mesh::Vertex_index Vertex_index;
typedef typename Mesh::Face_index Face_index;
os << "OFF\n" << sm.number_of_vertices() << " " << sm.number_of_faces() << " 0\n";
typename Mesh::template Property_map<typename Mesh::Vertex_index, CGAL::Color> vcolors;
bool has_vcolors;
boost::tie(vcolors, has_vcolors) = sm.template property_map<typename Mesh::Vertex_index, CGAL::Color >("v:color");
typename Mesh::template Property_map<typename Mesh::Face_index, CGAL::Color> fcolors;
bool has_fcolors;
boost::tie(fcolors, has_fcolors) = sm.template property_map<typename Mesh::Face_index, CGAL::Color >("f:color");
if(!has_fcolors && !has_vcolors)
os << "OFF\n" << sm.number_of_vertices() << " " << sm.number_of_faces() << " 0\n";
else
os << "COFF\n" << sm.number_of_vertices() << " " << sm.number_of_faces() << " 0\n";
std::vector<int> reindex;
reindex.resize(sm.num_vertices());
int n = 0;
BOOST_FOREACH(Vertex_index v, sm.vertices()){
os << sm.point(v) << '\n';
os << sm.point(v);
if(has_vcolors)
{
CGAL::Color color = vcolors[v];
os <<" "<< static_cast<int>(color.r())<<" "<< static_cast<int>(color.g())<<" "<< static_cast<int>(color.b());
}
os << '\n';
reindex[v]=n++;
}
@ -1983,6 +1999,11 @@ private: //------------------------------------------------------- private data
BOOST_FOREACH(Vertex_index v, CGAL::vertices_around_face(sm.halfedge(f),sm)){
os << " " << reindex[v];
}
if(has_fcolors)
{
CGAL::Color color = fcolors[f];
os <<" "<< static_cast<int>(color.r())<<" "<< static_cast<int>(color.g())<<" "<< static_cast<int>(color.b());
}
os << '\n';
}
return os;