example.cpp 17 KB

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