Addition of the facet colors.

This commit is contained in:
Maxime Gimeno 2016-04-08 15:22:51 +02:00
parent f04a6f98ce
commit 3fdff7e831
3 changed files with 167 additions and 127 deletions

View File

@ -53,7 +53,8 @@ Scene_surface_mesh_item::Scene_surface_mesh_item(SMesh* sm)
idx_edge_data_.push_back(im[target(ed, *smesh_)]);
}
has_colors = false;
has_vcolors = false;
has_fcolors = false;
}
@ -78,13 +79,16 @@ void Scene_surface_mesh_item::initializeBuffers(CGAL::Three::Viewer_interface* v
SMesh::Property_map<vertex_descriptor, CGAL::Color> vcolors =
smesh_->property_map<vertex_descriptor, CGAL::Color >("v:color").first;
SMesh::Property_map<face_descriptor, CGAL::Color> fcolors =
smesh_->property_map<face_descriptor, CGAL::Color >("f:color").first;
assert(positions.data() != NULL);
assert(vnormals.data() != NULL);
if(smesh_->property_map<vertex_descriptor, CGAL::Color >("v:color").second)
has_colors = true;
has_vcolors = true;
if(smesh_->property_map<face_descriptor, CGAL::Color >("f:color").second)
has_fcolors = true;
//compute the Flat data
flat_vertices.clear();
@ -102,10 +106,9 @@ void Scene_surface_mesh_item::initializeBuffers(CGAL::Three::Viewer_interface* v
flat_normals.push_back((gl_data)n.y());
flat_normals.push_back((gl_data)n.z());
if(has_colors)
if(has_fcolors)
{
CGAL::Color c = vcolors[source(hd, *smesh_)];
// qDebug()<<c.red()<<", "<<c.green()<<", "<<c.blue();
CGAL::Color c = fcolors[fd];
f_colors.push_back((float)c.red()/255);
f_colors.push_back((float)c.green()/255);
f_colors.push_back((float)c.blue()/255);
@ -113,6 +116,19 @@ void Scene_surface_mesh_item::initializeBuffers(CGAL::Three::Viewer_interface* v
}
}
if(has_vcolors)
{
BOOST_FOREACH(vertex_descriptor vd, vertices(*smesh_))
{
CGAL::Color c = vcolors[vd];
v_colors.push_back((float)c.red()/255);
v_colors.push_back((float)c.green()/255);
v_colors.push_back((float)c.blue()/255);
}
}
//vao containing the data for the flat facets
@ -126,15 +142,6 @@ void Scene_surface_mesh_item::initializeBuffers(CGAL::Three::Viewer_interface* v
program->enableAttributeArray("vertex");
program->setAttributeBuffer("vertex",GL_DATA,0,3);
buffers[Flat_vertices].release();
if(has_colors)
{
buffers[Colors].bind();
buffers[Colors].allocate(f_colors.data(),
static_cast<int>(f_colors.size()*sizeof(gl_data)));
program->enableAttributeArray("colors");
program->setAttributeBuffer("colors",GL_DATA,0,3);
buffers[Colors].release();
}
buffers[Flat_normals].bind();
buffers[Flat_normals].allocate(flat_normals.data(),
@ -142,6 +149,15 @@ void Scene_surface_mesh_item::initializeBuffers(CGAL::Three::Viewer_interface* v
program->enableAttributeArray("normals");
program->setAttributeBuffer("normals",GL_DATA,0,3);
buffers[Flat_normals].release();
if(has_fcolors)
{
buffers[FColors].bind();
buffers[FColors].allocate(f_colors.data(),
static_cast<int>(f_colors.size()*sizeof(gl_data)));
program->enableAttributeArray("colors");
program->setAttributeBuffer("colors",GL_DATA,0,3);
buffers[FColors].release();
}
vaos[Flat_facets]->release();
//vao containing the data for the smooth facets
@ -160,9 +176,15 @@ void Scene_surface_mesh_item::initializeBuffers(CGAL::Three::Viewer_interface* v
program->enableAttributeArray("normals");
program->setAttributeBuffer("normals",GL_DATA,0,3);
buffers[Smooth_normals].release();
if(has_vcolors)
{
buffers[VColors].bind();
buffers[VColors].allocate(v_colors.data(),
static_cast<int>(v_colors.size()*sizeof(gl_data)));
program->enableAttributeArray("colors");
program->setAttributeBuffer("colors",GL_DATA,0,3);
buffers[VColors].release();
}
vaos[Smooth_facets]->release();
program->release();
@ -189,11 +211,12 @@ void Scene_surface_mesh_item::draw(CGAL::Three::Viewer_interface *viewer) const
if(renderingMode() == Gouraud)
{
vaos[Smooth_facets]->bind();
program->setAttributeValue("colors", this->color());
if(is_selected)
program->setAttributeValue("is_selected", true);
else
program->setAttributeValue("is_selected", false);
if(!has_vcolors)
program->setAttributeValue("colors", this->color());
glDrawElements(GL_TRIANGLES, idx_data_.size(),
GL_UNSIGNED_INT, idx_data_.data());
vaos[Smooth_facets]->release();
@ -201,11 +224,12 @@ void Scene_surface_mesh_item::draw(CGAL::Three::Viewer_interface *viewer) const
else
{
vaos[Flat_facets]->bind();
program->setAttributeValue("colors", this->color());
if(is_selected)
program->setAttributeValue("is_selected", true);
else
program->setAttributeValue("is_selected", false);
if(!has_colors)
if(!has_fcolors)
program->setAttributeValue("colors", this->color());
glDrawArrays(GL_TRIANGLES,0,static_cast<GLsizei>(flat_vertices.size()/3));
vaos[Flat_facets]->release();

View File

@ -56,7 +56,8 @@ public:
Smooth_vertices,
Flat_normals,
Smooth_normals,
Colors,
VColors,
FColors,
NbOfVbos
};
@ -67,7 +68,8 @@ public:
public Q_SLOTS:
virtual void selection_changed(bool);
private:
mutable bool has_colors;
mutable bool has_vcolors;
mutable bool has_fcolors;
SMesh* smesh_;
void initializeBuffers(CGAL::Three::Viewer_interface *) const;
std::vector<unsigned int> idx_data_;
@ -75,6 +77,7 @@ private:
mutable std::vector<gl_data> flat_vertices;
mutable std::vector<gl_data> flat_normals;
mutable std::vector<gl_data> f_colors;
mutable std::vector<gl_data> v_colors;
mutable QOpenGLShaderProgram *program;
};

View File

@ -2669,6 +2669,91 @@ private: //------------------------------------------------------- private data
/// @endcond
inline CGAL::Color get_color_from_line(std::istream &is)
{
std::string color_info;
bool is_float = false;
// stores every not commented char until the end of the line
char c;
bool is_comment = false;
do{
is.get(c);
if(c == '#')
{
is_comment = true;
}
if(c == '.' && !is_comment)
is_float = true;
if(c != '\n' && !is_comment)
color_info.append(1,c);
}while(c != '\n');
// Counts the number of effective spaces
//holds the number of spaces in the end of the line
int nb_numbers = 0;
// helps to keep the number of spaces below 3 if there is only one int in the color info
bool prec_is_empty = false;
for(int i=0; i<static_cast<int>(color_info.length()); i++)
{
if(color_info.at(i) == ' ')
{
if(!prec_is_empty)
nb_numbers ++;
prec_is_empty = true;
}
else
{
prec_is_empty = false;
}
}
CGAL::Color color;
//colormap
if(nb_numbers < 3)
{
std::string id;
//converts the index into an RGB value
for(int i = 1; i<static_cast<int>(color_info.length()); i++)
{
if(color_info.at(i) != ' ')
id.append(1,color_info.at(i));
else
{
break;
}
}
color = getIndexColor(atoi(id.c_str()));
}
//extracts RGB value from color_info
else
{
std::string rgb[3];
int j = 0;
for(int i = 1; i<static_cast<int>(color_info.length()); i++)
{
if(color_info.at(i) != ' ')
rgb[j].append(1,color_info.at(i));
else
{
if(j<2)
j++;
else
break;
}
}
if(is_float)
{
color = CGAL::Color(atoi(rgb[0].c_str())*255,atoi(rgb[1].c_str())*255,atoi(rgb[2].c_str())*255 );
}
else
{
color = CGAL::Color(atoi(rgb[0].c_str()),atoi(rgb[1].c_str()),atoi(rgb[2].c_str()) );
}
}
return color;
}
/// \relates Surface_mesh
/// \relates Surface_mesh
@ -2680,12 +2765,12 @@ private: //------------------------------------------------------- private data
template <typename P>
std::istream& operator>>(std::istream& is, Surface_mesh<P>& sm)
{
typedef Surface_mesh<P> Mesh;
typedef typename Kernel_traits<P>::Kernel K;
typedef typename K::Vector_3 Vector_3;
typedef typename Mesh::Face_index Face_index;
typedef typename Mesh::Vertex_index Vertex_index;
typedef typename Mesh::size_type size_type;
typedef Surface_mesh<P> Mesh;
typedef typename Kernel_traits<P>::Kernel K;
typedef typename K::Vector_3 Vector_3;
typedef typename Mesh::Face_index Face_index;
typedef typename Mesh::Vertex_index Vertex_index;
typedef typename Mesh::size_type size_type;
sm.clear();
int n, f, e;
std::string off;
@ -2701,11 +2786,6 @@ private: //------------------------------------------------------- private data
typename Mesh::template Property_map<Vertex_index,Vector_3> vnormal;
bool vcolored = false, v_has_normals = false;
if((off == "COFF") || (off == "CNOFF")){
bool created;
boost::tie(vcolor, created) = sm.template add_property_map<Vertex_index,CGAL::Color>("v:color",CGAL::Color(0,0,0,0));
vcolored = true;
}
if((off == "NOFF") || (off == "CNOFF")){
bool created;
boost::tie(vnormal, created) = sm.template add_property_map<Vertex_index,Vector_3>("v:normal",Vector_3(0,0,0));
@ -2721,99 +2801,31 @@ private: //------------------------------------------------------- private data
is >> v;
vnormal[vi] = v;
}
if(i == 0 && ((off == "COFF") || (off == "CNOFF"))){
std::string col;
std::streampos pos = is.tellg();
std::getline(is, col);
std::istringstream iss(col);
if(iss >> ci){
bool created;
boost::tie(vcolor, created) = sm.template add_property_map<Vertex_index,CGAL::Color>("v:color",CGAL::Color(0,0,0));
vcolored = true;
}
is.seekg(pos);
}
if(vcolored){
std::string color_info;
bool is_float = false;
// stores every not commented char until the end of the line
char c;
bool is_comment = false;
do{
is.get(c);
if(c == '#')
{
is_comment = true;
}
if(c == '.' && !is_comment)
is_float = true;
if(c != '\n' && !is_comment)
color_info.append(1,c);
}while(c != '\n');
// Counts the number of effective spaces
//holds the number of spaces in the end of the line
int nb_numbers = 0;
// helps to keep the number of spaces below 3 if there is only one int in the color info
bool prec_is_empty = false;
for(int i=0; i<static_cast<int>(color_info.length()); i++)
{
if(color_info.at(i) == ' ')
{
if(!prec_is_empty)
nb_numbers ++;
prec_is_empty = true;
}
else
{
prec_is_empty = false;
}
}
CGAL::Color color;
//colormap
if(nb_numbers < 3)
{
std::string id;
//converts the index into an RGB value
for(int i = 1; i<static_cast<int>(color_info.length()); i++)
{
if(color_info.at(i) != ' ')
id.append(1,color_info.at(i));
else
{
break;
}
}
color = getIndexColor(atoi(id.c_str()));
}
//extracts RGB value from color_info
else
{
std::string rgb[3];
int j = 0;
for(int i = 1; i<static_cast<int>(color_info.length()); i++)
{
if(color_info.at(i) != ' ')
rgb[j].append(1,color_info.at(i));
else
{
if(j<2)
j++;
else
break;
}
}
if(is_float)
{
color = CGAL::Color(atoi(rgb[0].c_str())*255,atoi(rgb[1].c_str())*255,atoi(rgb[2].c_str())*255 );
}
else
{
color = CGAL::Color(atoi(rgb[0].c_str()),atoi(rgb[1].c_str()),atoi(rgb[2].c_str()) );
}
}
//stores the RGB value
vcolor[vi] = color;
vcolor[vi] = get_color_from_line(is);
}
}
std::vector<size_type> vr;
std::size_t d;
bool fcolored = false;
typename Mesh::template Property_map<Face_index,int> fcolor;
typename Mesh::template Property_map<Face_index,CGAL::Color> fcolor;
for(int i=0; i < f; i++){
is >> sm_skip_comments;
@ -2826,22 +2838,23 @@ private: //------------------------------------------------------- private data
Face_index fi = sm.add_face(vr);
// the first face will tell us if faces have a color map
// TODO: extend this to RGBA
if(i == 0){
if(i == 0 && ((off == "COFF") || (off == "CNOFF"))){
std::string col;
std::streampos pos = is.tellg();
std::getline(is, col);
std::istringstream iss(col);
if(iss >> ci){
bool created;
boost::tie(fcolor, created) = sm.template add_property_map<Face_index,int>("f:color",0);
boost::tie(fcolor, created) = sm.template add_property_map<Face_index,CGAL::Color>("f:color",CGAL::Color(0,0,0));
fcolored = true;
fcolor[fi] = ci;
}
} else {
if(fcolored){
is >> ci;
fcolor[fi] = ci;
}
is.seekg(pos);
}
if(fcolored){
fcolor[fi] = get_color_from_line(is);
}
}
return is;
}