ViewerData.cpp 20 KB

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