example.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. #include <igl/Camera.h>
  2. #include <igl/opengl/OpenGL_convenience.h>
  3. #include <igl/STR.h>
  4. #include <igl/barycenter.h>
  5. #include <igl/opengl2/draw_floor.h>
  6. #include <igl/opengl2/draw_mesh.h>
  7. #include <igl/get_seconds.h>
  8. #include <igl/jet.h>
  9. #include <igl/list_to_matrix.h>
  10. #include <igl/material_colors.h>
  11. #include <igl/matlab_format.h>
  12. #include <igl/normalize_row_lengths.h>
  13. #include <igl/pathinfo.h>
  14. #include <igl/per_face_normals.h>
  15. #include <igl/per_vertex_normals.h>
  16. #include <igl/polygon_mesh_to_triangle_mesh.h>
  17. #include <igl/quat_to_mat.h>
  18. #include <igl/readDMAT.h>
  19. #include <igl/readMESH.h>
  20. #include <igl/readOBJ.h>
  21. #include <igl/readOFF.h>
  22. #include <igl/readWRL.h>
  23. #include <igl/opengl/report_gl_error.h>
  24. #include <igl/snap_to_canonical_view_quat.h>
  25. #include <igl/snap_to_fixed_up.h>
  26. #include <igl/trackball.h>
  27. #include <igl/two_axis_valuator_fixed_up.h>
  28. #include <igl/anttweakbar/ReAntTweakBar.h>
  29. #include <igl/png/texture_from_file.h>
  30. #include <YImage.hpp>
  31. #ifdef __APPLE__
  32. # include <GLUT/glut.h>
  33. #else
  34. # include <GL/glut.h>
  35. #endif
  36. #include <Eigen/Core>
  37. #include <vector>
  38. #include <iostream>
  39. #include <algorithm>
  40. struct State
  41. {
  42. igl::Camera camera;
  43. } s;
  44. enum RotationType
  45. {
  46. ROTATION_TYPE_IGL_TRACKBALL = 0,
  47. ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP = 1,
  48. NUM_ROTATION_TYPES = 2,
  49. } rotation_type;
  50. bool is_rotating = false;
  51. int down_x,down_y;
  52. igl::Camera down_camera;
  53. bool is_animating = false;
  54. double animation_start_time = 0;
  55. double ANIMATION_DURATION = 0.5;
  56. Eigen::Quaterniond animation_from_quat;
  57. Eigen::Quaterniond animation_to_quat;
  58. // Use vector for range-based `for`
  59. std::vector<State> undo_stack;
  60. std::vector<State> redo_stack;
  61. void push_undo()
  62. {
  63. undo_stack.push_back(s);
  64. // Clear
  65. redo_stack = std::vector<State>();
  66. }
  67. void undo()
  68. {
  69. using namespace std;
  70. if(!undo_stack.empty())
  71. {
  72. redo_stack.push_back(s);
  73. s = undo_stack.front();
  74. undo_stack.pop_back();
  75. }
  76. }
  77. void redo()
  78. {
  79. using namespace std;
  80. if(!redo_stack.empty())
  81. {
  82. undo_stack.push_back(s);
  83. s = redo_stack.front();
  84. redo_stack.pop_back();
  85. }
  86. }
  87. void TW_CALL set_rotation_type(const void * value, void * clientData)
  88. {
  89. using namespace Eigen;
  90. using namespace std;
  91. using namespace igl;
  92. const RotationType old_rotation_type = rotation_type;
  93. rotation_type = *(const RotationType *)(value);
  94. if(rotation_type == ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP &&
  95. old_rotation_type != ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP)
  96. {
  97. push_undo();
  98. animation_from_quat = s.camera.m_rotation_conj;
  99. snap_to_fixed_up(animation_from_quat,animation_to_quat);
  100. // start animation
  101. animation_start_time = get_seconds();
  102. is_animating = true;
  103. }
  104. }
  105. void TW_CALL get_rotation_type(void * value, void *clientData)
  106. {
  107. RotationType * rt = (RotationType *)(value);
  108. *rt = rotation_type;
  109. }
  110. // Width and height of window
  111. int width,height;
  112. // Position of light
  113. float light_pos[4] = {0.1,0.1,-0.9,0};
  114. // Vertex positions, normals, colors and centroid
  115. Eigen::MatrixXd V,N,TC,mid;
  116. Eigen::MatrixXi F,TF;
  117. GLuint tex_id = 0;
  118. int selected_col = 0;
  119. // Faces
  120. // Bounding box diagonal length
  121. double bbd;
  122. // Running ambient occlusion
  123. Eigen::VectorXd S;
  124. int tot_num_samples = 0;
  125. #define REBAR_NAME "temp.rbr"
  126. igl::anttweakbar::ReTwBar rebar; // Pointer to the tweak bar
  127. bool flip_y = false;
  128. bool rotate_xy = false;
  129. void reshape(int width,int height)
  130. {
  131. using namespace std;
  132. // Save width and height
  133. ::width = width;
  134. ::height = height;
  135. glMatrixMode(GL_PROJECTION);
  136. glLoadIdentity();
  137. glViewport(0,0,width,height);
  138. // Send the new window size to AntTweakBar
  139. TwWindowSize(width, height);
  140. // Set aspect for all cameras
  141. s.camera.m_aspect = (double)width/(double)height;
  142. for(auto & s : undo_stack)
  143. {
  144. s.camera.m_aspect = (double)width/(double)height;
  145. }
  146. for(auto & s : redo_stack)
  147. {
  148. s.camera.m_aspect = (double)width/(double)height;
  149. }
  150. }
  151. void push_scene()
  152. {
  153. using namespace igl;
  154. using namespace std;
  155. glMatrixMode(GL_PROJECTION);
  156. glPushMatrix();
  157. glLoadIdentity();
  158. auto & camera = s.camera;
  159. gluPerspective(camera.m_angle,camera.m_aspect,camera.m_near,camera.m_far);
  160. glMatrixMode(GL_MODELVIEW);
  161. glPushMatrix();
  162. glLoadIdentity();
  163. gluLookAt(
  164. camera.eye()(0), camera.eye()(1), camera.eye()(2),
  165. camera.at()(0), camera.at()(1), camera.at()(2),
  166. camera.up()(0), camera.up()(1), camera.up()(2));
  167. }
  168. void pop_scene()
  169. {
  170. glMatrixMode(GL_PROJECTION);
  171. glPopMatrix();
  172. glMatrixMode(GL_MODELVIEW);
  173. glPopMatrix();
  174. }
  175. void pop_object()
  176. {
  177. glPopMatrix();
  178. }
  179. // Scale and shift for object
  180. void push_object()
  181. {
  182. glPushMatrix();
  183. glScaled(2./bbd,2./bbd,2./bbd);
  184. glTranslated(-mid(0,0),-mid(0,1),-mid(0,2));
  185. }
  186. // Set up double-sided lights
  187. void lights()
  188. {
  189. using namespace std;
  190. glEnable(GL_LIGHTING);
  191. glLightModelf(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
  192. glEnable(GL_LIGHT0);
  193. glEnable(GL_LIGHT1);
  194. float amb[4];
  195. amb[0] = amb[1] = amb[2] = 0;
  196. amb[3] = 1.0;
  197. float diff[4] = {0.0,0.0,0.0,0.0};
  198. diff[0] = diff[1] = diff[2] = (1.0 - 0/0.4);;
  199. diff[3] = 1.0;
  200. float zeros[4] = {0.0,0.0,0.0,0.0};
  201. float pos[4];
  202. copy(light_pos,light_pos+4,pos);
  203. glLightfv(GL_LIGHT0,GL_AMBIENT,amb);
  204. glLightfv(GL_LIGHT0,GL_DIFFUSE,diff);
  205. glLightfv(GL_LIGHT0,GL_SPECULAR,zeros);
  206. glLightfv(GL_LIGHT0,GL_POSITION,pos);
  207. pos[0] *= -1;
  208. pos[1] *= -1;
  209. pos[2] *= -1;
  210. glLightfv(GL_LIGHT1,GL_AMBIENT,amb);
  211. glLightfv(GL_LIGHT1,GL_DIFFUSE,diff);
  212. glLightfv(GL_LIGHT1,GL_SPECULAR,zeros);
  213. glLightfv(GL_LIGHT1,GL_POSITION,pos);
  214. }
  215. const float back[4] = {30.0/255.0,30.0/255.0,50.0/255.0,0};
  216. void display()
  217. {
  218. using namespace Eigen;
  219. using namespace igl;
  220. using namespace std;
  221. glClearColor(back[0],back[1],back[2],0);
  222. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  223. if(is_animating)
  224. {
  225. double t = (get_seconds() - animation_start_time)/ANIMATION_DURATION;
  226. if(t > 1)
  227. {
  228. t = 1;
  229. is_animating = false;
  230. }
  231. const Quaterniond q = animation_from_quat.slerp(t,animation_to_quat).normalized();
  232. s.camera.orbit(q.conjugate());
  233. }
  234. glDisable(GL_LIGHTING);
  235. lights();
  236. push_scene();
  237. glEnable(GL_DEPTH_TEST);
  238. glDepthFunc(GL_LESS);
  239. glEnable(GL_NORMALIZE);
  240. glEnable(GL_COLOR_MATERIAL);
  241. glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
  242. push_object();
  243. // Draw the model
  244. // Set material properties
  245. glEnable(GL_COLOR_MATERIAL);
  246. glColor3f(1,1,1);
  247. glEnable(GL_TEXTURE_2D);
  248. glBindTexture(GL_TEXTURE_2D,tex_id);
  249. MatrixXd _d;
  250. MatrixXi _i;
  251. glMatrixMode(GL_TEXTURE);
  252. glPushMatrix();
  253. glLoadIdentity();
  254. if(flip_y)
  255. {
  256. glTranslated(0,1,0);
  257. glScaled(1,-1,1);
  258. }
  259. if(rotate_xy)
  260. {
  261. glRotated(90,0,0,1);
  262. glTranslated(-1,0,0);
  263. }
  264. glMatrixMode(GL_MODELVIEW);
  265. igl::opengl2::draw_mesh(V,F,N,MatrixXi(),MatrixXd(),TC,TF,MatrixXd(),0,MatrixXi(),0);
  266. glMatrixMode(GL_TEXTURE);
  267. glPopMatrix();
  268. glMatrixMode(GL_MODELVIEW);
  269. pop_object();
  270. // Draw a nice floor
  271. glPushMatrix();
  272. const double floor_offset =
  273. -2./bbd*(V.col(1).maxCoeff()-mid(1));
  274. glTranslated(0,floor_offset,0);
  275. const float GREY[4] = {0.5,0.5,0.6,1.0};
  276. const float DARK_GREY[4] = {0.2,0.2,0.3,1.0};
  277. glEnable(GL_POLYGON_OFFSET_FILL);
  278. glPolygonOffset(-1,1);
  279. glBegin(GL_QUADS);
  280. glNormal3d(0,1,0);
  281. glTexCoord2d(0,1);
  282. glVertex3d(-1,0,1);
  283. glTexCoord2d(1,1);
  284. glVertex3d(1,0,1);
  285. glTexCoord2d(1,0);
  286. glVertex3d(1,0,-1);
  287. glTexCoord2d(0,0);
  288. glVertex3d(-1,0,-1);
  289. glEnd();
  290. glDisable(GL_POLYGON_OFFSET_FILL);
  291. glDisable(GL_TEXTURE_2D);
  292. igl::opengl2::draw_floor(GREY,DARK_GREY);
  293. glPopMatrix();
  294. pop_scene();
  295. igl::opengl::report_gl_error();
  296. TwDraw();
  297. glutSwapBuffers();
  298. if(is_animating)
  299. {
  300. glutPostRedisplay();
  301. }
  302. }
  303. void mouse_wheel(int wheel, int direction, int mouse_x, int mouse_y)
  304. {
  305. using namespace std;
  306. using namespace igl;
  307. using namespace Eigen;
  308. GLint viewport[4];
  309. glGetIntegerv(GL_VIEWPORT,viewport);
  310. if(wheel == 0 && TwMouseMotion(mouse_x, viewport[3] - mouse_y))
  311. {
  312. static double mouse_scroll_y = 0;
  313. const double delta_y = 0.125*direction;
  314. mouse_scroll_y += delta_y;
  315. TwMouseWheel(mouse_scroll_y);
  316. return;
  317. }
  318. push_undo();
  319. auto & camera = s.camera;
  320. if(wheel==0)
  321. {
  322. // factor of zoom change
  323. double s = (1.-0.01*direction);
  324. //// FOV zoom: just widen angle. This is hardly ever appropriate.
  325. //camera.m_angle *= s;
  326. //camera.m_angle = min(max(camera.m_angle,1),89);
  327. camera.push_away(s);
  328. }else
  329. {
  330. // Dolly zoom:
  331. camera.dolly_zoom((double)direction*1.0);
  332. }
  333. }
  334. void mouse(int glutButton, int glutState, int mouse_x, int mouse_y)
  335. {
  336. using namespace std;
  337. using namespace Eigen;
  338. using namespace igl;
  339. bool tw_using = TwEventMouseButtonGLUT(glutButton,glutState,mouse_x,mouse_y);
  340. switch(glutButton)
  341. {
  342. case GLUT_RIGHT_BUTTON:
  343. case GLUT_LEFT_BUTTON:
  344. {
  345. switch(glutState)
  346. {
  347. case 1:
  348. // up
  349. glutSetCursor(GLUT_CURSOR_LEFT_ARROW);
  350. is_rotating = false;
  351. break;
  352. case 0:
  353. // down
  354. if(!tw_using)
  355. {
  356. glutSetCursor(GLUT_CURSOR_CYCLE);
  357. // collect information for trackball
  358. is_rotating = true;
  359. down_camera = s.camera;
  360. down_x = mouse_x;
  361. down_y = mouse_y;
  362. }
  363. break;
  364. }
  365. break;
  366. }
  367. // Scroll down
  368. case 3:
  369. {
  370. mouse_wheel(0,-1,mouse_x,mouse_y);
  371. break;
  372. }
  373. // Scroll up
  374. case 4:
  375. {
  376. mouse_wheel(0,1,mouse_x,mouse_y);
  377. break;
  378. }
  379. // Scroll left
  380. case 5:
  381. {
  382. mouse_wheel(1,-1,mouse_x,mouse_y);
  383. break;
  384. }
  385. // Scroll right
  386. case 6:
  387. {
  388. mouse_wheel(1,1,mouse_x,mouse_y);
  389. break;
  390. }
  391. }
  392. glutPostRedisplay();
  393. }
  394. void mouse_drag(int mouse_x, int mouse_y)
  395. {
  396. using namespace igl;
  397. using namespace Eigen;
  398. if(is_rotating)
  399. {
  400. glutSetCursor(GLUT_CURSOR_CYCLE);
  401. Quaterniond q;
  402. auto & camera = s.camera;
  403. switch(rotation_type)
  404. {
  405. case ROTATION_TYPE_IGL_TRACKBALL:
  406. {
  407. // Rotate according to trackball
  408. igl::trackball<double>(
  409. width,
  410. height,
  411. 2.0,
  412. down_camera.m_rotation_conj.coeffs().data(),
  413. down_x,
  414. down_y,
  415. mouse_x,
  416. mouse_y,
  417. q.coeffs().data());
  418. break;
  419. }
  420. case ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP:
  421. {
  422. // Rotate according to two axis valuator with fixed up vector
  423. two_axis_valuator_fixed_up(
  424. width, height,
  425. 2.0,
  426. down_camera.m_rotation_conj,
  427. down_x, down_y, mouse_x, mouse_y,
  428. q);
  429. break;
  430. }
  431. default:
  432. break;
  433. }
  434. camera.orbit(q.conjugate());
  435. }else
  436. {
  437. TwEventMouseMotionGLUT(mouse_x, mouse_y);
  438. }
  439. glutPostRedisplay();
  440. }
  441. void key(unsigned char key, int mouse_x, int mouse_y)
  442. {
  443. using namespace std;
  444. switch(key)
  445. {
  446. // ESC
  447. case char(27):
  448. rebar.save(REBAR_NAME);
  449. // ^C
  450. case char(3):
  451. exit(0);
  452. default:
  453. if(!TwEventKeyboardGLUT(key,mouse_x,mouse_y))
  454. {
  455. cout<<"Unknown key command: "<<key<<" "<<int(key)<<endl;
  456. }
  457. }
  458. glutPostRedisplay();
  459. }
  460. int main(int argc, char * argv[])
  461. {
  462. using namespace Eigen;
  463. using namespace igl;
  464. using namespace std;
  465. // init mesh
  466. string filename = "../shared/animal.obj";
  467. string tfilename = "../shared/animal.png";
  468. if(argc < 3)
  469. {
  470. cerr<<"Usage:"<<endl<<" ./example input.obj texture.png"<<endl;
  471. cout<<endl<<"Opening default mesh..."<<endl;
  472. }else
  473. {
  474. // Read and prepare mesh
  475. filename = argv[1];
  476. tfilename = argv[2];
  477. }
  478. vector<vector<double > > vV,vN,vTC;
  479. vector<vector<int > > vF,vTF,vFN;
  480. // Convert extension to lower case
  481. if(!igl::readOBJ(filename,vV,vTC,vN,vF,vTF,vFN))
  482. {
  483. return 1;
  484. }
  485. if(vV.size() > 0)
  486. {
  487. if(!list_to_matrix(vV,V))
  488. {
  489. cerr<<"Bad V"<<endl;
  490. return 1;
  491. }
  492. polygon_mesh_to_triangle_mesh(vF,F);
  493. }
  494. if(vTC.size() > 0)
  495. {
  496. if(!list_to_matrix(vTC,TC))
  497. {
  498. cerr<<"Bad TC"<<endl;
  499. return 1;
  500. }
  501. }
  502. if(vTF.size() > 0)
  503. {
  504. if(!list_to_matrix(vTF,TF))
  505. {
  506. cerr<<"Bad TF"<<endl;
  507. return 1;
  508. }
  509. }
  510. //if(vN.size() > 0)
  511. //{
  512. // if(!list_to_matrix(vN,N))
  513. // {
  514. // return 1;
  515. // }
  516. //}else
  517. //{
  518. per_vertex_normals(V,F,N);
  519. //}
  520. // Compute normals, centroid, colors, bounding box diagonal
  521. mid = 0.5*(V.colwise().maxCoeff() + V.colwise().minCoeff());
  522. bbd = (V.colwise().maxCoeff() - V.colwise().minCoeff()).maxCoeff();
  523. // Init glut
  524. glutInit(&argc,argv);
  525. if( !TwInit(TW_OPENGL, NULL) )
  526. {
  527. // A fatal error occured
  528. fprintf(stderr, "AntTweakBar initialization failed: %s\n", TwGetLastError());
  529. return 1;
  530. }
  531. // Create a tweak bar
  532. rebar.TwNewBar("TweakBar");
  533. rebar.TwAddVarRW("camera_rotation", TW_TYPE_QUAT4D,
  534. s.camera.m_rotation_conj.coeffs().data(), "open readonly=true");
  535. s.camera.push_away(3);
  536. s.camera.dolly_zoom(25-s.camera.m_angle);
  537. TwType RotationTypeTW = igl::anttweakbar::ReTwDefineEnumFromString("RotationType",
  538. "igl_trackball,two-a...-fixed-up");
  539. rebar.TwAddVarCB( "rotation_type", RotationTypeTW,
  540. set_rotation_type,get_rotation_type,NULL,"keyIncr=] keyDecr=[");
  541. rebar.TwAddVarRW("flip_y", TW_TYPE_BOOLCPP, &flip_y,"key=f");
  542. rebar.TwAddVarRW("rotate_xy", TW_TYPE_BOOLCPP, &rotate_xy,"key=r");
  543. rebar.load(REBAR_NAME);
  544. glutInitDisplayString( "rgba depth double samples>=8 ");
  545. glutInitWindowSize(glutGet(GLUT_SCREEN_WIDTH)/2.0,glutGet(GLUT_SCREEN_HEIGHT));
  546. glutCreateWindow("colored-mesh");
  547. glutDisplayFunc(display);
  548. glutReshapeFunc(reshape);
  549. glutKeyboardFunc(key);
  550. glutMouseFunc(mouse);
  551. glutMotionFunc(mouse_drag);
  552. glutPassiveMotionFunc(
  553. [](int x, int y)
  554. {
  555. TwEventMouseMotionGLUT(x,y);
  556. glutPostRedisplay();
  557. });
  558. static std::function<void(int)> timer_bounce;
  559. auto timer = [] (int ms) {
  560. timer_bounce(ms);
  561. };
  562. timer_bounce = [&] (int ms) {
  563. glutTimerFunc(ms, timer, ms);
  564. glutPostRedisplay();
  565. };
  566. glutTimerFunc(500, timer, 500);
  567. // Must be called after opengl context is initialized
  568. if(!igl::png::texture_from_file(tfilename,tex_id))
  569. {
  570. return 1;
  571. }
  572. glutMainLoop();
  573. return 0;
  574. }