ViewerData.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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. show_faces(true),
  18. show_lines(true),
  19. invert_normals(false),
  20. show_overlay(true),
  21. show_overlay_depth(true),
  22. show_vertid(false),
  23. show_faceid(false),
  24. show_labels(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(1)
  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 | MeshGL::DIRTY_SPECULAR | MeshGL::DIRTY_AMBIENT;
  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::clear_points()
  264. {
  265. points.resize(0, 6);
  266. }
  267. IGL_INLINE void igl::opengl::ViewerData::set_edges(
  268. const Eigen::MatrixXd& P,
  269. const Eigen::MatrixXi& E,
  270. const Eigen::MatrixXd& C)
  271. {
  272. using namespace Eigen;
  273. lines.resize(E.rows(),9);
  274. assert(C.cols() == 3);
  275. for(int e = 0;e<E.rows();e++)
  276. {
  277. RowVector3d color;
  278. if(C.size() == 3)
  279. {
  280. color<<C;
  281. }else if(C.rows() == E.rows())
  282. {
  283. color<<C.row(e);
  284. }
  285. lines.row(e)<< P.row(E(e,0)), P.row(E(e,1)), color;
  286. }
  287. dirty |= MeshGL::DIRTY_OVERLAY_LINES;
  288. }
  289. IGL_INLINE void igl::opengl::ViewerData::add_edges(const Eigen::MatrixXd& P1, const Eigen::MatrixXd& P2, const Eigen::MatrixXd& C)
  290. {
  291. Eigen::MatrixXd P1_temp,P2_temp;
  292. // If P1 only has two columns, pad with a column of zeros
  293. if (P1.cols() == 2)
  294. {
  295. P1_temp = Eigen::MatrixXd::Zero(P1.rows(),3);
  296. P1_temp.block(0,0,P1.rows(),2) = P1;
  297. P2_temp = Eigen::MatrixXd::Zero(P2.rows(),3);
  298. P2_temp.block(0,0,P2.rows(),2) = P2;
  299. }
  300. else
  301. {
  302. P1_temp = P1;
  303. P2_temp = P2;
  304. }
  305. int lastid = lines.rows();
  306. lines.conservativeResize(lines.rows() + P1_temp.rows(),9);
  307. for (unsigned i=0; i<P1_temp.rows(); ++i)
  308. lines.row(lastid+i) << P1_temp.row(i), P2_temp.row(i), i<C.rows() ? C.row(i) : C.row(C.rows()-1);
  309. dirty |= MeshGL::DIRTY_OVERLAY_LINES;
  310. }
  311. IGL_INLINE void igl::opengl::ViewerData::clear_edges()
  312. {
  313. lines.resize(0, 9);
  314. }
  315. IGL_INLINE void igl::opengl::ViewerData::add_label(const Eigen::VectorXd& P, const std::string& str)
  316. {
  317. Eigen::RowVectorXd P_temp;
  318. // If P only has two columns, pad with a column of zeros
  319. if (P.size() == 2)
  320. {
  321. P_temp = Eigen::RowVectorXd::Zero(3);
  322. P_temp << P.transpose(), 0;
  323. }
  324. else
  325. P_temp = P;
  326. int lastid = labels_positions.rows();
  327. labels_positions.conservativeResize(lastid+1, 3);
  328. labels_positions.row(lastid) = P_temp;
  329. labels_strings.push_back(str);
  330. }
  331. IGL_INLINE void igl::opengl::ViewerData::set_labels(const Eigen::MatrixXd& P, const std::vector<std::string>& str)
  332. {
  333. assert(P.rows() == str.size() && "position # and label # do not match!");
  334. assert(P.cols() == 3 && "dimension of label positions incorrect!");
  335. labels_positions = P;
  336. labels_strings = str;
  337. }
  338. IGL_INLINE void igl::opengl::ViewerData::clear_labels()
  339. {
  340. labels_positions.resize(0,3);
  341. labels_strings.clear();
  342. }
  343. IGL_INLINE void igl::opengl::ViewerData::clear()
  344. {
  345. V = Eigen::MatrixXd (0,3);
  346. F = Eigen::MatrixXi (0,3);
  347. F_material_ambient = Eigen::MatrixXd (0,4);
  348. F_material_diffuse = Eigen::MatrixXd (0,4);
  349. F_material_specular = Eigen::MatrixXd (0,4);
  350. V_material_ambient = Eigen::MatrixXd (0,4);
  351. V_material_diffuse = Eigen::MatrixXd (0,4);
  352. V_material_specular = Eigen::MatrixXd (0,4);
  353. F_normals = Eigen::MatrixXd (0,3);
  354. V_normals = Eigen::MatrixXd (0,3);
  355. V_uv = Eigen::MatrixXd (0,2);
  356. F_uv = Eigen::MatrixXi (0,3);
  357. lines = Eigen::MatrixXd (0,9);
  358. points = Eigen::MatrixXd (0,6);
  359. labels_positions = Eigen::MatrixXd (0,3);
  360. labels_strings.clear();
  361. face_based = false;
  362. }
  363. IGL_INLINE void igl::opengl::ViewerData::compute_normals()
  364. {
  365. igl::per_face_normals(V, F, F_normals);
  366. igl::per_vertex_normals(V, F, F_normals, V_normals);
  367. dirty |= MeshGL::DIRTY_NORMAL;
  368. }
  369. IGL_INLINE void igl::opengl::ViewerData::uniform_colors(
  370. const Eigen::Vector3d& ambient,
  371. const Eigen::Vector3d& diffuse,
  372. const Eigen::Vector3d& specular)
  373. {
  374. Eigen::Vector4d ambient4;
  375. Eigen::Vector4d diffuse4;
  376. Eigen::Vector4d specular4;
  377. ambient4 << ambient, 1;
  378. diffuse4 << diffuse, 1;
  379. specular4 << specular, 1;
  380. uniform_colors(ambient4,diffuse4,specular4);
  381. }
  382. IGL_INLINE void igl::opengl::ViewerData::uniform_colors(
  383. const Eigen::Vector4d& ambient,
  384. const Eigen::Vector4d& diffuse,
  385. const Eigen::Vector4d& specular)
  386. {
  387. V_material_ambient.resize(V.rows(),4);
  388. V_material_diffuse.resize(V.rows(),4);
  389. V_material_specular.resize(V.rows(),4);
  390. for (unsigned i=0; i<V.rows();++i)
  391. {
  392. V_material_ambient.row(i) = ambient;
  393. V_material_diffuse.row(i) = diffuse;
  394. V_material_specular.row(i) = specular;
  395. }
  396. F_material_ambient.resize(F.rows(),4);
  397. F_material_diffuse.resize(F.rows(),4);
  398. F_material_specular.resize(F.rows(),4);
  399. for (unsigned i=0; i<F.rows();++i)
  400. {
  401. F_material_ambient.row(i) = ambient;
  402. F_material_diffuse.row(i) = diffuse;
  403. F_material_specular.row(i) = specular;
  404. }
  405. dirty |= MeshGL::DIRTY_SPECULAR | MeshGL::DIRTY_DIFFUSE | MeshGL::DIRTY_AMBIENT;
  406. }
  407. IGL_INLINE void igl::opengl::ViewerData::grid_texture()
  408. {
  409. // Don't do anything for an empty mesh
  410. if(V.rows() == 0)
  411. {
  412. V_uv.resize(V.rows(),2);
  413. return;
  414. }
  415. if (V_uv.rows() == 0)
  416. {
  417. V_uv = V.block(0, 0, V.rows(), 2);
  418. V_uv.col(0) = V_uv.col(0).array() - V_uv.col(0).minCoeff();
  419. V_uv.col(0) = V_uv.col(0).array() / V_uv.col(0).maxCoeff();
  420. V_uv.col(1) = V_uv.col(1).array() - V_uv.col(1).minCoeff();
  421. V_uv.col(1) = V_uv.col(1).array() / V_uv.col(1).maxCoeff();
  422. V_uv = V_uv.array() * 10;
  423. dirty |= MeshGL::DIRTY_TEXTURE;
  424. }
  425. unsigned size = 128;
  426. unsigned size2 = size/2;
  427. texture_R.resize(size, size);
  428. for (unsigned i=0; i<size; ++i)
  429. {
  430. for (unsigned j=0; j<size; ++j)
  431. {
  432. texture_R(i,j) = 0;
  433. if ((i<size2 && j<size2) || (i>=size2 && j>=size2))
  434. texture_R(i,j) = 255;
  435. }
  436. }
  437. texture_G = texture_R;
  438. texture_B = texture_R;
  439. texture_A = Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>::Constant(texture_R.rows(),texture_R.cols(),255);
  440. dirty |= MeshGL::DIRTY_TEXTURE;
  441. }
  442. IGL_INLINE void igl::opengl::ViewerData::updateGL(
  443. const igl::opengl::ViewerData& data,
  444. const bool invert_normals,
  445. igl::opengl::MeshGL& meshgl
  446. )
  447. {
  448. if (!meshgl.is_initialized)
  449. {
  450. meshgl.init();
  451. }
  452. bool per_corner_uv = (data.F_uv.rows() == data.F.rows());
  453. bool per_corner_normals = (data.F_normals.rows() == 3 * data.F.rows());
  454. meshgl.dirty |= data.dirty;
  455. // Input:
  456. // X #F by dim quantity
  457. // Output:
  458. // X_vbo #F*3 by dim scattering per corner
  459. const auto per_face = [&data](
  460. const Eigen::MatrixXd & X,
  461. MeshGL::RowMatrixXf & X_vbo)
  462. {
  463. assert(X.cols() == 4);
  464. X_vbo.resize(data.F.rows()*3,4);
  465. for (unsigned i=0; i<data.F.rows();++i)
  466. for (unsigned j=0;j<3;++j)
  467. X_vbo.row(i*3+j) = X.row(i).cast<float>();
  468. };
  469. // Input:
  470. // X #V by dim quantity
  471. // Output:
  472. // X_vbo #F*3 by dim scattering per corner
  473. const auto per_corner = [&data](
  474. const Eigen::MatrixXd & X,
  475. MeshGL::RowMatrixXf & X_vbo)
  476. {
  477. X_vbo.resize(data.F.rows()*3,X.cols());
  478. for (unsigned i=0; i<data.F.rows();++i)
  479. for (unsigned j=0;j<3;++j)
  480. X_vbo.row(i*3+j) = X.row(data.F(i,j)).cast<float>();
  481. };
  482. if (!data.face_based)
  483. {
  484. if (!(per_corner_uv || per_corner_normals))
  485. {
  486. // Vertex positions
  487. if (meshgl.dirty & MeshGL::DIRTY_POSITION)
  488. meshgl.V_vbo = data.V.cast<float>();
  489. // Vertex normals
  490. if (meshgl.dirty & MeshGL::DIRTY_NORMAL)
  491. {
  492. meshgl.V_normals_vbo = data.V_normals.cast<float>();
  493. if (invert_normals)
  494. meshgl.V_normals_vbo = -meshgl.V_normals_vbo;
  495. }
  496. // Per-vertex material settings
  497. if (meshgl.dirty & MeshGL::DIRTY_AMBIENT)
  498. meshgl.V_ambient_vbo = data.V_material_ambient.cast<float>();
  499. if (meshgl.dirty & MeshGL::DIRTY_DIFFUSE)
  500. meshgl.V_diffuse_vbo = data.V_material_diffuse.cast<float>();
  501. if (meshgl.dirty & MeshGL::DIRTY_SPECULAR)
  502. meshgl.V_specular_vbo = data.V_material_specular.cast<float>();
  503. // Face indices
  504. if (meshgl.dirty & MeshGL::DIRTY_FACE)
  505. meshgl.F_vbo = data.F.cast<unsigned>();
  506. // Texture coordinates
  507. if (meshgl.dirty & MeshGL::DIRTY_UV)
  508. {
  509. meshgl.V_uv_vbo = data.V_uv.cast<float>();
  510. }
  511. }
  512. else
  513. {
  514. // Per vertex properties with per corner UVs
  515. if (meshgl.dirty & MeshGL::DIRTY_POSITION)
  516. {
  517. per_corner(data.V,meshgl.V_vbo);
  518. }
  519. if (meshgl.dirty & MeshGL::DIRTY_AMBIENT)
  520. {
  521. meshgl.V_ambient_vbo.resize(data.F.rows()*3,4);
  522. for (unsigned i=0; i<data.F.rows();++i)
  523. for (unsigned j=0;j<3;++j)
  524. meshgl.V_ambient_vbo.row(i*3+j) = data.V_material_ambient.row(data.F(i,j)).cast<float>();
  525. }
  526. if (meshgl.dirty & MeshGL::DIRTY_DIFFUSE)
  527. {
  528. meshgl.V_diffuse_vbo.resize(data.F.rows()*3,4);
  529. for (unsigned i=0; i<data.F.rows();++i)
  530. for (unsigned j=0;j<3;++j)
  531. meshgl.V_diffuse_vbo.row(i*3+j) = data.V_material_diffuse.row(data.F(i,j)).cast<float>();
  532. }
  533. if (meshgl.dirty & MeshGL::DIRTY_SPECULAR)
  534. {
  535. meshgl.V_specular_vbo.resize(data.F.rows()*3,4);
  536. for (unsigned i=0; i<data.F.rows();++i)
  537. for (unsigned j=0;j<3;++j)
  538. meshgl.V_specular_vbo.row(i*3+j) = data.V_material_specular.row(data.F(i,j)).cast<float>();
  539. }
  540. if (meshgl.dirty & MeshGL::DIRTY_NORMAL)
  541. {
  542. meshgl.V_normals_vbo.resize(data.F.rows()*3,3);
  543. for (unsigned i=0; i<data.F.rows();++i)
  544. for (unsigned j=0;j<3;++j)
  545. meshgl.V_normals_vbo.row(i*3+j) =
  546. per_corner_normals ?
  547. data.F_normals.row(i*3+j).cast<float>() :
  548. data.V_normals.row(data.F(i,j)).cast<float>();
  549. if (invert_normals)
  550. meshgl.V_normals_vbo = -meshgl.V_normals_vbo;
  551. }
  552. if (meshgl.dirty & MeshGL::DIRTY_FACE)
  553. {
  554. meshgl.F_vbo.resize(data.F.rows(),3);
  555. for (unsigned i=0; i<data.F.rows();++i)
  556. meshgl.F_vbo.row(i) << i*3+0, i*3+1, i*3+2;
  557. }
  558. if (meshgl.dirty & MeshGL::DIRTY_UV)
  559. {
  560. meshgl.V_uv_vbo.resize(data.F.rows()*3,2);
  561. for (unsigned i=0; i<data.F.rows();++i)
  562. for (unsigned j=0;j<3;++j)
  563. meshgl.V_uv_vbo.row(i*3+j) =
  564. data.V_uv.row(per_corner_uv ?
  565. data.F_uv(i,j) : data.F(i,j)).cast<float>();
  566. }
  567. }
  568. }
  569. else
  570. {
  571. if (meshgl.dirty & MeshGL::DIRTY_POSITION)
  572. {
  573. per_corner(data.V,meshgl.V_vbo);
  574. }
  575. if (meshgl.dirty & MeshGL::DIRTY_AMBIENT)
  576. {
  577. per_face(data.F_material_ambient,meshgl.V_ambient_vbo);
  578. }
  579. if (meshgl.dirty & MeshGL::DIRTY_DIFFUSE)
  580. {
  581. per_face(data.F_material_diffuse,meshgl.V_diffuse_vbo);
  582. }
  583. if (meshgl.dirty & MeshGL::DIRTY_SPECULAR)
  584. {
  585. per_face(data.F_material_specular,meshgl.V_specular_vbo);
  586. }
  587. if (meshgl.dirty & MeshGL::DIRTY_NORMAL)
  588. {
  589. meshgl.V_normals_vbo.resize(data.F.rows()*3,3);
  590. for (unsigned i=0; i<data.F.rows();++i)
  591. for (unsigned j=0;j<3;++j)
  592. meshgl.V_normals_vbo.row(i*3+j) =
  593. per_corner_normals ?
  594. data.F_normals.row(i*3+j).cast<float>() :
  595. data.F_normals.row(i).cast<float>();
  596. if (invert_normals)
  597. meshgl.V_normals_vbo = -meshgl.V_normals_vbo;
  598. }
  599. if (meshgl.dirty & MeshGL::DIRTY_FACE)
  600. {
  601. meshgl.F_vbo.resize(data.F.rows(),3);
  602. for (unsigned i=0; i<data.F.rows();++i)
  603. meshgl.F_vbo.row(i) << i*3+0, i*3+1, i*3+2;
  604. }
  605. if (meshgl.dirty & MeshGL::DIRTY_UV)
  606. {
  607. meshgl.V_uv_vbo.resize(data.F.rows()*3,2);
  608. for (unsigned i=0; i<data.F.rows();++i)
  609. for (unsigned j=0;j<3;++j)
  610. 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>();
  611. }
  612. }
  613. if (meshgl.dirty & MeshGL::DIRTY_TEXTURE)
  614. {
  615. meshgl.tex_u = data.texture_R.rows();
  616. meshgl.tex_v = data.texture_R.cols();
  617. meshgl.tex.resize(data.texture_R.size()*4);
  618. for (unsigned i=0;i<data.texture_R.size();++i)
  619. {
  620. meshgl.tex(i*4+0) = data.texture_R(i);
  621. meshgl.tex(i*4+1) = data.texture_G(i);
  622. meshgl.tex(i*4+2) = data.texture_B(i);
  623. meshgl.tex(i*4+3) = data.texture_A(i);
  624. }
  625. }
  626. if (meshgl.dirty & MeshGL::DIRTY_OVERLAY_LINES)
  627. {
  628. meshgl.lines_V_vbo.resize(data.lines.rows()*2,3);
  629. meshgl.lines_V_colors_vbo.resize(data.lines.rows()*2,3);
  630. meshgl.lines_F_vbo.resize(data.lines.rows()*2,1);
  631. for (unsigned i=0; i<data.lines.rows();++i)
  632. {
  633. meshgl.lines_V_vbo.row(2*i+0) = data.lines.block<1, 3>(i, 0).cast<float>();
  634. meshgl.lines_V_vbo.row(2*i+1) = data.lines.block<1, 3>(i, 3).cast<float>();
  635. meshgl.lines_V_colors_vbo.row(2*i+0) = data.lines.block<1, 3>(i, 6).cast<float>();
  636. meshgl.lines_V_colors_vbo.row(2*i+1) = data.lines.block<1, 3>(i, 6).cast<float>();
  637. meshgl.lines_F_vbo(2*i+0) = 2*i+0;
  638. meshgl.lines_F_vbo(2*i+1) = 2*i+1;
  639. }
  640. }
  641. if (meshgl.dirty & MeshGL::DIRTY_OVERLAY_POINTS)
  642. {
  643. meshgl.points_V_vbo.resize(data.points.rows(),3);
  644. meshgl.points_V_colors_vbo.resize(data.points.rows(),3);
  645. meshgl.points_F_vbo.resize(data.points.rows(),1);
  646. for (unsigned i=0; i<data.points.rows();++i)
  647. {
  648. meshgl.points_V_vbo.row(i) = data.points.block<1, 3>(i, 0).cast<float>();
  649. meshgl.points_V_colors_vbo.row(i) = data.points.block<1, 3>(i, 3).cast<float>();
  650. meshgl.points_F_vbo(i) = i;
  651. }
  652. }
  653. }