ViewerData.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Daniele Panozzo <[email protected]>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "ViewerData.h"
  9. #include "ViewerCore.h"
  10. #include "../per_face_normals.h"
  11. #include "../material_colors.h"
  12. #include "../per_vertex_normals.h"
  13. // Really? Just for GL_NEAREST?
  14. #include "gl.h"
  15. #include <iostream>
  16. IGL_INLINE igl::opengl::ViewerData::ViewerData()
  17. :
  18. dirty(MeshGL::DIRTY_ALL),
  19. face_based (false),
  20. double_sided (false),
  21. invert_normals (false),
  22. pseudocolor_with_normals(false),
  23. is_visible (~unsigned(0)),
  24. show_custom_labels(0),
  25. show_face_labels (0),
  26. show_faces (~unsigned(0)),
  27. show_lines (~unsigned(0)),
  28. show_overlay (~unsigned(0)),
  29. show_overlay_depth(~unsigned(0)),
  30. show_texture (false),
  31. show_vertex_labels(0),
  32. use_matcap (false),
  33. point_size(30),
  34. line_width(0.5f),
  35. label_size(1),
  36. line_color(0,0,0,1),
  37. label_color(0,0,0.04,1),
  38. shininess(35.0f),
  39. id(-1)
  40. {
  41. clear();
  42. };
  43. IGL_INLINE void igl::opengl::ViewerData::set_face_based(bool newvalue)
  44. {
  45. if (face_based != newvalue)
  46. {
  47. face_based = newvalue;
  48. dirty = MeshGL::DIRTY_ALL;
  49. }
  50. }
  51. // Helpers that draws the most common meshes
  52. IGL_INLINE void igl::opengl::ViewerData::set_mesh(
  53. const Eigen::MatrixXd& _V, const Eigen::MatrixXi& _F)
  54. {
  55. using namespace std;
  56. Eigen::MatrixXd V_temp;
  57. // If V only has two columns, pad with a column of zeros
  58. if (_V.cols() == 2)
  59. {
  60. V_temp = Eigen::MatrixXd::Zero(_V.rows(),3);
  61. V_temp.block(0,0,_V.rows(),2) = _V;
  62. }
  63. else
  64. V_temp = _V;
  65. if (V.rows() == 0 && F.rows() == 0)
  66. {
  67. V = V_temp;
  68. F = _F;
  69. compute_normals();
  70. uniform_colors(
  71. Eigen::Vector3d(GOLD_AMBIENT[0], GOLD_AMBIENT[1], GOLD_AMBIENT[2]),
  72. Eigen::Vector3d(GOLD_DIFFUSE[0], GOLD_DIFFUSE[1], GOLD_DIFFUSE[2]),
  73. Eigen::Vector3d(GOLD_SPECULAR[0], GOLD_SPECULAR[1], GOLD_SPECULAR[2]));
  74. // Generates a checkerboard texture
  75. grid_texture();
  76. }
  77. else
  78. {
  79. if (_V.rows() == V.rows() && _F.rows() == F.rows())
  80. {
  81. V = V_temp;
  82. F = _F;
  83. }
  84. else
  85. cerr << "ERROR (set_mesh): The new mesh has a different number of vertices/faces. Please clear the mesh before plotting."<<endl;
  86. }
  87. dirty |= MeshGL::DIRTY_FACE | MeshGL::DIRTY_POSITION;
  88. }
  89. IGL_INLINE void igl::opengl::ViewerData::set_vertices(const Eigen::MatrixXd& _V)
  90. {
  91. V = _V;
  92. assert(F.size() == 0 || F.maxCoeff() < V.rows());
  93. dirty |= MeshGL::DIRTY_POSITION;
  94. }
  95. IGL_INLINE void igl::opengl::ViewerData::set_normals(const Eigen::MatrixXd& N)
  96. {
  97. using namespace std;
  98. if (N.rows() == V.rows())
  99. {
  100. set_face_based(false);
  101. V_normals = N;
  102. }
  103. else if (N.rows() == F.rows() || N.rows() == F.rows()*3)
  104. {
  105. set_face_based(true);
  106. F_normals = N;
  107. }
  108. else
  109. cerr << "ERROR (set_normals): Please provide a normal per face, per corner or per vertex."<<endl;
  110. dirty |= MeshGL::DIRTY_NORMAL;
  111. }
  112. IGL_INLINE void igl::opengl::ViewerData::set_visible(bool value, unsigned int core_id /*= 1*/)
  113. {
  114. if (value)
  115. is_visible |= core_id;
  116. else
  117. is_visible &= ~core_id;
  118. }
  119. IGL_INLINE void igl::opengl::ViewerData::copy_options(const ViewerCore &from, const ViewerCore &to)
  120. {
  121. to.set(show_overlay , from.is_set(show_overlay) );
  122. to.set(show_overlay_depth, from.is_set(show_overlay_depth));
  123. to.set(show_texture , from.is_set(show_texture) );
  124. to.set(use_matcap , from.is_set(use_matcap) );
  125. to.set(show_faces , from.is_set(show_faces) );
  126. to.set(show_lines , from.is_set(show_lines) );
  127. }
  128. IGL_INLINE void igl::opengl::ViewerData::set_colors(const Eigen::MatrixXd &C)
  129. {
  130. using namespace std;
  131. using namespace Eigen;
  132. // This Gouraud coloring should be deprecated in favor of Phong coloring in
  133. // set-data
  134. if(C.rows()>0 && C.cols() == 1)
  135. {
  136. assert(false && "deprecated: call set_data directly instead");
  137. return set_data(C);
  138. }
  139. // Ambient color should be darker color
  140. const auto ambient = [](const MatrixXd & C)->MatrixXd
  141. {
  142. MatrixXd T = 0.1*C;
  143. T.col(3) = C.col(3);
  144. return T;
  145. };
  146. // Specular color should be a less saturated and darker color: dampened
  147. // highlights
  148. const auto specular = [](const MatrixXd & C)->MatrixXd
  149. {
  150. const double grey = 0.3;
  151. MatrixXd T = grey+0.1*(C.array()-grey);
  152. T.col(3) = C.col(3);
  153. return T;
  154. };
  155. if (C.rows() == 1)
  156. {
  157. for (unsigned i=0;i<V_material_diffuse.rows();++i)
  158. {
  159. if (C.cols() == 3)
  160. V_material_diffuse.row(i) << C.row(0),1;
  161. else if (C.cols() == 4)
  162. V_material_diffuse.row(i) << C.row(0);
  163. }
  164. V_material_ambient = ambient(V_material_diffuse);
  165. V_material_specular = specular(V_material_diffuse);
  166. for (unsigned i=0;i<F_material_diffuse.rows();++i)
  167. {
  168. if (C.cols() == 3)
  169. F_material_diffuse.row(i) << C.row(0),1;
  170. else if (C.cols() == 4)
  171. F_material_diffuse.row(i) << C.row(0);
  172. }
  173. F_material_ambient = ambient(F_material_diffuse);
  174. F_material_specular = specular(F_material_diffuse);
  175. }
  176. else if(C.rows() == V.rows() || C.rows() == F.rows())
  177. {
  178. // face based colors?
  179. if((C.rows()==F.rows()) && (C.rows() != V.rows() || face_based))
  180. {
  181. set_face_based(true);
  182. for (unsigned i=0;i<F_material_diffuse.rows();++i)
  183. {
  184. if (C.cols() == 3)
  185. F_material_diffuse.row(i) << C.row(i), 1;
  186. else if (C.cols() == 4)
  187. F_material_diffuse.row(i) << C.row(i);
  188. }
  189. F_material_ambient = ambient(F_material_diffuse);
  190. F_material_specular = specular(F_material_diffuse);
  191. }
  192. else/*(C.rows() == V.rows())*/
  193. {
  194. set_face_based(false);
  195. for (unsigned i=0;i<V_material_diffuse.rows();++i)
  196. {
  197. if (C.cols() == 3)
  198. V_material_diffuse.row(i) << C.row(i), 1;
  199. else if (C.cols() == 4)
  200. V_material_diffuse.row(i) << C.row(i);
  201. }
  202. V_material_ambient = ambient(V_material_diffuse);
  203. V_material_specular = specular(V_material_diffuse);
  204. }
  205. }
  206. else
  207. cerr << "ERROR (set_colors): Please provide a single color, or a color per face or per vertex."<<endl;
  208. dirty |= MeshGL::DIRTY_DIFFUSE | MeshGL::DIRTY_SPECULAR | MeshGL::DIRTY_AMBIENT;
  209. }
  210. IGL_INLINE void igl::opengl::ViewerData::set_uv(const Eigen::MatrixXd& UV)
  211. {
  212. using namespace std;
  213. if (UV.rows() == V.rows())
  214. {
  215. V_uv = UV;
  216. }
  217. else
  218. cerr << "ERROR (set_UV): Please provide uv per vertex."<<endl;;
  219. dirty |= MeshGL::DIRTY_UV;
  220. }
  221. IGL_INLINE void igl::opengl::ViewerData::set_uv(const Eigen::MatrixXd& UV_V, const Eigen::MatrixXi& UV_F)
  222. {
  223. V_uv = UV_V.block(0,0,UV_V.rows(),2);
  224. F_uv = UV_F;
  225. dirty |= MeshGL::DIRTY_UV;
  226. }
  227. IGL_INLINE void igl::opengl::ViewerData::set_texture(
  228. const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& R,
  229. const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& G,
  230. const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& B)
  231. {
  232. texture_R = R;
  233. texture_G = G;
  234. texture_B = B;
  235. texture_A = Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>::Constant(R.rows(),R.cols(),255);
  236. dirty |= MeshGL::DIRTY_TEXTURE;
  237. }
  238. IGL_INLINE void igl::opengl::ViewerData::set_texture(
  239. const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& R,
  240. const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& G,
  241. const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& B,
  242. const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& A)
  243. {
  244. texture_R = R;
  245. texture_G = G;
  246. texture_B = B;
  247. texture_A = A;
  248. dirty |= MeshGL::DIRTY_TEXTURE;
  249. }
  250. IGL_INLINE void igl::opengl::ViewerData::set_data(
  251. const Eigen::VectorXd & D,
  252. double caxis_min,
  253. double caxis_max,
  254. igl::ColorMapType cmap,
  255. int num_steps)
  256. {
  257. if(!show_texture)
  258. {
  259. Eigen::MatrixXd CM;
  260. igl::colormap(cmap,Eigen::VectorXd::LinSpaced(num_steps,0,1).eval(),0,1,CM);
  261. set_colormap(CM);
  262. }
  263. Eigen::MatrixXd UV = ((D.array()-caxis_min)/(caxis_max-caxis_min)).replicate(1,2);
  264. if(D.size() == V.rows())
  265. {
  266. set_uv(UV);
  267. }else
  268. {
  269. assert(D.size() == F.rows());
  270. Eigen::MatrixXi UV_F =
  271. Eigen::VectorXi::LinSpaced(F.rows(),0,F.rows()-1).replicate(1,3);
  272. set_uv(UV,UV_F);
  273. }
  274. }
  275. IGL_INLINE void igl::opengl::ViewerData::set_data(const Eigen::VectorXd & D, igl::ColorMapType cmap, int num_steps)
  276. {
  277. const double caxis_min = D.minCoeff();
  278. const double caxis_max = D.maxCoeff();
  279. return set_data(D,caxis_min,caxis_max,cmap,num_steps);
  280. }
  281. IGL_INLINE void igl::opengl::ViewerData::set_colormap(const Eigen::MatrixXd & CM)
  282. {
  283. assert(CM.cols() == 3 && "colormap CM should have 3 columns");
  284. // Convert to R,G,B textures
  285. const Eigen::Matrix<unsigned char,Eigen::Dynamic, Eigen::Dynamic> R =
  286. (CM.col(0)*255.0).cast<unsigned char>();
  287. const Eigen::Matrix<unsigned char,Eigen::Dynamic, Eigen::Dynamic> G =
  288. (CM.col(1)*255.0).cast<unsigned char>();
  289. const Eigen::Matrix<unsigned char,Eigen::Dynamic, Eigen::Dynamic> B =
  290. (CM.col(2)*255.0).cast<unsigned char>();
  291. set_colors(Eigen::RowVector3d(1,1,1));
  292. set_texture(R,G,B);
  293. show_texture = ~unsigned(0);
  294. meshgl.tex_filter = GL_NEAREST;
  295. meshgl.tex_wrap = GL_CLAMP_TO_EDGE;
  296. }
  297. IGL_INLINE void igl::opengl::ViewerData::set_points(
  298. const Eigen::MatrixXd& P,
  299. const Eigen::MatrixXd& C)
  300. {
  301. // clear existing points
  302. points.resize(0,0);
  303. add_points(P,C);
  304. }
  305. IGL_INLINE void igl::opengl::ViewerData::add_points(const Eigen::MatrixXd& P, const Eigen::MatrixXd& C)
  306. {
  307. Eigen::MatrixXd P_temp;
  308. // If P only has two columns, pad with a column of zeros
  309. if (P.cols() == 2)
  310. {
  311. P_temp = Eigen::MatrixXd::Zero(P.rows(),3);
  312. P_temp.block(0,0,P.rows(),2) = P;
  313. }
  314. else
  315. P_temp = P;
  316. int lastid = points.rows();
  317. points.conservativeResize(points.rows() + P_temp.rows(),6);
  318. for (unsigned i=0; i<P_temp.rows(); ++i)
  319. points.row(lastid+i) << P_temp.row(i), i<C.rows() ? C.row(i) : C.row(C.rows()-1);
  320. dirty |= MeshGL::DIRTY_OVERLAY_POINTS;
  321. }
  322. IGL_INLINE void igl::opengl::ViewerData::clear_points()
  323. {
  324. points.resize(0, 6);
  325. }
  326. IGL_INLINE void igl::opengl::ViewerData::set_edges(
  327. const Eigen::MatrixXd& P,
  328. const Eigen::MatrixXi& E,
  329. const Eigen::MatrixXd& C)
  330. {
  331. using namespace Eigen;
  332. lines.resize(E.rows(),9);
  333. assert(C.cols() == 3);
  334. for(int e = 0;e<E.rows();e++)
  335. {
  336. RowVector3d color;
  337. if(C.size() == 3)
  338. {
  339. color<<C;
  340. }else if(C.rows() == E.rows())
  341. {
  342. color<<C.row(e);
  343. }
  344. if(P.cols() == 2)
  345. {
  346. lines.row(e)<< P.row(E(e,0)),0, P.row(E(e,1)),0, color;
  347. }else
  348. {
  349. lines.row(e)<< P.row(E(e,0)), P.row(E(e,1)), color;
  350. }
  351. }
  352. dirty |= MeshGL::DIRTY_OVERLAY_LINES;
  353. }
  354. IGL_INLINE void igl::opengl::ViewerData::set_edges_from_vector_field(
  355. const Eigen::MatrixXd& P,
  356. const Eigen::MatrixXd& V,
  357. const Eigen::MatrixXd& C)
  358. {
  359. assert(P.rows() == V.rows());
  360. Eigen::MatrixXi E(P.rows(),2);
  361. const Eigen::MatrixXd PV =
  362. (Eigen::MatrixXd(P.rows()+V.rows(),3)<<P,P+V).finished();
  363. for(int i = 0;i<P.rows();i++)
  364. {
  365. E(i,0) = i;
  366. E(i,1) = i+P.rows();
  367. }
  368. const Eigen::MatrixXd CC = C.replicate<2,1>();
  369. set_edges(PV,E, C.rows() == 1?C:C.replicate<2,1>());
  370. }
  371. IGL_INLINE void igl::opengl::ViewerData::add_edges(const Eigen::MatrixXd& P1, const Eigen::MatrixXd& P2, const Eigen::MatrixXd& C)
  372. {
  373. Eigen::MatrixXd P1_temp,P2_temp;
  374. // If P1 only has two columns, pad with a column of zeros
  375. if (P1.cols() == 2)
  376. {
  377. P1_temp = Eigen::MatrixXd::Zero(P1.rows(),3);
  378. P1_temp.block(0,0,P1.rows(),2) = P1;
  379. P2_temp = Eigen::MatrixXd::Zero(P2.rows(),3);
  380. P2_temp.block(0,0,P2.rows(),2) = P2;
  381. }
  382. else
  383. {
  384. P1_temp = P1;
  385. P2_temp = P2;
  386. }
  387. int lastid = lines.rows();
  388. lines.conservativeResize(lines.rows() + P1_temp.rows(),9);
  389. for (unsigned i=0; i<P1_temp.rows(); ++i)
  390. lines.row(lastid+i) << P1_temp.row(i), P2_temp.row(i), i<C.rows() ? C.row(i) : C.row(C.rows()-1);
  391. dirty |= MeshGL::DIRTY_OVERLAY_LINES;
  392. }
  393. IGL_INLINE void igl::opengl::ViewerData::clear_edges()
  394. {
  395. lines.resize(0, 9);
  396. }
  397. IGL_INLINE void igl::opengl::ViewerData::add_label(const Eigen::VectorXd& P, const std::string& str)
  398. {
  399. Eigen::RowVectorXd P_temp;
  400. // If P only has two columns, pad with a column of zeros
  401. if (P.size() == 2)
  402. {
  403. P_temp = Eigen::RowVectorXd::Zero(3);
  404. P_temp << P.transpose(), 0;
  405. }
  406. else
  407. P_temp = P;
  408. int lastid = labels_positions.rows();
  409. labels_positions.conservativeResize(lastid+1, 3);
  410. labels_positions.row(lastid) = P_temp;
  411. labels_strings.push_back(str);
  412. dirty |= MeshGL::DIRTY_CUSTOM_LABELS;
  413. }
  414. IGL_INLINE void igl::opengl::ViewerData::set_labels(const Eigen::MatrixXd& P, const std::vector<std::string>& str)
  415. {
  416. assert(P.rows() == str.size() && "position # and label # do not match!");
  417. assert(P.cols() == 3 && "dimension of label positions incorrect!");
  418. labels_positions = P;
  419. labels_strings = str;
  420. dirty |= MeshGL::DIRTY_CUSTOM_LABELS;
  421. }
  422. IGL_INLINE void igl::opengl::ViewerData::clear_labels()
  423. {
  424. labels_positions.resize(0,3);
  425. labels_strings.clear();
  426. }
  427. IGL_INLINE void igl::opengl::ViewerData::clear()
  428. {
  429. V = Eigen::MatrixXd (0,3);
  430. F = Eigen::MatrixXi (0,3);
  431. F_material_ambient = Eigen::MatrixXd (0,4);
  432. F_material_diffuse = Eigen::MatrixXd (0,4);
  433. F_material_specular = Eigen::MatrixXd (0,4);
  434. V_material_ambient = Eigen::MatrixXd (0,4);
  435. V_material_diffuse = Eigen::MatrixXd (0,4);
  436. V_material_specular = Eigen::MatrixXd (0,4);
  437. F_normals = Eigen::MatrixXd (0,3);
  438. V_normals = Eigen::MatrixXd (0,3);
  439. V_uv = Eigen::MatrixXd (0,2);
  440. F_uv = Eigen::MatrixXi (0,3);
  441. lines = Eigen::MatrixXd (0,9);
  442. points = Eigen::MatrixXd (0,6);
  443. vertex_labels_positions = Eigen::MatrixXd (0,3);
  444. face_labels_positions = Eigen::MatrixXd (0,3);
  445. labels_positions = Eigen::MatrixXd (0,3);
  446. vertex_labels_strings.clear();
  447. face_labels_strings.clear();
  448. labels_strings.clear();
  449. face_based = false;
  450. double_sided = false;
  451. invert_normals = false;
  452. pseudocolor_with_normals = false;
  453. show_texture = false;
  454. use_matcap = false;
  455. }
  456. IGL_INLINE void igl::opengl::ViewerData::compute_normals()
  457. {
  458. if(V.cols() == 2)
  459. {
  460. F_normals = Eigen::RowVector3d(0,0,1).replicate(F.rows(),1);
  461. V_normals = Eigen::RowVector3d(0,0,1).replicate(V.rows(),1);
  462. }else
  463. {
  464. assert(V.cols() == 3);
  465. igl::per_face_normals(V, F, F_normals);
  466. igl::per_vertex_normals(V, F, F_normals, V_normals);
  467. }
  468. dirty |= MeshGL::DIRTY_NORMAL;
  469. }
  470. IGL_INLINE void igl::opengl::ViewerData::uniform_colors(
  471. const Eigen::Vector3d& ambient,
  472. const Eigen::Vector3d& diffuse,
  473. const Eigen::Vector3d& specular)
  474. {
  475. Eigen::Vector4d ambient4;
  476. Eigen::Vector4d diffuse4;
  477. Eigen::Vector4d specular4;
  478. ambient4 << ambient, 1;
  479. diffuse4 << diffuse, 1;
  480. specular4 << specular, 1;
  481. uniform_colors(ambient4,diffuse4,specular4);
  482. }
  483. IGL_INLINE void igl::opengl::ViewerData::uniform_colors(
  484. const Eigen::Vector4d& ambient,
  485. const Eigen::Vector4d& diffuse,
  486. const Eigen::Vector4d& specular)
  487. {
  488. V_material_ambient.resize(V.rows(),4);
  489. V_material_diffuse.resize(V.rows(),4);
  490. V_material_specular.resize(V.rows(),4);
  491. for (unsigned i=0; i<V.rows();++i)
  492. {
  493. V_material_ambient.row(i) = ambient;
  494. V_material_diffuse.row(i) = diffuse;
  495. V_material_specular.row(i) = specular;
  496. }
  497. F_material_ambient.resize(F.rows(),4);
  498. F_material_diffuse.resize(F.rows(),4);
  499. F_material_specular.resize(F.rows(),4);
  500. for (unsigned i=0; i<F.rows();++i)
  501. {
  502. F_material_ambient.row(i) = ambient;
  503. F_material_diffuse.row(i) = diffuse;
  504. F_material_specular.row(i) = specular;
  505. }
  506. dirty |= MeshGL::DIRTY_SPECULAR | MeshGL::DIRTY_DIFFUSE | MeshGL::DIRTY_AMBIENT;
  507. }
  508. IGL_INLINE void igl::opengl::ViewerData::normal_matcap()
  509. {
  510. const int size = 512;
  511. texture_R.resize(size, size);
  512. texture_G.resize(size, size);
  513. texture_B.resize(size, size);
  514. const Eigen::Vector3d navy(0.3,0.3,0.5);
  515. static const auto clamp = [](double t){ return std::max(std::min(t,1.0),0.0);};
  516. for(int i = 0;i<size;i++)
  517. {
  518. const double x = (double(i)/double(size-1)*2.-1.);
  519. for(int j = 0;j<size;j++)
  520. {
  521. const double y = (double(j)/double(size-1)*2.-1.);
  522. const double z = sqrt(1.0-std::min(x*x+y*y,1.0));
  523. Eigen::Vector3d C = Eigen::Vector3d(x*0.5+0.5,y*0.5+0.5,z);
  524. texture_R(i,j) = clamp(C(0))*255;
  525. texture_G(i,j) = clamp(C(1))*255;
  526. texture_B(i,j) = clamp(C(2))*255;
  527. }
  528. }
  529. texture_A.setConstant(texture_R.rows(),texture_R.cols(),255);
  530. dirty |= MeshGL::DIRTY_TEXTURE;
  531. }
  532. IGL_INLINE void igl::opengl::ViewerData::grid_texture()
  533. {
  534. unsigned size = 128;
  535. unsigned size2 = size/2;
  536. texture_R.resize(size, size);
  537. for (unsigned i=0; i<size; ++i)
  538. {
  539. for (unsigned j=0; j<size; ++j)
  540. {
  541. texture_R(i,j) = 0;
  542. if ((i<size2 && j<size2) || (i>=size2 && j>=size2))
  543. texture_R(i,j) = 255;
  544. }
  545. }
  546. texture_G = texture_R;
  547. texture_B = texture_R;
  548. texture_A = Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>::Constant(texture_R.rows(),texture_R.cols(),255);
  549. dirty |= MeshGL::DIRTY_TEXTURE;
  550. }
  551. // Populate VBOs of a particular label stype (Vert, Face, Custom)
  552. IGL_INLINE void igl::opengl::ViewerData::update_labels(
  553. igl::opengl::MeshGL::TextGL& GL_labels,
  554. const Eigen::MatrixXd& positions,
  555. const std::vector<std::string>& strings
  556. ){
  557. if (positions.rows()>0)
  558. {
  559. int numCharsToRender = 0;
  560. for(size_t p=0; p<positions.rows(); p++)
  561. {
  562. numCharsToRender += strings.at(p).length();
  563. }
  564. GL_labels.label_pos_vbo.resize(numCharsToRender, 3);
  565. GL_labels.label_char_vbo.resize(numCharsToRender, 1);
  566. GL_labels.label_offset_vbo.resize(numCharsToRender, 1);
  567. GL_labels.label_indices_vbo.resize(numCharsToRender, 1);
  568. int idx=0;
  569. assert(strings.size() == positions.rows());
  570. for(size_t s=0; s<strings.size(); s++)
  571. {
  572. const auto & label = strings.at(s);
  573. for(size_t c=0; c<label.length(); c++)
  574. {
  575. GL_labels.label_pos_vbo.row(idx) = positions.row(s).cast<float>();
  576. GL_labels.label_char_vbo(idx) = (float)(label.at(c));
  577. GL_labels.label_offset_vbo(idx) = c;
  578. GL_labels.label_indices_vbo(idx) = idx;
  579. idx++;
  580. }
  581. }
  582. }
  583. }
  584. IGL_INLINE void igl::opengl::ViewerData::updateGL(
  585. const igl::opengl::ViewerData& data,
  586. const bool invert_normals,
  587. igl::opengl::MeshGL& meshgl
  588. )
  589. {
  590. if (!meshgl.is_initialized)
  591. {
  592. meshgl.init();
  593. }
  594. bool per_corner_uv = (data.F_uv.rows() == data.F.rows());
  595. bool per_corner_normals = (data.F_normals.rows() == 3 * data.F.rows());
  596. meshgl.dirty |= data.dirty;
  597. // Input:
  598. // X #F by dim quantity
  599. // Output:
  600. // X_vbo #F*3 by dim scattering per corner
  601. const auto per_face = [&data](
  602. const Eigen::MatrixXd & X,
  603. MeshGL::RowMatrixXf & X_vbo)
  604. {
  605. assert(X.cols() == 4);
  606. X_vbo.resize(data.F.rows()*3,4);
  607. for (unsigned i=0; i<data.F.rows();++i)
  608. for (unsigned j=0;j<3;++j)
  609. X_vbo.row(i*3+j) = X.row(i).cast<float>();
  610. };
  611. // Input:
  612. // X #V by dim quantity
  613. // Output:
  614. // X_vbo #F*3 by dim scattering per corner
  615. const auto per_corner = [&data](
  616. const Eigen::MatrixXd & X,
  617. MeshGL::RowMatrixXf & X_vbo)
  618. {
  619. X_vbo.resize(data.F.rows()*3,X.cols());
  620. for (unsigned i=0; i<data.F.rows();++i)
  621. for (unsigned j=0;j<3;++j)
  622. X_vbo.row(i*3+j) = X.row(data.F(i,j)).cast<float>();
  623. };
  624. if (!data.face_based)
  625. {
  626. if (!(per_corner_uv || per_corner_normals))
  627. {
  628. // Vertex positions
  629. if (meshgl.dirty & MeshGL::DIRTY_POSITION)
  630. meshgl.V_vbo = data.V.cast<float>();
  631. // Vertex normals
  632. if (meshgl.dirty & MeshGL::DIRTY_NORMAL)
  633. {
  634. meshgl.V_normals_vbo = data.V_normals.cast<float>();
  635. if (invert_normals)
  636. meshgl.V_normals_vbo = -meshgl.V_normals_vbo;
  637. }
  638. // Per-vertex material settings
  639. if (meshgl.dirty & MeshGL::DIRTY_AMBIENT)
  640. meshgl.V_ambient_vbo = data.V_material_ambient.cast<float>();
  641. if (meshgl.dirty & MeshGL::DIRTY_DIFFUSE)
  642. meshgl.V_diffuse_vbo = data.V_material_diffuse.cast<float>();
  643. if (meshgl.dirty & MeshGL::DIRTY_SPECULAR)
  644. meshgl.V_specular_vbo = data.V_material_specular.cast<float>();
  645. // Face indices
  646. if (meshgl.dirty & MeshGL::DIRTY_FACE)
  647. meshgl.F_vbo = data.F.cast<unsigned>();
  648. // Texture coordinates
  649. if (meshgl.dirty & MeshGL::DIRTY_UV)
  650. {
  651. meshgl.V_uv_vbo = data.V_uv.cast<float>();
  652. }
  653. }
  654. else
  655. {
  656. // Per vertex properties with per corner UVs
  657. if (meshgl.dirty & MeshGL::DIRTY_POSITION)
  658. {
  659. per_corner(data.V,meshgl.V_vbo);
  660. }
  661. if (meshgl.dirty & MeshGL::DIRTY_AMBIENT)
  662. {
  663. meshgl.V_ambient_vbo.resize(data.F.rows()*3,4);
  664. per_corner(data.V_material_ambient,meshgl.V_ambient_vbo);
  665. }
  666. if (meshgl.dirty & MeshGL::DIRTY_DIFFUSE)
  667. {
  668. meshgl.V_diffuse_vbo.resize(data.F.rows()*3,4);
  669. per_corner(data.V_material_diffuse,meshgl.V_diffuse_vbo);
  670. }
  671. if (meshgl.dirty & MeshGL::DIRTY_SPECULAR)
  672. {
  673. meshgl.V_specular_vbo.resize(data.F.rows()*3,4);
  674. per_corner(data.V_material_specular,meshgl.V_specular_vbo);
  675. }
  676. if (meshgl.dirty & MeshGL::DIRTY_NORMAL)
  677. {
  678. meshgl.V_normals_vbo.resize(data.F.rows()*3,3);
  679. per_corner(data.V_normals,meshgl.V_normals_vbo);
  680. if (invert_normals)
  681. meshgl.V_normals_vbo = -meshgl.V_normals_vbo;
  682. }
  683. if (meshgl.dirty & MeshGL::DIRTY_FACE)
  684. {
  685. meshgl.F_vbo.resize(data.F.rows(),3);
  686. for (unsigned i=0; i<data.F.rows();++i)
  687. meshgl.F_vbo.row(i) << i*3+0, i*3+1, i*3+2;
  688. }
  689. if ( (meshgl.dirty & MeshGL::DIRTY_UV) && data.V_uv.rows()>0)
  690. {
  691. meshgl.V_uv_vbo.resize(data.F.rows()*3,2);
  692. for (unsigned i=0; i<data.F.rows();++i)
  693. for (unsigned j=0;j<3;++j)
  694. meshgl.V_uv_vbo.row(i*3+j) =
  695. data.V_uv.row(per_corner_uv ?
  696. data.F_uv(i,j) : data.F(i,j)).cast<float>();
  697. }
  698. }
  699. } else
  700. {
  701. if (meshgl.dirty & MeshGL::DIRTY_POSITION)
  702. {
  703. per_corner(data.V,meshgl.V_vbo);
  704. }
  705. if (meshgl.dirty & MeshGL::DIRTY_AMBIENT)
  706. {
  707. per_face(data.F_material_ambient,meshgl.V_ambient_vbo);
  708. }
  709. if (meshgl.dirty & MeshGL::DIRTY_DIFFUSE)
  710. {
  711. per_face(data.F_material_diffuse,meshgl.V_diffuse_vbo);
  712. }
  713. if (meshgl.dirty & MeshGL::DIRTY_SPECULAR)
  714. {
  715. per_face(data.F_material_specular,meshgl.V_specular_vbo);
  716. }
  717. if (meshgl.dirty & MeshGL::DIRTY_NORMAL)
  718. {
  719. meshgl.V_normals_vbo.resize(data.F.rows()*3,3);
  720. for (unsigned i=0; i<data.F.rows();++i)
  721. for (unsigned j=0;j<3;++j)
  722. meshgl.V_normals_vbo.row(i*3+j) =
  723. per_corner_normals ?
  724. data.F_normals.row(i*3+j).cast<float>() :
  725. data.F_normals.row(i).cast<float>();
  726. if (invert_normals)
  727. meshgl.V_normals_vbo = -meshgl.V_normals_vbo;
  728. }
  729. if (meshgl.dirty & MeshGL::DIRTY_FACE)
  730. {
  731. meshgl.F_vbo.resize(data.F.rows(),3);
  732. for (unsigned i=0; i<data.F.rows();++i)
  733. meshgl.F_vbo.row(i) << i*3+0, i*3+1, i*3+2;
  734. }
  735. if( (meshgl.dirty & MeshGL::DIRTY_UV) && data.V_uv.rows()>0)
  736. {
  737. meshgl.V_uv_vbo.resize(data.F.rows()*3,2);
  738. for (unsigned i=0; i<data.F.rows();++i)
  739. for (unsigned j=0;j<3;++j)
  740. meshgl.V_uv_vbo.row(i*3+j) = data.V_uv.row(per_corner_uv ? data.F_uv(i,j) : data.F(i,j)).cast<float>();
  741. }
  742. }
  743. if (meshgl.dirty & MeshGL::DIRTY_TEXTURE)
  744. {
  745. meshgl.tex_u = data.texture_R.rows();
  746. meshgl.tex_v = data.texture_R.cols();
  747. meshgl.tex.resize(data.texture_R.size()*4);
  748. for (unsigned i=0;i<data.texture_R.size();++i)
  749. {
  750. meshgl.tex(i*4+0) = data.texture_R(i);
  751. meshgl.tex(i*4+1) = data.texture_G(i);
  752. meshgl.tex(i*4+2) = data.texture_B(i);
  753. meshgl.tex(i*4+3) = data.texture_A(i);
  754. }
  755. }
  756. if (meshgl.dirty & MeshGL::DIRTY_OVERLAY_LINES)
  757. {
  758. meshgl.lines_V_vbo.resize(data.lines.rows()*2,3);
  759. meshgl.lines_V_colors_vbo.resize(data.lines.rows()*2,3);
  760. meshgl.lines_F_vbo.resize(data.lines.rows()*2,1);
  761. for (unsigned i=0; i<data.lines.rows();++i)
  762. {
  763. meshgl.lines_V_vbo.row(2*i+0) = data.lines.block<1, 3>(i, 0).cast<float>();
  764. meshgl.lines_V_vbo.row(2*i+1) = data.lines.block<1, 3>(i, 3).cast<float>();
  765. meshgl.lines_V_colors_vbo.row(2*i+0) = data.lines.block<1, 3>(i, 6).cast<float>();
  766. meshgl.lines_V_colors_vbo.row(2*i+1) = data.lines.block<1, 3>(i, 6).cast<float>();
  767. meshgl.lines_F_vbo(2*i+0) = 2*i+0;
  768. meshgl.lines_F_vbo(2*i+1) = 2*i+1;
  769. }
  770. }
  771. if (meshgl.dirty & MeshGL::DIRTY_OVERLAY_POINTS)
  772. {
  773. meshgl.points_V_vbo.resize(data.points.rows(),3);
  774. meshgl.points_V_colors_vbo.resize(data.points.rows(),3);
  775. meshgl.points_F_vbo.resize(data.points.rows(),1);
  776. for (unsigned i=0; i<data.points.rows();++i)
  777. {
  778. meshgl.points_V_vbo.row(i) = data.points.block<1, 3>(i, 0).cast<float>();
  779. meshgl.points_V_colors_vbo.row(i) = data.points.block<1, 3>(i, 3).cast<float>();
  780. meshgl.points_F_vbo(i) = i;
  781. }
  782. }
  783. if (meshgl.dirty & MeshGL::DIRTY_FACE_LABELS)
  784. {
  785. if(face_labels_positions.rows()==0)
  786. {
  787. face_labels_positions.conservativeResize(F.rows(), 3);
  788. Eigen::MatrixXd faceNormals = F_normals.normalized();
  789. for (int f=0; f<F.rows();++f)
  790. {
  791. std::string faceName = std::to_string(f);
  792. face_labels_positions.row(f) = V.row(F.row(f)(0));
  793. face_labels_positions.row(f) += V.row(F.row(f)(1));
  794. face_labels_positions.row(f) += V.row(F.row(f)(2));
  795. face_labels_positions.row(f) /= 3.;
  796. face_labels_positions.row(f) = (faceNormals*0.05).row(f) + face_labels_positions.row(f);
  797. face_labels_strings.push_back(faceName);
  798. }
  799. }
  800. update_labels(
  801. meshgl.face_labels,
  802. face_labels_positions,
  803. face_labels_strings
  804. );
  805. }
  806. if (meshgl.dirty & MeshGL::DIRTY_VERTEX_LABELS)
  807. {
  808. if(vertex_labels_positions.rows()==0)
  809. {
  810. vertex_labels_positions.conservativeResize(V.rows(), 3);
  811. Eigen::MatrixXd normalized = V_normals.normalized();
  812. for (int v=0; v<V.rows();++v)
  813. {
  814. std::string vertName = std::to_string(v);
  815. vertex_labels_positions.row(v) = (normalized*0.1).row(v) + V.row(v);
  816. vertex_labels_strings.push_back(vertName);
  817. }
  818. }
  819. update_labels(
  820. meshgl.vertex_labels,
  821. vertex_labels_positions,
  822. vertex_labels_strings
  823. );
  824. }
  825. if (meshgl.dirty & MeshGL::DIRTY_CUSTOM_LABELS)
  826. {
  827. update_labels(
  828. meshgl.custom_labels,
  829. labels_positions,
  830. labels_strings
  831. );
  832. }
  833. }