ViewerData.cpp 26 KB

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