example.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. #define IGL_HEADER_ONLY
  2. #include <igl/OpenGL_convenience.h>
  3. #include <igl/per_face_normals.h>
  4. #include <igl/read.h>
  5. #include <igl/normalize_row_lengths.h>
  6. #include <igl/draw_mesh.h>
  7. #include <igl/draw_floor.h>
  8. #include <igl/unproject.h>
  9. #include <igl/quat_to_mat.h>
  10. #include <igl/embree/EmbreeIntersector.h>
  11. #include <igl/trackball.h>
  12. #include <igl/report_gl_error.h>
  13. #ifdef __APPLE__
  14. # include <GLUT/glut.h>
  15. #else
  16. # include <GL/glut.h>
  17. #endif
  18. #include <Eigen/Core>
  19. #include <vector>
  20. #include <iostream>
  21. // Width and height of window
  22. int width,height;
  23. // Rotation of scene
  24. float scene_rot[4] = {0,0,0,1};
  25. // information at mouse down
  26. float down_scene_rot[4] = {0,0,0,1};
  27. bool trackball_on = false;
  28. int down_mouse_x,down_mouse_y;
  29. // Position of light
  30. float light_pos[4] = {0.1,0.1,-0.9,0};
  31. // Vertex positions, normals, colors and centroid
  32. Eigen::MatrixXd V,N,C,mean;
  33. // Bounding box diagonal length
  34. double bbd;
  35. // Faces
  36. Eigen::MatrixXi F;
  37. // Embree intersection structure
  38. igl::EmbreeIntersector<double,int> ei;
  39. // Hits collected
  40. std::vector<igl::Hit > hits;
  41. // Ray information, "projection screen" corners
  42. Eigen::Vector3d win_s,s,d,dir,NW,NE,SE,SW;
  43. // Textures and framebuffers for "projection screen"
  44. GLuint tex_id = 0, fbo_id = 0, dfbo_id = 0;
  45. // Initialize textures and framebuffers. Must be called if window changes
  46. // dimension
  47. void init_texture()
  48. {
  49. using namespace igl;
  50. using namespace std;
  51. // Set up a "render-to-texture" frame buffer and texture combo
  52. glDeleteTextures(1,&tex_id);
  53. glDeleteFramebuffersEXT(1,&fbo_id);
  54. glDeleteFramebuffersEXT(1,&dfbo_id);
  55. // http://www.opengl.org/wiki/Framebuffer_Object_Examples#Quick_example.2C_render_to_texture_.282D.29
  56. glGenTextures(1, &tex_id);
  57. glBindTexture(GL_TEXTURE_2D, tex_id);
  58. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  59. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  60. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  61. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  62. //NULL means reserve texture memory, but texels are undefined
  63. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
  64. glBindTexture(GL_TEXTURE_2D, 0);
  65. //-------------------------
  66. glGenFramebuffersEXT(1, &fbo_id);
  67. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo_id);
  68. //Attach 2D texture to this FBO
  69. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, tex_id, 0);
  70. glGenRenderbuffersEXT(1, &dfbo_id);
  71. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, dfbo_id);
  72. glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, width, height);
  73. //-------------------------
  74. //Attach depth buffer to FBO
  75. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, dfbo_id);
  76. //-------------------------
  77. //Does the GPU support current FBO configuration?
  78. GLenum status;
  79. status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
  80. switch(status)
  81. {
  82. case GL_FRAMEBUFFER_COMPLETE_EXT:
  83. break;
  84. default:
  85. cout<<"error"<<endl;
  86. }
  87. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
  88. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  89. }
  90. void reshape(int width,int height)
  91. {
  92. using namespace std;
  93. // Save width and height
  94. ::width = width;
  95. ::height = height;
  96. // Re-initialize textures and frame bufferes
  97. init_texture();
  98. glMatrixMode(GL_PROJECTION);
  99. glLoadIdentity();
  100. glViewport(0,0,width,height);
  101. }
  102. // Set up double-sided lights
  103. void lights()
  104. {
  105. using namespace std;
  106. glEnable(GL_LIGHTING);
  107. glLightModelf(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
  108. glEnable(GL_LIGHT0);
  109. glEnable(GL_LIGHT1);
  110. float ones[4] = {1.0,1.0,1.0,1.0};
  111. float zeros[4] = {0.0,0.0,0.0,0.0};
  112. float pos[4];
  113. copy(light_pos,light_pos+4,pos);
  114. glLightfv(GL_LIGHT0,GL_AMBIENT,zeros);
  115. glLightfv(GL_LIGHT0,GL_DIFFUSE,ones);
  116. glLightfv(GL_LIGHT0,GL_SPECULAR,zeros);
  117. glLightfv(GL_LIGHT0,GL_POSITION,pos);
  118. pos[0] *= -1;
  119. pos[1] *= -1;
  120. pos[2] *= -1;
  121. glLightfv(GL_LIGHT1,GL_AMBIENT,zeros);
  122. glLightfv(GL_LIGHT1,GL_DIFFUSE,ones);
  123. glLightfv(GL_LIGHT1,GL_SPECULAR,zeros);
  124. glLightfv(GL_LIGHT1,GL_POSITION,pos);
  125. }
  126. // Set up projection and model view of scene
  127. void push_scene()
  128. {
  129. using namespace igl;
  130. glMatrixMode(GL_PROJECTION);
  131. glLoadIdentity();
  132. gluPerspective(45,(double)width/(double)height,1e-2,100);
  133. glMatrixMode(GL_MODELVIEW);
  134. glLoadIdentity();
  135. gluLookAt(0,0,3,0,0,0,0,1,0);
  136. glPushMatrix();
  137. float mat[4*4];
  138. quat_to_mat(scene_rot,mat);
  139. glMultMatrixf(mat);
  140. }
  141. void pop_scene()
  142. {
  143. glPopMatrix();
  144. }
  145. // Scale and shift for object
  146. void push_object()
  147. {
  148. glPushMatrix();
  149. glScaled(2./bbd,2./bbd,2./bbd);
  150. glTranslated(-mean(0,0),-mean(0,1),-mean(0,2));
  151. }
  152. void pop_object()
  153. {
  154. glPopMatrix();
  155. }
  156. const float back[4] = {190.0/255.0,190.0/255.0,190.0/255.0,0};
  157. void display()
  158. {
  159. using namespace Eigen;
  160. using namespace igl;
  161. using namespace std;
  162. glClearColor(back[0],back[1],back[2],0);
  163. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  164. // All smooth points
  165. glEnable( GL_POINT_SMOOTH );
  166. lights();
  167. push_scene();
  168. glEnable(GL_DEPTH_TEST);
  169. glDepthFunc(GL_LEQUAL);
  170. glEnable(GL_NORMALIZE);
  171. glEnable(GL_COLOR_MATERIAL);
  172. glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
  173. push_object();
  174. if(trackball_on)
  175. {
  176. // Draw a "laser" line
  177. glLineWidth(3.0);
  178. glDisable(GL_LIGHTING);
  179. glEnable(GL_DEPTH_TEST);
  180. glBegin(GL_LINES);
  181. glColor3f(1,0,0);
  182. glVertex3dv(s.data());
  183. glColor3f(1,0,0);
  184. glVertex3dv(d.data());
  185. glEnd();
  186. // Draw the start and end points used for ray
  187. glPointSize(10.0);
  188. glBegin(GL_POINTS);
  189. glColor3f(1,0,0);
  190. glVertex3dv(s.data());
  191. glColor3f(0,0,1);
  192. glVertex3dv(d.data());
  193. glEnd();
  194. }
  195. // Draw the model
  196. glEnable(GL_LIGHTING);
  197. draw_mesh(V,F,N,C);
  198. // Draw all hits
  199. glBegin(GL_POINTS);
  200. glColor3f(0,0.2,0.2);
  201. for(vector<igl::Hit>::iterator hit = hits.begin();
  202. hit != hits.end();
  203. hit++)
  204. {
  205. const double w0 = (1.0-hit->u-hit->v);
  206. const double w1 = hit->u;
  207. const double w2 = hit->v;
  208. VectorXd hitP =
  209. w0 * V.row(F(hit->id,0)) +
  210. w1 * V.row(F(hit->id,1)) +
  211. w2 * V.row(F(hit->id,2));
  212. glVertex3dv(hitP.data());
  213. }
  214. glEnd();
  215. pop_object();
  216. // Draw a nice floor
  217. glPushMatrix();
  218. glEnable(GL_LIGHTING);
  219. glTranslated(0,-1,0);
  220. draw_floor();
  221. glPopMatrix();
  222. // draw a transparent "projection screen" show model at time of hit (aka
  223. // mouse down)
  224. push_object();
  225. if(trackball_on)
  226. {
  227. glColor4f(0,0,0,1.0);
  228. glPointSize(10.0);
  229. glBegin(GL_POINTS);
  230. glVertex3dv(SW.data());
  231. glVertex3dv(SE.data());
  232. glVertex3dv(NE.data());
  233. glVertex3dv(NW.data());
  234. glEnd();
  235. glDisable(GL_LIGHTING);
  236. glEnable(GL_TEXTURE_2D);
  237. glBindTexture(GL_TEXTURE_2D, tex_id);
  238. glColor4f(1,1,1,0.7);
  239. glBegin(GL_QUADS);
  240. glTexCoord2d(0,0);
  241. glVertex3dv(SW.data());
  242. glTexCoord2d(1,0);
  243. glVertex3dv(SE.data());
  244. glTexCoord2d(1,1);
  245. glVertex3dv(NE.data());
  246. glTexCoord2d(0,1);
  247. glVertex3dv(NW.data());
  248. glEnd();
  249. glBindTexture(GL_TEXTURE_2D, 0);
  250. glDisable(GL_TEXTURE_2D);
  251. }
  252. pop_object();
  253. pop_scene();
  254. // Draw a faint point over mouse
  255. if(!trackball_on)
  256. {
  257. glDisable(GL_LIGHTING);
  258. glDisable(GL_COLOR_MATERIAL);
  259. glDisable(GL_DEPTH_TEST);
  260. glEnable(GL_BLEND);
  261. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  262. glColor4f(1.0,0.3,0.3,0.6);
  263. glMatrixMode(GL_PROJECTION);
  264. glLoadIdentity();
  265. gluOrtho2D(0,width,0,height);
  266. glMatrixMode(GL_MODELVIEW);
  267. glLoadIdentity();
  268. glPointSize(20.0);
  269. glBegin(GL_POINTS);
  270. glVertex2dv(win_s.data());
  271. glEnd();
  272. }
  273. report_gl_error();
  274. glutSwapBuffers();
  275. glutPostRedisplay();
  276. }
  277. // Initialize colors to a boring green
  278. void init_C()
  279. {
  280. C.col(0).setConstant(0.4);
  281. C.col(1).setConstant(0.8);
  282. C.col(2).setConstant(0.3);
  283. }
  284. void mouse_move(int mouse_x, int mouse_y)
  285. {
  286. using namespace std;
  287. using namespace Eigen;
  288. using namespace igl;
  289. using namespace std;
  290. init_C();
  291. glutSetCursor(GLUT_CURSOR_CROSSHAIR);
  292. // Push scene and object
  293. push_scene();
  294. push_object();
  295. // Unproject mouse at 0 depth and some positive depth
  296. win_s = Vector3d(mouse_x,height-mouse_y,0);
  297. Vector3d win_d(mouse_x,height-mouse_y,1);
  298. unproject(win_s,s);
  299. unproject(win_d,d);
  300. pop_object();
  301. pop_scene();
  302. report_gl_error();
  303. // Shoot ray at unprojected mouse in view direction
  304. dir = d-s;
  305. int num_rays_shot;
  306. ei.intersectRay(s,dir,hits,num_rays_shot);
  307. for(vector<igl::Hit>::iterator hit = hits.begin();
  308. hit != hits.end();
  309. hit++)
  310. {
  311. // Change color of hit faces
  312. C(hit->id,0) = 1;
  313. C(hit->id,1) = 0.4;
  314. C(hit->id,2) = 0.4;
  315. }
  316. }
  317. void mouse(int glutButton, int glutState, int mouse_x, int mouse_y)
  318. {
  319. using namespace std;
  320. using namespace Eigen;
  321. using namespace igl;
  322. switch(glutState)
  323. {
  324. case 1:
  325. // up
  326. glutSetCursor(GLUT_CURSOR_CROSSHAIR);
  327. trackball_on = false;
  328. hits.clear();
  329. init_C();
  330. break;
  331. case 0:
  332. // be sure this has been called recently
  333. mouse_move(mouse_x,mouse_y);
  334. // down
  335. glutSetCursor(GLUT_CURSOR_CYCLE);
  336. // collect information for trackball
  337. trackball_on = true;
  338. copy(scene_rot,scene_rot+4,down_scene_rot);
  339. down_mouse_x = mouse_x;
  340. down_mouse_y = mouse_y;
  341. // Collect "projection screen" locations
  342. push_scene();
  343. push_object();
  344. // unproject corners of window
  345. const double depth = 0.999;
  346. Vector3d win_NW( 0,height,depth);
  347. Vector3d win_NE(width,height,depth);
  348. Vector3d win_SE(width,0,depth);
  349. Vector3d win_SW(0,0,depth);
  350. unproject(win_NW,NW);
  351. unproject(win_NE,NE);
  352. unproject(win_SE,SE);
  353. unproject(win_SW,SW);
  354. // render to framebuffer
  355. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo_id);
  356. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, dfbo_id);
  357. glClearColor(0,0,0,1);
  358. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  359. // Render the model ---> to the framebuffer attached to the "projection
  360. // screen" texture
  361. glEnable(GL_COLOR_MATERIAL);
  362. glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
  363. glEnable(GL_LIGHTING);
  364. glEnable(GL_DEPTH_TEST);
  365. draw_mesh(V,F,N,C);
  366. pop_object();
  367. pop_scene();
  368. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  369. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
  370. break;
  371. }
  372. }
  373. void mouse_drag(int mouse_x, int mouse_y)
  374. {
  375. using namespace igl;
  376. if(trackball_on)
  377. {
  378. // Rotate according to trackball
  379. trackball<float>(
  380. width,
  381. height,
  382. 2,
  383. down_scene_rot,
  384. down_mouse_x,
  385. down_mouse_y,
  386. mouse_x,
  387. mouse_y,
  388. scene_rot);
  389. }
  390. }
  391. void key(unsigned char key, int mouse_x, int mouse_y)
  392. {
  393. using namespace std;
  394. switch(key)
  395. {
  396. // Ctrl-c and esc exit
  397. case char(3):
  398. case char(27):
  399. exit(0);
  400. default:
  401. cout<<"Unknown key command: "<<key<<" "<<int(key)<<endl;
  402. }
  403. }
  404. int main(int argc, char * argv[])
  405. {
  406. using namespace Eigen;
  407. using namespace igl;
  408. using namespace std;
  409. // init mesh
  410. string filename = "../shared/decimated-knight.obj";
  411. if(argc < 2)
  412. {
  413. cerr<<"Usage:"<<endl<<" ./example input.obj"<<endl;
  414. cout<<endl<<"Opening default mesh..."<<endl;
  415. }else
  416. {
  417. // Read and prepare mesh
  418. filename = argv[1];
  419. }
  420. if(!read(filename,V,F))
  421. {
  422. return 1;
  423. }
  424. // Compute normals, centroid, colors, bounding box diagonal
  425. per_face_normals(V,F,N);
  426. normalize_row_lengths(N,N);
  427. mean = V.colwise().mean();
  428. C.resize(F.rows(),3);
  429. init_C();
  430. bbd =
  431. (V.colwise().maxCoeff() -
  432. V.colwise().minCoeff()).maxCoeff();
  433. // Init embree
  434. ei = EmbreeIntersector<double,int>(V,F);
  435. // Init glut
  436. glutInit(&argc,argv);
  437. glutInitDisplayString( "rgba depth double samples>=8 ");
  438. glutInitWindowSize(glutGet(GLUT_SCREEN_WIDTH)/2.0,glutGet(GLUT_SCREEN_HEIGHT));
  439. glutCreateWindow("embree");
  440. glutDisplayFunc(display);
  441. glutReshapeFunc(reshape);
  442. glutKeyboardFunc(key);
  443. glutMouseFunc(mouse);
  444. glutMotionFunc(mouse_drag);
  445. glutPassiveMotionFunc(mouse_move);
  446. glutMainLoop();
  447. return 0;
  448. }