example.cpp 18 KB

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