example.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #include <igl/OpenGL_convenience.h>
  2. #include <igl/per_face_normals.h>
  3. #include <igl/read.h>
  4. #include <igl/normalize_row_lengths.h>
  5. #include <igl/draw_mesh.h>
  6. #include <igl/draw_floor.h>
  7. #include <igl/unproject.h>
  8. #include <igl/quat_to_mat.h>
  9. #include <igl/embree/EmbreeIntersector.h>
  10. #ifdef __APPLE__
  11. # include <GLUT/glut.h>
  12. #else
  13. # include <GL/glut.h>
  14. #endif
  15. #include <Eigen/Core>
  16. #include <iostream>
  17. int width,height;
  18. float scene_rot[4] = {0,0,0,1};
  19. float light_pos[4] = {0.1,0.1,-0.9,0};
  20. Eigen::MatrixXd V,N,C,mean;
  21. double bbd;
  22. Eigen::MatrixXi F;
  23. igl::EmbreeIntersector<Eigen::MatrixXd,Eigen::MatrixXi,Eigen::Vector3d> ei;
  24. // Ray
  25. Eigen::Vector3d s,d,dir;
  26. void reshape(int width,int height)
  27. {
  28. using namespace std;
  29. ::width = width;
  30. ::height = height;
  31. glMatrixMode(GL_PROJECTION);
  32. glLoadIdentity();
  33. glViewport(0,0,width,height);
  34. }
  35. void lights()
  36. {
  37. using namespace std;
  38. glEnable(GL_LIGHTING);
  39. glLightModelf(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
  40. glEnable(GL_LIGHT0);
  41. glEnable(GL_LIGHT1);
  42. float ones[4] = {1.0,1.0,1.0,1.0};
  43. float zeros[4] = {0.0,0.0,0.0,0.0};
  44. float pos[4];
  45. copy(light_pos,light_pos+4,pos);
  46. glLightfv(GL_LIGHT0,GL_AMBIENT,zeros);
  47. glLightfv(GL_LIGHT0,GL_DIFFUSE,ones);
  48. glLightfv(GL_LIGHT0,GL_SPECULAR,zeros);
  49. glLightfv(GL_LIGHT0,GL_POSITION,pos);
  50. pos[0] *= -1;
  51. pos[1] *= -1;
  52. pos[2] *= -1;
  53. glLightfv(GL_LIGHT1,GL_AMBIENT,zeros);
  54. glLightfv(GL_LIGHT1,GL_DIFFUSE,ones);
  55. glLightfv(GL_LIGHT1,GL_SPECULAR,zeros);
  56. glLightfv(GL_LIGHT1,GL_POSITION,pos);
  57. }
  58. void push_scene()
  59. {
  60. using namespace igl;
  61. //gluOrtho2D(0,width,0,height);
  62. gluPerspective(45,(double)width/(double)height,1e-2,100);
  63. glMatrixMode(GL_MODELVIEW);
  64. glLoadIdentity();
  65. gluLookAt(0,0,3,0,0,0,0,1,0);
  66. glPushMatrix();
  67. float mat[4*4];
  68. quat_to_mat(scene_rot,mat);
  69. glMultMatrixf(mat);
  70. }
  71. void push_object()
  72. {
  73. glPushMatrix();
  74. glScaled(2./bbd,2./bbd,2./bbd);
  75. glTranslated(-mean(0,0),-mean(0,1),-mean(0,2));
  76. }
  77. void pop_scene()
  78. {
  79. glPopMatrix();
  80. }
  81. void pop_object()
  82. {
  83. glPopMatrix();
  84. }
  85. const float back[4] = {190.0/255.0,190.0/255.0,190.0/255.0,0};
  86. void display()
  87. {
  88. using namespace Eigen;
  89. using namespace igl;
  90. glClearColor(back[0],back[1],back[2],0);
  91. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  92. lights();
  93. push_scene();
  94. glEnable(GL_DEPTH_TEST);
  95. glEnable(GL_NORMALIZE);
  96. glEnable(GL_COLOR_MATERIAL);
  97. glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
  98. //glColorMaterial(GL_FRONT, GL_DIFFUSE);
  99. //glColorMaterial(GL_FRONT, GL_AMBIENT);
  100. //glColorMaterial(GL_FRONT, GL_SPECULAR);
  101. push_object();
  102. draw_mesh(V,F,N,C);
  103. glDisable(GL_COLOR_MATERIAL);
  104. glDisable(GL_LIGHTING);
  105. glBegin(GL_POINTS);
  106. glColor3f(1,0,0);
  107. glVertex3dv(s.data());
  108. glColor3f(0,0,1);
  109. glVertex3dv(d.data());
  110. glEnd();
  111. Vector3d n,f;
  112. n = s+1000.0*dir;
  113. f = d-1000.0*dir;
  114. glBegin(GL_LINE);
  115. glColor3f(1,0,0);
  116. glVertex3dv(n.data());
  117. glColor3f(1,0,0);
  118. glVertex3dv(f.data());
  119. glEnd();
  120. pop_object();
  121. glPushMatrix();
  122. glEnable(GL_LIGHTING);
  123. glTranslated(0,-1,0);
  124. draw_floor();
  125. glPopMatrix();
  126. pop_scene();
  127. glutSwapBuffers();
  128. glutPostRedisplay();
  129. }
  130. void init_C()
  131. {
  132. C.col(0).setConstant(0.4);
  133. C.col(1).setConstant(0.8);
  134. C.col(2).setConstant(0.3);
  135. }
  136. void mouse_move(int mouse_x, int mouse_y)
  137. {
  138. using namespace std;
  139. using namespace Eigen;
  140. using namespace igl;
  141. init_C();
  142. push_scene();
  143. push_object();
  144. Vector3d win_s(mouse_x,height-mouse_y,0);
  145. Vector3d win_d(mouse_x,height-mouse_y,1);
  146. unproject(win_s,s);
  147. unproject(win_d,d);
  148. dir = d-s;
  149. embree::Hit hit;
  150. if(ei.intersectRay(s,d,hit))
  151. {
  152. cout<<"hit!"<<endl;
  153. C(hit.id0 % F.rows(),0) = 1;
  154. C(hit.id0 % F.rows(),1) = 0.4;
  155. C(hit.id0 % F.rows(),2) = 0.4;
  156. }
  157. pop_object();
  158. pop_scene();
  159. }
  160. void key(unsigned char key, int mouse_x, int mouse_y)
  161. {
  162. using namespace std;
  163. switch(key)
  164. {
  165. case char(27):
  166. exit(0);
  167. default:
  168. cout<<"Unknown key command: "<<key<<" "<<int(key)<<endl;
  169. }
  170. }
  171. int main(int argc, char * argv[])
  172. {
  173. using namespace Eigen;
  174. using namespace igl;
  175. using namespace std;
  176. // init mesh
  177. if(!read("../shared/cheburashka.obj",V,F))
  178. {
  179. return 1;
  180. }
  181. per_face_normals(V,F,N);
  182. mean = V.colwise().mean();
  183. C.resize(F.rows(),3);
  184. init_C();
  185. bbd =
  186. (V.colwise().maxCoeff() -
  187. V.colwise().minCoeff()).maxCoeff();
  188. normalize_row_lengths(N,N);
  189. // Init embree
  190. cout<<"Flipping faces..."<<endl;
  191. MatrixXi FF;
  192. FF.resize(F.rows()*2,F.cols());
  193. FF << F, F.rowwise().reverse().eval();
  194. cout<<"Initializing Embree..."<<endl;
  195. ei = EmbreeIntersector<MatrixXd,MatrixXi,Vector3d>(V,FF);
  196. // Init glut
  197. glutInit(&argc,argv);
  198. glutInitDisplayString( "rgba depth double samples>=8 ");
  199. glutInitWindowSize(glutGet(GLUT_SCREEN_WIDTH),glutGet(GLUT_SCREEN_HEIGHT));
  200. glutInitWindowSize(450,300);
  201. glutCreateWindow("embree");
  202. glutDisplayFunc(display);
  203. glutReshapeFunc(reshape);
  204. glutKeyboardFunc(key);
  205. glutPassiveMotionFunc(mouse_move);
  206. glutMainLoop();
  207. return 0;
  208. }