// This file is part of libigl, a simple c++ geometry processing library. // // Copyright (C) 2014 Daniele Panozzo // // This Source Code Form is subject to the terms of the Mozilla Public License // v. 2.0. If a copy of the MPL was not distributed with this file, You can // obtain one at http://mozilla.org/MPL/2.0/. #include "ViewerData.h" #include "../per_face_normals.h" #include "../material_colors.h" #include "../parula.h" #include "../per_vertex_normals.h" #include IGL_INLINE igl::opengl::ViewerData::ViewerData() : dirty(MeshGL::DIRTY_ALL), show_faces(true), show_lines(true), invert_normals(false), show_overlay(true), show_overlay_depth(true), show_vertid(false), show_faceid(false), show_texture(false), point_size(30), line_width(0.5f), line_color(0,0,0,1), shininess(35.0f), id(-1) { clear(); }; IGL_INLINE void igl::opengl::ViewerData::set_face_based(bool newvalue) { if (face_based != newvalue) { face_based = newvalue; dirty = MeshGL::DIRTY_ALL; } } // Helpers that draws the most common meshes IGL_INLINE void igl::opengl::ViewerData::set_mesh( const Eigen::MatrixXd& _V, const Eigen::MatrixXi& _F) { using namespace std; Eigen::MatrixXd V_temp; // If V only has two columns, pad with a column of zeros if (_V.cols() == 2) { V_temp = Eigen::MatrixXd::Zero(_V.rows(),3); V_temp.block(0,0,_V.rows(),2) = _V; } else V_temp = _V; if (V.rows() == 0 && F.rows() == 0) { V = V_temp; F = _F; compute_normals(); uniform_colors( Eigen::Vector3d(GOLD_AMBIENT[0], GOLD_AMBIENT[1], GOLD_AMBIENT[2]), Eigen::Vector3d(GOLD_DIFFUSE[0], GOLD_DIFFUSE[1], GOLD_DIFFUSE[2]), Eigen::Vector3d(GOLD_SPECULAR[0], GOLD_SPECULAR[1], GOLD_SPECULAR[2])); grid_texture(); } else { if (_V.rows() == V.rows() && _F.rows() == F.rows()) { V = V_temp; F = _F; } else cerr << "ERROR (set_mesh): The new mesh has a different number of vertices/faces. Please clear the mesh before plotting."<0 && C.cols() == 1) { Eigen::MatrixXd C3; igl::parula(C,true,C3); return set_colors(C3); } // Ambient color should be darker color const auto ambient = [](const MatrixXd & C)->MatrixXd { MatrixXd T = 0.1*C; T.col(3) = C.col(3); return T; }; // Specular color should be a less saturated and darker color: dampened // highlights const auto specular = [](const MatrixXd & C)->MatrixXd { const double grey = 0.3; MatrixXd T = grey+0.1*(C.array()-grey); T.col(3) = C.col(3); return T; }; if (C.rows() == 1) { for (unsigned i=0;i& R, const Eigen::Matrix& G, const Eigen::Matrix& B) { texture_R = R; texture_G = G; texture_B = B; texture_A = Eigen::Matrix::Constant(R.rows(),R.cols(),255); dirty |= MeshGL::DIRTY_TEXTURE; } IGL_INLINE void igl::opengl::ViewerData::set_texture( const Eigen::Matrix& R, const Eigen::Matrix& G, const Eigen::Matrix& B, const Eigen::Matrix& A) { texture_R = R; texture_G = G; texture_B = B; texture_A = A; dirty |= MeshGL::DIRTY_TEXTURE; } IGL_INLINE void igl::opengl::ViewerData::set_points( const Eigen::MatrixXd& P, const Eigen::MatrixXd& C) { // clear existing points points.resize(0,0); add_points(P,C); } IGL_INLINE void igl::opengl::ViewerData::add_points(const Eigen::MatrixXd& P, const Eigen::MatrixXd& C) { Eigen::MatrixXd P_temp; // If P only has two columns, pad with a column of zeros if (P.cols() == 2) { P_temp = Eigen::MatrixXd::Zero(P.rows(),3); P_temp.block(0,0,P.rows(),2) = P; } else P_temp = P; int lastid = points.rows(); points.conservativeResize(points.rows() + P_temp.rows(),6); for (unsigned i=0; i=size2 && j>=size2)) texture_R(i,j) = 255; } } texture_G = texture_R; texture_B = texture_R; texture_A = Eigen::Matrix::Constant(texture_R.rows(),texture_R.cols(),255); dirty |= MeshGL::DIRTY_TEXTURE; } IGL_INLINE void igl::opengl::ViewerData::updateGL( const igl::opengl::ViewerData& data, const bool invert_normals, igl::opengl::MeshGL& meshgl ) { if (!meshgl.is_initialized) { meshgl.init(); } bool per_corner_uv = (data.F_uv.rows() == data.F.rows()); bool per_corner_normals = (data.F_normals.rows() == 3 * data.F.rows()); meshgl.dirty |= data.dirty; // Input: // X #F by dim quantity // Output: // X_vbo #F*3 by dim scattering per corner const auto per_face = [&data]( const Eigen::MatrixXd & X, MeshGL::RowMatrixXf & X_vbo) { assert(X.cols() == 4); X_vbo.resize(data.F.rows()*3,4); for (unsigned i=0; i(); }; // Input: // X #V by dim quantity // Output: // X_vbo #F*3 by dim scattering per corner const auto per_corner = [&data]( const Eigen::MatrixXd & X, MeshGL::RowMatrixXf & X_vbo) { X_vbo.resize(data.F.rows()*3,X.cols()); for (unsigned i=0; i(); }; if (!data.face_based) { if (!(per_corner_uv || per_corner_normals)) { // Vertex positions if (meshgl.dirty & MeshGL::DIRTY_POSITION) meshgl.V_vbo = data.V.cast(); // Vertex normals if (meshgl.dirty & MeshGL::DIRTY_NORMAL) { meshgl.V_normals_vbo = data.V_normals.cast(); if (invert_normals) meshgl.V_normals_vbo = -meshgl.V_normals_vbo; } // Per-vertex material settings if (meshgl.dirty & MeshGL::DIRTY_AMBIENT) meshgl.V_ambient_vbo = data.V_material_ambient.cast(); if (meshgl.dirty & MeshGL::DIRTY_DIFFUSE) meshgl.V_diffuse_vbo = data.V_material_diffuse.cast(); if (meshgl.dirty & MeshGL::DIRTY_SPECULAR) meshgl.V_specular_vbo = data.V_material_specular.cast(); // Face indices if (meshgl.dirty & MeshGL::DIRTY_FACE) meshgl.F_vbo = data.F.cast(); // Texture coordinates if (meshgl.dirty & MeshGL::DIRTY_UV) { meshgl.V_uv_vbo = data.V_uv.cast(); } } else { // Per vertex properties with per corner UVs if (meshgl.dirty & MeshGL::DIRTY_POSITION) { per_corner(data.V,meshgl.V_vbo); } if (meshgl.dirty & MeshGL::DIRTY_AMBIENT) { meshgl.V_ambient_vbo.resize(data.F.rows()*3,4); for (unsigned i=0; i(); } if (meshgl.dirty & MeshGL::DIRTY_DIFFUSE) { meshgl.V_diffuse_vbo.resize(data.F.rows()*3,4); for (unsigned i=0; i(); } if (meshgl.dirty & MeshGL::DIRTY_SPECULAR) { meshgl.V_specular_vbo.resize(data.F.rows()*3,4); for (unsigned i=0; i(); } if (meshgl.dirty & MeshGL::DIRTY_NORMAL) { meshgl.V_normals_vbo.resize(data.F.rows()*3,3); for (unsigned i=0; i() : data.V_normals.row(data.F(i,j)).cast(); if (invert_normals) meshgl.V_normals_vbo = -meshgl.V_normals_vbo; } if (meshgl.dirty & MeshGL::DIRTY_FACE) { meshgl.F_vbo.resize(data.F.rows(),3); for (unsigned i=0; i(); } } } else { if (meshgl.dirty & MeshGL::DIRTY_POSITION) { per_corner(data.V,meshgl.V_vbo); } if (meshgl.dirty & MeshGL::DIRTY_AMBIENT) { per_face(data.F_material_ambient,meshgl.V_ambient_vbo); } if (meshgl.dirty & MeshGL::DIRTY_DIFFUSE) { per_face(data.F_material_diffuse,meshgl.V_diffuse_vbo); } if (meshgl.dirty & MeshGL::DIRTY_SPECULAR) { per_face(data.F_material_specular,meshgl.V_specular_vbo); } if (meshgl.dirty & MeshGL::DIRTY_NORMAL) { meshgl.V_normals_vbo.resize(data.F.rows()*3,3); for (unsigned i=0; i() : data.F_normals.row(i).cast(); if (invert_normals) meshgl.V_normals_vbo = -meshgl.V_normals_vbo; } if (meshgl.dirty & MeshGL::DIRTY_FACE) { meshgl.F_vbo.resize(data.F.rows(),3); for (unsigned i=0; i(); } } if (meshgl.dirty & MeshGL::DIRTY_TEXTURE) { meshgl.tex_u = data.texture_R.rows(); meshgl.tex_v = data.texture_R.cols(); meshgl.tex.resize(data.texture_R.size()*4); for (unsigned i=0;i(i, 0).cast(); meshgl.lines_V_vbo.row(2*i+1) = data.lines.block<1, 3>(i, 3).cast(); meshgl.lines_V_colors_vbo.row(2*i+0) = data.lines.block<1, 3>(i, 6).cast(); meshgl.lines_V_colors_vbo.row(2*i+1) = data.lines.block<1, 3>(i, 6).cast(); meshgl.lines_F_vbo(2*i+0) = 2*i+0; meshgl.lines_F_vbo(2*i+1) = 2*i+1; } } if (meshgl.dirty & MeshGL::DIRTY_OVERLAY_POINTS) { meshgl.points_V_vbo.resize(data.points.rows(),3); meshgl.points_V_colors_vbo.resize(data.points.rows(),3); meshgl.points_F_vbo.resize(data.points.rows(),1); for (unsigned i=0; i(i, 0).cast(); meshgl.points_V_colors_vbo.row(i) = data.points.block<1, 3>(i, 3).cast(); meshgl.points_F_vbo(i) = i; } } }