example.cpp 8.1 KB

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