example.cpp 13 KB

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