ViewerData.cpp 20 KB

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