example.cpp 21 KB

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