draw_mesh.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <alecjacobson@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 "draw_mesh.h"
  9. #ifndef IGL_NO_OPENGL
  10. IGL_INLINE void igl::draw_mesh(
  11. const Eigen::MatrixXd & V,
  12. const Eigen::MatrixXi & F,
  13. const Eigen::MatrixXd & N)
  14. {
  15. glBegin(GL_TRIANGLES);
  16. // loop over faces
  17. for(int i = 0; i<F.rows();i++)
  18. {
  19. // loop over corners of triangle
  20. for(int j = 0;j<3;j++)
  21. {
  22. if(N.rows() == V.rows())
  23. {
  24. glNormal3d(N(F(i,j),0),N(F(i,j),1),N(F(i,j),2));
  25. }else if(N.rows() == F.rows()*3)
  26. {
  27. glNormal3d(N(i*3+j,0),N(i*3+j,1),N(i*3+j,2));
  28. }else if(N.rows() == F.rows())
  29. {
  30. glNormal3d(N(i,0),N(i,1),N(i,2));
  31. }
  32. glVertex3d(V(F(i,j),0),V(F(i,j),1),V(F(i,j),2));
  33. }
  34. }
  35. glEnd();
  36. }
  37. IGL_INLINE void igl::draw_mesh(
  38. const Eigen::MatrixXd & V,
  39. const Eigen::MatrixXi & F,
  40. const Eigen::MatrixXd & N,
  41. const Eigen::MatrixXd & C)
  42. {
  43. glBegin(GL_TRIANGLES);
  44. // loop over faces
  45. for(int i = 0; i<F.rows();i++)
  46. {
  47. // loop over corners of triangle
  48. for(int j = 0;j<3;j++)
  49. {
  50. if(C.rows() == 1)
  51. {
  52. glColor3d(C(0,0),C(0,1),C(0,2));
  53. }else if(C.rows() == V.rows())
  54. {
  55. glColor3d(C(F(i,j),0),C(F(i,j),1),C(F(i,j),2));
  56. }else if(C.rows() == F.rows()*3)
  57. {
  58. glColor3d(C(i*3+j,0),C(i*3+j,1),C(i*3+j,2));
  59. }else if(C.rows() == F.rows())
  60. {
  61. glColor3d(C(i,0),C(i,1),C(i,2));
  62. }else
  63. {
  64. assert(C.size() == 0);
  65. }
  66. if(N.rows() == V.rows())
  67. {
  68. glNormal3d(N(F(i,j),0),N(F(i,j),1),N(F(i,j),2));
  69. }else if(N.rows() == F.rows()*3)
  70. {
  71. glNormal3d(N(i*3+j,0),N(i*3+j,1),N(i*3+j,2));
  72. }else if(N.rows() == F.rows())
  73. {
  74. glNormal3d(N(i,0),N(i,1),N(i,2));
  75. }
  76. glVertex3d(V(F(i,j),0),V(F(i,j),1),V(F(i,j),2));
  77. }
  78. }
  79. glEnd();
  80. }
  81. IGL_INLINE void igl::draw_mesh(
  82. const Eigen::MatrixXd & V,
  83. const Eigen::MatrixXi & F,
  84. const Eigen::MatrixXd & N,
  85. const Eigen::MatrixXd & C,
  86. const Eigen::MatrixXd & TC)
  87. {
  88. glBegin(GL_TRIANGLES);
  89. // loop over faces
  90. for(int i = 0; i<F.rows();i++)
  91. {
  92. // loop over corners of triangle
  93. for(int j = 0;j<3;j++)
  94. {
  95. if(TC.rows() == 1)
  96. {
  97. glTexCoord2d(TC(F(i,j),0),TC(F(i,j),1));
  98. }else if(TC.rows() == V.rows())
  99. {
  100. glTexCoord2d(TC(F(i,j),0),TC(F(i,j),1));
  101. }else if(TC.rows() == F.rows()*2)
  102. {
  103. glTexCoord2d(TC(i*2+j,0),TC(i*2+j,1));
  104. }else if(TC.rows() == F.rows())
  105. {
  106. glTexCoord2d(TC(i,0),TC(i,1));
  107. }else
  108. {
  109. assert(TC.size() == 0);
  110. }
  111. if(C.rows() == 1)
  112. {
  113. glColor3d(C(0,0),C(0,1),C(0,2));
  114. }else if(C.rows() == V.rows())
  115. {
  116. glColor3d(C(F(i,j),0),C(F(i,j),1),C(F(i,j),2));
  117. }else if(C.rows() == F.rows()*3)
  118. {
  119. glColor3d(C(i*3+j,0),C(i*3+j,1),C(i*3+j,2));
  120. }else if(C.rows() == F.rows())
  121. {
  122. glColor3d(C(i,0),C(i,1),C(i,2));
  123. }else
  124. {
  125. assert(C.size() == 0);
  126. }
  127. if(N.rows() == V.rows())
  128. {
  129. glNormal3d(N(F(i,j),0),N(F(i,j),1),N(F(i,j),2));
  130. }else if(N.rows() == F.rows()*3)
  131. {
  132. glNormal3d(N(i*3+j,0),N(i*3+j,1),N(i*3+j,2));
  133. }else if(N.rows() == F.rows())
  134. {
  135. glNormal3d(N(i,0),N(i,1),N(i,2));
  136. }else
  137. {
  138. assert(N.size() == 0);
  139. }
  140. glVertex3d(V(F(i,j),0),V(F(i,j),1),V(F(i,j),2));
  141. }
  142. }
  143. glEnd();
  144. }
  145. IGL_INLINE void igl::draw_mesh(
  146. const Eigen::MatrixXd & V,
  147. const Eigen::MatrixXi & F,
  148. const Eigen::MatrixXd & N,
  149. const Eigen::MatrixXd & C,
  150. const Eigen::MatrixXd & TC,
  151. const Eigen::MatrixXd & W,
  152. const GLuint W_index,
  153. const Eigen::MatrixXi & WI,
  154. const GLuint WI_index)
  155. {
  156. using namespace std;
  157. if(F.size() > 0)
  158. {
  159. assert(F.maxCoeff() < V.rows());
  160. assert(V.cols() == 3);
  161. assert(C.rows() == V.rows() || C.rows() == F.rows()*3 || C.size() == 0);
  162. assert(TC.rows() == V.rows() || TC.rows() == F.rows()*3 || TC.size() == 0);
  163. assert(C.cols() == 3 || C.size() == 0);
  164. assert(
  165. N.rows() == V.rows() || N.rows() == F.rows()*3 || N.rows() ==F.rows());
  166. assert(N.cols() == 3);
  167. }
  168. if(W.size()>0)
  169. {
  170. assert(W.rows() == V.rows());
  171. assert(WI.rows() == V.rows());
  172. assert(W.cols() == WI.cols());
  173. }
  174. glBegin(GL_TRIANGLES);
  175. // loop over faces
  176. for(int i = 0; i<F.rows();i++)
  177. {
  178. // loop over corners of triangle
  179. for(int j = 0;j<3;j++)
  180. {
  181. if(W.size()>0 && W_index !=0 && WI_index != 0)
  182. {
  183. int weights_left = W.cols();
  184. while(weights_left != 0)
  185. {
  186. int pass_size = std::min(4,weights_left);
  187. int weights_already_passed = W.cols()-weights_left;
  188. // Get attribute location of next 4 weights
  189. int pass_W_index = W_index + weights_already_passed/4;
  190. int pass_WI_index = WI_index + weights_already_passed/4;
  191. switch(pass_size)
  192. {
  193. case 1:
  194. glVertexAttrib1d(
  195. pass_W_index,
  196. W(F(i,j),0+weights_already_passed));
  197. glVertexAttrib1d(
  198. pass_WI_index,
  199. WI(F(i,j),0+weights_already_passed));
  200. break;
  201. case 2:
  202. glVertexAttrib2d(
  203. pass_W_index,
  204. W(F(i,j),0+weights_already_passed),
  205. W(F(i,j),1+weights_already_passed));
  206. glVertexAttrib2d(
  207. pass_WI_index,
  208. WI(F(i,j),0+weights_already_passed),
  209. WI(F(i,j),1+weights_already_passed));
  210. break;
  211. case 3:
  212. glVertexAttrib3d(
  213. pass_W_index,
  214. W(F(i,j),0+weights_already_passed),
  215. W(F(i,j),1+weights_already_passed),
  216. W(F(i,j),2+weights_already_passed));
  217. glVertexAttrib3d(
  218. pass_WI_index,
  219. WI(F(i,j),0+weights_already_passed),
  220. WI(F(i,j),1+weights_already_passed),
  221. WI(F(i,j),2+weights_already_passed));
  222. break;
  223. default:
  224. glVertexAttrib4d(
  225. pass_W_index,
  226. W(F(i,j),0+weights_already_passed),
  227. W(F(i,j),1+weights_already_passed),
  228. W(F(i,j),2+weights_already_passed),
  229. W(F(i,j),3+weights_already_passed));
  230. glVertexAttrib4d(
  231. pass_WI_index,
  232. WI(F(i,j),0+weights_already_passed),
  233. WI(F(i,j),1+weights_already_passed),
  234. WI(F(i,j),2+weights_already_passed),
  235. WI(F(i,j),3+weights_already_passed));
  236. break;
  237. }
  238. weights_left -= pass_size;
  239. }
  240. }
  241. if(TC.rows() == V.rows())
  242. {
  243. glTexCoord2d(TC(F(i,j),0),TC(F(i,j),1));
  244. }else if(TC.rows() == F.rows()*3)
  245. {
  246. glTexCoord2d(TC(F(i,j),0),TC(F(i,j),1));
  247. }
  248. if(C.rows() == V.rows())
  249. {
  250. glColor3d(C(F(i,j),0),C(F(i,j),1),C(F(i,j),2));
  251. }else if(C.rows() == F.rows()*3)
  252. {
  253. glColor3d(C(i*3+j,0), C(i*3+j,1), C(i*3+j,2));
  254. }
  255. if(N.rows() == V.rows())
  256. {
  257. glNormal3d(N(F(i,j),0),N(F(i,j),1),N(F(i,j),2));
  258. }else if(N.rows() == F.rows()*3)
  259. {
  260. glNormal3d(N(i*3+j,0),N(i*3+j,1),N(i*3+j,2));
  261. }else if(N.rows() == F.rows())
  262. {
  263. glNormal3d(N(i,0),N(i,1),N(i,2));
  264. }
  265. glVertex3d(V(F(i,j),0),V(F(i,j),1),V(F(i,j),2));
  266. }
  267. }
  268. glEnd();
  269. }
  270. IGL_INLINE void igl::draw_mesh(
  271. const Eigen::MatrixXd & V,
  272. const Eigen::MatrixXi & F,
  273. const Eigen::MatrixXd & N,
  274. const Eigen::MatrixXi & NF,
  275. const Eigen::MatrixXd & C,
  276. const Eigen::MatrixXd & TC,
  277. const Eigen::MatrixXi & TF,
  278. const Eigen::MatrixXd & W,
  279. const GLuint W_index,
  280. const Eigen::MatrixXi & WI,
  281. const GLuint WI_index)
  282. {
  283. using namespace std;
  284. if(F.size() > 0)
  285. {
  286. assert(F.maxCoeff() < V.rows());
  287. assert(V.cols() == 3);
  288. assert(C.rows() == V.rows() || C.rows() == F.rows()*3 || C.size() == 0);
  289. assert(C.cols() == 3 || C.size() == 0);
  290. assert(N.cols() == 3);
  291. assert(TC.cols() == 2 || TC.size() == 0);
  292. }
  293. if(W.size()>0)
  294. {
  295. assert(W.rows() == V.rows());
  296. assert(WI.rows() == V.rows());
  297. assert(W.cols() == WI.cols());
  298. }
  299. glBegin(GL_TRIANGLES);
  300. // loop over faces
  301. for(int i = 0; i<F.rows();i++)
  302. {
  303. // loop over corners of triangle
  304. for(int j = 0;j<3;j++)
  305. {
  306. if(W.size()>0 && W_index !=0 && WI_index != 0)
  307. {
  308. int weights_left = W.cols();
  309. while(weights_left != 0)
  310. {
  311. int pass_size = std::min(4,weights_left);
  312. int weights_already_passed = W.cols()-weights_left;
  313. // Get attribute location of next 4 weights
  314. int pass_W_index = W_index + weights_already_passed/4;
  315. int pass_WI_index = WI_index + weights_already_passed/4;
  316. switch(pass_size)
  317. {
  318. case 1:
  319. glVertexAttrib1d(
  320. pass_W_index,
  321. W(F(i,j),0+weights_already_passed));
  322. glVertexAttrib1d(
  323. pass_WI_index,
  324. WI(F(i,j),0+weights_already_passed));
  325. break;
  326. case 2:
  327. glVertexAttrib2d(
  328. pass_W_index,
  329. W(F(i,j),0+weights_already_passed),
  330. W(F(i,j),1+weights_already_passed));
  331. glVertexAttrib2d(
  332. pass_WI_index,
  333. WI(F(i,j),0+weights_already_passed),
  334. WI(F(i,j),1+weights_already_passed));
  335. break;
  336. case 3:
  337. glVertexAttrib3d(
  338. pass_W_index,
  339. W(F(i,j),0+weights_already_passed),
  340. W(F(i,j),1+weights_already_passed),
  341. W(F(i,j),2+weights_already_passed));
  342. glVertexAttrib3d(
  343. pass_WI_index,
  344. WI(F(i,j),0+weights_already_passed),
  345. WI(F(i,j),1+weights_already_passed),
  346. WI(F(i,j),2+weights_already_passed));
  347. break;
  348. default:
  349. glVertexAttrib4d(
  350. pass_W_index,
  351. W(F(i,j),0+weights_already_passed),
  352. W(F(i,j),1+weights_already_passed),
  353. W(F(i,j),2+weights_already_passed),
  354. W(F(i,j),3+weights_already_passed));
  355. glVertexAttrib4d(
  356. pass_WI_index,
  357. WI(F(i,j),0+weights_already_passed),
  358. WI(F(i,j),1+weights_already_passed),
  359. WI(F(i,j),2+weights_already_passed),
  360. WI(F(i,j),3+weights_already_passed));
  361. break;
  362. }
  363. weights_left -= pass_size;
  364. }
  365. }
  366. if(TC.rows() > 0 && TF.rows() > 0)
  367. {
  368. if(TF(i,j) >= 0 && TF(i,j)<TC.rows())
  369. {
  370. glTexCoord2d(TC(TF(i,j),0),TC(TF(i,j),1));
  371. //printf("TexCoord: %d %g %g\n",TF(i,j), TC(TF(i,j),0),TC(TF(i,j),1));
  372. }// else what?
  373. }
  374. if(C.rows() == V.rows())
  375. {
  376. glColor3d(C(F(i,j),0),C(F(i,j),1),C(F(i,j),2));
  377. }else if(C.rows() == F.rows()*3)
  378. {
  379. glColor3d(C(i*3+j,0), C(i*3+j,1), C(i*3+j,2));
  380. }
  381. if(NF.rows() > 0)
  382. {
  383. const int nfij = NF(i,j);
  384. if(nfij >= 0 && nfij<N.rows())
  385. {
  386. glNormal3d(N(nfij,0),N(nfij,1),N(nfij,2));
  387. //printf("Normal: %d %g %g %g\n",nfij, N(nfij,0),N(nfij,1),N(nfij,2));
  388. }// else what?
  389. } else
  390. {
  391. if(N.rows() == V.rows())
  392. {
  393. glNormal3d(N(F(i,j),0),N(F(i,j),1),N(F(i,j),2));
  394. }else if(N.rows() == F.rows()*3)
  395. {
  396. glNormal3d(N(i*3+j,0),N(i*3+j,1),N(i*3+j,2));
  397. }else if(N.rows() == F.rows())
  398. {
  399. glNormal3d(N(i,0),N(i,1),N(i,2));
  400. }
  401. }
  402. glVertex3d(V(F(i,j),0),V(F(i,j),1),V(F(i,j),2));
  403. //printf("Vertex: %d %g %g %g\n",F(i,j), V(F(i,j),0),V(F(i,j),1),V(F(i,j),2));
  404. }
  405. //printf("\n");
  406. }
  407. glEnd();
  408. }
  409. #endif