example.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  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/normalize_row_lengths.h>
  27. #include <igl/boost/components.h>
  28. #include <igl/boost/bfs_orient.h>
  29. #include <igl/orient_outward.h>
  30. #include <igl/embree/orient_outward_ao.h>
  31. #include <igl/unique_simplices.h>
  32. #include <Eigen/Core>
  33. #include <Eigen/Geometry>
  34. #ifdef WIN32
  35. #include <GL/glut.h>
  36. #else
  37. #include <GLUT/glut.h>
  38. #endif
  39. #ifndef GLUT_WHEEL_UP
  40. #define GLUT_WHEEL_UP 3
  41. #define GLUT_WHEEL_DOWN 4
  42. #define GLUT_WHEEL_RIGHT 5
  43. #define GLUT_WHEEL_LEFT 6
  44. #define GLUT_ACTIVE_COMMAND 1
  45. #endif
  46. #include <ctime>
  47. #include <string>
  48. #include <vector>
  49. #include <stack>
  50. #include <iostream>
  51. int cc_selected = -1;
  52. Eigen::MatrixXd V;
  53. Eigen::VectorXd Vmid,Vmin,Vmax;
  54. double bbd = 1.0;
  55. Eigen::MatrixXi F;
  56. Eigen::VectorXi CC;
  57. struct State
  58. {
  59. igl::Camera camera;
  60. Eigen::MatrixXd N;
  61. Eigen::MatrixXd C;
  62. } s;
  63. // See README for descriptions
  64. enum RotationType
  65. {
  66. ROTATION_TYPE_IGL_TRACKBALL = 0,
  67. ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP = 1,
  68. NUM_ROTATION_TYPES = 2,
  69. } rotation_type;
  70. enum OrientMethod
  71. {
  72. ORIENT_METHOD_OUTWARD = 0,
  73. ORIENT_METHOD_AO = 1,
  74. NUM_ORIENT_METHODS = 2,
  75. } orient_method = ORIENT_METHOD_AO;
  76. std::stack<State> undo_stack;
  77. std::stack<State> redo_stack;
  78. bool wireframe_visible = false;
  79. bool fill_visible = true;
  80. bool is_rotating = false;
  81. int down_x,down_y;
  82. igl::Camera down_camera;
  83. bool is_animating = false;
  84. double animation_start_time = 0;
  85. double ANIMATION_DURATION = 0.5;
  86. Eigen::Quaterniond animation_from_quat;
  87. Eigen::Quaterniond animation_to_quat;
  88. int width,height;
  89. Eigen::Vector4f light_pos(-0.1,-0.1,0.9,0);
  90. #define REBAR_NAME "temp.rbr"
  91. igl::ReTwBar rebar;
  92. // Forward
  93. void init_patches();
  94. void init_relative();
  95. void push_undo()
  96. {
  97. undo_stack.push(s);
  98. // Clear
  99. redo_stack = std::stack<State>();
  100. }
  101. void TW_CALL set_camera_rotation(const void * value, void *clientData)
  102. {
  103. using namespace std;
  104. // case current value to double
  105. const double * quat = (const double *)(value);
  106. std::copy(quat,quat+4,s.camera.rotation);
  107. }
  108. void TW_CALL set_orient_method(const void * value, void * clientData)
  109. {
  110. const OrientMethod old_orient_method = orient_method;
  111. orient_method = *(const OrientMethod *)value;
  112. if(orient_method != old_orient_method)
  113. {
  114. init_patches();
  115. init_relative();
  116. }
  117. }
  118. void TW_CALL get_orient_method(void * value, void *clientData)
  119. {
  120. OrientMethod * om = (OrientMethod *)(value);
  121. *om = orient_method;
  122. }
  123. void TW_CALL set_rotation_type(const void * value, void * clientData)
  124. {
  125. using namespace Eigen;
  126. using namespace std;
  127. using namespace igl;
  128. const RotationType old_rotation_type = rotation_type;
  129. rotation_type = *(const RotationType *)(value);
  130. if(rotation_type == ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP &&
  131. old_rotation_type != ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP)
  132. {
  133. push_undo();
  134. copy(s.camera.rotation,s.camera.rotation+4,animation_from_quat.coeffs().data());
  135. const Vector3d up = animation_from_quat.matrix() * Vector3d(0,1,0);
  136. Vector3d proj_up(0,up(1),up(2));
  137. if(proj_up.norm() == 0)
  138. {
  139. proj_up = Vector3d(0,1,0);
  140. }
  141. proj_up.normalize();
  142. Quaterniond dq;
  143. dq = Quaterniond::FromTwoVectors(up,proj_up);
  144. animation_to_quat = dq * animation_from_quat;
  145. // start animation
  146. animation_start_time = get_seconds();
  147. is_animating = true;
  148. }
  149. }
  150. void TW_CALL get_rotation_type(void * value, void *clientData)
  151. {
  152. RotationType * rt = (RotationType *)(value);
  153. *rt = rotation_type;
  154. }
  155. void TW_CALL get_camera_rotation(void * value, void *clientData)
  156. {
  157. using namespace std;
  158. // case current value to double
  159. double * quat = (double *)(value);
  160. std::copy(s.camera.rotation,s.camera.rotation+4,quat);
  161. }
  162. void reshape(int width, int height)
  163. {
  164. ::width = width;
  165. ::height = height;
  166. glViewport(0,0,width,height);
  167. // Send the new window size to AntTweakBar
  168. TwWindowSize(width, height);
  169. }
  170. void push_scene()
  171. {
  172. using namespace igl;
  173. using namespace std;
  174. const double angle = s.camera.angle;
  175. glMatrixMode(GL_PROJECTION);
  176. glPushMatrix();
  177. glLoadIdentity();
  178. double zNear = 1e-2;
  179. double zFar = 100;
  180. double aspect = ((double)width)/((double)height);
  181. // Amount of scaling needed to "fix" perspective z-shift
  182. double z_fix = 1.0;
  183. // 5 is far enough to see unit "things" well
  184. const double camera_z = 2;
  185. // Test if should be using true orthographic projection
  186. if(angle == 0)
  187. {
  188. glOrtho(
  189. -0.5*camera_z*aspect,
  190. 0.5*camera_z*aspect,
  191. -0.5*camera_z,
  192. 0.5*camera_z,
  193. zNear,
  194. zFar);
  195. }else
  196. {
  197. // Make sure aspect is sane
  198. aspect = aspect < 0.01 ? 0.01 : aspect;
  199. gluPerspective(angle,aspect,zNear,zFar);
  200. z_fix = 2.*tan(angle/2./360.*2.*M_PI);
  201. }
  202. glMatrixMode(GL_MODELVIEW);
  203. glPushMatrix();
  204. glLoadIdentity();
  205. gluLookAt(0,0,camera_z,0,0,0,0,1,0);
  206. // Adjust scale to correct perspective
  207. glScaled(z_fix,z_fix,z_fix);
  208. // scale, pan
  209. glScaled( s.camera.zoom, s.camera.zoom, s.camera.zoom);
  210. double mat[4*4];
  211. quat_to_mat(s.camera.rotation,mat);
  212. glMultMatrixd(mat);
  213. }
  214. void push_object()
  215. {
  216. using namespace igl;
  217. glPushMatrix();
  218. glScaled(2./bbd,2./bbd,2./bbd);
  219. glTranslated(-Vmid(0),-Vmid(1),-Vmid(2));
  220. }
  221. void pop_object()
  222. {
  223. glPopMatrix();
  224. }
  225. void pop_scene()
  226. {
  227. glMatrixMode(GL_PROJECTION);
  228. glPopMatrix();
  229. glMatrixMode(GL_MODELVIEW);
  230. glPopMatrix();
  231. }
  232. // Set up double-sided lights
  233. void lights()
  234. {
  235. using namespace std;
  236. using namespace Eigen;
  237. glEnable(GL_LIGHTING);
  238. //glLightModelf(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
  239. glEnable(GL_LIGHT0);
  240. float WHITE[4] = {1,1,1,1.};
  241. float GREY[4] = {0.4,0.4,0.4,1.};
  242. float BLACK[4] = {0.,0.,0.,1.};
  243. float NEAR_BLACK[4] = {0.1,0.1,0.1,1.};
  244. Vector4f pos = light_pos;
  245. glLightfv(GL_LIGHT0,GL_AMBIENT,BLACK);
  246. glLightfv(GL_LIGHT0,GL_DIFFUSE,WHITE);
  247. glLightfv(GL_LIGHT0,GL_SPECULAR,BLACK);
  248. glLightfv(GL_LIGHT0,GL_POSITION,pos.data());
  249. //glEnable(GL_LIGHT1);
  250. //pos(0) *= -1;
  251. //pos(1) *= -1;
  252. //pos(2) *= -1;
  253. //glLightfv(GL_LIGHT1,GL_AMBIENT,BLACK);
  254. //glLightfv(GL_LIGHT1,GL_DIFFUSE,NEAR_BLACK);
  255. //glLightfv(GL_LIGHT1,GL_SPECULAR,BLACK);
  256. //glLightfv(GL_LIGHT1,GL_POSITION,pos.data());
  257. }
  258. void display()
  259. {
  260. using namespace igl;
  261. using namespace std;
  262. using namespace Eigen;
  263. glClearColor(1,1,1,0);
  264. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  265. if(is_animating)
  266. {
  267. double t = (get_seconds() - animation_start_time)/ANIMATION_DURATION;
  268. if(t > 1)
  269. {
  270. t = 1;
  271. is_animating = false;
  272. }
  273. Quaterniond q;
  274. q.coeffs() =
  275. animation_to_quat.coeffs()*t + animation_from_quat.coeffs()*(1.-t);
  276. q.normalize();
  277. copy(q.coeffs().data(),q.coeffs().data()+4,s.camera.rotation);
  278. }
  279. glEnable(GL_DEPTH_TEST);
  280. glEnable(GL_NORMALIZE);
  281. lights();
  282. push_scene();
  283. push_object();
  284. // Set material properties
  285. glEnable(GL_COLOR_MATERIAL);
  286. glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
  287. if(wireframe_visible)
  288. {
  289. glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
  290. if(fill_visible)
  291. {
  292. glColor3f(0,0,0);
  293. draw_mesh(V,F,s.N);
  294. }else
  295. {
  296. draw_mesh(V,F,s.N,s.C);
  297. }
  298. // visualize selected patch
  299. glLineWidth(10);
  300. glBegin(GL_TRIANGLES);
  301. glColor3d(0, 0, 0);
  302. // loop over faces
  303. for(int i = 0; i<F.rows();i++)
  304. {
  305. if (CC(i) != cc_selected) continue;
  306. // loop over corners of triangle
  307. for(int j = 0;j<3;j++)
  308. {
  309. glVertex3d(V(F(i,j),0),V(F(i,j),1),V(F(i,j),2));
  310. }
  311. }
  312. glEnd();
  313. glLineWidth(1);
  314. glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
  315. glBegin(GL_TRIANGLES);
  316. glColor3d(1, 0, 0);
  317. // loop over faces
  318. for(int i = 0; i<F.rows();i++)
  319. {
  320. if (CC(i) != cc_selected) continue;
  321. // loop over corners of triangle
  322. glNormal3d(s.N(i,0),s.N(i,1),s.N(i,2));
  323. for(int j = 0;j<3;j++)
  324. {
  325. glVertex3d(V(F(i,j),0),V(F(i,j),1),V(F(i,j),2));
  326. }
  327. }
  328. glEnd();
  329. }
  330. glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
  331. if(fill_visible)
  332. {
  333. glEnable(GL_POLYGON_OFFSET_FILL); // Avoid Stitching!
  334. glPolygonOffset(1.0, 0);
  335. draw_mesh(V,F,s.N,s.C);
  336. }
  337. pop_object();
  338. // Draw a nice floor
  339. glPushMatrix();
  340. const double floor_offset =
  341. -2./bbd*(V.col(1).maxCoeff()-Vmid(1));
  342. glTranslated(0,floor_offset,0);
  343. const float GREY[4] = {0.5,0.5,0.6,1.0};
  344. const float DARK_GREY[4] = {0.2,0.2,0.3,1.0};
  345. //draw_floor(GREY,DARK_GREY);
  346. glPopMatrix();
  347. pop_scene();
  348. report_gl_error();
  349. TwDraw();
  350. glutSwapBuffers();
  351. glutPostRedisplay();
  352. }
  353. void mouse_wheel(int wheel, int direction, int mouse_x, int mouse_y)
  354. {
  355. using namespace std;
  356. if(wheel == 0)
  357. {
  358. static double mouse_scroll_y = 0;
  359. const double delta_y = 0.125*direction;
  360. mouse_scroll_y += delta_y;
  361. // absolute scale difference when changing zooms (+1)
  362. const double z_diff = 0.01;
  363. GLint viewport[4];
  364. glGetIntegerv(GL_VIEWPORT,viewport);
  365. if(TwMouseMotion(mouse_x, viewport[3] - mouse_y))
  366. {
  367. TwMouseWheel(mouse_scroll_y);
  368. }else
  369. {
  370. s.camera.zoom *= (1.0+double(direction)*z_diff);
  371. const double min_zoom = 0.01;
  372. const double max_zoom = 10.0;
  373. s.camera.zoom = min(max_zoom,max(min_zoom,s.camera.zoom));
  374. }
  375. }else
  376. {
  377. if(!is_rotating)
  378. {
  379. // Change viewing angle (reshape will take care of adjust zoom)
  380. const double a_diff = 1.0;
  381. s.camera.angle += double(direction)*a_diff;
  382. const double min_angle = 15.0;
  383. s.camera.angle =
  384. min(90.0,max(min_angle,s.camera.angle));
  385. }
  386. }
  387. }
  388. void mouse(int glutButton, int glutState, int mouse_x, int mouse_y)
  389. {
  390. using namespace std;
  391. using namespace Eigen;
  392. using namespace igl;
  393. bool tw_using = TwEventMouseButtonGLUT(glutButton,glutState,mouse_x,mouse_y);
  394. switch(glutButton)
  395. {
  396. case GLUT_RIGHT_BUTTON:
  397. case GLUT_LEFT_BUTTON:
  398. {
  399. switch(glutState)
  400. {
  401. case 1:
  402. // up
  403. glutSetCursor(GLUT_CURSOR_INHERIT);
  404. is_rotating = false;
  405. break;
  406. case 0:
  407. if(!tw_using)
  408. {
  409. push_undo();
  410. glutSetCursor(GLUT_CURSOR_CYCLE);
  411. // collect information for trackball
  412. is_rotating = true;
  413. down_camera = s.camera;
  414. down_x = mouse_x;
  415. down_y = mouse_y;
  416. }
  417. break;
  418. }
  419. break;
  420. // Scroll down
  421. case GLUT_WHEEL_DOWN:
  422. {
  423. mouse_wheel(0,-1,mouse_x,mouse_y);
  424. break;
  425. }
  426. // Scroll up
  427. case GLUT_WHEEL_UP:
  428. {
  429. mouse_wheel(0,1,mouse_x,mouse_y);
  430. break;
  431. }
  432. // Scroll left
  433. case GLUT_WHEEL_LEFT:
  434. {
  435. mouse_wheel(1,-1,mouse_x,mouse_y);
  436. break;
  437. }
  438. // Scroll right
  439. case GLUT_WHEEL_RIGHT:
  440. {
  441. mouse_wheel(1,1,mouse_x,mouse_y);
  442. break;
  443. }
  444. }
  445. }
  446. }
  447. void mouse_drag(int mouse_x, int mouse_y)
  448. {
  449. using namespace igl;
  450. using namespace std;
  451. using namespace Eigen;
  452. bool tw_using = TwMouseMotion(mouse_x,mouse_y);
  453. if(is_rotating)
  454. {
  455. glutSetCursor(GLUT_CURSOR_CYCLE);
  456. switch(rotation_type)
  457. {
  458. case ROTATION_TYPE_IGL_TRACKBALL:
  459. {
  460. // Rotate according to trackball
  461. igl::trackball<double>(
  462. width,
  463. height,
  464. 2.0,
  465. down_camera.rotation,
  466. down_x,
  467. down_y,
  468. mouse_x,
  469. mouse_y,
  470. s.camera.rotation);
  471. break;
  472. }
  473. case ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP:
  474. {
  475. Quaterniond down_q;
  476. copy(down_camera.rotation,down_camera.rotation+4,down_q.coeffs().data());
  477. Vector3d axis(0,1,0);
  478. const double speed = 2.0;
  479. Quaterniond q;
  480. q = down_q *
  481. Quaterniond(
  482. AngleAxisd(
  483. M_PI*((double)(mouse_x-down_x))/(double)width*speed/2.0,
  484. axis.normalized()));
  485. q.normalize();
  486. {
  487. Vector3d axis(1,0,0);
  488. const double speed = 2.0;
  489. if(axis.norm() != 0)
  490. {
  491. q =
  492. Quaterniond(
  493. AngleAxisd(
  494. M_PI*(mouse_y-down_y)/(double)width*speed/2.0,
  495. axis.normalized())) * q;
  496. q.normalize();
  497. }
  498. }
  499. copy(q.coeffs().data(),q.coeffs().data()+4,s.camera.rotation);
  500. break;
  501. }
  502. default:
  503. break;
  504. }
  505. }
  506. }
  507. void init_relative()
  508. {
  509. using namespace Eigen;
  510. using namespace igl;
  511. per_face_normals(V,F,s.N);
  512. normalize_row_lengths(s.N,s.N);
  513. Vmax = V.colwise().maxCoeff();
  514. Vmin = V.colwise().minCoeff();
  515. Vmid = 0.5*(Vmax + Vmin);
  516. bbd = (Vmax-Vmin).norm();
  517. }
  518. void randomly_color(
  519. const Eigen::VectorXi & CC,
  520. Eigen::MatrixXd & C)
  521. {
  522. using namespace Eigen;
  523. using namespace igl;
  524. using namespace std;
  525. VectorXi I;
  526. srand ( unsigned ( time(0) ) );
  527. double num_cc = (double)CC.maxCoeff()+1.0;
  528. randperm(num_cc,I);
  529. C.resize(CC.rows(),3);
  530. for(int f = 0;f<CC.rows();f++)
  531. {
  532. jet(
  533. (double)I(CC(f))/num_cc,
  534. C(f,0),
  535. C(f,1),
  536. C(f,2));
  537. }
  538. }
  539. void TW_CALL randomize_colors(void * /*clientData*/)
  540. {
  541. push_undo();
  542. randomly_color(CC,s.C);
  543. }
  544. void init_patches()
  545. {
  546. using namespace Eigen;
  547. using namespace igl;
  548. using namespace std;
  549. {
  550. VectorXi VCC;
  551. components(F,VCC);
  552. cout<<"There are "<<VCC.maxCoeff()+1<<" connected components of vertices."<<endl;
  553. }
  554. bfs_orient(F,F,CC);
  555. VectorXi I;
  556. switch(orient_method)
  557. {
  558. case ORIENT_METHOD_AO:
  559. cout<<"orient_outward_ao()"<<endl;
  560. orient_outward_ao(V,F,CC,100, F.rows() * 100,F,I);
  561. break;
  562. case ORIENT_METHOD_OUTWARD:
  563. default:
  564. cout<<"orient_outward()"<<endl;
  565. orient_outward(V,F,CC,F,I);
  566. break;
  567. }
  568. double num_cc = (double)CC.maxCoeff()+1.0;
  569. cout<<"There are "<<num_cc<<" 'manifold/orientable' patches of faces."<<endl;
  570. }
  571. void undo()
  572. {
  573. using namespace std;
  574. if(!undo_stack.empty())
  575. {
  576. redo_stack.push(s);
  577. s = undo_stack.top();
  578. undo_stack.pop();
  579. }
  580. }
  581. void redo()
  582. {
  583. using namespace std;
  584. if(!redo_stack.empty())
  585. {
  586. undo_stack.push(s);
  587. s = redo_stack.top();
  588. redo_stack.pop();
  589. }
  590. }
  591. void key(unsigned char key, int mouse_x, int mouse_y)
  592. {
  593. using namespace std;
  594. int mod = glutGetModifiers();
  595. switch(key)
  596. {
  597. // ESC
  598. case char(27):
  599. rebar.save(REBAR_NAME);
  600. // ^C
  601. case char(3):
  602. exit(0);
  603. case 'I':
  604. case 'i':
  605. {
  606. push_undo();
  607. s.N *= -1.0;
  608. break;
  609. }
  610. case 'z':
  611. case 'Z':
  612. if(mod & GLUT_ACTIVE_COMMAND)
  613. {
  614. if(mod & GLUT_ACTIVE_SHIFT)
  615. {
  616. redo();
  617. }else
  618. {
  619. undo();
  620. }
  621. break;
  622. }else
  623. {
  624. push_undo();
  625. igl::snap_to_canonical_view_quat<double>(
  626. s.camera.rotation,
  627. 1.0,
  628. s.camera.rotation);
  629. break;
  630. }
  631. case 'u':
  632. mouse_wheel(0, 1,mouse_x,mouse_y);
  633. break;
  634. case 'j':
  635. mouse_wheel(0,-1,mouse_x,mouse_y);
  636. break;
  637. case 'n':
  638. cc_selected = (cc_selected + 1) % (CC.maxCoeff() + 2);
  639. cout << "selected cc: " << cc_selected << endl;
  640. glutPostRedisplay();
  641. break;
  642. default:
  643. if(!TwEventKeyboardGLUT(key,mouse_x,mouse_y))
  644. {
  645. cout<<"Unknown key command: "<<key<<" "<<int(key)<<endl;
  646. }
  647. }
  648. }
  649. int main(int argc, char * argv[])
  650. {
  651. using namespace std;
  652. using namespace Eigen;
  653. using namespace igl;
  654. string filename = "../shared/truck.obj";
  655. if(argc < 2)
  656. {
  657. cerr<<"Usage:"<<endl<<" ./example input.obj"<<endl;
  658. cout<<endl<<"Opening default mesh..."<<endl;
  659. }else
  660. {
  661. // Read and prepare mesh
  662. filename = argv[1];
  663. }
  664. // print key commands
  665. cout<<"[Click] and [drag] Rotate model using trackball."<<endl;
  666. cout<<"[Z,z] Snap rotation to canonical view."<<endl;
  667. cout<<"[Command+Z] Undo."<<endl;
  668. cout<<"[Shift+Command+Z] Redo."<<endl;
  669. cout<<"[^C,ESC] Exit."<<endl;
  670. // dirname, basename, extension and filename
  671. string d,b,ext,f;
  672. pathinfo(filename,d,b,ext,f);
  673. // Convert extension to lower case
  674. transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
  675. vector<vector<double > > vV,vN,vTC;
  676. vector<vector<int > > vF,vFTC,vFN;
  677. if(ext == "obj")
  678. {
  679. // Convert extension to lower case
  680. if(!igl::readOBJ(filename,vV,vTC,vN,vF,vFTC,vFN))
  681. {
  682. return 1;
  683. }
  684. }else if(ext == "off")
  685. {
  686. // Convert extension to lower case
  687. if(!igl::readOFF(filename,vV,vF,vN))
  688. {
  689. return 1;
  690. }
  691. }else if(ext == "wrl")
  692. {
  693. // Convert extension to lower case
  694. if(!igl::readWRL(filename,vV,vF))
  695. {
  696. return 1;
  697. }
  698. //}else
  699. //{
  700. // // Convert extension to lower case
  701. // MatrixXi T;
  702. // if(!igl::readMESH(filename,V,T,F))
  703. // {
  704. // return 1;
  705. // }
  706. // //if(F.size() > T.size() || F.size() == 0)
  707. // {
  708. // boundary_faces(T,F);
  709. // }
  710. }
  711. if(vV.size() > 0)
  712. {
  713. if(!list_to_matrix(vV,V))
  714. {
  715. return 1;
  716. }
  717. triangulate(vF,F);
  718. }
  719. MatrixXi F_unique;
  720. unique_simplices(F, F_unique);
  721. F = F_unique;
  722. init_patches();
  723. init_relative();
  724. randomly_color(CC,s.C);
  725. // Init glut
  726. glutInit(&argc,argv);
  727. if( !TwInit(TW_OPENGL, NULL) )
  728. {
  729. // A fatal error occured
  730. fprintf(stderr, "AntTweakBar initialization failed: %s\n", TwGetLastError());
  731. return 1;
  732. }
  733. // Create a tweak bar
  734. rebar.TwNewBar("TweakBar");
  735. rebar.TwAddVarCB("camera_rotation", TW_TYPE_QUAT4D, set_camera_rotation,get_camera_rotation, NULL, "open readonly=true");
  736. TwEnumVal RotationTypesEV[NUM_ROTATION_TYPES] =
  737. {
  738. {ROTATION_TYPE_IGL_TRACKBALL,"igl trackball"},
  739. {ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP,"two axis fixed up"}
  740. };
  741. TwType RotationTypeTW =
  742. ReTwDefineEnum(
  743. "RotationType",
  744. RotationTypesEV,
  745. NUM_ROTATION_TYPES);
  746. rebar.TwAddVarCB( "rotation_type", RotationTypeTW,
  747. set_rotation_type,get_rotation_type,NULL,"keyIncr=] keyDecr=[");
  748. TwEnumVal OrientMethodEV[NUM_ORIENT_METHODS] =
  749. {
  750. {ORIENT_METHOD_OUTWARD,"outward"},
  751. {ORIENT_METHOD_AO,"ambient occlusion"}
  752. };
  753. TwType OrientMethodTW =
  754. ReTwDefineEnum(
  755. "OrientMethod",
  756. OrientMethodEV,
  757. NUM_ROTATION_TYPES);
  758. rebar.TwAddVarCB( "orient_method", OrientMethodTW,
  759. set_orient_method,get_orient_method,NULL,"keyIncr=< keyDecr=>");
  760. rebar.TwAddVarRW("wireframe_visible",TW_TYPE_BOOLCPP,&wireframe_visible,"key=l");
  761. rebar.TwAddVarRW("fill_visible",TW_TYPE_BOOLCPP,&fill_visible,"key=f");
  762. rebar.TwAddButton("randomize colors",randomize_colors,NULL,"key=c");
  763. rebar.load(REBAR_NAME);
  764. animation_from_quat = Quaterniond(1,0,0,0);
  765. copy(s.camera.rotation,s.camera.rotation+4,animation_to_quat.coeffs().data());
  766. animation_start_time = get_seconds();
  767. // Init antweakbar
  768. #ifdef __APPLE__
  769. glutInitDisplayString( "rgba depth double samples>=8");
  770. #else
  771. glutInitDisplayString( "rgba depth double "); // samples>=8 somehow not supported on Kenshi's machines...?
  772. #endif
  773. glutInitWindowSize(glutGet(GLUT_SCREEN_WIDTH)/2.0,glutGet(GLUT_SCREEN_HEIGHT)/2.0);
  774. glutCreateWindow("patches");
  775. glutDisplayFunc(display);
  776. glutReshapeFunc(reshape);
  777. glutKeyboardFunc(key);
  778. glutMouseFunc(mouse);
  779. glutMotionFunc(mouse_drag);
  780. glutPassiveMotionFunc((GLUTmousemotionfun)TwEventMouseMotionGLUT);
  781. glutMainLoop();
  782. return 0;
  783. }