#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 #include #include #include #ifdef __APPLE__ #include #include #else #include #endif #include #include #include #include #include #include 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; } std::string next_filename( const std::string & prefix, const int zeros, const std::string & suffix) { using namespace std; using namespace igl; // O(n) int i = 0; while(true) { string next = STR(prefix << setfill('0') << setw(zeros)< 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.coeffs().data(), down_x, down_y, mouse_x, mouse_y, q.coeffs().data()); 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"); 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; if(!mesh_with_skeleton(V,F,C,{},BE,{},10,TV,TT,_1)) { cout< > vV,vN,vTC; vector > vF,vFTC,vFN; if(ext == "obj") { // Convert extension to lower case if(!igl::readOBJ(filename,vV,vTC,vN,vF,vFTC,vFN)) { return 1; } }else if(ext == "off") { // Convert extension to lower case if(!igl::readOFF(filename,vV,vF,vN)) { return 1; } }else if(ext == "wrl") { // Convert extension to lower case if(!igl::readWRL(filename,vV,vF)) { return 1; } //}else //{ // // Convert extension to lower case // MatrixXi T; // if(!igl::readMESH(filename,V,T,F)) // { // return 1; // } // //if(F.size() > T.size() || F.size() == 0) // { // boundary_facets(T,F); // } } if(vV.size() > 0) { if(!list_to_matrix(vV,V)) { return 1; } polygon_mesh_to_triangle_mesh(vF,F); } if(output_prefix.size() == 0) { output_prefix = dir+"/"+name+"-pose-"; } { string output_filename = next_filename(output_prefix,4,".dmat"); 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; }