example.cpp 13 KB

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