example.cpp 16 KB

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