ViewerData.cpp 19 KB

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