ViewerData.cpp 22 KB

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