example.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. #include <igl/OpenGL_convenience.h>
  2. #include <igl/per_face_normals.h>
  3. #include <igl/per_vertex_normals.h>
  4. #include <igl/two_axis_valuator_fixed_up.h>
  5. #include <igl/normalize_row_lengths.h>
  6. #include <igl/draw_mesh.h>
  7. #include <igl/draw_floor.h>
  8. #include <igl/quat_to_mat.h>
  9. #include <igl/report_gl_error.h>
  10. #include <igl/readOBJ.h>
  11. #include <igl/readDMAT.h>
  12. #include <igl/readOFF.h>
  13. #include <igl/readMESH.h>
  14. #include <igl/jet.h>
  15. #include <igl/readWRL.h>
  16. #include <igl/trackball.h>
  17. #include <igl/list_to_matrix.h>
  18. #include <igl/snap_to_canonical_view_quat.h>
  19. #include <igl/snap_to_fixed_up.h>
  20. #include <igl/triangulate.h>
  21. #include <igl/material_colors.h>
  22. #include <igl/barycenter.h>
  23. #include <igl/matlab_format.h>
  24. #include <igl/ReAntTweakBar.h>
  25. #include <igl/pathinfo.h>
  26. #include <igl/Camera.h>
  27. #include <igl/get_seconds.h>
  28. #include <igl/texture_from_tga.h>
  29. #include <igl/png/texture_from_file.h>
  30. #include <igl/png/texture_from_png.h>
  31. #include <igl/STR.h>
  32. #include <YImage.hpp>
  33. #ifdef __APPLE__
  34. # include <GLUT/glut.h>
  35. #else
  36. # include <GL/glut.h>
  37. #endif
  38. #include <Eigen/Core>
  39. #include <vector>
  40. #include <iostream>
  41. #include <algorithm>
  42. struct State
  43. {
  44. igl::Camera camera;
  45. } s;
  46. enum RotationType
  47. {
  48. ROTATION_TYPE_IGL_TRACKBALL = 0,
  49. ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP = 1,
  50. NUM_ROTATION_TYPES = 2,
  51. } rotation_type;
  52. bool is_rotating = false;
  53. int down_x,down_y;
  54. igl::Camera down_camera;
  55. bool is_animating = false;
  56. double animation_start_time = 0;
  57. double ANIMATION_DURATION = 0.5;
  58. Eigen::Quaterniond animation_from_quat;
  59. Eigen::Quaterniond animation_to_quat;
  60. // Use vector for range-based `for`
  61. std::vector<State> undo_stack;
  62. std::vector<State> redo_stack;
  63. void push_undo()
  64. {
  65. undo_stack.push_back(s);
  66. // Clear
  67. redo_stack = std::vector<State>();
  68. }
  69. void undo()
  70. {
  71. using namespace std;
  72. if(!undo_stack.empty())
  73. {
  74. redo_stack.push_back(s);
  75. s = undo_stack.front();
  76. undo_stack.pop_back();
  77. }
  78. }
  79. void redo()
  80. {
  81. using namespace std;
  82. if(!redo_stack.empty())
  83. {
  84. undo_stack.push_back(s);
  85. s = redo_stack.front();
  86. redo_stack.pop_back();
  87. }
  88. }
  89. void TW_CALL set_rotation_type(const void * value, void * clientData)
  90. {
  91. using namespace Eigen;
  92. using namespace std;
  93. using namespace igl;
  94. const RotationType old_rotation_type = rotation_type;
  95. rotation_type = *(const RotationType *)(value);
  96. if(rotation_type == ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP &&
  97. old_rotation_type != ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP)
  98. {
  99. push_undo();
  100. animation_from_quat = s.camera.m_rotation_conj;
  101. snap_to_fixed_up(animation_from_quat,animation_to_quat);
  102. // start animation
  103. animation_start_time = get_seconds();
  104. is_animating = true;
  105. }
  106. }
  107. void TW_CALL get_rotation_type(void * value, void *clientData)
  108. {
  109. RotationType * rt = (RotationType *)(value);
  110. *rt = rotation_type;
  111. }
  112. // Width and height of window
  113. int width,height;
  114. // Position of light
  115. float light_pos[4] = {0.1,0.1,-0.9,0};
  116. // Vertex positions, normals, colors and centroid
  117. Eigen::MatrixXd V,N,TC,mid;
  118. Eigen::MatrixXi F,TF;
  119. GLuint tex_id = 0;
  120. int selected_col = 0;
  121. // Faces
  122. // Bounding box diagonal length
  123. double bbd;
  124. // Running ambient occlusion
  125. Eigen::VectorXd S;
  126. int tot_num_samples = 0;
  127. #define REBAR_NAME "temp.rbr"
  128. igl::ReTwBar rebar; // Pointer to the tweak bar
  129. bool flip_y = false;
  130. void reshape(int width,int height)
  131. {
  132. using namespace std;
  133. // Save width and height
  134. ::width = width;
  135. ::height = height;
  136. glMatrixMode(GL_PROJECTION);
  137. glLoadIdentity();
  138. glViewport(0,0,width,height);
  139. // Send the new window size to AntTweakBar
  140. TwWindowSize(width, height);
  141. // Set aspect for all cameras
  142. s.camera.m_aspect = (double)width/(double)height;
  143. for(auto & s : undo_stack)
  144. {
  145. s.camera.m_aspect = (double)width/(double)height;
  146. }
  147. for(auto & s : redo_stack)
  148. {
  149. s.camera.m_aspect = (double)width/(double)height;
  150. }
  151. }
  152. void push_scene()
  153. {
  154. using namespace igl;
  155. using namespace std;
  156. glMatrixMode(GL_PROJECTION);
  157. glPushMatrix();
  158. glLoadIdentity();
  159. auto & camera = s.camera;
  160. gluPerspective(camera.m_angle,camera.m_aspect,camera.m_near,camera.m_far);
  161. glMatrixMode(GL_MODELVIEW);
  162. glPushMatrix();
  163. glLoadIdentity();
  164. gluLookAt(
  165. camera.eye()(0), camera.eye()(1), camera.eye()(2),
  166. camera.at()(0), camera.at()(1), camera.at()(2),
  167. camera.up()(0), camera.up()(1), camera.up()(2));
  168. }
  169. void pop_scene()
  170. {
  171. glMatrixMode(GL_PROJECTION);
  172. glPopMatrix();
  173. glMatrixMode(GL_MODELVIEW);
  174. glPopMatrix();
  175. }
  176. void pop_object()
  177. {
  178. glPopMatrix();
  179. }
  180. // Scale and shift for object
  181. void push_object()
  182. {
  183. glPushMatrix();
  184. glScaled(2./bbd,2./bbd,2./bbd);
  185. glTranslated(-mid(0,0),-mid(0,1),-mid(0,2));
  186. }
  187. // Set up double-sided lights
  188. void lights()
  189. {
  190. using namespace std;
  191. glEnable(GL_LIGHTING);
  192. glLightModelf(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
  193. glEnable(GL_LIGHT0);
  194. glEnable(GL_LIGHT1);
  195. float amb[4];
  196. amb[0] = amb[1] = amb[2] = 0;
  197. amb[3] = 1.0;
  198. float diff[4] = {0.0,0.0,0.0,0.0};
  199. diff[0] = diff[1] = diff[2] = (1.0 - 0/0.4);;
  200. diff[3] = 1.0;
  201. float zeros[4] = {0.0,0.0,0.0,0.0};
  202. float pos[4];
  203. copy(light_pos,light_pos+4,pos);
  204. glLightfv(GL_LIGHT0,GL_AMBIENT,amb);
  205. glLightfv(GL_LIGHT0,GL_DIFFUSE,diff);
  206. glLightfv(GL_LIGHT0,GL_SPECULAR,zeros);
  207. glLightfv(GL_LIGHT0,GL_POSITION,pos);
  208. pos[0] *= -1;
  209. pos[1] *= -1;
  210. pos[2] *= -1;
  211. glLightfv(GL_LIGHT1,GL_AMBIENT,amb);
  212. glLightfv(GL_LIGHT1,GL_DIFFUSE,diff);
  213. glLightfv(GL_LIGHT1,GL_SPECULAR,zeros);
  214. glLightfv(GL_LIGHT1,GL_POSITION,pos);
  215. }
  216. const float back[4] = {30.0/255.0,30.0/255.0,50.0/255.0,0};
  217. void display()
  218. {
  219. using namespace Eigen;
  220. using namespace igl;
  221. using namespace std;
  222. glClearColor(back[0],back[1],back[2],0);
  223. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  224. if(is_animating)
  225. {
  226. double t = (get_seconds() - animation_start_time)/ANIMATION_DURATION;
  227. if(t > 1)
  228. {
  229. t = 1;
  230. is_animating = false;
  231. }
  232. const Quaterniond q = animation_from_quat.slerp(t,animation_to_quat).normalized();
  233. s.camera.orbit(q.conjugate());
  234. }
  235. glDisable(GL_LIGHTING);
  236. lights();
  237. push_scene();
  238. glEnable(GL_DEPTH_TEST);
  239. glDepthFunc(GL_LESS);
  240. glEnable(GL_NORMALIZE);
  241. glEnable(GL_COLOR_MATERIAL);
  242. glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
  243. push_object();
  244. // Draw the model
  245. // Set material properties
  246. glEnable(GL_COLOR_MATERIAL);
  247. glColor3f(1,1,1);
  248. glEnable(GL_TEXTURE_2D);
  249. glBindTexture(GL_TEXTURE_2D,tex_id);
  250. MatrixXd _d;
  251. MatrixXi _i;
  252. glMatrixMode(GL_TEXTURE);
  253. glPushMatrix();
  254. glLoadIdentity();
  255. if(flip_y)
  256. {
  257. glTranslated(0,1,0);
  258. glScaled(1,-1,1);
  259. }
  260. glMatrixMode(GL_MODELVIEW);
  261. draw_mesh(V,F,N,MatrixXi(),MatrixXd(),TC,TF,MatrixXd(),0,MatrixXi(),0);
  262. glMatrixMode(GL_TEXTURE);
  263. glPopMatrix();
  264. glMatrixMode(GL_MODELVIEW);
  265. pop_object();
  266. // Draw a nice floor
  267. glPushMatrix();
  268. const double floor_offset =
  269. -2./bbd*(V.col(1).maxCoeff()-mid(1));
  270. glTranslated(0,floor_offset,0);
  271. const float GREY[4] = {0.5,0.5,0.6,1.0};
  272. const float DARK_GREY[4] = {0.2,0.2,0.3,1.0};
  273. glEnable(GL_POLYGON_OFFSET_FILL);
  274. glPolygonOffset(-1,1);
  275. glBegin(GL_QUADS);
  276. glNormal3d(0,1,0);
  277. glTexCoord2d(0,1);
  278. glVertex3d(-1,0,1);
  279. glTexCoord2d(1,1);
  280. glVertex3d(1,0,1);
  281. glTexCoord2d(1,0);
  282. glVertex3d(1,0,-1);
  283. glTexCoord2d(0,0);
  284. glVertex3d(-1,0,-1);
  285. glEnd();
  286. glDisable(GL_POLYGON_OFFSET_FILL);
  287. glDisable(GL_TEXTURE_2D);
  288. draw_floor(GREY,DARK_GREY);
  289. glPopMatrix();
  290. pop_scene();
  291. report_gl_error();
  292. TwDraw();
  293. glutSwapBuffers();
  294. if(is_animating)
  295. {
  296. glutPostRedisplay();
  297. }
  298. }
  299. void mouse_wheel(int wheel, int direction, int mouse_x, int mouse_y)
  300. {
  301. using namespace std;
  302. using namespace igl;
  303. using namespace Eigen;
  304. GLint viewport[4];
  305. glGetIntegerv(GL_VIEWPORT,viewport);
  306. if(wheel == 0 && TwMouseMotion(mouse_x, viewport[3] - mouse_y))
  307. {
  308. static double mouse_scroll_y = 0;
  309. const double delta_y = 0.125*direction;
  310. mouse_scroll_y += delta_y;
  311. TwMouseWheel(mouse_scroll_y);
  312. return;
  313. }
  314. push_undo();
  315. auto & camera = s.camera;
  316. if(wheel==0)
  317. {
  318. // factor of zoom change
  319. double s = (1.-0.01*direction);
  320. //// FOV zoom: just widen angle. This is hardly ever appropriate.
  321. //camera.m_angle *= s;
  322. //camera.m_angle = min(max(camera.m_angle,1),89);
  323. camera.push_away(s);
  324. }else
  325. {
  326. // Dolly zoom:
  327. camera.dolly_zoom((double)direction*1.0);
  328. }
  329. }
  330. void mouse(int glutButton, int glutState, int mouse_x, int mouse_y)
  331. {
  332. using namespace std;
  333. using namespace Eigen;
  334. using namespace igl;
  335. bool tw_using = TwEventMouseButtonGLUT(glutButton,glutState,mouse_x,mouse_y);
  336. switch(glutButton)
  337. {
  338. case GLUT_RIGHT_BUTTON:
  339. case GLUT_LEFT_BUTTON:
  340. {
  341. switch(glutState)
  342. {
  343. case 1:
  344. // up
  345. glutSetCursor(GLUT_CURSOR_LEFT_ARROW);
  346. is_rotating = false;
  347. break;
  348. case 0:
  349. // down
  350. if(!tw_using)
  351. {
  352. glutSetCursor(GLUT_CURSOR_CYCLE);
  353. // collect information for trackball
  354. is_rotating = true;
  355. down_camera = s.camera;
  356. down_x = mouse_x;
  357. down_y = mouse_y;
  358. }
  359. break;
  360. }
  361. }
  362. // Scroll down
  363. case 3:
  364. {
  365. mouse_wheel(0,-1,mouse_x,mouse_y);
  366. break;
  367. }
  368. // Scroll up
  369. case 4:
  370. {
  371. mouse_wheel(0,1,mouse_x,mouse_y);
  372. break;
  373. }
  374. // Scroll left
  375. case 5:
  376. {
  377. mouse_wheel(1,-1,mouse_x,mouse_y);
  378. break;
  379. }
  380. // Scroll right
  381. case 6:
  382. {
  383. mouse_wheel(1,1,mouse_x,mouse_y);
  384. break;
  385. }
  386. }
  387. glutPostRedisplay();
  388. }
  389. void mouse_drag(int mouse_x, int mouse_y)
  390. {
  391. using namespace igl;
  392. using namespace Eigen;
  393. if(is_rotating)
  394. {
  395. glutSetCursor(GLUT_CURSOR_CYCLE);
  396. Quaterniond q;
  397. auto & camera = s.camera;
  398. switch(rotation_type)
  399. {
  400. case ROTATION_TYPE_IGL_TRACKBALL:
  401. {
  402. // Rotate according to trackball
  403. igl::trackball<double>(
  404. width,
  405. height,
  406. 2.0,
  407. down_camera.m_rotation_conj.coeffs().data(),
  408. down_x,
  409. down_y,
  410. mouse_x,
  411. mouse_y,
  412. q.coeffs().data());
  413. break;
  414. }
  415. case ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP:
  416. {
  417. // Rotate according to two axis valuator with fixed up vector
  418. two_axis_valuator_fixed_up(
  419. width, height,
  420. 2.0,
  421. down_camera.m_rotation_conj,
  422. down_x, down_y, mouse_x, mouse_y,
  423. q);
  424. break;
  425. }
  426. default:
  427. break;
  428. }
  429. camera.orbit(q.conjugate());
  430. }else
  431. {
  432. TwEventMouseMotionGLUT(mouse_x, mouse_y);
  433. }
  434. glutPostRedisplay();
  435. }
  436. void key(unsigned char key, int mouse_x, int mouse_y)
  437. {
  438. using namespace std;
  439. switch(key)
  440. {
  441. // ESC
  442. case char(27):
  443. rebar.save(REBAR_NAME);
  444. // ^C
  445. case char(3):
  446. exit(0);
  447. default:
  448. if(!TwEventKeyboardGLUT(key,mouse_x,mouse_y))
  449. {
  450. cout<<"Unknown key command: "<<key<<" "<<int(key)<<endl;
  451. }
  452. }
  453. glutPostRedisplay();
  454. }
  455. int main(int argc, char * argv[])
  456. {
  457. using namespace Eigen;
  458. using namespace igl;
  459. using namespace std;
  460. // init mesh
  461. string filename = "../shared/animal.obj";
  462. string tfilename = "../shared/animal.png";
  463. if(argc < 3)
  464. {
  465. cerr<<"Usage:"<<endl<<" ./example input.obj texture.png"<<endl;
  466. cout<<endl<<"Opening default mesh..."<<endl;
  467. }else
  468. {
  469. // Read and prepare mesh
  470. filename = argv[1];
  471. tfilename = argv[2];
  472. }
  473. vector<vector<double > > vV,vN,vTC;
  474. vector<vector<int > > vF,vTF,vFN;
  475. // Convert extension to lower case
  476. if(!igl::readOBJ(filename,vV,vTC,vN,vF,vTF,vFN))
  477. {
  478. return 1;
  479. }
  480. if(vV.size() > 0)
  481. {
  482. if(!list_to_matrix(vV,V))
  483. {
  484. return 1;
  485. }
  486. triangulate(vF,F);
  487. }
  488. if(vTC.size() > 0)
  489. {
  490. if(!list_to_matrix(vTC,TC))
  491. {
  492. return 1;
  493. }
  494. }
  495. if(vTF.size() > 0)
  496. {
  497. if(!list_to_matrix(vTF,TF))
  498. {
  499. return 1;
  500. }
  501. }
  502. //if(vN.size() > 0)
  503. //{
  504. // if(!list_to_matrix(vN,N))
  505. // {
  506. // return 1;
  507. // }
  508. //}else
  509. //{
  510. per_vertex_normals(V,F,N);
  511. //}
  512. // Compute normals, centroid, colors, bounding box diagonal
  513. mid = 0.5*(V.colwise().maxCoeff() + V.colwise().minCoeff());
  514. bbd = (V.colwise().maxCoeff() - V.colwise().minCoeff()).maxCoeff();
  515. // Init glut
  516. glutInit(&argc,argv);
  517. if( !TwInit(TW_OPENGL, NULL) )
  518. {
  519. // A fatal error occured
  520. fprintf(stderr, "AntTweakBar initialization failed: %s\n", TwGetLastError());
  521. return 1;
  522. }
  523. // Create a tweak bar
  524. rebar.TwNewBar("TweakBar");
  525. rebar.TwAddVarRW("camera_rotation", TW_TYPE_QUAT4D,
  526. s.camera.m_rotation_conj.coeffs().data(), "open readonly=true");
  527. s.camera.push_away(3);
  528. s.camera.dolly_zoom(25-s.camera.m_angle);
  529. TwType RotationTypeTW = ReTwDefineEnumFromString("RotationType",
  530. "igl_trackball,two-a...-fixed-up");
  531. rebar.TwAddVarCB( "rotation_type", RotationTypeTW,
  532. set_rotation_type,get_rotation_type,NULL,"keyIncr=] keyDecr=[");
  533. rebar.TwAddVarRW("flip_y", TW_TYPE_BOOLCPP, &flip_y,"key=f");
  534. rebar.load(REBAR_NAME);
  535. glutInitDisplayString( "rgba depth double samples>=8 ");
  536. glutInitWindowSize(glutGet(GLUT_SCREEN_WIDTH)/2.0,glutGet(GLUT_SCREEN_HEIGHT));
  537. glutCreateWindow("colored-mesh");
  538. glutDisplayFunc(display);
  539. glutReshapeFunc(reshape);
  540. glutKeyboardFunc(key);
  541. glutMouseFunc(mouse);
  542. glutMotionFunc(mouse_drag);
  543. glutPassiveMotionFunc(
  544. [](int x, int y)
  545. {
  546. TwEventMouseMotionGLUT(x,y);
  547. glutPostRedisplay();
  548. });
  549. static std::function<void(int)> timer_bounce;
  550. auto timer = [] (int ms) {
  551. timer_bounce(ms);
  552. };
  553. timer_bounce = [&] (int ms) {
  554. glutTimerFunc(ms, timer, ms);
  555. glutPostRedisplay();
  556. };
  557. glutTimerFunc(500, timer, 500);
  558. // Must be called after opengl context is initialized
  559. if(!texture_from_file(tfilename,tex_id))
  560. {
  561. return 1;
  562. }
  563. glutMainLoop();
  564. return 0;
  565. }