example.cpp 14 KB

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