ViewerData.cpp 26 KB

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