example.cpp 7.8 KB

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