example.cpp 17 KB

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