ViewerData.cpp 22 KB

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