example.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. #include <igl/Viewport.h>
  2. #include <igl/Camera.h>
  3. #include <igl/matlab_format.h>
  4. #include <igl/report_gl_error.h>
  5. #include <igl/ReAntTweakBar.h>
  6. #include <igl/trackball.h>
  7. #include <igl/two_axis_valuator_fixed_up.h>
  8. #include <igl/PI.h>
  9. #include <igl/EPS.h>
  10. #include <igl/get_seconds.h>
  11. #include <igl/material_colors.h>
  12. #include <igl/draw_mesh.h>
  13. #include <igl/readOFF.h>
  14. #include <igl/per_face_normals.h>
  15. #include <igl/draw_floor.h>
  16. #include <igl/project.h>
  17. #include <igl/unproject.h>
  18. #include <Eigen/Core>
  19. #include <Eigen/Geometry>
  20. #ifdef __APPLE__
  21. #include <GLUT/glut.h>
  22. #else
  23. #include <GL/glut.h>
  24. #endif
  25. #include <vector>
  26. #include <stack>
  27. #include <iostream>
  28. #include <algorithm>
  29. enum RotationType
  30. {
  31. ROTATION_TYPE_IGL_TRACKBALL = 0,
  32. ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP = 1,
  33. NUM_ROTATION_TYPES = 2,
  34. } rotation_type = ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP;
  35. enum CenterType
  36. {
  37. CENTER_TYPE_ORBIT = 0,
  38. CENTER_TYPE_FPS = 1,
  39. NUM_CENTER_TYPES = 2,
  40. } center_type = CENTER_TYPE_ORBIT;
  41. int width,height;
  42. #define REBAR_NAME "temp.rbr"
  43. igl::ReTwBar rebar;
  44. struct State
  45. {
  46. std::vector<igl::Camera> cameras;
  47. std::vector<GLuint> tex_ids;
  48. std::vector<GLuint> fbo_ids;
  49. std::vector<GLuint> dfbo_ids;
  50. State():cameras(4),
  51. tex_ids(cameras.size()),
  52. fbo_ids(cameras.size()),
  53. dfbo_ids(cameras.size())
  54. {}
  55. } s;
  56. const Eigen::Vector4d back(1,1,1,1);
  57. std::stack<State> undo_stack;
  58. bool is_rotating = false;
  59. igl::Camera down_camera;
  60. int down_x,down_y;
  61. std::stack<State> redo_stack;
  62. Eigen::MatrixXd V,N;
  63. Eigen::MatrixXi F;
  64. Eigen::Vector4f light_pos(-0.1,-0.1,0.9,0);
  65. void push_undo()
  66. {
  67. undo_stack.push(s);
  68. // Clear
  69. redo_stack = std::stack<State>();
  70. }
  71. void undo()
  72. {
  73. if(!undo_stack.empty())
  74. {
  75. redo_stack.push(s);
  76. s = undo_stack.top();
  77. undo_stack.pop();
  78. }
  79. }
  80. void redo()
  81. {
  82. if(!redo_stack.empty())
  83. {
  84. undo_stack.push(s);
  85. s = redo_stack.top();
  86. redo_stack.pop();
  87. }
  88. }
  89. void print(const igl::Camera & camera)
  90. {
  91. using namespace std;
  92. cout<<
  93. "rotation: "<<camera.m_rotation_conj.conjugate().coeffs().transpose()<<endl<<
  94. "translation: "<<camera.m_translation.transpose()<<endl<<
  95. "eye: "<<camera.eye().transpose()<<endl<<
  96. "at: "<<camera.at().transpose()<<endl<<
  97. "up: "<<camera.up().transpose()<<endl<<
  98. endl;
  99. }
  100. void init_cameras()
  101. {
  102. using namespace Eigen;
  103. using namespace std;
  104. s.cameras[0].look_at(
  105. Vector3d(0,0,1),
  106. Vector3d(0,0,0),
  107. Vector3d(0,1,0));
  108. s.cameras[1].look_at(
  109. Vector3d(0,0,-1),
  110. Vector3d(0,0,0),
  111. Vector3d(0,1,0));
  112. s.cameras[2].look_at(
  113. Vector3d(-2,0,0),
  114. Vector3d(0,0,0),
  115. Vector3d(0,1,0));
  116. s.cameras[3].look_at(
  117. Vector3d(3,0,0),
  118. Vector3d(0,0,0),
  119. Vector3d(0,1,0));
  120. }
  121. bool init_render_to_texture(
  122. const int width,
  123. const int height,
  124. GLuint & tex_id,
  125. GLuint & fbo_id,
  126. GLuint & dfbo_id)
  127. {
  128. using namespace igl;
  129. using namespace std;
  130. // Set up a "render-to-texture" frame buffer and texture combo
  131. glDeleteTextures(1,&tex_id);
  132. glDeleteFramebuffersEXT(1,&fbo_id);
  133. glDeleteFramebuffersEXT(1,&dfbo_id);
  134. // http://www.opengl.org/wiki/Framebuffer_Object_Examples#Quick_example.2C_render_to_texture_.282D.29
  135. glGenTextures(1, &tex_id);
  136. glBindTexture(GL_TEXTURE_2D, tex_id);
  137. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  138. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  139. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  140. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  141. //NULL means reserve texture memory, but texels are undefined
  142. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
  143. glBindTexture(GL_TEXTURE_2D, 0);
  144. //-------------------------
  145. glGenFramebuffersEXT(1, &fbo_id);
  146. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo_id);
  147. //Attach 2D texture to this FBO
  148. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, tex_id, 0);
  149. glGenRenderbuffersEXT(1, &dfbo_id);
  150. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, dfbo_id);
  151. glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, width, height);
  152. //-------------------------
  153. //Attach depth buffer to FBO
  154. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, dfbo_id);
  155. //-------------------------
  156. //Does the GPU support current FBO configuration?
  157. GLenum status;
  158. status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
  159. switch(status)
  160. {
  161. case GL_FRAMEBUFFER_COMPLETE_EXT:
  162. break;
  163. default:
  164. return false;
  165. }
  166. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
  167. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  168. return true;
  169. }
  170. void reshape(int width, int height)
  171. {
  172. ::width = width;
  173. ::height = height;
  174. glViewport(0,0,width,height);
  175. // Send the new window size to AntTweakBar
  176. TwWindowSize(width, height);
  177. }
  178. // Set up double-sided lights
  179. void lights()
  180. {
  181. using namespace std;
  182. using namespace Eigen;
  183. glEnable(GL_LIGHTING);
  184. glLightModelf(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
  185. glEnable(GL_LIGHT0);
  186. float WHITE[4] = {0.8,0.8,0.8,1.};
  187. float GREY[4] = {0.4,0.4,0.4,1.};
  188. float BLACK[4] = {0.,0.,0.,1.};
  189. Vector4f pos = light_pos;
  190. glLightfv(GL_LIGHT0,GL_AMBIENT,GREY);
  191. glLightfv(GL_LIGHT0,GL_DIFFUSE,WHITE);
  192. glLightfv(GL_LIGHT0,GL_SPECULAR,BLACK);
  193. glLightfv(GL_LIGHT0,GL_POSITION,pos.data());
  194. }
  195. void draw_scene(const igl::Camera & v_camera,
  196. const bool render_to_texture,
  197. const GLuint & v_tex_id,
  198. const GLuint & v_fbo_id,
  199. const GLuint & v_dfbo_id)
  200. {
  201. using namespace igl;
  202. using namespace std;
  203. using namespace Eigen;
  204. if(render_to_texture)
  205. {
  206. // render to framebuffer
  207. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, v_fbo_id);
  208. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, v_dfbo_id);
  209. glClearColor(back(0),back(1),back(2),1);
  210. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  211. }
  212. glMatrixMode(GL_PROJECTION);
  213. glPushMatrix();
  214. glLoadIdentity();
  215. glMultMatrixd(v_camera.projection().data());
  216. //{
  217. // Matrix4d m;
  218. // glGetDoublev(GL_PROJECTION_MATRIX,m.data());
  219. // cout<<matlab_format(m,"Camera")<<endl;
  220. //}
  221. glMatrixMode(GL_MODELVIEW);
  222. glPushMatrix();
  223. glLoadIdentity();
  224. gluLookAt(
  225. v_camera.eye()(0), v_camera.eye()(1), v_camera.eye()(2),
  226. v_camera.at()(0), v_camera.at()(1), v_camera.at()(2),
  227. v_camera.up()(0), v_camera.up()(1), v_camera.up()(2));
  228. //glLoadIdentity();
  229. //glMultMatrixd(v_camera.inverse().matrix().data());
  230. if(!render_to_texture)
  231. {
  232. Vector3d win;
  233. igl::project(Vector3d(0,0,0),win);
  234. cout<<win.transpose()<<endl;
  235. }
  236. for(int c = 0;c<(int)s.cameras.size();c++)
  237. {
  238. auto & camera = s.cameras[c];
  239. if(&v_camera == &camera)
  240. {
  241. continue;
  242. }
  243. // draw camera
  244. glPushMatrix();
  245. glMultMatrixd(camera.affine().matrix().data());
  246. // eye
  247. glColor4f(0,0,0,1);
  248. glPointSize(10.f);
  249. glBegin(GL_POINTS);
  250. glVertex3f(0,0,0);
  251. glEnd();
  252. // frustrum
  253. const Vector3d u = camera.unit_plane();
  254. glBegin(GL_LINES);
  255. for(int x = -1;x<=1;x+=2)
  256. {
  257. for(int y = -1;y<=1;y+=2)
  258. {
  259. glVertex3f(0,0,0);
  260. glVertex3f(x*u(0),y*u(1),u(2));
  261. }
  262. }
  263. glEnd();
  264. const Vector3d n = u*(camera.m_near-FLOAT_EPS);
  265. glBegin(GL_QUADS);
  266. glVertex3f( n(0),-n(1),n(2));
  267. glVertex3f(-n(0),-n(1),n(2));
  268. glVertex3f(-n(0), n(1),n(2));
  269. glVertex3f( n(0), n(1),n(2));
  270. glEnd();
  271. for(int pass = 0;pass<2;pass++)
  272. {
  273. switch(pass)
  274. {
  275. case 1:
  276. glColor4f(1,1,1,0.5);
  277. glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
  278. //glEnable(GL_BLEND);
  279. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  280. glEnable(GL_TEXTURE_2D);
  281. glBindTexture(GL_TEXTURE_2D,s.tex_ids[c]);
  282. break;
  283. default:
  284. case 0:
  285. glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
  286. glColor4f(0,0,0,1);
  287. break;
  288. }
  289. glBegin(GL_QUADS);
  290. glTexCoord2d(1,0);
  291. glVertex3f( 0.5*u(0),-0.5*u(1),0.5*u(2));
  292. glTexCoord2d(0,0);
  293. glVertex3f(-0.5*u(0),-0.5*u(1),0.5*u(2));
  294. glTexCoord2d(0,1);
  295. glVertex3f(-0.5*u(0), 0.5*u(1),0.5*u(2));
  296. glTexCoord2d(1,1);
  297. glVertex3f( 0.5*u(0), 0.5*u(1),0.5*u(2));
  298. glEnd();
  299. switch(pass)
  300. {
  301. case 1:
  302. glBindTexture(GL_TEXTURE_2D, 0);
  303. glDisable(GL_TEXTURE_2D);
  304. glDisable(GL_BLEND);
  305. break;
  306. default:
  307. break;
  308. }
  309. }
  310. glPopMatrix();
  311. }
  312. // Set material properties
  313. lights();
  314. glEnable(GL_LIGHTING);
  315. glDisable(GL_COLOR_MATERIAL);
  316. glMaterialfv(GL_FRONT, GL_AMBIENT, GOLD_AMBIENT);
  317. glMaterialfv(GL_FRONT, GL_DIFFUSE, GOLD_DIFFUSE );
  318. glMaterialfv(GL_FRONT, GL_SPECULAR, GOLD_SPECULAR);
  319. glMaterialf (GL_FRONT, GL_SHININESS, 128);
  320. glMaterialfv(GL_BACK, GL_AMBIENT, SILVER_AMBIENT);
  321. glMaterialfv(GL_BACK, GL_DIFFUSE, FAST_GREEN_DIFFUSE );
  322. glMaterialfv(GL_BACK, GL_SPECULAR, SILVER_SPECULAR);
  323. glMaterialf (GL_BACK, GL_SHININESS, 128);
  324. draw_mesh(V,F,N);
  325. glDisable(GL_LIGHTING);
  326. glEnable(GL_COLOR_MATERIAL);
  327. //glLineWidth(3.f);
  328. //glColor4f(1,0,1,1);
  329. //glutWireCube(0.25);
  330. //glColor4f(1,0.5,0.5,1);
  331. ////glutWireSphere(0.125,20,20);
  332. {
  333. glPushMatrix();
  334. glTranslated(0,-1,0);
  335. draw_floor();
  336. glPopMatrix();
  337. }
  338. // Axes
  339. for(int d = 0;d<3;d++)
  340. {
  341. glColor4f(d==0,d==1,d==2,1);
  342. glBegin(GL_LINES);
  343. glVertex3f(0,0,0);
  344. glVertex3f(d==0,d==1,d==2);
  345. glEnd();
  346. }
  347. glMatrixMode(GL_PROJECTION);
  348. glPopMatrix();
  349. glMatrixMode(GL_MODELVIEW);
  350. glPopMatrix();
  351. report_gl_error();
  352. if(render_to_texture)
  353. {
  354. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  355. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
  356. }
  357. }
  358. void display()
  359. {
  360. using namespace igl;
  361. using namespace std;
  362. using namespace Eigen;
  363. glClearColor(back(0),back(1),back(2),0);
  364. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  365. glEnable(GL_DEPTH_TEST);
  366. // Update aspect ratios (may have changed since undo/redo)
  367. {
  368. const double aspect = (double)width/(double)height;
  369. for(int c = 0;c<(int)s.cameras.size();c++)
  370. {
  371. auto & camera = s.cameras[c];
  372. auto & tex_id = s.tex_ids[c];
  373. auto & fbo_id = s.fbo_ids[c];
  374. auto & dfbo_id = s.dfbo_ids[c];
  375. if(aspect != camera.m_aspect)
  376. {
  377. cout<<"Initializing camera #"<<c<<"..."<<endl;
  378. camera.m_aspect = aspect;
  379. bool ret = init_render_to_texture(width,height,tex_id,fbo_id,dfbo_id);
  380. assert(ret);
  381. }
  382. draw_scene(camera,true,tex_id,fbo_id,dfbo_id);
  383. }
  384. }
  385. {
  386. auto & camera = s.cameras[0];
  387. draw_scene(camera,false,0,0,0);
  388. }
  389. TwDraw();
  390. glutSwapBuffers();
  391. }
  392. void mouse_wheel(int wheel, int direction, int mouse_x, int mouse_y)
  393. {
  394. using namespace std;
  395. using namespace igl;
  396. using namespace Eigen;
  397. GLint viewport[4];
  398. glGetIntegerv(GL_VIEWPORT,viewport);
  399. if(wheel == 0 && TwMouseMotion(mouse_x, viewport[3] - mouse_y))
  400. {
  401. static double mouse_scroll_y = 0;
  402. const double delta_y = 0.125*direction;
  403. mouse_scroll_y += delta_y;
  404. TwMouseWheel(mouse_scroll_y);
  405. return;
  406. }
  407. auto & camera = s.cameras[0];
  408. switch(center_type)
  409. {
  410. case CENTER_TYPE_ORBIT:
  411. if(wheel==0)
  412. {
  413. // factor of zoom change
  414. double s = (1.-0.01*direction);
  415. //// FOV zoom: just widen angle. This is hardly ever appropriate.
  416. //camera.m_angle *= s;
  417. //camera.m_angle = min(max(camera.m_angle,1),89);
  418. camera.push_away(s);
  419. }else
  420. {
  421. // Dolly zoom:
  422. camera.dolly_zoom((double)direction*1.0);
  423. }
  424. break;
  425. default:
  426. case CENTER_TYPE_FPS:
  427. // Move `eye` and `at`
  428. camera.dolly((wheel==0?Vector3d(0,0,1):Vector3d(-1,0,0))*0.1*direction);
  429. break;
  430. }
  431. glutPostRedisplay();
  432. }
  433. void mouse(int glutButton, int glutState, int mouse_x, int mouse_y)
  434. {
  435. using namespace std;
  436. using namespace Eigen;
  437. using namespace igl;
  438. bool tw_using = TwEventMouseButtonGLUT(glutButton,glutState,mouse_x,mouse_y);
  439. switch(glutButton)
  440. {
  441. case GLUT_RIGHT_BUTTON:
  442. case GLUT_LEFT_BUTTON:
  443. {
  444. switch(glutState)
  445. {
  446. case 1:
  447. // up
  448. glutSetCursor(GLUT_CURSOR_INHERIT);
  449. is_rotating = false;
  450. break;
  451. case 0:
  452. if(!tw_using)
  453. {
  454. push_undo();
  455. glutSetCursor(GLUT_CURSOR_CYCLE);
  456. // collect information for trackball
  457. is_rotating = true;
  458. down_camera = s.cameras[0];
  459. down_x = mouse_x;
  460. down_y = mouse_y;
  461. }
  462. break;
  463. }
  464. break;
  465. #ifdef GLUT_WHEEL_DOWN
  466. // Scroll down
  467. case GLUT_WHEEL_DOWN:
  468. {
  469. mouse_wheel(0,-1,mouse_x,mouse_y);
  470. break;
  471. }
  472. #endif
  473. #ifdef GLUT_WHEEL_UP
  474. // Scroll up
  475. case GLUT_WHEEL_UP:
  476. {
  477. mouse_wheel(0,1,mouse_x,mouse_y);
  478. break;
  479. }
  480. #endif
  481. #ifdef GLUT_WHEEL_LEFT
  482. // Scroll left
  483. case GLUT_WHEEL_LEFT:
  484. {
  485. mouse_wheel(1,-1,mouse_x,mouse_y);
  486. break;
  487. }
  488. #endif
  489. #ifdef GLUT_WHEEL_RIGHT
  490. // Scroll right
  491. case GLUT_WHEEL_RIGHT:
  492. {
  493. mouse_wheel(1,1,mouse_x,mouse_y);
  494. break;
  495. }
  496. #endif
  497. }
  498. }
  499. glutPostRedisplay();
  500. }
  501. void mouse_drag(int mouse_x, int mouse_y)
  502. {
  503. using namespace igl;
  504. using namespace std;
  505. using namespace Eigen;
  506. /*bool tw_using =*/ TwMouseMotion(mouse_x,mouse_y);
  507. if(is_rotating)
  508. {
  509. glutSetCursor(GLUT_CURSOR_CYCLE);
  510. auto & camera = s.cameras[0];
  511. Quaterniond q;
  512. switch(rotation_type)
  513. {
  514. case ROTATION_TYPE_IGL_TRACKBALL:
  515. {
  516. // Rotate according to trackball
  517. igl::trackball(
  518. width, height,
  519. 2.0,
  520. down_camera.m_rotation_conj,
  521. down_x, down_y, mouse_x, mouse_y,
  522. q);
  523. break;
  524. }
  525. case ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP:
  526. {
  527. // Rotate according to two axis valuator with fixed up vector
  528. two_axis_valuator_fixed_up(
  529. width, height,
  530. 2.0,
  531. down_camera.m_rotation_conj,
  532. down_x, down_y, mouse_x, mouse_y,
  533. q);
  534. break;
  535. }
  536. default:
  537. break;
  538. }
  539. switch(center_type)
  540. {
  541. default:
  542. case CENTER_TYPE_ORBIT:
  543. camera.orbit(q.conjugate());
  544. break;
  545. case CENTER_TYPE_FPS:
  546. camera.turn_eye(q.conjugate());
  547. break;
  548. }
  549. }
  550. glutPostRedisplay();
  551. }
  552. void key(unsigned char key, int mouse_x, int mouse_y)
  553. {
  554. using namespace std;
  555. int mod = glutGetModifiers();
  556. switch(key)
  557. {
  558. // ESC
  559. case char(27):
  560. rebar.save(REBAR_NAME);
  561. // ^C
  562. case char(3):
  563. exit(0);
  564. case 'o':
  565. case 'O':
  566. {
  567. s.cameras[0].m_orthographic ^= true;
  568. break;
  569. }
  570. case 'z':
  571. case 'Z':
  572. if(mod & GLUT_ACTIVE_COMMAND)
  573. {
  574. if(mod & GLUT_ACTIVE_SHIFT)
  575. {
  576. redo();
  577. }else
  578. {
  579. undo();
  580. }
  581. break;
  582. }
  583. default:
  584. if(!TwEventKeyboardGLUT(key,mouse_x,mouse_y))
  585. {
  586. cout<<"Unknown key command: "<<key<<" "<<int(key)<<endl;
  587. }
  588. }
  589. glutPostRedisplay();
  590. }
  591. int main(int argc, char * argv[])
  592. {
  593. using namespace std;
  594. using namespace Eigen;
  595. using namespace igl;
  596. // print key commands
  597. cout<<"[Command+Z] Undo."<<endl;
  598. cout<<"[Shift+Command+Z] Redo."<<endl;
  599. cout<<"[^C,ESC] Exit."<<endl;
  600. if(!readOFF("../shared/cheburashka.off",V,F))
  601. {
  602. cerr<<"Failed to read in mesh..."<<endl;
  603. return 1;
  604. }
  605. V.rowwise() -= V.colwise().minCoeff().eval();
  606. per_face_normals(V,F,N);
  607. // Init glut
  608. glutInit(&argc,argv);
  609. if( !TwInit(TW_OPENGL, NULL) )
  610. {
  611. // A fatal error occured
  612. fprintf(stderr, "AntTweakBar initialization failed: %s\n", TwGetLastError());
  613. return 1;
  614. }
  615. // Create a tweak bar
  616. rebar.TwNewBar("bar");
  617. TwDefine("bar label='camera' size='200 550' text=light alpha='200' color='68 68 68'");
  618. TwType RotationTypeTW = ReTwDefineEnumFromString("RotationType","igl_trackball,two_axis_fixed_up");
  619. rebar.TwAddVarRW("rotation_type", RotationTypeTW,&rotation_type,
  620. "keyIncr=] keyDecr=[");
  621. TwType CenterTypeTW = ReTwDefineEnumFromString("CenterType","orbit,fps");
  622. rebar.TwAddVarRW("center_type", CenterTypeTW,&center_type,
  623. "keyIncr={ keyDecr=}");
  624. rebar.TwAddVarRW("rotation", TW_TYPE_QUAT4D,s.cameras[0].m_rotation_conj.coeffs().data(),"");
  625. rebar.load(REBAR_NAME);
  626. init_cameras();
  627. // Init antweakbar
  628. glutInitDisplayString( "rgba depth double samples>=8");
  629. // Top right corner
  630. glutInitWindowSize(glutGet(GLUT_SCREEN_WIDTH)/2.0,glutGet(GLUT_SCREEN_HEIGHT)/2.0);
  631. glutInitWindowPosition(glutGet(GLUT_SCREEN_WIDTH)/2.0,-1);
  632. glutCreateWindow("camera");
  633. glutDisplayFunc(display);
  634. glutReshapeFunc(reshape);
  635. glutKeyboardFunc(key);
  636. glutMouseFunc(mouse);
  637. glutPassiveMotionFunc((GLUTmousemotionfun)TwEventMouseMotionGLUT);
  638. glutMotionFunc(mouse_drag);
  639. glutMainLoop();
  640. return 0;
  641. }