example.cpp 21 KB

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