example.cpp 19 KB

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