#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef __APPLE__ # include #else # include #endif #include #include #include // Width and height of window int width,height; // Rotation of scene float scene_rot[4] = {0,0,0,1}; // information at mouse down float down_scene_rot[4] = {0,0,0,1}; bool trackball_on = false; int down_mouse_x,down_mouse_y; // Position of light float light_pos[4] = {0.1,0.1,-0.9,0}; // Vertex positions, normals, colors and centroid Eigen::MatrixXd V,N,C,mid; // Faces Eigen::MatrixXi F; // Bounding box diagonal length double bbd; igl::EmbreeIntersector ei; // Running ambient occlusion Eigen::VectorXd S; int tot_num_samples = 0; void reshape(int width,int height) { using namespace std; // Save width and height ::width = width; ::height = height; glMatrixMode(GL_PROJECTION); glLoadIdentity(); glViewport(0,0,width,height); } // Set up projection and model view of scene void push_scene() { using namespace igl; glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45,(double)width/(double)height,1e-2,100); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0,0,3,0,0,0,0,1,0); glPushMatrix(); float mat[4*4]; quat_to_mat(scene_rot,mat); glMultMatrixf(mat); } void pop_scene() { glPopMatrix(); } // Scale and shift for object void push_object() { glPushMatrix(); glScaled(2./bbd,2./bbd,2./bbd); glTranslated(-mid(0,0),-mid(0,1),-mid(0,2)); } void pop_object() { glPopMatrix(); } const float back[4] = {30.0/255.0,30.0/255.0,50.0/255.0,0}; void display() { using namespace Eigen; using namespace igl; using namespace std; if(!trackball_on && tot_num_samples < 10000) { if(S.size() == 0) { S.resize(V.rows()); S.setZero(); } VectorXd Si; const int num_samples = 20; ambient_occlusion(ei,V,N,num_samples,Si); S *= (double)tot_num_samples; S += Si*(double)num_samples; tot_num_samples += num_samples; S /= (double)tot_num_samples; // Convert to 1-intensity C.resize(S.rows(),3); C<( width, height, 2, down_scene_rot, down_mouse_x, down_mouse_y, mouse_x, mouse_y, scene_rot); } } void key(unsigned char key, int mouse_x, int mouse_y) { using namespace std; switch(key) { // Ctrl-c and esc exit case char(3): case char(27): exit(0); default: cout<<"Unknown key command: "<(V,F); // Init glut glutInit(&argc,argv); glutInitDisplayString( "rgba depth double samples>=8 "); glutInitWindowSize(glutGet(GLUT_SCREEN_WIDTH)/2.0,glutGet(GLUT_SCREEN_HEIGHT)); glutCreateWindow("ambient-occlusion"); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(key); glutMouseFunc(mouse); glutMotionFunc(mouse_drag); glutMainLoop(); return 0; }