example.cpp 7.7 KB

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