example.cpp 17 KB

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