draw_mesh.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. }