example.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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. for(int c = 0;c<(int)s.cameras.size();c++)
  231. {
  232. auto & camera = s.cameras[c];
  233. if(&v_camera == &camera)
  234. {
  235. continue;
  236. }
  237. // draw camera
  238. glPushMatrix();
  239. glMultMatrixd(camera.affine().matrix().data());
  240. // eye
  241. glColor4f(0,0,0,1);
  242. glPointSize(10.f);
  243. glBegin(GL_POINTS);
  244. glVertex3f(0,0,0);
  245. glEnd();
  246. // frustrum
  247. const Vector3d u = camera.unit_plane();
  248. glBegin(GL_LINES);
  249. for(int x = -1;x<=1;x+=2)
  250. {
  251. for(int y = -1;y<=1;y+=2)
  252. {
  253. glVertex3f(0,0,0);
  254. glVertex3f(x*u(0),y*u(1),u(2));
  255. }
  256. }
  257. glEnd();
  258. const Vector3d n = u*(camera.m_near-FLOAT_EPS);
  259. glBegin(GL_QUADS);
  260. glVertex3f( n(0),-n(1),n(2));
  261. glVertex3f(-n(0),-n(1),n(2));
  262. glVertex3f(-n(0), n(1),n(2));
  263. glVertex3f( n(0), n(1),n(2));
  264. glEnd();
  265. for(int pass = 0;pass<2;pass++)
  266. {
  267. switch(pass)
  268. {
  269. case 1:
  270. glColor4f(1,1,1,0.5);
  271. glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
  272. //glEnable(GL_BLEND);
  273. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  274. glEnable(GL_TEXTURE_2D);
  275. glBindTexture(GL_TEXTURE_2D,s.tex_ids[c]);
  276. break;
  277. default:
  278. case 0:
  279. glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
  280. glColor4f(0,0,0,1);
  281. break;
  282. }
  283. glBegin(GL_QUADS);
  284. glTexCoord2d(1,0);
  285. glVertex3f( 0.5*u(0),-0.5*u(1),0.5*u(2));
  286. glTexCoord2d(0,0);
  287. glVertex3f(-0.5*u(0),-0.5*u(1),0.5*u(2));
  288. glTexCoord2d(0,1);
  289. glVertex3f(-0.5*u(0), 0.5*u(1),0.5*u(2));
  290. glTexCoord2d(1,1);
  291. glVertex3f( 0.5*u(0), 0.5*u(1),0.5*u(2));
  292. glEnd();
  293. switch(pass)
  294. {
  295. case 1:
  296. glBindTexture(GL_TEXTURE_2D, 0);
  297. glDisable(GL_TEXTURE_2D);
  298. glDisable(GL_BLEND);
  299. break;
  300. default:
  301. break;
  302. }
  303. }
  304. glPopMatrix();
  305. }
  306. // Set material properties
  307. lights();
  308. glEnable(GL_LIGHTING);
  309. glDisable(GL_COLOR_MATERIAL);
  310. glMaterialfv(GL_FRONT, GL_AMBIENT, GOLD_AMBIENT);
  311. glMaterialfv(GL_FRONT, GL_DIFFUSE, GOLD_DIFFUSE );
  312. glMaterialfv(GL_FRONT, GL_SPECULAR, GOLD_SPECULAR);
  313. glMaterialf (GL_FRONT, GL_SHININESS, 128);
  314. glMaterialfv(GL_BACK, GL_AMBIENT, SILVER_AMBIENT);
  315. glMaterialfv(GL_BACK, GL_DIFFUSE, FAST_GREEN_DIFFUSE );
  316. glMaterialfv(GL_BACK, GL_SPECULAR, SILVER_SPECULAR);
  317. glMaterialf (GL_BACK, GL_SHININESS, 128);
  318. draw_mesh(V,F,N);
  319. glDisable(GL_LIGHTING);
  320. glEnable(GL_COLOR_MATERIAL);
  321. //glLineWidth(3.f);
  322. //glColor4f(1,0,1,1);
  323. //glutWireCube(0.25);
  324. //glColor4f(1,0.5,0.5,1);
  325. ////glutWireSphere(0.125,20,20);
  326. {
  327. glPushMatrix();
  328. glTranslated(0,-1,0);
  329. draw_floor();
  330. glPopMatrix();
  331. }
  332. // Axes
  333. for(int d = 0;d<3;d++)
  334. {
  335. glColor4f(d==0,d==1,d==2,1);
  336. glBegin(GL_LINES);
  337. glVertex3f(0,0,0);
  338. glVertex3f(d==0,d==1,d==2);
  339. glEnd();
  340. }
  341. glMatrixMode(GL_PROJECTION);
  342. glPopMatrix();
  343. glMatrixMode(GL_MODELVIEW);
  344. glPopMatrix();
  345. report_gl_error();
  346. if(render_to_texture)
  347. {
  348. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  349. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
  350. }
  351. }
  352. void display()
  353. {
  354. using namespace igl;
  355. using namespace std;
  356. using namespace Eigen;
  357. glClearColor(back(0),back(1),back(2),0);
  358. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  359. glEnable(GL_DEPTH_TEST);
  360. // Update aspect ratios (may have changed since undo/redo)
  361. {
  362. const double aspect = (double)width/(double)height;
  363. for(int c = 0;c<(int)s.cameras.size();c++)
  364. {
  365. auto & camera = s.cameras[c];
  366. auto & tex_id = s.tex_ids[c];
  367. auto & fbo_id = s.fbo_ids[c];
  368. auto & dfbo_id = s.dfbo_ids[c];
  369. if(aspect != camera.m_aspect)
  370. {
  371. cout<<"Initializing camera #"<<c<<"..."<<endl;
  372. camera.m_aspect = aspect;
  373. bool ret = init_render_to_texture(width,height,tex_id,fbo_id,dfbo_id);
  374. assert(ret);
  375. }
  376. draw_scene(camera,true,tex_id,fbo_id,dfbo_id);
  377. }
  378. }
  379. {
  380. auto & camera = s.cameras[0];
  381. draw_scene(camera,false,0,0,0);
  382. }
  383. TwDraw();
  384. glutSwapBuffers();
  385. }
  386. void mouse_wheel(int wheel, int direction, int mouse_x, int mouse_y)
  387. {
  388. using namespace std;
  389. using namespace igl;
  390. using namespace Eigen;
  391. GLint viewport[4];
  392. glGetIntegerv(GL_VIEWPORT,viewport);
  393. if(wheel == 0 && TwMouseMotion(mouse_x, viewport[3] - mouse_y))
  394. {
  395. static double mouse_scroll_y = 0;
  396. const double delta_y = 0.125*direction;
  397. mouse_scroll_y += delta_y;
  398. TwMouseWheel(mouse_scroll_y);
  399. return;
  400. }
  401. auto & camera = s.cameras[0];
  402. switch(center_type)
  403. {
  404. case CENTER_TYPE_ORBIT:
  405. if(wheel==0)
  406. {
  407. // factor of zoom change
  408. double s = (1.-0.01*direction);
  409. //// FOV zoom: just widen angle. This is hardly ever appropriate.
  410. //camera.m_angle *= s;
  411. //camera.m_angle = min(max(camera.m_angle,1),89);
  412. camera.push_away(s);
  413. }else
  414. {
  415. // Dolly zoom:
  416. camera.dolly_zoom((double)direction*1.0);
  417. }
  418. break;
  419. default:
  420. case CENTER_TYPE_FPS:
  421. // Move `eye` and `at`
  422. camera.dolly((wheel==0?Vector3d(0,0,1):Vector3d(-1,0,0))*0.1*direction);
  423. break;
  424. }
  425. glutPostRedisplay();
  426. }
  427. void mouse(int glutButton, int glutState, int mouse_x, int mouse_y)
  428. {
  429. using namespace std;
  430. using namespace Eigen;
  431. using namespace igl;
  432. bool tw_using = TwEventMouseButtonGLUT(glutButton,glutState,mouse_x,mouse_y);
  433. switch(glutButton)
  434. {
  435. case GLUT_RIGHT_BUTTON:
  436. case GLUT_LEFT_BUTTON:
  437. {
  438. switch(glutState)
  439. {
  440. case 1:
  441. // up
  442. glutSetCursor(GLUT_CURSOR_INHERIT);
  443. is_rotating = false;
  444. break;
  445. case 0:
  446. if(!tw_using)
  447. {
  448. push_undo();
  449. glutSetCursor(GLUT_CURSOR_CYCLE);
  450. // collect information for trackball
  451. is_rotating = true;
  452. down_camera = s.cameras[0];
  453. down_x = mouse_x;
  454. down_y = mouse_y;
  455. }
  456. break;
  457. }
  458. break;
  459. }
  460. #ifdef GLUT_WHEEL_DOWN
  461. // Scroll down
  462. case GLUT_WHEEL_DOWN:
  463. {
  464. mouse_wheel(0,-1,mouse_x,mouse_y);
  465. break;
  466. }
  467. #endif
  468. #ifdef GLUT_WHEEL_UP
  469. // Scroll up
  470. case GLUT_WHEEL_UP:
  471. {
  472. mouse_wheel(0,1,mouse_x,mouse_y);
  473. break;
  474. }
  475. #endif
  476. #ifdef GLUT_WHEEL_LEFT
  477. // Scroll left
  478. case GLUT_WHEEL_LEFT:
  479. {
  480. mouse_wheel(1,-1,mouse_x,mouse_y);
  481. break;
  482. }
  483. #endif
  484. #ifdef GLUT_WHEEL_RIGHT
  485. // Scroll right
  486. case GLUT_WHEEL_RIGHT:
  487. {
  488. mouse_wheel(1,1,mouse_x,mouse_y);
  489. break;
  490. }
  491. #endif
  492. }
  493. glutPostRedisplay();
  494. }
  495. void mouse_drag(int mouse_x, int mouse_y)
  496. {
  497. using namespace igl;
  498. using namespace std;
  499. using namespace Eigen;
  500. /*bool tw_using =*/ TwMouseMotion(mouse_x,mouse_y);
  501. if(is_rotating)
  502. {
  503. glutSetCursor(GLUT_CURSOR_CYCLE);
  504. auto & camera = s.cameras[0];
  505. Quaterniond q;
  506. switch(rotation_type)
  507. {
  508. case ROTATION_TYPE_IGL_TRACKBALL:
  509. {
  510. // Rotate according to trackball
  511. igl::trackball(
  512. width, height,
  513. 2.0,
  514. down_camera.m_rotation_conj,
  515. down_x, down_y, mouse_x, mouse_y,
  516. q);
  517. break;
  518. }
  519. case ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP:
  520. {
  521. // Rotate according to two axis valuator with fixed up vector
  522. two_axis_valuator_fixed_up(
  523. width, height,
  524. 2.0,
  525. down_camera.m_rotation_conj,
  526. down_x, down_y, mouse_x, mouse_y,
  527. q);
  528. break;
  529. }
  530. default:
  531. break;
  532. }
  533. switch(center_type)
  534. {
  535. default:
  536. case CENTER_TYPE_ORBIT:
  537. camera.orbit(q.conjugate());
  538. break;
  539. case CENTER_TYPE_FPS:
  540. camera.turn_eye(q.conjugate());
  541. break;
  542. }
  543. }
  544. glutPostRedisplay();
  545. }
  546. void key(unsigned char key, int mouse_x, int mouse_y)
  547. {
  548. using namespace std;
  549. int mod = glutGetModifiers();
  550. switch(key)
  551. {
  552. // ESC
  553. case char(27):
  554. rebar.save(REBAR_NAME);
  555. // ^C
  556. case char(3):
  557. exit(0);
  558. case 'o':
  559. case 'O':
  560. {
  561. s.cameras[0].m_orthographic ^= true;
  562. break;
  563. }
  564. case 'z':
  565. case 'Z':
  566. if(mod & GLUT_ACTIVE_COMMAND)
  567. {
  568. if(mod & GLUT_ACTIVE_SHIFT)
  569. {
  570. redo();
  571. }else
  572. {
  573. undo();
  574. }
  575. break;
  576. }
  577. default:
  578. if(!TwEventKeyboardGLUT(key,mouse_x,mouse_y))
  579. {
  580. cout<<"Unknown key command: "<<key<<" "<<int(key)<<endl;
  581. }
  582. }
  583. glutPostRedisplay();
  584. }
  585. int main(int argc, char * argv[])
  586. {
  587. using namespace std;
  588. using namespace Eigen;
  589. using namespace igl;
  590. // print key commands
  591. cout<<"[Command+Z] Undo."<<endl;
  592. cout<<"[Shift+Command+Z] Redo."<<endl;
  593. cout<<"[^C,ESC] Exit."<<endl;
  594. if(!readOFF("../shared/cheburashka.off",V,F))
  595. {
  596. cerr<<"Failed to read in mesh..."<<endl;
  597. return 1;
  598. }
  599. V.rowwise() -= V.colwise().minCoeff().eval();
  600. per_face_normals(V,F,N);
  601. // Init glut
  602. glutInit(&argc,argv);
  603. if( !TwInit(TW_OPENGL, NULL) )
  604. {
  605. // A fatal error occured
  606. fprintf(stderr, "AntTweakBar initialization failed: %s\n", TwGetLastError());
  607. return 1;
  608. }
  609. // Create a tweak bar
  610. rebar.TwNewBar("bar");
  611. TwDefine("bar label='camera' size='200 550' text=light alpha='200' color='68 68 68'");
  612. TwType RotationTypeTW = ReTwDefineEnumFromString("RotationType","igl_trackball,two_axis_fixed_up");
  613. rebar.TwAddVarRW("rotation_type", RotationTypeTW,&rotation_type,
  614. "keyIncr=] keyDecr=[");
  615. TwType CenterTypeTW = ReTwDefineEnumFromString("CenterType","orbit,fps");
  616. rebar.TwAddVarRW("center_type", CenterTypeTW,&center_type,
  617. "keyIncr={ keyDecr=}");
  618. rebar.TwAddVarRW("rotation", TW_TYPE_QUAT4D,s.cameras[0].m_rotation_conj.coeffs().data(),"");
  619. rebar.load(REBAR_NAME);
  620. init_cameras();
  621. // Init antweakbar
  622. glutInitDisplayString( "rgba depth double samples>=8");
  623. // Top right corner
  624. glutInitWindowSize(glutGet(GLUT_SCREEN_WIDTH)/2.0,glutGet(GLUT_SCREEN_HEIGHT)/2.0);
  625. glutInitWindowPosition(glutGet(GLUT_SCREEN_WIDTH)/2.0,-1);
  626. glutCreateWindow("camera");
  627. glutDisplayFunc(display);
  628. glutReshapeFunc(reshape);
  629. glutKeyboardFunc(key);
  630. glutMouseFunc(mouse);
  631. glutPassiveMotionFunc((GLUTmousemotionfun)TwEventMouseMotionGLUT);
  632. glutMotionFunc(mouse_drag);
  633. glutMainLoop();
  634. return 0;
  635. }