example.cpp 14 KB

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