draw_mesh.cpp 10.0 KB

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