example.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. #include <igl/Viewport.h>
  2. #include <igl/report_gl_error.h>
  3. #include <igl/ReAntTweakBar.h>
  4. #include <igl/trackball.h>
  5. #include <igl/two_axis_valuator_fixed_up.h>
  6. #include <igl/PI.h>
  7. #include <igl/EPS.h>
  8. #include <igl/get_seconds.h>
  9. #include <igl/draw_floor.h>
  10. #include <Eigen/Core>
  11. #include <Eigen/Geometry>
  12. #ifdef WIN32
  13. #include <GL/glut.h>
  14. #else
  15. #include <GLUT/glut.h>
  16. #endif
  17. #include <vector>
  18. #include <stack>
  19. #include <iostream>
  20. class Camera
  21. {
  22. public:
  23. // m_zoom Zoom of camera lens {1}
  24. // m_angle Field of view angle in degrees {45}
  25. // m_aspect Aspect ratio {1}
  26. // m_near near clipping plane {1e-2}
  27. // m_far far clipping plane {100}
  28. // m_at_dist distance of looking at point {1}
  29. // m_rotation Rotation part of rigid transformation of camera {identity}.
  30. // Note that this seems to the inverse of what's stored in the old
  31. // igl::Camera class.
  32. // m_translation Translation part of rigid transformation of camera
  33. // {(0,0,1)}
  34. double m_zoom, m_angle, m_aspect, m_near, m_far, m_at_dist;
  35. Eigen::Quaterniond m_rotation;
  36. Eigen::Vector3d m_translation;
  37. Camera():
  38. m_zoom(1),m_angle(45.0),m_aspect(1),m_near(1e-2),m_far(100),m_at_dist(1),
  39. m_rotation(1,0,0,0),
  40. m_translation(0,0,1)
  41. {
  42. }
  43. // Return projection matrix that takes relative camera coordinates and
  44. // transforms it to viewport coordinates
  45. Eigen::Matrix4d projection() const
  46. {
  47. Eigen::Matrix4d P;
  48. using namespace std;
  49. using namespace igl;
  50. // http://stackoverflow.com/a/3738696/148668
  51. const double yScale = tan(PI*0.5 - 0.5*m_angle*PI/180.);
  52. // http://stackoverflow.com/a/14975139/148668
  53. const double xScale = yScale/m_aspect;
  54. P<<
  55. xScale, 0, 0, 0,
  56. 0, yScale, 0, 0,
  57. 0, 0, -(m_far+m_near)/(m_far-m_near), -1,
  58. 0, 0, -2.*m_near*m_far/(m_far-m_near), 0;
  59. return P.transpose();
  60. }
  61. // Return an Affine transformation that takes a world 3d coordinate and
  62. // transforms it into the relative camera coordinates.
  63. Eigen::Affine3d affine() const
  64. {
  65. using namespace Eigen;
  66. Affine3d t = Affine3d::Identity();
  67. t.rotate(m_rotation);
  68. t.translate(m_translation);
  69. return t;
  70. }
  71. // Returns world coordinates position of center or "eye" of camera.
  72. Eigen::Vector3d eye() const
  73. {
  74. using namespace Eigen;
  75. return affine() * Vector3d(0,0,0);
  76. }
  77. // Returns world coordinate position of a point "eye" is looking at.
  78. Eigen::Vector3d at() const
  79. {
  80. using namespace Eigen;
  81. return affine() * (Vector3d(0,0,-1)*m_at_dist);
  82. }
  83. // Returns world coordinate unit vector of "up" vector
  84. Eigen::Vector3d up() const
  85. {
  86. using namespace Eigen;
  87. Affine3d t = Affine3d::Identity();
  88. t.rotate(m_rotation);
  89. return t * Vector3d(0,1,0);
  90. }
  91. // Move dv in the relative coordinate frame of the camera (move the FPS)
  92. //
  93. // Inputs:
  94. // dv (x,y,z) displacement vector
  95. //
  96. void dolly(const Eigen::Vector3d & dv)
  97. {
  98. m_translation += dv;
  99. }
  100. // "Scale zoom": Move `eye`, but leave `at`
  101. //
  102. // Input:
  103. // s amount to scale distance to at
  104. void push_away(const double s)
  105. {
  106. using namespace Eigen;
  107. #ifndef NDEBUG
  108. Vector3d old_at = camera.at();
  109. #endif
  110. const double old_at_dist = m_at_dist;
  111. m_at_dist = old_at_dist * s;
  112. dolly(Vector3d(0,0,1)*(m_at_dist - old_at_dist));
  113. assert((old_at-camera.at()).squaredNorm() < DOUBLE_EPS);
  114. }
  115. // Aka "Hitchcock", "Vertigo", "Spielberg" or "Trombone" zoom:
  116. // simultaneously dolly while changing angle so that `at` not only stays
  117. // put in relative coordinates but also projected coordinates. That is
  118. //
  119. // Inputs:
  120. // da change in angle in degrees
  121. void dolly_zoom(const double da)
  122. {
  123. using namespace std;
  124. using namespace Eigen;
  125. #ifndef NDEBUG
  126. Vector3d old_at = camera.at();
  127. #endif
  128. const double old_angle = m_angle;
  129. m_angle += da;
  130. m_angle = min(89.,max(5.,m_angle));
  131. const double s =
  132. (2.*tan(old_angle/2./180.*M_PI)) /
  133. (2.*tan(m_angle/2./180.*M_PI)) ;
  134. const double old_at_dist = m_at_dist;
  135. m_at_dist = old_at_dist * s;
  136. dolly(Vector3d(0,0,1)*(m_at_dist - old_at_dist));
  137. assert((old_at-camera.at()).squaredNorm() < DOUBLE_EPS);
  138. }
  139. // Return top right corner of unit plane in relative coordinates, that is
  140. // (w/2,h/2,1)
  141. Eigen::Vector3d unit_plane() const
  142. {
  143. using namespace igl;
  144. // Distance of center pixel to eye
  145. const double d = 1.0;
  146. const double a = m_aspect;
  147. const double theta = m_angle*PI/180.;
  148. const double w =
  149. 2.*sqrt(-d*d/(a*a*pow(tan(0.5*theta),2.)-1.))*a*tan(0.5*theta);
  150. const double h = w/a;
  151. return Eigen::Vector3d(w*0.5,h*0.5,-d);
  152. }
  153. // Rotate and translate so that camera is situated at "eye" looking at "at"
  154. // with "up" pointing up.
  155. //
  156. // Inputs:
  157. // eye (x,y,z) coordinates of eye position
  158. // at (x,y,z) coordinates of at position
  159. // up (x,y,z) coordinates of up vector
  160. void look_at(
  161. const Eigen::Vector3d & eye,
  162. const Eigen::Vector3d & at,
  163. const Eigen::Vector3d & up)
  164. {
  165. using namespace Eigen;
  166. using namespace std;
  167. using namespace igl;
  168. // http://www.opengl.org/sdk/docs/man2/xhtml/gluLookAt.xml
  169. // Normalize vector from at to eye
  170. Vector3d F = eye-at;
  171. m_at_dist = F.norm();
  172. F.normalize();
  173. // Project up onto plane orthogonal to F and normalize
  174. const Vector3d proj_up = (up-(up.dot(F))*F).normalized();
  175. Quaterniond a,b;
  176. a.setFromTwoVectors(Vector3d(0,0,-1),-F);
  177. b.setFromTwoVectors(a*Vector3d(0,1,0),proj_up);
  178. m_rotation = a*b;
  179. m_translation = m_rotation.conjugate() * eye;
  180. assert( (eye-this->eye()).squaredNorm() < DOUBLE_EPS);
  181. assert((F-(this->eye()-this->at()).normalized()).squaredNorm() <
  182. DOUBLE_EPS);
  183. assert( (at-this->at()).squaredNorm() < DOUBLE_EPS);
  184. assert( (proj_up-this->up()).squaredNorm() < DOUBLE_EPS);
  185. }
  186. };
  187. enum RotationType
  188. {
  189. ROTATION_TYPE_IGL_TRACKBALL = 0,
  190. ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP = 1,
  191. NUM_ROTATION_TYPES = 2,
  192. } rotation_type = ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP;
  193. enum CenterType
  194. {
  195. CENTER_TYPE_ORBIT = 0,
  196. CENTER_TYPE_FPS = 1,
  197. NUM_CENTER_TYPES = 2,
  198. } center_type = CENTER_TYPE_ORBIT;
  199. int width,height;
  200. #define REBAR_NAME "temp.rbr"
  201. igl::ReTwBar rebar;
  202. struct State
  203. {
  204. int viewing_camera;
  205. std::vector<Camera> cameras;
  206. std::vector<GLuint> tex_ids;
  207. std::vector<GLuint> fbo_ids;
  208. std::vector<GLuint> dfbo_ids;
  209. State():viewing_camera(0),cameras(4),
  210. tex_ids(cameras.size()),
  211. fbo_ids(cameras.size()),
  212. dfbo_ids(cameras.size())
  213. {}
  214. } s;
  215. const Eigen::Vector4d back(1,1,1,1);
  216. std::stack<State> undo_stack;
  217. bool is_rotating = false;
  218. Camera down_camera;
  219. int down_x,down_y;
  220. std::stack<State> redo_stack;
  221. void push_undo()
  222. {
  223. undo_stack.push(s);
  224. // Clear
  225. redo_stack = std::stack<State>();
  226. }
  227. void undo()
  228. {
  229. if(!undo_stack.empty())
  230. {
  231. redo_stack.push(s);
  232. s = undo_stack.top();
  233. undo_stack.pop();
  234. }
  235. }
  236. void redo()
  237. {
  238. if(!redo_stack.empty())
  239. {
  240. undo_stack.push(s);
  241. s = redo_stack.top();
  242. redo_stack.pop();
  243. }
  244. }
  245. void print(const Camera & camera)
  246. {
  247. using namespace std;
  248. cout<<
  249. "rotation: "<<camera.m_rotation.coeffs().transpose()<<endl<<
  250. "translation: "<<camera.m_translation.transpose()<<endl<<
  251. "eye: "<<camera.eye().transpose()<<endl<<
  252. "at: "<<camera.at().transpose()<<endl<<
  253. "up: "<<camera.up().transpose()<<endl<<
  254. endl;
  255. }
  256. void init_cameras()
  257. {
  258. using namespace Eigen;
  259. using namespace std;
  260. s.cameras[0].look_at(
  261. Vector3d(0,0,1),
  262. Vector3d(0,0,0),
  263. Vector3d(0,1,0));
  264. s.cameras[1].look_at(
  265. Vector3d(0,0,-1),
  266. Vector3d(0,0,0),
  267. Vector3d(0,1,0));
  268. s.cameras[2].look_at(
  269. Vector3d(-2,0,0),
  270. Vector3d(0,0,0),
  271. Vector3d(0,1,0));
  272. s.cameras[3].look_at(
  273. Vector3d(3,0,0),
  274. Vector3d(0,0,0),
  275. Vector3d(0,1,0));
  276. }
  277. bool init_render_to_texture(
  278. const int width,
  279. const int height,
  280. GLuint & tex_id,
  281. GLuint & fbo_id,
  282. GLuint & dfbo_id)
  283. {
  284. using namespace igl;
  285. using namespace std;
  286. // Set up a "render-to-texture" frame buffer and texture combo
  287. glDeleteTextures(1,&tex_id);
  288. glDeleteFramebuffersEXT(1,&fbo_id);
  289. glDeleteFramebuffersEXT(1,&dfbo_id);
  290. // http://www.opengl.org/wiki/Framebuffer_Object_Examples#Quick_example.2C_render_to_texture_.282D.29
  291. glGenTextures(1, &tex_id);
  292. glBindTexture(GL_TEXTURE_2D, tex_id);
  293. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  294. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  295. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  296. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  297. //NULL means reserve texture memory, but texels are undefined
  298. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
  299. glBindTexture(GL_TEXTURE_2D, 0);
  300. //-------------------------
  301. glGenFramebuffersEXT(1, &fbo_id);
  302. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo_id);
  303. //Attach 2D texture to this FBO
  304. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, tex_id, 0);
  305. glGenRenderbuffersEXT(1, &dfbo_id);
  306. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, dfbo_id);
  307. glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, width, height);
  308. //-------------------------
  309. //Attach depth buffer to FBO
  310. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, dfbo_id);
  311. //-------------------------
  312. //Does the GPU support current FBO configuration?
  313. GLenum status;
  314. status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
  315. switch(status)
  316. {
  317. case GL_FRAMEBUFFER_COMPLETE_EXT:
  318. break;
  319. default:
  320. return false;
  321. }
  322. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
  323. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  324. return true;
  325. }
  326. void reshape(int width, int height)
  327. {
  328. ::width = width;
  329. ::height = height;
  330. glViewport(0,0,width,height);
  331. // Send the new window size to AntTweakBar
  332. TwWindowSize(width, height);
  333. }
  334. void draw_scene(const Camera & v_camera,
  335. const bool render_to_texture,
  336. const GLuint & v_tex_id,
  337. const GLuint & v_fbo_id,
  338. const GLuint & v_dfbo_id)
  339. {
  340. using namespace igl;
  341. using namespace std;
  342. using namespace Eigen;
  343. if(render_to_texture)
  344. {
  345. // render to framebuffer
  346. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, v_fbo_id);
  347. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, v_dfbo_id);
  348. glClearColor(back(0),back(1),back(2),1);
  349. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  350. }
  351. glMatrixMode(GL_PROJECTION);
  352. glPushMatrix();
  353. glLoadIdentity();
  354. //gluPerspective(v_camera.m_angle,v_camera.m_aspect,v_camera.m_near,v_camera.m_far);
  355. glMultMatrixd(v_camera.projection().data());
  356. glMatrixMode(GL_MODELVIEW);
  357. glPushMatrix();
  358. glLoadIdentity();
  359. gluLookAt(
  360. v_camera.eye()(0), v_camera.eye()(1), v_camera.eye()(2),
  361. v_camera.at()(0), v_camera.at()(1), v_camera.at()(2),
  362. v_camera.up()(0), v_camera.up()(1), v_camera.up()(2));
  363. for(int c = 0;c<(int)s.cameras.size();c++)
  364. {
  365. auto & camera = s.cameras[c];
  366. if(&v_camera == &camera)
  367. {
  368. continue;
  369. }
  370. // draw camera
  371. glPushMatrix();
  372. glMultMatrixd(camera.affine().matrix().data());
  373. // eye
  374. glColor4f(0,0,0,1);
  375. glPointSize(10.f);
  376. glBegin(GL_POINTS);
  377. glVertex3f(0,0,0);
  378. glEnd();
  379. // frustrum
  380. const Vector3d u = camera.unit_plane();
  381. glBegin(GL_LINES);
  382. for(int x = -1;x<=1;x+=2)
  383. {
  384. for(int y = -1;y<=1;y+=2)
  385. {
  386. glVertex3f(0,0,0);
  387. glVertex3f(x*u(0),y*u(1),u(2));
  388. }
  389. }
  390. glEnd();
  391. const Vector3d n = u*(camera.m_near-FLOAT_EPS);
  392. glBegin(GL_QUADS);
  393. glVertex3f( n(0),-n(1),n(2));
  394. glVertex3f(-n(0),-n(1),n(2));
  395. glVertex3f(-n(0), n(1),n(2));
  396. glVertex3f( n(0), n(1),n(2));
  397. glEnd();
  398. for(int pass = 0;pass<2;pass++)
  399. {
  400. switch(pass)
  401. {
  402. case 1:
  403. glColor4f(1,1,1,0.5);
  404. glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
  405. //glEnable(GL_BLEND);
  406. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  407. glEnable(GL_TEXTURE_2D);
  408. glBindTexture(GL_TEXTURE_2D,s.tex_ids[c]);
  409. break;
  410. default:
  411. case 0:
  412. glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
  413. glColor4f(0,0,0,1);
  414. break;
  415. }
  416. glBegin(GL_QUADS);
  417. glTexCoord2d(1,0);
  418. glVertex3f( 0.5*u(0),-0.5*u(1),0.5*u(2));
  419. glTexCoord2d(0,0);
  420. glVertex3f(-0.5*u(0),-0.5*u(1),0.5*u(2));
  421. glTexCoord2d(0,1);
  422. glVertex3f(-0.5*u(0), 0.5*u(1),0.5*u(2));
  423. glTexCoord2d(1,1);
  424. glVertex3f( 0.5*u(0), 0.5*u(1),0.5*u(2));
  425. glEnd();
  426. switch(pass)
  427. {
  428. case 1:
  429. glBindTexture(GL_TEXTURE_2D, 0);
  430. glDisable(GL_TEXTURE_2D);
  431. glDisable(GL_BLEND);
  432. break;
  433. default:
  434. break;
  435. }
  436. }
  437. glPopMatrix();
  438. }
  439. glDisable(GL_LIGHTING);
  440. glEnable(GL_COLOR_MATERIAL);
  441. glLineWidth(3.f);
  442. glColor4f(1,0,1,1);
  443. glutWireCube(0.25);
  444. glColor4f(1,0.5,0.5,1);
  445. //glutWireSphere(0.125,20,20);
  446. {
  447. glPushMatrix();
  448. glTranslated(0,-1,0);
  449. draw_floor();
  450. glPopMatrix();
  451. }
  452. // Axes
  453. for(int d = 0;d<3;d++)
  454. {
  455. glColor4f(d==0,d==1,d==2,1);
  456. glBegin(GL_LINES);
  457. glVertex3f(0,0,0);
  458. glVertex3f(d==0,d==1,d==2);
  459. glEnd();
  460. }
  461. glMatrixMode(GL_PROJECTION);
  462. glPopMatrix();
  463. glMatrixMode(GL_MODELVIEW);
  464. glPopMatrix();
  465. report_gl_error();
  466. if(render_to_texture)
  467. {
  468. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  469. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
  470. }
  471. }
  472. void display()
  473. {
  474. using namespace igl;
  475. using namespace std;
  476. using namespace Eigen;
  477. glClearColor(back(0),back(1),back(2),0);
  478. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  479. glEnable(GL_DEPTH_TEST);
  480. // Update aspect ratios (may have changed since undo/redo)
  481. {
  482. const double aspect = (double)width/(double)height;
  483. for(int c = 0;c<(int)s.cameras.size();c++)
  484. {
  485. auto & camera = s.cameras[c];
  486. auto & tex_id = s.tex_ids[c];
  487. auto & fbo_id = s.fbo_ids[c];
  488. auto & dfbo_id = s.dfbo_ids[c];
  489. if(aspect != camera.m_aspect)
  490. {
  491. cout<<"Initializing camera #"<<c<<"..."<<endl;
  492. camera.m_aspect = aspect;
  493. bool ret = init_render_to_texture(width,height,tex_id,fbo_id,dfbo_id);
  494. assert(ret);
  495. }
  496. draw_scene(camera,true,tex_id,fbo_id,dfbo_id);
  497. }
  498. }
  499. {
  500. auto & camera = s.cameras[s.viewing_camera];
  501. draw_scene(camera,false,0,0,0);
  502. }
  503. TwDraw();
  504. glutSwapBuffers();
  505. glutPostRedisplay();
  506. }
  507. void mouse_wheel(int wheel, int direction, int mouse_x, int mouse_y)
  508. {
  509. using namespace std;
  510. using namespace igl;
  511. using namespace Eigen;
  512. GLint viewport[4];
  513. glGetIntegerv(GL_VIEWPORT,viewport);
  514. if(wheel == 0 && TwMouseMotion(mouse_x, viewport[3] - mouse_y))
  515. {
  516. static double mouse_scroll_y = 0;
  517. const double delta_y = 0.125*direction;
  518. mouse_scroll_y += delta_y;
  519. TwMouseWheel(mouse_scroll_y);
  520. }
  521. auto & camera = s.cameras[s.viewing_camera];
  522. switch(center_type)
  523. {
  524. case CENTER_TYPE_ORBIT:
  525. if(wheel==0)
  526. {
  527. // factor of zoom change
  528. double s = (1.-0.01*direction);
  529. //// FOV zoom: just widen angle. This is hardly ever appropriate.
  530. //camera.m_angle *= s;
  531. //camera.m_angle = min(max(camera.m_angle,1),89);
  532. camera.push_away(s);
  533. // Rotation should stay around at
  534. }else
  535. {
  536. // Dolly zoom:
  537. camera.dolly_zoom((double)direction*1.0);
  538. }
  539. break;
  540. default:
  541. case CENTER_TYPE_FPS:
  542. // Move `eye` and `at`
  543. Vector3d diff = (wheel==0?Vector3d(0,0,1):Vector3d(-1,0,0))*0.1;
  544. camera.dolly(diff*direction);
  545. break;
  546. }
  547. }
  548. void mouse(int glutButton, int glutState, int mouse_x, int mouse_y)
  549. {
  550. using namespace std;
  551. using namespace Eigen;
  552. using namespace igl;
  553. bool tw_using = TwEventMouseButtonGLUT(glutButton,glutState,mouse_x,mouse_y);
  554. switch(glutButton)
  555. {
  556. case GLUT_RIGHT_BUTTON:
  557. case GLUT_LEFT_BUTTON:
  558. {
  559. switch(glutState)
  560. {
  561. case 1:
  562. // up
  563. glutSetCursor(GLUT_CURSOR_INHERIT);
  564. is_rotating = false;
  565. break;
  566. case 0:
  567. if(!tw_using)
  568. {
  569. push_undo();
  570. glutSetCursor(GLUT_CURSOR_CYCLE);
  571. // collect information for trackball
  572. is_rotating = true;
  573. down_camera = s.cameras[s.viewing_camera];
  574. down_x = mouse_x;
  575. down_y = mouse_y;
  576. }
  577. break;
  578. }
  579. break;
  580. // Scroll down
  581. case GLUT_WHEEL_DOWN:
  582. {
  583. mouse_wheel(0,-1,mouse_x,mouse_y);
  584. break;
  585. }
  586. // Scroll up
  587. case GLUT_WHEEL_UP:
  588. {
  589. mouse_wheel(0,1,mouse_x,mouse_y);
  590. break;
  591. }
  592. // Scroll left
  593. case GLUT_WHEEL_LEFT:
  594. {
  595. mouse_wheel(1,-1,mouse_x,mouse_y);
  596. break;
  597. }
  598. // Scroll right
  599. case GLUT_WHEEL_RIGHT:
  600. {
  601. mouse_wheel(1,1,mouse_x,mouse_y);
  602. break;
  603. }
  604. }
  605. }
  606. }
  607. void mouse_drag(int mouse_x, int mouse_y)
  608. {
  609. using namespace igl;
  610. using namespace std;
  611. using namespace Eigen;
  612. /*bool tw_using =*/ TwMouseMotion(mouse_x,mouse_y);
  613. if(is_rotating)
  614. {
  615. glutSetCursor(GLUT_CURSOR_CYCLE);
  616. auto & camera = s.cameras[s.viewing_camera];
  617. Quaterniond q;
  618. switch(rotation_type)
  619. {
  620. case ROTATION_TYPE_IGL_TRACKBALL:
  621. {
  622. // Rotate according to trackball
  623. igl::trackball(
  624. width,
  625. height,
  626. 2.0,
  627. down_camera.m_rotation.conjugate(),
  628. down_x,
  629. down_y,
  630. mouse_x,
  631. mouse_y,
  632. q);
  633. break;
  634. }
  635. case ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP:
  636. {
  637. two_axis_valuator_fixed_up(
  638. width,
  639. height,
  640. 2.0,
  641. down_camera.m_rotation.conjugate(),
  642. down_x,
  643. down_y,
  644. mouse_x,
  645. mouse_y,
  646. q);
  647. break;
  648. }
  649. default:
  650. break;
  651. }
  652. camera.m_rotation = q.conjugate();
  653. switch(center_type)
  654. {
  655. default:
  656. case CENTER_TYPE_ORBIT:
  657. // at should be fixed
  658. //
  659. // at_1 = R_1 * t_1 - R_1 * z = at_0
  660. // t_1 = R_1' * (at_0 + R_1 * z)
  661. camera.m_translation =
  662. camera.m_rotation.conjugate() *
  663. (down_camera.at() +
  664. camera.m_rotation * Vector3d(0,0,1) * camera.m_at_dist);
  665. assert((down_camera.at() - camera.at()).squaredNorm() < DOUBLE_EPS);
  666. break;
  667. case CENTER_TYPE_FPS:
  668. // eye should be fixed
  669. //
  670. // eye_1 = R_1 * t_1 = eye_0
  671. // t_1 = R_1' * eye_0
  672. camera.m_translation = camera.m_rotation.conjugate() * down_camera.eye();
  673. assert((down_camera.eye() - camera.eye()).squaredNorm() < DOUBLE_EPS);
  674. break;
  675. }
  676. }
  677. }
  678. void key(unsigned char key, int mouse_x, int mouse_y)
  679. {
  680. using namespace std;
  681. int mod = glutGetModifiers();
  682. switch(key)
  683. {
  684. // ESC
  685. case char(27):
  686. rebar.save(REBAR_NAME);
  687. // ^C
  688. case char(3):
  689. exit(0);
  690. case 'z':
  691. case 'Z':
  692. if(mod & GLUT_ACTIVE_COMMAND)
  693. {
  694. if(mod & GLUT_ACTIVE_SHIFT)
  695. {
  696. redo();
  697. }else
  698. {
  699. undo();
  700. }
  701. break;
  702. }
  703. default:
  704. if(!TwEventKeyboardGLUT(key,mouse_x,mouse_y))
  705. {
  706. cout<<"Unknown key command: "<<key<<" "<<int(key)<<endl;
  707. }
  708. }
  709. }
  710. int main(int argc, char * argv[])
  711. {
  712. using namespace std;
  713. using namespace Eigen;
  714. using namespace igl;
  715. // print key commands
  716. cout<<"[Command+Z] Undo."<<endl;
  717. cout<<"[Shift+Command+Z] Redo."<<endl;
  718. cout<<"[^C,ESC] Exit."<<endl;
  719. // Init glut
  720. glutInit(&argc,argv);
  721. if( !TwInit(TW_OPENGL, NULL) )
  722. {
  723. // A fatal error occured
  724. fprintf(stderr, "AntTweakBar initialization failed: %s\n", TwGetLastError());
  725. return 1;
  726. }
  727. // Create a tweak bar
  728. rebar.TwNewBar("bar");
  729. TwDefine("bar label='camera' size='200 550' text=light alpha='200' color='68 68 68'");
  730. TwType RotationTypeTW = ReTwDefineEnumFromString("RotationType","igl_trackball,two_axis_fixed_up");
  731. rebar.TwAddVarRW("rotation_type", RotationTypeTW,&rotation_type,
  732. "keyIncr=] keyDecr=[");
  733. TwType CenterTypeTW = ReTwDefineEnumFromString("CenterType","orbit,fps");
  734. rebar.TwAddVarRW("center_type", CenterTypeTW,&center_type,
  735. "keyIncr={ keyDecr=}");
  736. rebar.load(REBAR_NAME);
  737. init_cameras();
  738. // Init antweakbar
  739. glutInitDisplayString( "rgba depth double samples>=8");
  740. // Top right corner
  741. glutInitWindowSize(glutGet(GLUT_SCREEN_WIDTH)/2.0,glutGet(GLUT_SCREEN_HEIGHT)/2.0);
  742. glutInitWindowPosition(glutGet(GLUT_SCREEN_WIDTH)/2.0,-1);
  743. glutCreateWindow("camera");
  744. glutDisplayFunc(display);
  745. glutReshapeFunc(reshape);
  746. glutKeyboardFunc(key);
  747. glutMouseFunc(mouse);
  748. glutPassiveMotionFunc((GLUTmousemotionfun)TwEventMouseMotionGLUT);
  749. glutMotionFunc(mouse_drag);
  750. glutMainLoop();
  751. return 0;
  752. }