example.cpp 21 KB

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