ViewerData.cpp 26 KB

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