example.cpp 17 KB

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