example.cpp 13 KB

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