example.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. // Small GLUT application to test different scene rotation paradigms
  2. //
  3. #include <igl/readOBJ.h>
  4. #include <igl/writeOBJ.h>
  5. #include <igl/writeOFF.h>
  6. #include <igl/readWRL.h>
  7. #include <igl/report_gl_error.h>
  8. #include <igl/triangulate.h>
  9. #include <igl/readOFF.h>
  10. #include <igl/readMESH.h>
  11. #include <igl/draw_mesh.h>
  12. #include <igl/draw_floor.h>
  13. #include <igl/pathinfo.h>
  14. #include <igl/list_to_matrix.h>
  15. #include <igl/quat_to_mat.h>
  16. #include <igl/per_face_normals.h>
  17. #include <igl/material_colors.h>
  18. #include <igl/trackball.h>
  19. #include <igl/snap_to_canonical_view_quat.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/jet.h>
  25. #include <igl/randperm.h>
  26. #include <igl/boost/manifold_patches.h>
  27. #include <igl/boost/components.h>
  28. #include <Eigen/Core>
  29. #include <Eigen/Geometry>
  30. #include <GLUT/glut.h>
  31. #include <string>
  32. #include <vector>
  33. #include <stack>
  34. #include <iostream>
  35. Eigen::MatrixXd V,C;
  36. Eigen::VectorXd Vmid,Vmin,Vmax;
  37. double bbd = 1.0;
  38. Eigen::MatrixXi F;
  39. struct State
  40. {
  41. igl::Camera camera;
  42. Eigen::MatrixXd N;
  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. void TW_CALL set_camera_rotation(const void * value, void *clientData)
  72. {
  73. using namespace std;
  74. // case current value to double
  75. const double * quat = (const double *)(value);
  76. std::copy(quat,quat+4,s.camera.rotation);
  77. }
  78. void TW_CALL set_rotation_type(const void * value, void * clientData)
  79. {
  80. using namespace Eigen;
  81. using namespace std;
  82. using namespace igl;
  83. const RotationType old_rotation_type = rotation_type;
  84. rotation_type = *(const RotationType *)(value);
  85. if(rotation_type == ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP &&
  86. old_rotation_type != ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP)
  87. {
  88. push_undo();
  89. copy(s.camera.rotation,s.camera.rotation+4,animation_from_quat.coeffs().data());
  90. const Vector3d up = animation_from_quat.matrix() * Vector3d(0,1,0);
  91. Vector3d proj_up(0,up(1),up(2));
  92. if(proj_up.norm() == 0)
  93. {
  94. proj_up = Vector3d(0,1,0);
  95. }
  96. proj_up.normalize();
  97. Quaterniond dq;
  98. dq = Quaterniond::FromTwoVectors(up,proj_up);
  99. animation_to_quat = dq * animation_from_quat;
  100. // start animation
  101. animation_start_time = get_seconds();
  102. is_animating = true;
  103. }
  104. }
  105. void TW_CALL get_rotation_type(void * value, void *clientData)
  106. {
  107. RotationType * rt = (RotationType *)(value);
  108. *rt = rotation_type;
  109. }
  110. void TW_CALL get_camera_rotation(void * value, void *clientData)
  111. {
  112. using namespace std;
  113. // case current value to double
  114. double * quat = (double *)(value);
  115. std::copy(s.camera.rotation,s.camera.rotation+4,quat);
  116. }
  117. void reshape(int width, int height)
  118. {
  119. ::width = width;
  120. ::height = height;
  121. glViewport(0,0,width,height);
  122. // Send the new window size to AntTweakBar
  123. TwWindowSize(width, height);
  124. }
  125. void push_scene()
  126. {
  127. using namespace igl;
  128. using namespace std;
  129. const double angle = s.camera.angle;
  130. glMatrixMode(GL_PROJECTION);
  131. glPushMatrix();
  132. glLoadIdentity();
  133. double zNear = 1e-2;
  134. double zFar = 100;
  135. double aspect = ((double)width)/((double)height);
  136. // Amount of scaling needed to "fix" perspective z-shift
  137. double z_fix = 1.0;
  138. // 5 is far enough to see unit "things" well
  139. const double camera_z = 2;
  140. // Test if should be using true orthographic projection
  141. if(angle == 0)
  142. {
  143. glOrtho(
  144. -0.5*camera_z*aspect,
  145. 0.5*camera_z*aspect,
  146. -0.5*camera_z,
  147. 0.5*camera_z,
  148. zNear,
  149. zFar);
  150. }else
  151. {
  152. // Make sure aspect is sane
  153. aspect = aspect < 0.01 ? 0.01 : aspect;
  154. gluPerspective(angle,aspect,zNear,zFar);
  155. z_fix = 2.*tan(angle/2./360.*2.*M_PI);
  156. }
  157. glMatrixMode(GL_MODELVIEW);
  158. glPushMatrix();
  159. glLoadIdentity();
  160. gluLookAt(0,0,camera_z,0,0,0,0,1,0);
  161. // Adjust scale to correct perspective
  162. glScaled(z_fix,z_fix,z_fix);
  163. // scale, pan
  164. glScaled( s.camera.zoom, s.camera.zoom, s.camera.zoom);
  165. double mat[4*4];
  166. quat_to_mat(s.camera.rotation,mat);
  167. glMultMatrixd(mat);
  168. }
  169. void push_object()
  170. {
  171. using namespace igl;
  172. glPushMatrix();
  173. glScaled(2./bbd,2./bbd,2./bbd);
  174. glTranslated(-Vmid(0),-Vmid(1),-Vmid(2));
  175. }
  176. void pop_object()
  177. {
  178. glPopMatrix();
  179. }
  180. void pop_scene()
  181. {
  182. glMatrixMode(GL_PROJECTION);
  183. glPopMatrix();
  184. glMatrixMode(GL_MODELVIEW);
  185. glPopMatrix();
  186. }
  187. // Set up double-sided lights
  188. void lights()
  189. {
  190. using namespace std;
  191. using namespace Eigen;
  192. glEnable(GL_LIGHTING);
  193. //glLightModelf(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
  194. glEnable(GL_LIGHT0);
  195. float WHITE[4] = {0.7,0.7,0.7,1.};
  196. float GREY[4] = {0.4,0.4,0.4,1.};
  197. float BLACK[4] = {0.,0.,0.,1.};
  198. float NEAR_BLACK[4] = {0.1,0.1,0.1,1.};
  199. Vector4f pos = light_pos;
  200. glLightfv(GL_LIGHT0,GL_AMBIENT,GREY);
  201. glLightfv(GL_LIGHT0,GL_DIFFUSE,WHITE);
  202. glLightfv(GL_LIGHT0,GL_SPECULAR,BLACK);
  203. glLightfv(GL_LIGHT0,GL_POSITION,pos.data());
  204. //glEnable(GL_LIGHT1);
  205. //pos(0) *= -1;
  206. //pos(1) *= -1;
  207. //pos(2) *= -1;
  208. //glLightfv(GL_LIGHT1,GL_AMBIENT,BLACK);
  209. //glLightfv(GL_LIGHT1,GL_DIFFUSE,NEAR_BLACK);
  210. //glLightfv(GL_LIGHT1,GL_SPECULAR,BLACK);
  211. //glLightfv(GL_LIGHT1,GL_POSITION,pos.data());
  212. }
  213. void display()
  214. {
  215. using namespace igl;
  216. using namespace std;
  217. using namespace Eigen;
  218. glClearColor(1,1,1,0);
  219. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  220. if(is_animating)
  221. {
  222. double t = (get_seconds() - animation_start_time)/ANIMATION_DURATION;
  223. if(t > 1)
  224. {
  225. t = 1;
  226. is_animating = false;
  227. }
  228. Quaterniond q;
  229. q.coeffs() =
  230. animation_to_quat.coeffs()*t + animation_from_quat.coeffs()*(1.-t);
  231. q.normalize();
  232. copy(q.coeffs().data(),q.coeffs().data()+4,s.camera.rotation);
  233. }
  234. glEnable(GL_DEPTH_TEST);
  235. glEnable(GL_NORMALIZE);
  236. lights();
  237. push_scene();
  238. push_object();
  239. // Set material properties
  240. glEnable(GL_COLOR_MATERIAL);
  241. glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
  242. draw_mesh(V,F,s.N,C);
  243. pop_object();
  244. // Draw a nice floor
  245. glPushMatrix();
  246. const double floor_offset =
  247. -2./bbd*(V.col(1).maxCoeff()-Vmid(1));
  248. glTranslated(0,floor_offset,0);
  249. const float GREY[4] = {0.5,0.5,0.6,1.0};
  250. const float DARK_GREY[4] = {0.2,0.2,0.3,1.0};
  251. draw_floor(GREY,DARK_GREY);
  252. glPopMatrix();
  253. pop_scene();
  254. report_gl_error();
  255. TwDraw();
  256. glutSwapBuffers();
  257. glutPostRedisplay();
  258. }
  259. void mouse_wheel(int wheel, int direction, int mouse_x, int mouse_y)
  260. {
  261. using namespace std;
  262. push_undo();
  263. if(wheel == 0)
  264. {
  265. static double mouse_scroll_y = 0;
  266. const double delta_y = 0.125*direction;
  267. mouse_scroll_y += delta_y;
  268. // absolute scale difference when changing zooms (+1)
  269. const double z_diff = 0.01;
  270. GLint viewport[4];
  271. glGetIntegerv(GL_VIEWPORT,viewport);
  272. if(TwMouseMotion(mouse_x, viewport[3] - mouse_y))
  273. {
  274. TwMouseWheel(mouse_scroll_y);
  275. }else
  276. {
  277. s.camera.zoom *= (1.0+double(direction)*z_diff);
  278. const double min_zoom = 0.01;
  279. const double max_zoom = 10.0;
  280. s.camera.zoom = min(max_zoom,max(min_zoom,s.camera.zoom));
  281. }
  282. }else
  283. {
  284. if(!is_rotating)
  285. {
  286. // Change viewing angle (reshape will take care of adjust zoom)
  287. const double a_diff = 1.0;
  288. s.camera.angle += double(direction)*a_diff;
  289. const double min_angle = 15.0;
  290. s.camera.angle =
  291. min(90.0,max(min_angle,s.camera.angle));
  292. }
  293. }
  294. }
  295. void mouse(int glutButton, int glutState, int mouse_x, int mouse_y)
  296. {
  297. using namespace std;
  298. using namespace Eigen;
  299. using namespace igl;
  300. bool tw_using = TwEventMouseButtonGLUT(glutButton,glutState,mouse_x,mouse_y);
  301. switch(glutButton)
  302. {
  303. case GLUT_RIGHT_BUTTON:
  304. case GLUT_LEFT_BUTTON:
  305. {
  306. switch(glutState)
  307. {
  308. case 1:
  309. // up
  310. glutSetCursor(GLUT_CURSOR_INHERIT);
  311. is_rotating = false;
  312. break;
  313. case 0:
  314. if(!tw_using)
  315. {
  316. push_undo();
  317. glutSetCursor(GLUT_CURSOR_CYCLE);
  318. // collect information for trackball
  319. is_rotating = true;
  320. down_camera = s.camera;
  321. down_x = mouse_x;
  322. down_y = mouse_y;
  323. }
  324. break;
  325. }
  326. break;
  327. // Scroll down
  328. case GLUT_WHEEL_DOWN:
  329. {
  330. mouse_wheel(0,-1,mouse_x,mouse_y);
  331. break;
  332. }
  333. // Scroll up
  334. case GLUT_WHEEL_UP:
  335. {
  336. mouse_wheel(0,1,mouse_x,mouse_y);
  337. break;
  338. }
  339. // Scroll left
  340. case GLUT_WHEEL_LEFT:
  341. {
  342. mouse_wheel(1,-1,mouse_x,mouse_y);
  343. break;
  344. }
  345. // Scroll right
  346. case GLUT_WHEEL_RIGHT:
  347. {
  348. mouse_wheel(1,1,mouse_x,mouse_y);
  349. break;
  350. }
  351. }
  352. }
  353. }
  354. void mouse_drag(int mouse_x, int mouse_y)
  355. {
  356. using namespace igl;
  357. using namespace std;
  358. using namespace Eigen;
  359. if(is_rotating)
  360. {
  361. glutSetCursor(GLUT_CURSOR_CYCLE);
  362. switch(rotation_type)
  363. {
  364. case ROTATION_TYPE_IGL_TRACKBALL:
  365. {
  366. // Rotate according to trackball
  367. igl::trackball<double>(
  368. width,
  369. height,
  370. 2.0,
  371. down_camera.rotation,
  372. down_x,
  373. down_y,
  374. mouse_x,
  375. mouse_y,
  376. s.camera.rotation);
  377. break;
  378. }
  379. case ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP:
  380. {
  381. Quaterniond down_q;
  382. copy(down_camera.rotation,down_camera.rotation+4,down_q.coeffs().data());
  383. Vector3d axis(0,1,0);
  384. const double speed = 2.0;
  385. Quaterniond q;
  386. q = down_q *
  387. Quaterniond(
  388. AngleAxisd(
  389. M_PI*((double)(mouse_x-down_x))/(double)width*speed/2.0,
  390. axis.normalized()));
  391. q.normalize();
  392. {
  393. Vector3d axis(1,0,0);
  394. const double speed = 2.0;
  395. if(axis.norm() != 0)
  396. {
  397. q =
  398. Quaterniond(
  399. AngleAxisd(
  400. M_PI*(mouse_y-down_y)/(double)width*speed/2.0,
  401. axis.normalized())) * q;
  402. q.normalize();
  403. }
  404. }
  405. copy(q.coeffs().data(),q.coeffs().data()+4,s.camera.rotation);
  406. break;
  407. }
  408. default:
  409. break;
  410. }
  411. }
  412. }
  413. void init_relative()
  414. {
  415. using namespace Eigen;
  416. using namespace igl;
  417. per_face_normals(V,F,s.N);
  418. Vmax = V.colwise().maxCoeff();
  419. Vmin = V.colwise().minCoeff();
  420. Vmid = 0.5*(Vmax + Vmin);
  421. bbd = (Vmax-Vmin).norm();
  422. }
  423. void init_patches()
  424. {
  425. using namespace Eigen;
  426. using namespace igl;
  427. using namespace std;
  428. VectorXi CC;
  429. SparseMatrix<int> A;
  430. manifold_patches(F,CC,A);
  431. //components(F,CC);
  432. C.resize(CC.rows(),3);
  433. double num_cc = (double)CC.maxCoeff()+1.0;
  434. VectorXi I;
  435. randperm(num_cc,I);
  436. for(int f = 0;f<CC.rows();f++)
  437. {
  438. jet(
  439. (double)I(CC(f))/num_cc,
  440. C(f,0),
  441. C(f,1),
  442. C(f,2));
  443. }
  444. }
  445. void undo()
  446. {
  447. using namespace std;
  448. if(!undo_stack.empty())
  449. {
  450. redo_stack.push(s);
  451. s = undo_stack.top();
  452. undo_stack.pop();
  453. }
  454. }
  455. void redo()
  456. {
  457. using namespace std;
  458. if(!redo_stack.empty())
  459. {
  460. undo_stack.push(s);
  461. s = redo_stack.top();
  462. redo_stack.pop();
  463. }
  464. }
  465. void key(unsigned char key, int mouse_x, int mouse_y)
  466. {
  467. using namespace std;
  468. int mod = glutGetModifiers();
  469. switch(key)
  470. {
  471. // ESC
  472. case char(27):
  473. rebar.save(REBAR_NAME);
  474. // ^C
  475. case char(3):
  476. exit(0);
  477. case 'I':
  478. case 'i':
  479. {
  480. push_undo();
  481. s.N *= -1.0;
  482. break;
  483. }
  484. case 'z':
  485. case 'Z':
  486. if(mod & GLUT_ACTIVE_COMMAND)
  487. {
  488. if(mod & GLUT_ACTIVE_SHIFT)
  489. {
  490. redo();
  491. }else
  492. {
  493. undo();
  494. }
  495. break;
  496. }else
  497. {
  498. push_undo();
  499. igl::snap_to_canonical_view_quat<double>(
  500. s.camera.rotation,
  501. 1.0,
  502. s.camera.rotation);
  503. break;
  504. }
  505. default:
  506. if(!TwEventKeyboardGLUT(key,mouse_x,mouse_y))
  507. {
  508. cout<<"Unknown key command: "<<key<<" "<<int(key)<<endl;
  509. }
  510. }
  511. }
  512. int main(int argc, char * argv[])
  513. {
  514. using namespace std;
  515. using namespace Eigen;
  516. using namespace igl;
  517. string filename = "../shared/cheburashka.obj";
  518. if(argc < 2)
  519. {
  520. cerr<<"Usage:"<<endl<<" ./example input.obj"<<endl;
  521. cout<<endl<<"Opening default mesh..."<<endl;
  522. }else
  523. {
  524. // Read and prepare mesh
  525. filename = argv[1];
  526. }
  527. // print key commands
  528. cout<<"[Click] and [drag] Rotate model using trackball."<<endl;
  529. cout<<"[Z,z] Snap rotation to canonical view."<<endl;
  530. cout<<"[⌘ Z] Undo."<<endl;
  531. cout<<"[⇧ ⌘ Z] Redo."<<endl;
  532. cout<<"[^C,ESC] Exit."<<endl;
  533. // dirname, basename, extension and filename
  534. string d,b,ext,f;
  535. pathinfo(filename,d,b,ext,f);
  536. // Convert extension to lower case
  537. transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
  538. vector<vector<double > > vV,vN,vTC;
  539. vector<vector<int > > vF,vFTC,vFN;
  540. if(ext == "obj")
  541. {
  542. // Convert extension to lower case
  543. if(!igl::readOBJ(filename,vV,vTC,vN,vF,vFTC,vFN))
  544. {
  545. return 1;
  546. }
  547. }else if(ext == "off")
  548. {
  549. // Convert extension to lower case
  550. if(!igl::readOFF(filename,vV,vF,vN))
  551. {
  552. return 1;
  553. }
  554. }else if(ext == "wrl")
  555. {
  556. // Convert extension to lower case
  557. if(!igl::readWRL(filename,vV,vF))
  558. {
  559. return 1;
  560. }
  561. //}else
  562. //{
  563. // // Convert extension to lower case
  564. // MatrixXi T;
  565. // if(!igl::readMESH(filename,V,T,F))
  566. // {
  567. // return 1;
  568. // }
  569. // //if(F.size() > T.size() || F.size() == 0)
  570. // {
  571. // boundary_faces(T,F);
  572. // }
  573. }
  574. if(vV.size() > 0)
  575. {
  576. if(!list_to_matrix(vV,V))
  577. {
  578. return 1;
  579. }
  580. triangulate(vF,F);
  581. }
  582. init_patches();
  583. init_relative();
  584. // Init glut
  585. glutInit(&argc,argv);
  586. if( !TwInit(TW_OPENGL, NULL) )
  587. {
  588. // A fatal error occured
  589. fprintf(stderr, "AntTweakBar initialization failed: %s\n", TwGetLastError());
  590. return 1;
  591. }
  592. // Create a tweak bar
  593. rebar.TwNewBar("TweakBar");
  594. rebar.TwAddVarCB("camera_rotation", TW_TYPE_QUAT4D, set_camera_rotation,get_camera_rotation, NULL, "open");
  595. TwEnumVal RotationTypesEV[NUM_ROTATION_TYPES] =
  596. {
  597. {ROTATION_TYPE_IGL_TRACKBALL,"igl trackball"},
  598. {ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP,"two axis fixed up"}
  599. };
  600. TwType RotationTypeTW =
  601. ReTwDefineEnum(
  602. "RotationType",
  603. RotationTypesEV,
  604. NUM_ROTATION_TYPES);
  605. rebar.TwAddVarCB( "rotation_type", RotationTypeTW,
  606. set_rotation_type,get_rotation_type,NULL,"keyIncr=] keyDecr=[");
  607. rebar.load(REBAR_NAME);
  608. // Init antweakbar
  609. glutInitDisplayString( "rgba depth double samples>=8 ");
  610. glutInitWindowSize(glutGet(GLUT_SCREEN_WIDTH)/2.0,glutGet(GLUT_SCREEN_HEIGHT)/2.0);
  611. glutCreateWindow("patches");
  612. glutDisplayFunc(display);
  613. glutReshapeFunc(reshape);
  614. glutKeyboardFunc(key);
  615. glutMouseFunc(mouse);
  616. glutMotionFunc(mouse_drag);
  617. glutPassiveMotionFunc((GLUTmousemotionfun)TwEventMouseMotionGLUT);
  618. glutMainLoop();
  619. return 0;
  620. }