ViewerData.cpp 19 KB

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