example.cpp 17 KB

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