example.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. // Small GLUT application to test shadow mapping for closed shapes
  2. //
  3. #include <igl/readOBJ.h>
  4. #include <igl/writeOBJ.h>
  5. #include <igl/writeOFF.h>
  6. #include <igl/readWRL.h>
  7. #include <igl/report_gl_error.h>
  8. #include <igl/polygon_mesh_to_triangle_mesh.h>
  9. #include <igl/readOFF.h>
  10. #include <igl/readMESH.h>
  11. #include <igl/draw_mesh.h>
  12. #include <igl/draw_floor.h>
  13. #include <igl/pathinfo.h>
  14. #include <igl/list_to_matrix.h>
  15. #include <igl/quat_to_mat.h>
  16. #include <igl/per_face_normals.h>
  17. #include <igl/material_colors.h>
  18. #include <igl/trackball.h>
  19. #include <igl/snap_to_canonical_view_quat.h>
  20. #include <igl/REDRUM.h>
  21. #include <igl/ReAntTweakBar.h>
  22. #include <igl/get_seconds.h>
  23. #include <igl/jet.h>
  24. #include <igl/randperm.h>
  25. #include <igl/normalize_row_lengths.h>
  26. #include <igl/boost/components.h>
  27. #include <igl/boost/bfs_orient.h>
  28. #include <igl/orient_outward.h>
  29. //#include <igl/embree/orient_outward_ao.h>
  30. #include <igl/unique_simplices.h>
  31. #include <igl/C_STR.h>
  32. #include <igl/write_triangle_mesh.h>
  33. #include <Eigen/Core>
  34. #include <Eigen/Geometry>
  35. #if defined(__APPLE__)
  36. #include <GLUT/glut.h>
  37. #else
  38. #include <GL/glut.h>
  39. #endif
  40. #ifndef GLUT_WHEEL_UP
  41. #define GLUT_WHEEL_UP 3
  42. #endif
  43. #ifndef GLUT_WHEEL_DOWN
  44. #define GLUT_WHEEL_DOWN 4
  45. #endif
  46. #ifndef GLUT_WHEEL_RIGHT
  47. #define GLUT_WHEEL_RIGHT 5
  48. #endif
  49. #ifndef GLUT_WHEEL_LEFT
  50. #define GLUT_WHEEL_LEFT 6
  51. #endif
  52. #ifndef GLUT_ACTIVE_COMMAND
  53. #define GLUT_ACTIVE_COMMAND 8
  54. #endif
  55. #include <ctime>
  56. #include <string>
  57. #include <vector>
  58. #include <stack>
  59. #include <iostream>
  60. struct Camera
  61. {
  62. Eigen::Vector3d pan;
  63. Eigen::Quaterniond rotation;
  64. double zoom;
  65. double angle;
  66. Camera():pan(0,0,0),rotation(1,0,0,0),zoom(1),angle(25){}
  67. };
  68. // Initialize shadow textures. Should be called on reshape()
  69. //
  70. // Inputs:
  71. // windowWidth width of viewport
  72. // windowHeight width of viewport
  73. // factor up-/down-sampling factor {1}
  74. // Outputs:
  75. // shadowMapTexture texture id of shadow map
  76. // fbo buffer id of depth frame buffer
  77. // cfbo buffer id of color frame buffer
  78. bool initialize_shadows(
  79. const double windowWidth,
  80. const double windowHeight,
  81. const double factor,
  82. GLuint & shadowMapTexture,
  83. GLuint & fbo,
  84. GLuint & cfbo);
  85. // Implementation
  86. bool initialize_shadows(
  87. const double windowWidth,
  88. const double windowHeight,
  89. const double factor,
  90. GLuint & shadowMapTexture,
  91. GLuint & fbo,
  92. GLuint & cfbo)
  93. {
  94. //Create the shadow map texture
  95. glDeleteTextures(1,&shadowMapTexture);
  96. glGenTextures(1, &shadowMapTexture);
  97. glBindTexture(GL_TEXTURE_2D, shadowMapTexture);
  98. glTexImage2D(
  99. GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT,
  100. factor*windowWidth,
  101. factor*windowHeight,
  102. 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);
  103. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  104. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  105. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  106. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  107. glBindTexture(GL_TEXTURE_2D, 0);
  108. // Frame buffer
  109. glGenFramebuffersEXT(1, &fbo);
  110. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
  111. glFramebufferTexture2DEXT(
  112. GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D,
  113. shadowMapTexture,0);
  114. // Attach color render buffer
  115. glGenRenderbuffersEXT(1,&cfbo);
  116. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT,cfbo);
  117. glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA8,
  118. factor*windowWidth, factor*windowHeight);
  119. //-------------------------
  120. //Attach color buffer to FBO
  121. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
  122. GL_RENDERBUFFER_EXT, cfbo);
  123. //-------------------------
  124. //Does the GPU support current FBO configuration?
  125. GLenum status;
  126. status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
  127. switch(status)
  128. {
  129. case GL_FRAMEBUFFER_COMPLETE_EXT:
  130. break;
  131. default:
  132. assert(false);
  133. return false;
  134. }
  135. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
  136. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  137. return true;
  138. }
  139. GLuint shadowMapTexture=0,fbo=0,cfbo=0;
  140. #define MAX_SPEED 0.01
  141. struct Mesh
  142. {
  143. Eigen::MatrixXd V,N;
  144. Eigen::MatrixXi F;
  145. Eigen::Affine3d a,da;
  146. Mesh():V(),N(),F(),a(Eigen::Affine3d::Identity()),da(Eigen::Affine3d::Identity())
  147. {
  148. using namespace Eigen;
  149. Quaterniond r(Eigen::Vector4d::Random());
  150. Quaterniond i(1,0,0,0);
  151. const double speed = 0.001;
  152. Quaterniond q = Quaterniond(( speed)*r.coeffs() + (1.-speed)*i.coeffs()).normalized();
  153. da.rotate(q);
  154. da.translate(Eigen::Vector3d::Random().normalized()*MAX_SPEED);
  155. }
  156. };
  157. std::vector<Mesh> meshes;
  158. struct State
  159. {
  160. ::Camera camera;
  161. State():
  162. camera()
  163. {
  164. camera.pan[1] = 1;
  165. }
  166. } s;
  167. // See README for descriptions
  168. enum RotationType
  169. {
  170. ROTATION_TYPE_IGL_TRACKBALL = 0,
  171. ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP = 1,
  172. NUM_ROTATION_TYPES = 2,
  173. } rotation_type;
  174. std::stack<State> undo_stack;
  175. std::stack<State> redo_stack;
  176. bool is_rotating = false;
  177. int down_x,down_y;
  178. ::Camera down_camera;
  179. bool is_animating = false;
  180. double animation_start_time = 0;
  181. double ANIMATION_DURATION = 0.5;
  182. Eigen::Quaterniond animation_from_quat;
  183. Eigen::Quaterniond animation_to_quat;
  184. int width,height;
  185. bool is_view_from_light = false;
  186. Eigen::Vector4f light_pos(9,9,1,1);
  187. #define REBAR_NAME "temp.rbr"
  188. igl::ReTwBar rebar;
  189. // Forward
  190. void init_mesh();
  191. void push_undo()
  192. {
  193. undo_stack.push(s);
  194. // Clear
  195. redo_stack = std::stack<State>();
  196. }
  197. void reshape(int width, int height)
  198. {
  199. ::width = width;
  200. ::height = height;
  201. glViewport(0,0,width,height);
  202. // Send the new window size to AntTweakBar
  203. TwWindowSize(width, height);
  204. if(!initialize_shadows(width,height,1,shadowMapTexture,fbo,cfbo))
  205. {
  206. assert(false);
  207. exit(-1);
  208. }
  209. }
  210. void push_scene(
  211. const int width,
  212. const int height,
  213. const double angle,
  214. const double zNear = 1e-2,
  215. const double zFar = 100,
  216. const bool correct_perspective_scaling = true)
  217. {
  218. using namespace igl;
  219. using namespace std;
  220. glMatrixMode(GL_PROJECTION);
  221. glPushMatrix();
  222. glLoadIdentity();
  223. double aspect = ((double)width)/((double)height);
  224. // Amount of scaling needed to "fix" perspective z-shift
  225. double z_fix = 1.0;
  226. // 5 is far enough to see unit "things" well
  227. const double camera_z = 2;
  228. // Test if should be using true orthographic projection
  229. if(angle == 0)
  230. {
  231. glOrtho(
  232. -0.5*camera_z*aspect,
  233. 0.5*camera_z*aspect,
  234. -0.5*camera_z,
  235. 0.5*camera_z,
  236. zNear,
  237. zFar);
  238. }else
  239. {
  240. // Make sure aspect is sane
  241. aspect = aspect < 0.01 ? 0.01 : aspect;
  242. gluPerspective(angle,aspect,zNear,zFar);
  243. if(correct_perspective_scaling)
  244. {
  245. z_fix = 2.*tan(angle/2./360.*2.*M_PI);
  246. }
  247. }
  248. glMatrixMode(GL_MODELVIEW);
  249. glPushMatrix();
  250. glLoadIdentity();
  251. glTranslatef(0,0,-camera_z);
  252. // Adjust scale to correct perspective
  253. if(correct_perspective_scaling)
  254. {
  255. glScaled(z_fix,z_fix,z_fix);
  256. }
  257. }
  258. void push_lightview_camera(const Eigen::Vector4f & light_pos)
  259. {
  260. using namespace igl;
  261. glMatrixMode(GL_MODELVIEW);
  262. glPushMatrix();
  263. //glTranslatef(-light_pos(0),-light_pos(1),-light_pos(2));
  264. gluLookAt(
  265. light_pos(0), light_pos(1), light_pos(2),
  266. 0,0,0,
  267. 0,1,0);
  268. }
  269. void push_camera(const ::Camera & camera)
  270. {
  271. using namespace igl;
  272. glMatrixMode(GL_MODELVIEW);
  273. glPushMatrix();
  274. // scale, pan
  275. glScaled(camera.zoom, camera.zoom, camera.zoom);
  276. double mat[4*4];
  277. quat_to_mat(camera.rotation.coeffs().data(),mat);
  278. glMultMatrixd(mat);
  279. }
  280. void pop_camera()
  281. {
  282. glMatrixMode(GL_MODELVIEW);
  283. glPopMatrix();
  284. }
  285. void pop_scene()
  286. {
  287. glMatrixMode(GL_PROJECTION);
  288. glPopMatrix();
  289. glMatrixMode(GL_MODELVIEW);
  290. glPopMatrix();
  291. }
  292. // Set up double-sided lights
  293. void lights(const Eigen::Vector4f & pos)
  294. {
  295. using namespace std;
  296. using namespace Eigen;
  297. glEnable(GL_LIGHTING);
  298. glLightModelf(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
  299. glEnable(GL_LIGHT0);
  300. float WHITE[4] = {1,1,1,1.};
  301. float BLACK[4] = {0.,0.,0.,1.};
  302. glLightfv(GL_LIGHT0,GL_AMBIENT,BLACK);
  303. glLightfv(GL_LIGHT0,GL_DIFFUSE,WHITE);
  304. glLightfv(GL_LIGHT0,GL_SPECULAR,BLACK);
  305. glLightfv(GL_LIGHT0,GL_POSITION,pos.data());
  306. }
  307. void update_meshes()
  308. {
  309. using namespace Eigen;
  310. for(auto & mesh : meshes)
  311. {
  312. //mesh.da.translate(Vector3d::Random()*0.001);
  313. mesh.a = mesh.a * mesh.da;
  314. Vector3d o = mesh.a.translation();
  315. const Vector3d xo(5,5,5);
  316. const Vector3d no(5,0,5);
  317. for(int d = 0;d<3;d++)
  318. {
  319. if(o(d) > xo(d))
  320. {
  321. o(d) = xo(d);
  322. mesh.da.translation()(d) *= -1.;
  323. }
  324. if(o(d) < -no(d))
  325. {
  326. o(d) = -no(d);
  327. mesh.da.translation()(d) *= -1.;
  328. }
  329. }
  330. mesh.a.translation() = o;
  331. mesh.da.translation() = MAX_SPEED*mesh.da.translation().normalized();
  332. }
  333. }
  334. void draw_objects()
  335. {
  336. using namespace igl;
  337. using namespace std;
  338. using namespace Eigen;
  339. for(int m = 0;m<(int)meshes.size();m++)
  340. {
  341. Mesh & mesh = meshes[m];
  342. Vector4f color(0,0,0,1);
  343. jet(1.f-(float)m/(float)meshes.size(),color.data());
  344. // Set material properties
  345. glDisable(GL_COLOR_MATERIAL);
  346. glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, SILVER_AMBIENT);
  347. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, color.data());
  348. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, SILVER_SPECULAR);
  349. glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, 128);
  350. glPushMatrix();
  351. glMultMatrixd(mesh.a.matrix().data());
  352. draw_mesh(mesh.V,mesh.F,mesh.N);
  353. glPopMatrix();
  354. }
  355. // Draw a nice floor
  356. glPushMatrix();
  357. {
  358. const float GREY[4] = {0.5,0.5,0.6,1.0};
  359. const float DARK_GREY[4] = {0.2,0.2,0.3,1.0};
  360. //draw_floor(GREY,DARK_GREY);
  361. glTranslatef(0,0.3,0);
  362. glScaled(10,0.1,10);
  363. glDisable(GL_COLOR_MATERIAL);
  364. glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, DARK_GREY);
  365. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, GREY);
  366. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, SILVER_SPECULAR);
  367. glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, 128);
  368. glutSolidCube(1.0);
  369. }
  370. glPopMatrix();
  371. }
  372. void draw_scene()
  373. {
  374. glEnable(GL_DEPTH_TEST);
  375. glEnable(GL_NORMALIZE);
  376. push_scene(width,height,s.camera.angle,1e-2,100,true);
  377. if(is_view_from_light)
  378. {
  379. push_lightview_camera(light_pos);
  380. }else
  381. {
  382. push_camera(s.camera);
  383. }
  384. lights(light_pos);
  385. draw_objects();
  386. glPushMatrix();
  387. glTranslatef(light_pos(0),light_pos(1),light_pos(2));
  388. glutWireSphere(1,20,20);
  389. glPopMatrix();
  390. pop_camera();
  391. pop_scene();
  392. ////First pass - from light's point of view
  393. //glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
  394. //glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, cfbo);
  395. //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  396. //glMatrixMode(GL_PROJECTION);
  397. //glLoadMatrixf(lightProjectionMatrix);
  398. //glMatrixMode(GL_MODELVIEW);
  399. //glLoadMatrixf(lightViewMatrix);
  400. ////Use viewport the same size as the shadow map
  401. //glViewport(0, 0, factor*windowWidth, factor*windowHeight);
  402. ////Draw back faces into the shadow map
  403. //glEnable(GL_CULL_FACE);
  404. //glCullFace(GL_FRONT);
  405. ////Disable color writes, and use flat shading for speed
  406. //glShadeModel(GL_FLAT);
  407. //glColorMask(0, 0, 0, 0);
  408. ////Draw the scene
  409. //draw_objects();
  410. //////Read the depth buffer into the shadow map texture
  411. ////glBindTexture(GL_TEXTURE_2D, shadowMapTexture);
  412. ////glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, factor*windowWidth, factor*windowHeight);
  413. //glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  414. //glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
  415. }
  416. void display()
  417. {
  418. using namespace igl;
  419. using namespace std;
  420. using namespace Eigen;
  421. glClearColor(1,1,1,0);
  422. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  423. if(is_animating)
  424. {
  425. double t = (get_seconds() - animation_start_time)/ANIMATION_DURATION;
  426. if(t > 1)
  427. {
  428. t = 1;
  429. is_animating = false;
  430. }
  431. Quaterniond q;
  432. q.coeffs() =
  433. animation_to_quat.coeffs()*t + animation_from_quat.coeffs()*(1.-t);
  434. q.normalize();
  435. s.camera.rotation = q;
  436. }
  437. update_meshes();
  438. draw_scene();
  439. report_gl_error();
  440. TwDraw();
  441. glutSwapBuffers();
  442. glutPostRedisplay();
  443. }
  444. void mouse_wheel(int wheel, int direction, int mouse_x, int mouse_y)
  445. {
  446. using namespace std;
  447. if(wheel == 0)
  448. {
  449. static double mouse_scroll_y = 0;
  450. const double delta_y = 0.125*direction;
  451. mouse_scroll_y += delta_y;
  452. // absolute scale difference when changing zooms (+1)
  453. const double z_diff = 0.01;
  454. GLint viewport[4];
  455. glGetIntegerv(GL_VIEWPORT,viewport);
  456. if(TwMouseMotion(mouse_x, viewport[3] - mouse_y))
  457. {
  458. TwMouseWheel(mouse_scroll_y);
  459. }else
  460. {
  461. s.camera.zoom *= (1.0+double(direction)*z_diff);
  462. const double min_zoom = 0.01;
  463. const double max_zoom = 10.0;
  464. s.camera.zoom = min(max_zoom,max(min_zoom,s.camera.zoom));
  465. }
  466. }else
  467. {
  468. if(!is_rotating)
  469. {
  470. // Change viewing angle (reshape will take care of adjust zoom)
  471. const double a_diff = 1.0;
  472. s.camera.angle += double(direction)*a_diff;
  473. const double min_angle = 15.0;
  474. s.camera.angle =
  475. min(90.0,max(min_angle,s.camera.angle));
  476. }
  477. }
  478. }
  479. void mouse(int glutButton, int glutState, int mouse_x, int mouse_y)
  480. {
  481. using namespace std;
  482. using namespace Eigen;
  483. using namespace igl;
  484. bool tw_using = TwEventMouseButtonGLUT(glutButton,glutState,mouse_x,mouse_y);
  485. switch(glutButton)
  486. {
  487. case GLUT_RIGHT_BUTTON:
  488. case GLUT_LEFT_BUTTON:
  489. {
  490. switch(glutState)
  491. {
  492. case 1:
  493. // up
  494. glutSetCursor(GLUT_CURSOR_INHERIT);
  495. is_rotating = false;
  496. break;
  497. case 0:
  498. if(!tw_using)
  499. {
  500. push_undo();
  501. glutSetCursor(GLUT_CURSOR_CYCLE);
  502. // collect information for trackball
  503. is_rotating = true;
  504. down_camera = s.camera;
  505. down_x = mouse_x;
  506. down_y = mouse_y;
  507. }
  508. break;
  509. }
  510. break;
  511. // Scroll down
  512. case GLUT_WHEEL_DOWN:
  513. {
  514. mouse_wheel(0,-1,mouse_x,mouse_y);
  515. break;
  516. }
  517. // Scroll up
  518. case GLUT_WHEEL_UP:
  519. {
  520. mouse_wheel(0,1,mouse_x,mouse_y);
  521. break;
  522. }
  523. // Scroll left
  524. case GLUT_WHEEL_LEFT:
  525. {
  526. mouse_wheel(1,-1,mouse_x,mouse_y);
  527. break;
  528. }
  529. // Scroll right
  530. case GLUT_WHEEL_RIGHT:
  531. {
  532. mouse_wheel(1,1,mouse_x,mouse_y);
  533. break;
  534. }
  535. }
  536. }
  537. }
  538. void mouse_drag(int mouse_x, int mouse_y)
  539. {
  540. using namespace igl;
  541. using namespace std;
  542. using namespace Eigen;
  543. /*bool tw_using =*/ TwMouseMotion(mouse_x,mouse_y);
  544. if(is_rotating)
  545. {
  546. glutSetCursor(GLUT_CURSOR_CYCLE);
  547. switch(rotation_type)
  548. {
  549. case ROTATION_TYPE_IGL_TRACKBALL:
  550. {
  551. // Rotate according to trackball
  552. igl::trackball(
  553. width,
  554. height,
  555. 2.0,
  556. down_camera.rotation,
  557. down_x,
  558. down_y,
  559. mouse_x,
  560. mouse_y,
  561. s.camera.rotation);
  562. break;
  563. }
  564. case ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP:
  565. {
  566. Quaterniond down_q = down_camera.rotation;
  567. Vector3d axis(0,1,0);
  568. const double speed = 2.0;
  569. Quaterniond q;
  570. q = down_q *
  571. Quaterniond(
  572. AngleAxisd(
  573. M_PI*((double)(mouse_x-down_x))/(double)width*speed/2.0,
  574. axis.normalized()));
  575. q.normalize();
  576. {
  577. Vector3d axis(1,0,0);
  578. const double speed = 2.0;
  579. if(axis.norm() != 0)
  580. {
  581. q =
  582. Quaterniond(
  583. AngleAxisd(
  584. M_PI*(mouse_y-down_y)/(double)width*speed/2.0,
  585. axis.normalized())) * q;
  586. q.normalize();
  587. }
  588. }
  589. s.camera.rotation = q;
  590. break;
  591. }
  592. default:
  593. break;
  594. }
  595. }
  596. }
  597. void init_mesh(Mesh & mesh)
  598. {
  599. using namespace Eigen;
  600. using namespace igl;
  601. per_face_normals(mesh.V,mesh.F,mesh.N);
  602. normalize_row_lengths(mesh.N,mesh.N);
  603. // Rescale so bounding box fits in unit ball
  604. Vector3d Vmax = mesh.V.colwise().maxCoeff();
  605. Vector3d Vmin = mesh.V.colwise().minCoeff();
  606. Vector3d Vmid = 0.5*(Vmax + Vmin);
  607. mesh.V.rowwise() -= Vmid.transpose();
  608. const double bbd = (Vmax-Vmin).norm();
  609. mesh.V /= (bbd*0.5);
  610. }
  611. void undo()
  612. {
  613. using namespace std;
  614. if(!undo_stack.empty())
  615. {
  616. redo_stack.push(s);
  617. s = undo_stack.top();
  618. undo_stack.pop();
  619. }
  620. }
  621. void redo()
  622. {
  623. using namespace std;
  624. if(!redo_stack.empty())
  625. {
  626. undo_stack.push(s);
  627. s = redo_stack.top();
  628. redo_stack.pop();
  629. }
  630. }
  631. void key(unsigned char key, int mouse_x, int mouse_y)
  632. {
  633. using namespace std;
  634. int mod = glutGetModifiers();
  635. switch(key)
  636. {
  637. // ESC
  638. case char(27):
  639. rebar.save(REBAR_NAME);
  640. // ^C
  641. case char(3):
  642. exit(0);
  643. case 'z':
  644. case 'Z':
  645. if(mod & GLUT_ACTIVE_COMMAND)
  646. {
  647. if(mod & GLUT_ACTIVE_SHIFT)
  648. {
  649. redo();
  650. }else
  651. {
  652. undo();
  653. }
  654. break;
  655. }else
  656. {
  657. push_undo();
  658. igl::snap_to_canonical_view_quat(
  659. s.camera.rotation,
  660. 1.0,
  661. s.camera.rotation);
  662. break;
  663. }
  664. default:
  665. if(!TwEventKeyboardGLUT(key,mouse_x,mouse_y))
  666. {
  667. cout<<"Unknown key command: "<<key<<" "<<int(key)<<endl;
  668. }
  669. }
  670. }
  671. void TW_CALL set_rotation_type(const void * value, void * clientData)
  672. {
  673. using namespace Eigen;
  674. using namespace std;
  675. using namespace igl;
  676. const RotationType old_rotation_type = rotation_type;
  677. rotation_type = *(const RotationType *)(value);
  678. if(rotation_type == ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP &&
  679. old_rotation_type != ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP)
  680. {
  681. push_undo();
  682. animation_from_quat = s.camera.rotation;
  683. const Vector3d up = animation_from_quat.matrix() * Vector3d(0,1,0);
  684. Vector3d proj_up(0,up(1),up(2));
  685. if(proj_up.norm() == 0)
  686. {
  687. proj_up = Vector3d(0,1,0);
  688. }
  689. proj_up.normalize();
  690. Quaterniond dq;
  691. dq = Quaterniond::FromTwoVectors(up,proj_up);
  692. animation_to_quat = dq * animation_from_quat;
  693. // start animation
  694. animation_start_time = get_seconds();
  695. is_animating = true;
  696. }
  697. }
  698. void TW_CALL get_rotation_type(void * value, void *clientData)
  699. {
  700. RotationType * rt = (RotationType *)(value);
  701. *rt = rotation_type;
  702. }
  703. int main(int argc, char * argv[])
  704. {
  705. using namespace std;
  706. using namespace Eigen;
  707. using namespace igl;
  708. vector<string> filenames;
  709. switch(argc)
  710. {
  711. default:
  712. // Read and prepare meshes
  713. for(int a = 1;a<argc;a++)
  714. {
  715. filenames.push_back(argv[a]);
  716. }
  717. break;
  718. case 1:
  719. cerr<<"Usage:"<<endl<<
  720. " ./example input1.obj input2.obj input3.obj ..."<<endl;
  721. cerr<<endl<<"Opening default mesh..."<<endl;
  722. string filename = "../shared/truck.obj";
  723. filenames.push_back(filename);
  724. break;
  725. }
  726. // print key commands
  727. cout<<"[Click] and [drag] Rotate model using trackball."<<endl;
  728. cout<<"[Z,z] Snap rotation to canonical view."<<endl;
  729. cout<<"[Command+Z] Undo."<<endl;
  730. cout<<"[Shift+Command+Z] Redo."<<endl;
  731. cout<<"[^C,ESC] Exit."<<endl;
  732. for(auto & filename : filenames)
  733. {
  734. meshes.push_back(Mesh());
  735. Mesh & mesh = meshes.back();
  736. mesh.a.translate(Vector3d(0,1,0));
  737. Vector3d offset = Vector3d::Random()*3;
  738. offset(1) = 0;
  739. mesh.a.translate(offset);
  740. // dirname, basename, extension and filename
  741. string d,b,ext,f;
  742. pathinfo(filename,d,b,ext,f);
  743. // Convert extension to lower case
  744. transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
  745. vector<vector<double > > vV,vN,vTC;
  746. vector<vector<int > > vF,vFTC,vFN;
  747. if(ext == "obj")
  748. {
  749. // Convert extension to lower case
  750. if(!igl::readOBJ(filename,vV,vTC,vN,vF,vFTC,vFN))
  751. {
  752. return 1;
  753. }
  754. }else if(ext == "off")
  755. {
  756. // Convert extension to lower case
  757. if(!igl::readOFF(filename,vV,vF,vN))
  758. {
  759. return 1;
  760. }
  761. }else if(ext == "wrl")
  762. {
  763. // Convert extension to lower case
  764. if(!igl::readWRL(filename,vV,vF))
  765. {
  766. return 1;
  767. }
  768. }
  769. if(vV.size() > 0)
  770. {
  771. if(!list_to_matrix(vV,mesh.V))
  772. {
  773. return 1;
  774. }
  775. polygon_mesh_to_triangle_mesh(vF,mesh.F);
  776. }
  777. init_mesh(mesh);
  778. }
  779. // Init glut
  780. glutInit(&argc,argv);
  781. if( !TwInit(TW_OPENGL, NULL) )
  782. {
  783. // A fatal error occured
  784. fprintf(stderr, "AntTweakBar initialization failed: %s\n", TwGetLastError());
  785. return 1;
  786. }
  787. // Create a tweak bar
  788. rebar.TwNewBar("bar");
  789. TwDefine("bar label='Shadow Mapping' size='200 550' text=light alpha='200' color='68 68 68'");
  790. rebar.TwAddVarRW("camera_zoom", TW_TYPE_DOUBLE,&s.camera.zoom,"");
  791. rebar.TwAddVarRW("camera_rotation", TW_TYPE_QUAT4D,s.camera.rotation.coeffs().data(),"");
  792. TwType RotationTypeTW = ReTwDefineEnumFromString("RotationType","igl_trackball,two_axis_fixed_up");
  793. rebar.TwAddVarCB( "rotation_type", RotationTypeTW,
  794. set_rotation_type,get_rotation_type,NULL,"keyIncr=] keyDecr=[");
  795. rebar.TwAddVarRW( "is_view_from_light",TW_TYPE_BOOLCPP,&is_view_from_light,
  796. "key=l");
  797. rebar.load(REBAR_NAME);
  798. animation_from_quat = Quaterniond(1,0,0,0);
  799. animation_from_quat = s.camera.rotation;
  800. animation_start_time = get_seconds();
  801. // Init antweakbar
  802. glutInitDisplayString( "rgba depth double samples>=8");
  803. // Top right corner
  804. glutInitWindowSize(glutGet(GLUT_SCREEN_WIDTH)/2.0,glutGet(GLUT_SCREEN_HEIGHT)/2.0);
  805. glutInitWindowPosition(glutGet(GLUT_SCREEN_WIDTH)/2.0,-1);
  806. glutCreateWindow("Shadow Mapping");
  807. glutDisplayFunc(display);
  808. glutReshapeFunc(reshape);
  809. glutKeyboardFunc(key);
  810. glutMouseFunc(mouse);
  811. glutMotionFunc(mouse_drag);
  812. glutPassiveMotionFunc((GLUTmousemotionfun)TwEventMouseMotionGLUT);
  813. glutMainLoop();
  814. return 0;
  815. }