#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef __APPLE__ #include #include #else #include #endif #include #include #include #include #include #include #define VERBOSE enum SkelStyleType { SKEL_STYLE_TYPE_3D = 0, SKEL_STYLE_TYPE_VECTOR_GRAPHICS = 1, NUM_SKEL_STYLE_TYPE = 2 }skel_style; Eigen::MatrixXd V,N,W,M; Eigen::Vector3d Vmid; double bbd = 1.0; Eigen::MatrixXi F; igl::Camera camera; Eigen::MatrixXd C; Eigen::MatrixXi BE; Eigen::VectorXi P,RP; struct State { igl::MouseController mouse; Eigen::MatrixXf colors; } s; bool wireframe = false; // See README for descriptions enum RotationType { ROTATION_TYPE_IGL_TRACKBALL = 0, ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP = 1, NUM_ROTATION_TYPES = 2, } rotation_type = ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP; std::stack undo_stack; std::stack redo_stack; bool is_rotating = false; bool centroid_is_visible = true; int down_x,down_y; igl::Camera down_camera; std::string output_prefix; struct CameraAnimation { bool is_animating = false; double DURATION = 0.5; double start_time = 0; Eigen::Quaterniond from_quat,to_quat; } canim; typedef std::vector< Eigen::Quaterniond, Eigen::aligned_allocator > RotationList; struct PoseAnimation { bool is_animating = false; double DURATION = 2; double start_time = 0; RotationList pose; } panim; int width,height; Eigen::Vector4f light_pos(-0.1,-0.1,0.9,0); #define REBAR_NAME "temp.rbr" igl::ReTwBar rebar; void push_undo() { undo_stack.push(s); // Clear redo_stack = std::stack(); } // No-op setter, does nothing void TW_CALL no_op(const void * /*value*/, void * /*clientData*/) { } void TW_CALL set_rotation_type(const void * value, void * clientData) { using namespace Eigen; using namespace std; using namespace igl; const RotationType old_rotation_type = rotation_type; rotation_type = *(const RotationType *)(value); if(rotation_type == ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP && old_rotation_type != ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP) { push_undo(); canim.from_quat = camera.m_rotation_conj; snap_to_fixed_up(canim.from_quat,canim.to_quat); // start animation canim.start_time = get_seconds(); canim.is_animating = true; } } void TW_CALL get_rotation_type(void * value, void *clientData) { RotationType * rt = (RotationType *)(value); *rt = rotation_type; } void reshape(int width, int height) { ::width = width; ::height = height; glViewport(0,0,width,height); // Send the new window size to AntTweakBar TwWindowSize(width, height); camera.m_aspect = (double)width/(double)height; s.mouse.reshape(width,height); } void push_scene() { using namespace igl; using namespace std; glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); gluPerspective(camera.m_angle,camera.m_aspect,camera.m_near,camera.m_far); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); gluLookAt( camera.eye()(0), camera.eye()(1), camera.eye()(2), camera.at()(0), camera.at()(1), camera.at()(2), camera.up()(0), camera.up()(1), camera.up()(2)); } void push_object() { using namespace igl; glPushMatrix(); glScaled(2./bbd,2./bbd,2./bbd); glTranslated(-Vmid(0),-Vmid(1),-Vmid(2)); } void pop_object() { glPopMatrix(); } void pop_scene() { glMatrixMode(GL_PROJECTION); glPopMatrix(); glMatrixMode(GL_MODELVIEW); glPopMatrix(); } // Set up double-sided lights void lights() { using namespace std; using namespace Eigen; glEnable(GL_LIGHTING); glLightModelf(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE); glEnable(GL_LIGHT0); glEnable(GL_LIGHT1); float WHITE[4] = {0.8,0.8,0.8,1.}; float GREY[4] = {0.4,0.4,0.4,1.}; float BLACK[4] = {0.,0.,0.,1.}; Vector4f pos = light_pos; glLightfv(GL_LIGHT0,GL_AMBIENT,GREY); glLightfv(GL_LIGHT0,GL_DIFFUSE,WHITE); glLightfv(GL_LIGHT0,GL_SPECULAR,BLACK); glLightfv(GL_LIGHT0,GL_POSITION,pos.data()); pos(0) *= -1; pos(1) *= -1; pos(2) *= -1; glLightfv(GL_LIGHT1,GL_AMBIENT,GREY); glLightfv(GL_LIGHT1,GL_DIFFUSE,WHITE); glLightfv(GL_LIGHT1,GL_SPECULAR,BLACK); glLightfv(GL_LIGHT1,GL_POSITION,pos.data()); } void display() { using namespace igl; using namespace std; using namespace Eigen; const float back[4] = {0.75, 0.75, 0.75,0}; glClearColor(back[0],back[1],back[2],0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); if(canim.is_animating) { double t = (get_seconds() - canim.start_time)/canim.DURATION; if(t > 1) { t = 1; canim.is_animating = false; } Quaterniond q = canim.from_quat.slerp(t,canim.to_quat).normalized(); camera.orbit(q.conjugate()); } glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glEnable(GL_NORMALIZE); lights(); push_scene(); // Draw a nice floor glEnable(GL_DEPTH_TEST); glPushMatrix(); const double floor_offset = -2./bbd*(V.col(1).maxCoeff()-Vmid(1)); glTranslated(0,floor_offset,0); const float GREY[4] = {0.5,0.5,0.6,1.0}; const float DARK_GREY[4] = {0.2,0.2,0.3,1.0}; glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); glEnable(GL_CULL_FACE); glCullFace(GL_BACK); draw_floor(GREY,DARK_GREY); glDisable(GL_CULL_FACE); glPopMatrix(); push_object(); const auto & draw_skeleton = [](const MatrixXd & T) { switch(skel_style) { default: case SKEL_STYLE_TYPE_3D: { draw_skeleton_3d(C,BE,T,s.colors); break; } case SKEL_STYLE_TYPE_VECTOR_GRAPHICS: draw_skeleton_vector_graphics(C,BE,T); break; } }; // Set material properties glDisable(GL_COLOR_MATERIAL); glMaterialfv(GL_FRONT, GL_AMBIENT,GOLD_AMBIENT); glMaterialfv(GL_FRONT, GL_DIFFUSE,GOLD_DIFFUSE); glMaterialfv(GL_FRONT, GL_SPECULAR,GOLD_SPECULAR); glMaterialf (GL_FRONT, GL_SHININESS, 128); glMaterialfv(GL_BACK, GL_AMBIENT,SILVER_AMBIENT); glMaterialfv(GL_BACK, GL_DIFFUSE,FAST_GREEN_DIFFUSE); glMaterialfv(GL_BACK, GL_SPECULAR,SILVER_SPECULAR); glMaterialf (GL_BACK, GL_SHININESS, 128); if(wireframe) { glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); } glLineWidth(1.0); MatrixXd T; RotationList dQ; if(panim.is_animating) { double t = (get_seconds() - panim.start_time)/panim.DURATION; if(t > 1) { t = 1; panim.is_animating = false; } const auto & ease = [](const double t) { return 3.*t*t-2.*t*t*t; }; double f = (t<0.5?ease(2.*t):ease(2.-2.*t)); dQ.resize(panim.pose.size()); for(int e = 0;e<(int)panim.pose.size();e++) { dQ[e] = panim.pose[e].slerp(f,Quaterniond::Identity()).normalized(); } }else { dQ = s.mouse.rotations(); } forward_kinematics(C,BE,P,dQ,T); MatrixXd U = M*T; MatrixXd UN; per_face_normals(U,F,UN); draw_mesh(U,F,UN); glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); glDisable(GL_DEPTH_TEST); draw_skeleton(T); if(centroid_is_visible) { Vector3d cen; centroid(U,F,cen); glEnable(GL_DEPTH_TEST); glPushMatrix(); glTranslated(cen(0),cen(1),cen(2)); glScaled(bbd/2.,bbd/2.,bbd/2.); glScaled(0.1,0.1,0.1); glEnable(GL_POLYGON_OFFSET_FILL); glPolygonOffset(0,-100000); draw_beach_ball(); glDisable(GL_POLYGON_OFFSET_FILL); glPopMatrix(); } // Mouse is always on top glDisable(GL_DEPTH_TEST); if(!panim.is_animating) { s.mouse.draw(); } pop_object(); pop_scene(); report_gl_error(); TwDraw(); glutSwapBuffers(); if(canim.is_animating || panim.is_animating) { glutPostRedisplay(); } } void mouse_wheel(int wheel, int direction, int mouse_x, int mouse_y) { using namespace std; using namespace igl; using namespace Eigen; GLint viewport[4]; glGetIntegerv(GL_VIEWPORT,viewport); if(wheel == 0 && TwMouseMotion(mouse_x, viewport[3] - mouse_y)) { static double mouse_scroll_y = 0; const double delta_y = 0.125*direction; mouse_scroll_y += delta_y; TwMouseWheel(mouse_scroll_y); return; } push_undo(); if(wheel==0) { // factor of zoom change double s = (1.-0.01*direction); //// FOV zoom: just widen angle. This is hardly ever appropriate. //camera.m_angle *= s; //camera.m_angle = min(max(camera.m_angle,1),89); camera.push_away(s); }else { // Dolly zoom: camera.dolly_zoom((double)direction*1.0); } glutPostRedisplay(); } void mouse(int glutButton, int glutState, int mouse_x, int mouse_y) { using namespace std; using namespace Eigen; using namespace igl; bool tw_using = TwEventMouseButtonGLUT(glutButton,glutState,mouse_x,mouse_y); const int mod = (glutButton <=2 ? glutGetModifiers() : 0); const bool option_down = mod & GLUT_ACTIVE_ALT; switch(glutButton) { case GLUT_RIGHT_BUTTON: case GLUT_LEFT_BUTTON: { push_scene(); push_object(); switch(glutState) { case 1: { // up const bool mouse_was_selecting = s.mouse.is_selecting(); is_rotating = false; s.mouse.up(mouse_x,mouse_y); glutSetCursor(GLUT_CURSOR_INHERIT); if(mouse_was_selecting) { s.mouse.set_selection_from_last_drag(C,BE,P,RP); MouseController::VectorXb S; MouseController::propogate_to_descendants_if( s.mouse.selection(),P,S); MouseController::color_if(S,MAYA_SEA_GREEN,MAYA_VIOLET,s.colors); } break; } case 0: if(!tw_using) { down_x = mouse_x; down_y = mouse_y; if(option_down || glutButton==GLUT_RIGHT_BUTTON) { glutSetCursor(GLUT_CURSOR_CYCLE); // collect information for trackball is_rotating = true; down_camera = camera; }else { push_undo(); s.mouse.down(mouse_x,mouse_y); } } break; } pop_object(); pop_scene(); break; } // Scroll down case 3: { mouse_wheel(0,-1,mouse_x,mouse_y); break; } // Scroll up case 4: { mouse_wheel(0,1,mouse_x,mouse_y); break; } // Scroll left case 5: { mouse_wheel(1,-1,mouse_x,mouse_y); break; } // Scroll right case 6: { mouse_wheel(1,1,mouse_x,mouse_y); break; } } glutPostRedisplay(); } void mouse_drag(int mouse_x, int mouse_y) { using namespace igl; using namespace std; using namespace Eigen; push_scene(); push_object(); if(is_rotating) { glutSetCursor(GLUT_CURSOR_CYCLE); Quaterniond q; switch(rotation_type) { case ROTATION_TYPE_IGL_TRACKBALL: { // Rotate according to trackball igl::trackball( width, height, 2.0, down_camera.m_rotation_conj, down_x, down_y, mouse_x, mouse_y, q); break; } case ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP: { // Rotate according to two axis valuator with fixed up vector two_axis_valuator_fixed_up( width, height, 2.0, down_camera.m_rotation_conj, down_x, down_y, mouse_x, mouse_y, q); break; } default: break; } camera.orbit(q.conjugate()); }else if(s.mouse.drag(mouse_x,mouse_y)) { } pop_object(); pop_scene(); glutPostRedisplay(); } void init_relative() { using namespace Eigen; using namespace igl; using namespace std; per_face_normals(V,F,N); const auto Vmax = V.colwise().maxCoeff(); const auto Vmin = V.colwise().minCoeff(); Vmid = 0.5*(Vmax + Vmin); bbd = (Vmax-Vmin).norm(); camera.push_away(2); } void undo() { using namespace std; if(!undo_stack.empty()) { redo_stack.push(s); s = undo_stack.top(); undo_stack.pop(); s.mouse.reshape(width,height); } } void redo() { using namespace std; if(!redo_stack.empty()) { undo_stack.push(s); s = redo_stack.top(); redo_stack.pop(); s.mouse.reshape(width,height); } } bool save() { using namespace std; using namespace igl; using namespace Eigen; string output_filename; next_filename(output_prefix,4,".dmat",output_filename); MatrixXd T; forward_kinematics(C,BE,P,s.mouse.rotations(),T); if(writeDMAT(output_filename,T)) { cout<thresh).cast().sum(); MatrixXi CT(count,TT.cols()); int c = 0; for(int t = 0;tthresh) { CT.row(c++) = TT.row(t); } } assert(c==count); boundary_facets(CT,CF); } return true; } bool robust_weights( const Eigen::MatrixXd & V, const Eigen::MatrixXi & F, const Eigen::MatrixXd & C, const Eigen::MatrixXi & BE, Eigen::MatrixXd & W) { using namespace igl; using namespace Eigen; using namespace std; // clean mesh MatrixXd CV; MatrixXi CF; if(!clean(V,F,CV,CF)) { return false; } MatrixXd TV; MatrixXi TT; // compute tet-mesh { MatrixXi _1; #ifdef VERBOSE cout<<"mesh_with_skeleton"<thresh).cast().sum(); TT.resize(count,oldTT.cols()); int c = 0; for(int t = 0;tthresh) { TT.row(c++) = oldTT.row(t); } } } // compute weights VectorXi b; MatrixXd bc; if(!boundary_conditions(TV,TT,C,{},BE,{},b,bc)) { cout<(0,BE.rows()-1,RP); assert(RP.size() == BE.rows()); // Bone parents bone_parents(BE,P); if(weights_filename.size() == 0) { robust_weights(V,F,C,BE,W); }else { // Read in weights and precompute LBS matrix readDMAT(weights_filename,W); } lbs_matrix(V,W,M); init_relative(); // Init glut glutInit(&argc,argv); if( !TwInit(TW_OPENGL, NULL) ) { // A fatal error occured fprintf(stderr, "AntTweakBar initialization failed: %s\n", TwGetLastError()); return 1; } // Create a tweak bar rebar.TwNewBar("TweakBar"); rebar.TwAddVarRW("camera_rotation", TW_TYPE_QUAT4D, camera.m_rotation_conj.coeffs().data(), "open readonly=true"); TwType RotationTypeTW = ReTwDefineEnumFromString("RotationType", "igl_trackball,two-a...-fixed-up"); rebar.TwAddVarCB( "rotation_type", RotationTypeTW, set_rotation_type,get_rotation_type,NULL,"keyIncr=] keyDecr=["); rebar.TwAddVarRW("wireframe", TW_TYPE_BOOLCPP,&wireframe,"key=l"); rebar.TwAddVarRW("centroid_is_visible", TW_TYPE_BOOLCPP,¢roid_is_visible, "keyIncr=C keyDecr=c label='centroid visible?'"); TwType SkelStyleTypeTW = ReTwDefineEnumFromString("SkelStyleType", "3d,vector-graphics"); rebar.TwAddVarRW("style",SkelStyleTypeTW,&skel_style,""); rebar.load(REBAR_NAME); // Init antweakbar glutInitDisplayString( "rgba depth double samples>=8 "); glutInitWindowSize(glutGet(GLUT_SCREEN_WIDTH)/2.0,glutGet(GLUT_SCREEN_HEIGHT)/2.0); glutCreateWindow("skeleton-poser"); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(key); glutMouseFunc(mouse); glutMotionFunc(mouse_drag); glutPassiveMotionFunc((GLUTmousemotionfun)TwEventMouseMotionGLUT); glutMainLoop(); return 0; }