example.cpp 15 KB

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