ViewerData.cpp 20 KB

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