example.cpp 17 KB

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