draw_mesh.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. IGL_INLINE void igl::opengl2::draw_mesh(
  10. const Eigen::MatrixXd & V,
  11. const Eigen::MatrixXi & F,
  12. const Eigen::MatrixXd & N)
  13. {
  14. using namespace Eigen;
  15. MatrixXd _d;
  16. MatrixXi _i;
  17. return draw_mesh(V,F,N,_i,_d,_d,_i,_d,0,_i,0);
  18. }
  19. IGL_INLINE void igl::opengl2::draw_mesh(
  20. const Eigen::MatrixXd & V,
  21. const Eigen::MatrixXi & F,
  22. const Eigen::MatrixXd & N,
  23. const Eigen::MatrixXd & C)
  24. {
  25. using namespace Eigen;
  26. MatrixXd _d;
  27. MatrixXi _i;
  28. return draw_mesh(V,F,N,_i,C,_d,_i,_d,0,_i,0);
  29. }
  30. IGL_INLINE void igl::opengl2::draw_mesh(
  31. const Eigen::MatrixXd & V,
  32. const Eigen::MatrixXi & F,
  33. const Eigen::MatrixXd & N,
  34. const Eigen::MatrixXd & C,
  35. const Eigen::MatrixXd & TC)
  36. {
  37. using namespace Eigen;
  38. MatrixXd _d;
  39. MatrixXi _i;
  40. return draw_mesh(V,F,N,_i,C,TC,_i,_d,0,_i,0);
  41. }
  42. IGL_INLINE void igl::opengl2::draw_mesh(
  43. const Eigen::MatrixXd & V,
  44. const Eigen::MatrixXi & F,
  45. const Eigen::MatrixXd & N,
  46. const Eigen::MatrixXd & C,
  47. const Eigen::MatrixXd & TC,
  48. const Eigen::MatrixXd & W,
  49. const GLuint W_index,
  50. const Eigen::MatrixXi & WI,
  51. const GLuint WI_index)
  52. {
  53. using namespace Eigen;
  54. return draw_mesh(V,F,N,MatrixXi(),C,TC,MatrixXi(),W,W_index,WI,WI_index);
  55. }
  56. IGL_INLINE void igl::opengl2::draw_mesh(
  57. const Eigen::MatrixXd & V,
  58. const Eigen::MatrixXi & F,
  59. const Eigen::MatrixXd & N,
  60. const Eigen::MatrixXi & NF,
  61. const Eigen::MatrixXd & C,
  62. const Eigen::MatrixXd & TC,
  63. const Eigen::MatrixXi & TF,
  64. const Eigen::MatrixXd & W,
  65. const GLuint W_index,
  66. const Eigen::MatrixXi & WI,
  67. const GLuint WI_index)
  68. {
  69. using namespace std;
  70. using namespace Eigen;
  71. const int rF = F.rows();
  72. const int cF = F.cols();
  73. const int cC = C.cols();
  74. const int rC = C.rows();
  75. const int cW = W.cols();
  76. const int rW = W.rows();
  77. const int rV = V.rows();
  78. const int rTC = TC.rows();
  79. const int rTF = TF.rows();
  80. const int rNF = NF.rows();
  81. const int rN = N.rows();
  82. if(F.size() > 0)
  83. {
  84. assert(F.maxCoeff() < V.rows());
  85. assert(V.cols() == 3);
  86. assert(rC == rV || rC == rF || rC == rF*3 || rC==1 || C.size() == 0);
  87. assert(C.cols() >= 3 || C.size() == 0);
  88. assert(N.cols() == 3 || N.size() == 0);
  89. assert(TC.cols() == 2 || TC.size() == 0);
  90. assert(cF == 3 || cF == 4);
  91. assert(TF.size() == 0 || TF.cols() == F.cols());
  92. assert(NF.size() == 0 || NF.cols() == NF.cols());
  93. }
  94. if(W.size()>0)
  95. {
  96. assert(W.rows() == V.rows());
  97. assert(WI.rows() == V.rows());
  98. assert(W.cols() == WI.cols());
  99. }
  100. switch(F.cols())
  101. {
  102. default:
  103. case 3:
  104. glBegin(GL_TRIANGLES);
  105. break;
  106. case 4:
  107. glBegin(GL_QUADS);
  108. break;
  109. }
  110. // loop over faces
  111. for(int i = 0; i<rF;i++)
  112. {
  113. // loop over corners of triangle
  114. for(int j = 0;j<cF;j++)
  115. {
  116. int tc = -1;
  117. if(rTF != 0)
  118. {
  119. tc = TF(i,j);
  120. } else if(rTC == 1)
  121. {
  122. tc = 0;
  123. }else if(rTC == rV)
  124. {
  125. tc = F(i,j);
  126. }else if(rTC == rF*cF)
  127. {
  128. tc = i*cF + j;
  129. }else if(rTC == rF)
  130. {
  131. tc = i;
  132. }else
  133. {
  134. assert(TC.size() == 0);
  135. }
  136. // RGB(A)
  137. Matrix<MatrixXd::Scalar,1,Dynamic> color;
  138. if(rC == 1)
  139. {
  140. color = C.row(0);
  141. }else if(rC == rV)
  142. {
  143. color = C.row(F(i,j));
  144. }else if(rC == rF*cF)
  145. {
  146. color = C.row(i*cF+j);
  147. }else if(rC == rF)
  148. {
  149. color = C.row(i);
  150. }else
  151. {
  152. assert(C.size() == 0);
  153. }
  154. int n = -1;
  155. if(rNF != 0)
  156. {
  157. n = NF(i,j); // indexed normals
  158. } else if(rN == 1)
  159. {
  160. n = 0; // uniform normals
  161. }else if(rN == rF)
  162. {
  163. n = i; // face normals
  164. }else if(rN == rV)
  165. {
  166. n = F(i,j); // vertex normals
  167. }else if(rN == rF*cF)
  168. {
  169. n = i*cF + j; // corner normals
  170. }else
  171. {
  172. assert(N.size() == 0);
  173. }
  174. {
  175. if(rW>0 && W_index !=0 && WI_index != 0)
  176. {
  177. int weights_left = cW;
  178. while(weights_left != 0)
  179. {
  180. int pass_size = std::min(4,weights_left);
  181. int weights_already_passed = cW-weights_left;
  182. // Get attribute location of next 4 weights
  183. int pass_W_index = W_index + weights_already_passed/4;
  184. int pass_WI_index = WI_index + weights_already_passed/4;
  185. switch(pass_size)
  186. {
  187. case 1:
  188. glVertexAttrib1d(
  189. pass_W_index,
  190. W(F(i,j),0+weights_already_passed));
  191. glVertexAttrib1d(
  192. pass_WI_index,
  193. WI(F(i,j),0+weights_already_passed));
  194. break;
  195. case 2:
  196. glVertexAttrib2d(
  197. pass_W_index,
  198. W(F(i,j),0+weights_already_passed),
  199. W(F(i,j),1+weights_already_passed));
  200. glVertexAttrib2d(
  201. pass_WI_index,
  202. WI(F(i,j),0+weights_already_passed),
  203. WI(F(i,j),1+weights_already_passed));
  204. break;
  205. case 3:
  206. glVertexAttrib3d(
  207. pass_W_index,
  208. W(F(i,j),0+weights_already_passed),
  209. W(F(i,j),1+weights_already_passed),
  210. W(F(i,j),2+weights_already_passed));
  211. glVertexAttrib3d(
  212. pass_WI_index,
  213. WI(F(i,j),0+weights_already_passed),
  214. WI(F(i,j),1+weights_already_passed),
  215. WI(F(i,j),2+weights_already_passed));
  216. break;
  217. default:
  218. glVertexAttrib4d(
  219. pass_W_index,
  220. W(F(i,j),0+weights_already_passed),
  221. W(F(i,j),1+weights_already_passed),
  222. W(F(i,j),2+weights_already_passed),
  223. W(F(i,j),3+weights_already_passed));
  224. glVertexAttrib4d(
  225. pass_WI_index,
  226. WI(F(i,j),0+weights_already_passed),
  227. WI(F(i,j),1+weights_already_passed),
  228. WI(F(i,j),2+weights_already_passed),
  229. WI(F(i,j),3+weights_already_passed));
  230. break;
  231. }
  232. weights_left -= pass_size;
  233. }
  234. }
  235. if(tc != -1)
  236. {
  237. glTexCoord2d(TC(tc,0),TC(tc,1));
  238. }
  239. if(rC>0)
  240. {
  241. switch(cC)
  242. {
  243. case 3:
  244. glColor3dv(color.data());
  245. break;
  246. case 4:
  247. glColor4dv(color.data());
  248. break;
  249. default:
  250. break;
  251. }
  252. }
  253. if(n != -1)
  254. {
  255. glNormal3d(N(n,0),N(n,1),N(n,2));
  256. }
  257. glVertex3d(V(F(i,j),0),V(F(i,j),1),V(F(i,j),2));
  258. }
  259. }
  260. }
  261. glEnd();
  262. }
  263. #ifdef IGL_STATIC_LIBRARY
  264. // Explicit template specialization
  265. #endif