draw_mesh.cpp 10 KB

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