example.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. //#define IGL_HEADER_ONLY
  2. //#include <igl/OpenGL_convenience.h>
  3. //#include <igl/per_face_normals.h>
  4. //#include <igl/read.h>
  5. //#include <igl/normalize_row_lengths.h>
  6. //#include <igl/draw_mesh.h>
  7. //#include <igl/draw_floor.h>
  8. //#include <igl/unproject.h>
  9. //#include <igl/quat_to_mat.h>
  10. //#include <igl/trackball.h>
  11. //#include <igl/report_gl_error.h>
  12. #include <igl/embree/EmbreeIntersector.h>
  13. //#ifdef __APPLE__
  14. //# include <GLUT/glut.h>
  15. //#else
  16. //# include <GL/glut.h>
  17. //#endif
  18. //#include <Eigen/Core>
  19. //
  20. //#include <vector>
  21. #include <iostream>
  22. //// Width and height of window
  23. //int width,height;
  24. //// Rotation of scene
  25. //float scene_rot[4] = {0,0,0,1};
  26. //// information at mouse down
  27. //float down_scene_rot[4] = {0,0,0,1};
  28. //bool trackball_on = false;
  29. //int down_mouse_x,down_mouse_y;
  30. //// Position of light
  31. //float light_pos[4] = {0.1,0.1,-0.9,0};
  32. //// Vertex positions, normals, colors and centroid
  33. //Eigen::MatrixXd V,N,C,mean;
  34. //// Bounding box diagonal length
  35. //double bbd;
  36. //// Faces
  37. //Eigen::MatrixXi F;
  38. // Embree intersection structure
  39. igl::EmbreeIntersector<double,int> ei;
  40. //// Hits collected
  41. //std::vector<igl::Hit > hits;
  42. //// Ray information, "projection screen" corners
  43. //Eigen::Vector3d win_s,s,d,dir,NW,NE,SE,SW;
  44. //// Textures and framebuffers for "projection screen"
  45. //GLuint tex_id = 0, fbo_id = 0, dfbo_id = 0;
  46. //
  47. //// Initialize textures and framebuffers. Must be called if window changes
  48. //// dimension
  49. //void init_texture()
  50. //{
  51. // using namespace igl;
  52. // using namespace std;
  53. // // Set up a "render-to-texture" frame buffer and texture combo
  54. // glDeleteTextures(1,&tex_id);
  55. // glDeleteFramebuffersEXT(1,&fbo_id);
  56. // glDeleteFramebuffersEXT(1,&dfbo_id);
  57. // // http://www.opengl.org/wiki/Framebuffer_Object_Examples#Quick_example.2C_render_to_texture_.282D.29
  58. // glGenTextures(1, &tex_id);
  59. // glBindTexture(GL_TEXTURE_2D, tex_id);
  60. // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  61. // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  62. // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  63. // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  64. // //NULL means reserve texture memory, but texels are undefined
  65. // glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
  66. // glBindTexture(GL_TEXTURE_2D, 0);
  67. // //-------------------------
  68. // glGenFramebuffersEXT(1, &fbo_id);
  69. // glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo_id);
  70. // //Attach 2D texture to this FBO
  71. // glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, tex_id, 0);
  72. // glGenRenderbuffersEXT(1, &dfbo_id);
  73. // glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, dfbo_id);
  74. // glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, width, height);
  75. // //-------------------------
  76. // //Attach depth buffer to FBO
  77. // glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, dfbo_id);
  78. // //-------------------------
  79. // //Does the GPU support current FBO configuration?
  80. // GLenum status;
  81. // status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
  82. // switch(status)
  83. // {
  84. // case GL_FRAMEBUFFER_COMPLETE_EXT:
  85. // break;
  86. // default:
  87. // cout<<"error"<<endl;
  88. // }
  89. // glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
  90. // glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  91. //}
  92. //
  93. //void reshape(int width,int height)
  94. //{
  95. // using namespace std;
  96. // // Save width and height
  97. // ::width = width;
  98. // ::height = height;
  99. // // Re-initialize textures and frame bufferes
  100. // init_texture();
  101. // glMatrixMode(GL_PROJECTION);
  102. // glLoadIdentity();
  103. // glViewport(0,0,width,height);
  104. //}
  105. //
  106. //// Set up double-sided lights
  107. //void lights()
  108. //{
  109. // using namespace std;
  110. // glEnable(GL_LIGHTING);
  111. // glLightModelf(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
  112. // glEnable(GL_LIGHT0);
  113. // glEnable(GL_LIGHT1);
  114. // float ones[4] = {1.0,1.0,1.0,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,zeros);
  119. // glLightfv(GL_LIGHT0,GL_DIFFUSE,ones);
  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,zeros);
  126. // glLightfv(GL_LIGHT1,GL_DIFFUSE,ones);
  127. // glLightfv(GL_LIGHT1,GL_SPECULAR,zeros);
  128. // glLightfv(GL_LIGHT1,GL_POSITION,pos);
  129. //}
  130. //
  131. //// Set up projection and model view of scene
  132. //void push_scene()
  133. //{
  134. // using namespace igl;
  135. // glMatrixMode(GL_PROJECTION);
  136. // glLoadIdentity();
  137. // gluPerspective(45,(double)width/(double)height,1e-2,100);
  138. // glMatrixMode(GL_MODELVIEW);
  139. // glLoadIdentity();
  140. // gluLookAt(0,0,3,0,0,0,0,1,0);
  141. // glPushMatrix();
  142. // float mat[4*4];
  143. // quat_to_mat(scene_rot,mat);
  144. // glMultMatrixf(mat);
  145. //}
  146. //
  147. //void pop_scene()
  148. //{
  149. // glPopMatrix();
  150. //}
  151. //
  152. //// Scale and shift for object
  153. //void push_object()
  154. //{
  155. // glPushMatrix();
  156. // glScaled(2./bbd,2./bbd,2./bbd);
  157. // glTranslated(-mean(0,0),-mean(0,1),-mean(0,2));
  158. //}
  159. //
  160. //void pop_object()
  161. //{
  162. // glPopMatrix();
  163. //}
  164. //
  165. //const float back[4] = {190.0/255.0,190.0/255.0,190.0/255.0,0};
  166. //void display()
  167. //{
  168. // using namespace Eigen;
  169. // using namespace igl;
  170. // using namespace std;
  171. // glClearColor(back[0],back[1],back[2],0);
  172. // glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  173. // // All smooth points
  174. // glEnable( GL_POINT_SMOOTH );
  175. //
  176. // lights();
  177. // push_scene();
  178. // glEnable(GL_DEPTH_TEST);
  179. // glDepthFunc(GL_LEQUAL);
  180. // glEnable(GL_NORMALIZE);
  181. // glEnable(GL_COLOR_MATERIAL);
  182. // glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
  183. // push_object();
  184. //
  185. // if(trackball_on)
  186. // {
  187. // // Draw a "laser" line
  188. // glLineWidth(3.0);
  189. // glDisable(GL_LIGHTING);
  190. // glEnable(GL_DEPTH_TEST);
  191. // glBegin(GL_LINES);
  192. // glColor3f(1,0,0);
  193. // glVertex3dv(s.data());
  194. // glColor3f(1,0,0);
  195. // glVertex3dv(d.data());
  196. // glEnd();
  197. //
  198. // // Draw the start and end points used for ray
  199. // glPointSize(10.0);
  200. // glBegin(GL_POINTS);
  201. // glColor3f(1,0,0);
  202. // glVertex3dv(s.data());
  203. // glColor3f(0,0,1);
  204. // glVertex3dv(d.data());
  205. // glEnd();
  206. // }
  207. //
  208. // // Draw the model
  209. // glEnable(GL_LIGHTING);
  210. // draw_mesh(V,F,N,C);
  211. //
  212. // // Draw all hits
  213. // glBegin(GL_POINTS);
  214. // glColor3f(0,0.2,0.2);
  215. // for(vector<igl::Hit>::iterator hit = hits.begin();
  216. // hit != hits.end();
  217. // hit++)
  218. // {
  219. // const double w0 = (1.0-hit->u-hit->v);
  220. // const double w1 = hit->u;
  221. // const double w2 = hit->v;
  222. // VectorXd hitP =
  223. // w0 * V.row(F(hit->id,0)) +
  224. // w1 * V.row(F(hit->id,1)) +
  225. // w2 * V.row(F(hit->id,2));
  226. // glVertex3dv(hitP.data());
  227. // }
  228. // glEnd();
  229. //
  230. // pop_object();
  231. //
  232. // // Draw a nice floor
  233. // glPushMatrix();
  234. // glEnable(GL_LIGHTING);
  235. // glTranslated(0,-1,0);
  236. // draw_floor();
  237. // glPopMatrix();
  238. //
  239. // // draw a transparent "projection screen" show model at time of hit (aka
  240. // // mouse down)
  241. // push_object();
  242. // if(trackball_on)
  243. // {
  244. // glColor4f(0,0,0,1.0);
  245. // glPointSize(10.0);
  246. // glBegin(GL_POINTS);
  247. // glVertex3dv(SW.data());
  248. // glVertex3dv(SE.data());
  249. // glVertex3dv(NE.data());
  250. // glVertex3dv(NW.data());
  251. // glEnd();
  252. //
  253. // glDisable(GL_LIGHTING);
  254. // glEnable(GL_TEXTURE_2D);
  255. // glBindTexture(GL_TEXTURE_2D, tex_id);
  256. // glColor4f(1,1,1,0.7);
  257. // glBegin(GL_QUADS);
  258. // glTexCoord2d(0,0);
  259. // glVertex3dv(SW.data());
  260. // glTexCoord2d(1,0);
  261. // glVertex3dv(SE.data());
  262. // glTexCoord2d(1,1);
  263. // glVertex3dv(NE.data());
  264. // glTexCoord2d(0,1);
  265. // glVertex3dv(NW.data());
  266. // glEnd();
  267. // glBindTexture(GL_TEXTURE_2D, 0);
  268. // glDisable(GL_TEXTURE_2D);
  269. // }
  270. // pop_object();
  271. // pop_scene();
  272. //
  273. // // Draw a faint point over mouse
  274. // if(!trackball_on)
  275. // {
  276. // glDisable(GL_LIGHTING);
  277. // glDisable(GL_COLOR_MATERIAL);
  278. // glDisable(GL_DEPTH_TEST);
  279. // glEnable(GL_BLEND);
  280. // glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  281. // glColor4f(1.0,0.3,0.3,0.6);
  282. // glMatrixMode(GL_PROJECTION);
  283. // glLoadIdentity();
  284. // gluOrtho2D(0,width,0,height);
  285. // glMatrixMode(GL_MODELVIEW);
  286. // glLoadIdentity();
  287. // glPointSize(20.0);
  288. // glBegin(GL_POINTS);
  289. // glVertex2dv(win_s.data());
  290. // glEnd();
  291. // }
  292. // report_gl_error();
  293. //
  294. // glutSwapBuffers();
  295. // glutPostRedisplay();
  296. //}
  297. //
  298. //// Initialize colors to a boring green
  299. //void init_C()
  300. //{
  301. // C.col(0).setConstant(0.4);
  302. // C.col(1).setConstant(0.8);
  303. // C.col(2).setConstant(0.3);
  304. //}
  305. //
  306. //void mouse_move(int mouse_x, int mouse_y)
  307. //{
  308. // using namespace std;
  309. // using namespace Eigen;
  310. // using namespace igl;
  311. // using namespace std;
  312. // init_C();
  313. // glutSetCursor(GLUT_CURSOR_CROSSHAIR);
  314. // // Push scene and object
  315. // push_scene();
  316. // push_object();
  317. // // Unproject mouse at 0 depth and some positive depth
  318. // win_s = Vector3d(mouse_x,height-mouse_y,0);
  319. // Vector3d win_d(mouse_x,height-mouse_y,1);
  320. // unproject(win_s,s);
  321. // unproject(win_d,d);
  322. // pop_object();
  323. // pop_scene();
  324. // report_gl_error();
  325. // // Shoot ray at unprojected mouse in view direction
  326. // dir = d-s;
  327. // int num_rays_shot;
  328. // ei.intersectRay(s,dir,hits,num_rays_shot);
  329. // for(vector<igl::Hit>::iterator hit = hits.begin();
  330. // hit != hits.end();
  331. // hit++)
  332. // {
  333. // // Change color of hit faces
  334. // C(hit->id,0) = 1;
  335. // C(hit->id,1) = 0.4;
  336. // C(hit->id,2) = 0.4;
  337. // }
  338. //}
  339. //
  340. //void mouse(int glutButton, int glutState, int mouse_x, int mouse_y)
  341. //{
  342. // using namespace std;
  343. // using namespace Eigen;
  344. // using namespace igl;
  345. // switch(glutState)
  346. // {
  347. // case 1:
  348. // // up
  349. // glutSetCursor(GLUT_CURSOR_CROSSHAIR);
  350. // trackball_on = false;
  351. // hits.clear();
  352. // init_C();
  353. // break;
  354. // case 0:
  355. // // be sure this has been called recently
  356. // mouse_move(mouse_x,mouse_y);
  357. // // down
  358. // glutSetCursor(GLUT_CURSOR_CYCLE);
  359. // // collect information for trackball
  360. // trackball_on = true;
  361. // copy(scene_rot,scene_rot+4,down_scene_rot);
  362. // down_mouse_x = mouse_x;
  363. // down_mouse_y = mouse_y;
  364. // // Collect "projection screen" locations
  365. // push_scene();
  366. // push_object();
  367. // // unproject corners of window
  368. // const double depth = 0.999;
  369. // Vector3d win_NW( 0,height,depth);
  370. // Vector3d win_NE(width,height,depth);
  371. // Vector3d win_SE(width,0,depth);
  372. // Vector3d win_SW(0,0,depth);
  373. // unproject(win_NW,NW);
  374. // unproject(win_NE,NE);
  375. // unproject(win_SE,SE);
  376. // unproject(win_SW,SW);
  377. // // render to framebuffer
  378. // glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo_id);
  379. // glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, dfbo_id);
  380. // glClearColor(0,0,0,1);
  381. // glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  382. // // Render the model ---> to the framebuffer attached to the "projection
  383. // // screen" texture
  384. // glEnable(GL_COLOR_MATERIAL);
  385. // glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
  386. // glEnable(GL_LIGHTING);
  387. // glEnable(GL_DEPTH_TEST);
  388. // draw_mesh(V,F,N,C);
  389. // pop_object();
  390. // pop_scene();
  391. // glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  392. // glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
  393. // break;
  394. // }
  395. //}
  396. //
  397. //void mouse_drag(int mouse_x, int mouse_y)
  398. //{
  399. // using namespace igl;
  400. //
  401. // if(trackball_on)
  402. // {
  403. // // Rotate according to trackball
  404. // trackball<float>(
  405. // width,
  406. // height,
  407. // 2,
  408. // down_scene_rot,
  409. // down_mouse_x,
  410. // down_mouse_y,
  411. // mouse_x,
  412. // mouse_y,
  413. // scene_rot);
  414. // }
  415. //}
  416. //
  417. //
  418. //void key(unsigned char key, int mouse_x, int mouse_y)
  419. //{
  420. // using namespace std;
  421. // switch(key)
  422. // {
  423. // // Ctrl-c and esc exit
  424. // case char(3):
  425. // case char(27):
  426. // exit(0);
  427. // default:
  428. // cout<<"Unknown key command: "<<key<<" "<<int(key)<<endl;
  429. // }
  430. //
  431. //}
  432. //
  433. //int main(int argc, char * argv[])
  434. int main()
  435. {
  436. // using namespace Eigen;
  437. // using namespace igl;
  438. using namespace std;
  439. // // init mesh
  440. // string filename = "../shared/decimated-knight.obj";
  441. // if(argc < 2)
  442. // {
  443. // cerr<<"Usage:"<<endl<<" ./example input.obj"<<endl;
  444. // cout<<endl<<"Opening default mesh..."<<endl;
  445. // }else
  446. // {
  447. // // Read and prepare mesh
  448. // filename = argv[1];
  449. // }
  450. // if(!read(filename,V,F))
  451. // {
  452. // return 1;
  453. // }
  454. // // Compute normals, centroid, colors, bounding box diagonal
  455. // per_face_normals(V,F,N);
  456. // normalize_row_lengths(N,N);
  457. // mean = V.colwise().mean();
  458. // C.resize(F.rows(),3);
  459. // init_C();
  460. // bbd =
  461. // (V.colwise().maxCoeff() -
  462. // V.colwise().minCoeff()).maxCoeff();
  463. //
  464. // // Init embree
  465. // ei = EmbreeIntersector<double,int>(V,F);
  466. //
  467. // // Init glut
  468. // glutInit(&argc,argv);
  469. // glutInitDisplayString( "rgba depth double samples>=8 ");
  470. // glutInitWindowSize(glutGet(GLUT_SCREEN_WIDTH)/2.0,glutGet(GLUT_SCREEN_HEIGHT));
  471. // glutCreateWindow("embree");
  472. // glutDisplayFunc(display);
  473. // glutReshapeFunc(reshape);
  474. // glutKeyboardFunc(key);
  475. // glutMouseFunc(mouse);
  476. // glutMotionFunc(mouse_drag);
  477. // glutPassiveMotionFunc(mouse_move);
  478. // glutMainLoop();
  479. return 0;
  480. }