example.cpp 15 KB

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