example.cpp 16 KB

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