example.cpp 14 KB

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