example.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. #include <igl/OpenGL_convenience.h>
  2. #include <igl/per_face_normals.h>
  3. #include <igl/per_vertex_normals.h>
  4. #include <igl/normalize_row_lengths.h>
  5. #include <igl/draw_mesh.h>
  6. #include <igl/draw_floor.h>
  7. #include <igl/quat_to_mat.h>
  8. #include <igl/report_gl_error.h>
  9. #include <igl/readOBJ.h>
  10. #include <igl/readOFF.h>
  11. #include <igl/readWRL.h>
  12. #include <igl/trackball.h>
  13. #include <igl/list_to_matrix.h>
  14. #include <igl/triangulate.h>
  15. #include <igl/material_colors.h>
  16. #include <igl/barycenter.h>
  17. #include <igl/matlab_format.h>
  18. #include <igl/embree/EmbreeIntersector.h>
  19. #include <igl/embree/ambient_occlusion.h>
  20. #include <igl/ReAntTweakBar.h>
  21. #include <igl/pathinfo.h>
  22. #ifdef __APPLE__
  23. # include <GLUT/glut.h>
  24. #else
  25. # include <GL/glut.h>
  26. #endif
  27. #include <Eigen/Core>
  28. #include <vector>
  29. #include <iostream>
  30. #include <algorithm>
  31. // Width and height of window
  32. int width,height;
  33. // Rotation of scene
  34. float scene_rot[4] = {0,0,0,1};
  35. // information at mouse down
  36. float down_scene_rot[4] = {0,0,0,1};
  37. bool trackball_on = false;
  38. int down_mouse_x,down_mouse_y;
  39. // Position of light
  40. float light_pos[4] = {0.1,0.1,-0.9,0};
  41. // Vertex positions, normals, colors and centroid
  42. Eigen::MatrixXd V,N,C,mid;
  43. // Faces
  44. Eigen::MatrixXi F;
  45. // Bounding box diagonal length
  46. double bbd;
  47. igl::EmbreeIntersector<Eigen::MatrixXd::Scalar,Eigen::MatrixXi::Scalar> ei;
  48. // Running ambient occlusion
  49. Eigen::VectorXd S;
  50. int tot_num_samples = 0;
  51. #define REBAR_NAME "temp.rbr"
  52. igl::ReTwBar rebar; // Pointer to the tweak bar
  53. bool lights_on = true;
  54. Eigen::Vector4f color(0.4,0.8,0.3,1.0);
  55. double ao_factor = 1.0;
  56. bool ao_normalize = false;
  57. bool ao_on = false;
  58. double light_intensity = 1.0;
  59. void reshape(int width,int height)
  60. {
  61. using namespace std;
  62. // Save width and height
  63. ::width = width;
  64. ::height = height;
  65. glMatrixMode(GL_PROJECTION);
  66. glLoadIdentity();
  67. glViewport(0,0,width,height);
  68. // Send the new window size to AntTweakBar
  69. TwWindowSize(width, height);
  70. }
  71. // Set up projection and model view of scene
  72. void push_scene()
  73. {
  74. using namespace igl;
  75. glMatrixMode(GL_PROJECTION);
  76. glLoadIdentity();
  77. gluPerspective(45,(double)width/(double)height,1e-2,100);
  78. glMatrixMode(GL_MODELVIEW);
  79. glLoadIdentity();
  80. gluLookAt(0,0,3,0,0,0,0,1,0);
  81. glPushMatrix();
  82. float mat[4*4];
  83. quat_to_mat(scene_rot,mat);
  84. glMultMatrixf(mat);
  85. }
  86. void pop_scene()
  87. {
  88. glPopMatrix();
  89. }
  90. // Scale and shift for object
  91. void push_object()
  92. {
  93. glPushMatrix();
  94. glScaled(2./bbd,2./bbd,2./bbd);
  95. glTranslated(-mid(0,0),-mid(0,1),-mid(0,2));
  96. }
  97. void pop_object()
  98. {
  99. glPopMatrix();
  100. }
  101. // Set up double-sided lights
  102. void lights()
  103. {
  104. using namespace std;
  105. glEnable(GL_LIGHTING);
  106. glLightModelf(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
  107. glEnable(GL_LIGHT0);
  108. glEnable(GL_LIGHT1);
  109. float amb[4];
  110. amb[0] = amb[1] = amb[2] = light_intensity;
  111. amb[3] = 1.0;
  112. float diff[4] = {0.0,0.0,0.0,0.0};
  113. diff[0] = diff[1] = diff[2] = (1.0 - light_intensity/0.4);;
  114. diff[3] = 1.0;
  115. float zeros[4] = {0.0,0.0,0.0,0.0};
  116. float pos[4];
  117. copy(light_pos,light_pos+4,pos);
  118. glLightfv(GL_LIGHT0,GL_AMBIENT,amb);
  119. glLightfv(GL_LIGHT0,GL_DIFFUSE,diff);
  120. glLightfv(GL_LIGHT0,GL_SPECULAR,zeros);
  121. glLightfv(GL_LIGHT0,GL_POSITION,pos);
  122. pos[0] *= -1;
  123. pos[1] *= -1;
  124. pos[2] *= -1;
  125. glLightfv(GL_LIGHT1,GL_AMBIENT,amb);
  126. glLightfv(GL_LIGHT1,GL_DIFFUSE,diff);
  127. glLightfv(GL_LIGHT1,GL_SPECULAR,zeros);
  128. glLightfv(GL_LIGHT1,GL_POSITION,pos);
  129. }
  130. const float back[4] = {30.0/255.0,30.0/255.0,50.0/255.0,0};
  131. void display()
  132. {
  133. using namespace Eigen;
  134. using namespace igl;
  135. using namespace std;
  136. if(!trackball_on && tot_num_samples < 10000)
  137. {
  138. if(S.size() == 0)
  139. {
  140. S.resize(V.rows());
  141. S.setZero();
  142. }
  143. VectorXd Si;
  144. const int num_samples = 20;
  145. ambient_occlusion(ei,V,N,num_samples,Si);
  146. S *= (double)tot_num_samples;
  147. S += Si*(double)num_samples;
  148. tot_num_samples += num_samples;
  149. S /= (double)tot_num_samples;
  150. }
  151. // Convert to 1-intensity
  152. C.conservativeResize(S.rows(),3);
  153. if(ao_on)
  154. {
  155. C<<S,S,S;
  156. C.array() = (1.0-ao_factor*C.array());
  157. }else
  158. {
  159. C.setConstant(1.0);
  160. }
  161. if(ao_normalize)
  162. {
  163. C.col(0) *= ((double)C.rows())/C.col(0).sum();
  164. C.col(1) *= ((double)C.rows())/C.col(1).sum();
  165. C.col(2) *= ((double)C.rows())/C.col(2).sum();
  166. }
  167. C.col(0) *= color(0);
  168. C.col(1) *= color(1);
  169. C.col(2) *= color(2);
  170. glClearColor(back[0],back[1],back[2],0);
  171. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  172. // All smooth points
  173. glEnable( GL_POINT_SMOOTH );
  174. glDisable(GL_LIGHTING);
  175. if(lights_on)
  176. {
  177. lights();
  178. }
  179. push_scene();
  180. glEnable(GL_DEPTH_TEST);
  181. glDepthFunc(GL_LEQUAL);
  182. glEnable(GL_NORMALIZE);
  183. glEnable(GL_COLOR_MATERIAL);
  184. glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
  185. push_object();
  186. // Draw the model
  187. // Set material properties
  188. glEnable(GL_COLOR_MATERIAL);
  189. draw_mesh(V,F,N,C);
  190. pop_object();
  191. // Draw a nice floor
  192. glPushMatrix();
  193. const double floor_offset =
  194. -2./bbd*(V.col(1).maxCoeff()-mid(1));
  195. glTranslated(0,floor_offset,0);
  196. const float GREY[4] = {0.5,0.5,0.6,1.0};
  197. const float DARK_GREY[4] = {0.2,0.2,0.3,1.0};
  198. draw_floor(GREY,DARK_GREY);
  199. glPopMatrix();
  200. pop_scene();
  201. report_gl_error();
  202. TwDraw();
  203. glutSwapBuffers();
  204. glutPostRedisplay();
  205. }
  206. void mouse(int glutButton, int glutState, int mouse_x, int mouse_y)
  207. {
  208. using namespace std;
  209. using namespace Eigen;
  210. using namespace igl;
  211. bool tw_using = TwEventMouseButtonGLUT(glutButton,glutState,mouse_x,mouse_y);
  212. switch(glutState)
  213. {
  214. case 1:
  215. // up
  216. glutSetCursor(GLUT_CURSOR_LEFT_ARROW);
  217. trackball_on = false;
  218. break;
  219. case 0:
  220. // down
  221. if(!tw_using)
  222. {
  223. glutSetCursor(GLUT_CURSOR_CYCLE);
  224. // collect information for trackball
  225. trackball_on = true;
  226. copy(scene_rot,scene_rot+4,down_scene_rot);
  227. down_mouse_x = mouse_x;
  228. down_mouse_y = mouse_y;
  229. }
  230. break;
  231. }
  232. }
  233. void mouse_drag(int mouse_x, int mouse_y)
  234. {
  235. using namespace igl;
  236. if(trackball_on)
  237. {
  238. // Rotate according to trackball
  239. trackball<float>(
  240. width,
  241. height,
  242. 2,
  243. down_scene_rot,
  244. down_mouse_x,
  245. down_mouse_y,
  246. mouse_x,
  247. mouse_y,
  248. scene_rot);
  249. }else
  250. {
  251. TwEventMouseMotionGLUT(mouse_x, mouse_y);
  252. }
  253. }
  254. void key(unsigned char key, int mouse_x, int mouse_y)
  255. {
  256. using namespace std;
  257. switch(key)
  258. {
  259. // ESC
  260. case char(27):
  261. rebar.save(REBAR_NAME);
  262. // ^C
  263. case char(3):
  264. exit(0);
  265. default:
  266. if(!TwEventKeyboardGLUT(key,mouse_x,mouse_y))
  267. {
  268. cout<<"Unknown key command: "<<key<<" "<<int(key)<<endl;
  269. }
  270. }
  271. }
  272. int main(int argc, char * argv[])
  273. {
  274. using namespace Eigen;
  275. using namespace igl;
  276. using namespace std;
  277. // init mesh
  278. string filename = "../shared/beast.obj";
  279. if(argc < 2)
  280. {
  281. cerr<<"Usage:"<<endl<<" ./example input.obj"<<endl;
  282. cout<<endl<<"Opening default mesh..."<<endl;
  283. }else
  284. {
  285. // Read and prepare mesh
  286. filename = argv[1];
  287. }
  288. // dirname, basename, extension and filename
  289. string d,b,ext,f;
  290. pathinfo(filename,d,b,ext,f);
  291. // Convert extension to lower case
  292. transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
  293. vector<vector<double > > vV,vN,vTC;
  294. vector<vector<int > > vF,vFTC,vFN;
  295. if(ext == "obj")
  296. {
  297. // Convert extension to lower case
  298. if(!igl::readOBJ(filename,vV,vTC,vN,vF,vFTC,vFN))
  299. {
  300. return 1;
  301. }
  302. }else if(ext == "off")
  303. {
  304. // Convert extension to lower case
  305. if(!igl::readOFF(filename,vV,vF,vN))
  306. {
  307. return 1;
  308. }
  309. }else if(ext == "wrl")
  310. {
  311. // Convert extension to lower case
  312. if(!igl::readWRL(filename,vV,vF))
  313. {
  314. return 1;
  315. }
  316. //}else
  317. //{
  318. // // Convert extension to lower case
  319. // MatrixXi T;
  320. // if(!igl::readMESH(filename,V,T,F))
  321. // {
  322. // return 1;
  323. // }
  324. // //if(F.size() > T.size() || F.size() == 0)
  325. // {
  326. // boundary_faces(T,F);
  327. // }
  328. }
  329. if(vV.size() > 0)
  330. {
  331. if(!list_to_matrix(vV,V))
  332. {
  333. return 1;
  334. }
  335. triangulate(vF,F);
  336. }
  337. // Compute normals, centroid, colors, bounding box diagonal
  338. per_vertex_normals(V,F,N);
  339. mid = 0.5*(V.colwise().maxCoeff() + V.colwise().minCoeff());
  340. bbd = (V.colwise().maxCoeff() - V.colwise().minCoeff()).maxCoeff();
  341. // Init embree
  342. cout<<"init embree..."<<endl;
  343. ei = EmbreeIntersector<MatrixXd::Scalar,MatrixXi::Scalar>(V,F);
  344. // Init glut
  345. glutInit(&argc,argv);
  346. if( !TwInit(TW_OPENGL, NULL) )
  347. {
  348. // A fatal error occured
  349. fprintf(stderr, "AntTweakBar initialization failed: %s\n", TwGetLastError());
  350. return 1;
  351. }
  352. // Create a tweak bar
  353. rebar.TwNewBar("TweakBar");
  354. rebar.TwAddVarRW("scene_rot", TW_TYPE_QUAT4F, &scene_rot, "");
  355. rebar.TwAddVarRW("lights_on", TW_TYPE_BOOLCPP, &lights_on, "key=l");
  356. rebar.TwAddVarRW("color", TW_TYPE_COLOR4F, color.data(), "colormode=hls");
  357. rebar.TwAddVarRW("ao_factor", TW_TYPE_DOUBLE, &ao_factor, "min=0 max=1 step=0.2 keyIncr=] keyDecr=[ ");
  358. rebar.TwAddVarRW("ao_normalize", TW_TYPE_BOOLCPP, &ao_normalize, "key=n");
  359. rebar.TwAddVarRW("ao_on", TW_TYPE_BOOLCPP, &ao_on, "key=a");
  360. rebar.TwAddVarRW("light_intensity", TW_TYPE_DOUBLE, &light_intensity, "min=0 max=0.4 step=0.1 keyIncr=} keyDecr={ ");
  361. rebar.load(REBAR_NAME);
  362. glutInitDisplayString( "rgba depth double samples>=8 ");
  363. glutInitWindowSize(glutGet(GLUT_SCREEN_WIDTH)/2.0,glutGet(GLUT_SCREEN_HEIGHT));
  364. glutCreateWindow("ambient-occlusion");
  365. glutDisplayFunc(display);
  366. glutReshapeFunc(reshape);
  367. glutKeyboardFunc(key);
  368. glutMouseFunc(mouse);
  369. glutMotionFunc(mouse_drag);
  370. glutPassiveMotionFunc((GLUTmousemotionfun)TwEventMouseMotionGLUT);
  371. glutMainLoop();
  372. return 0;
  373. }