draw_mesh.cpp 12 KB

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