ViewerData.cpp 20 KB

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