├── README.md └── SkinnedMeshDemo_opengl.cpp /README.md: -------------------------------------------------------------------------------- 1 | # SkinnedMeshDemo 2 | 本git库包含 《Skinned Mesh 原理解析和一个最简单的实现示例》一文的skinned mesh示例程序。 3 | 这篇文章写于2008年,最初发表在CSDN上,至今已超过10年了,但仍然有人在看。考虑到是基础性的知识,于当前仍然有一定的意义,所以将代码和文章留存在github上。 4 | 文章备份于本git库的wiki上,有关此文的讨论欢迎在此进行。 5 | -------------------------------------------------------------------------------- /SkinnedMeshDemo_opengl.cpp: -------------------------------------------------------------------------------- 1 | // A simplest Skinned Mesh demo, written by n5, 2008.10, 2 | 3 | // My email:happyfirecn@yahoo.com.cn 4 | 5 | // My blog: http://blog.csdn.net/n5 6 | 7 | 8 | 9 | #include 10 | 11 | 12 | 13 | #define NULL 0 14 | 15 | 16 | 17 | //------------------------------------------------------------- 18 | 19 | 20 | 21 | class BoneOffset 22 | 23 | { 24 | 25 | public: 26 | 27 | //BoneOffset transform a vertex from mesh space to bone space. 28 | 29 | //In other words, it is the offset from mesh space to a bone's space. 30 | 31 | //For each bone, there is a BoneOffest. 32 | 33 | //If we add the offset to the vertex's pos (in mesh space), we get the vertex's pos in bone space 34 | 35 | //For example: if a vertex's pos in mesh space is (100,0,0), the bone offset is (-20,0,0), so the vertex's pos in bone space is (80,0,0) 36 | 37 | //Actually, BoneOffset is the invert transform of that we place a bone in mesh space, that is (-20,0,0) means the bone is at (20,0,0) in mesh space 38 | 39 | float m_offx, m_offy, m_offz; 40 | 41 | }; 42 | 43 | 44 | 45 | //---------------------------------------------------------------- 46 | 47 | 48 | 49 | class Bone 50 | 51 | { 52 | 53 | public: 54 | 55 | Bone() {} 56 | 57 | Bone(float x, float y, float z):m_pSibling(NULL),m_pFirstChild(NULL),m_pFather(NULL),m_x(x),m_y(y),m_z(z){} 58 | 59 | 60 | 61 | ~Bone() {} 62 | 63 | 64 | 65 | Bone* m_pSibling; 66 | 67 | Bone* m_pFirstChild; 68 | 69 | Bone* m_pFather; //only for draw bone 70 | 71 | 72 | 73 | void SetFirstChild(Bone* pChild) { m_pFirstChild = pChild; m_pFirstChild->m_pFather = this; } 74 | 75 | void SetSibling(Bone* pSibling) { m_pSibling = pSibling; m_pSibling->m_pFather = m_pFather; } 76 | 77 | 78 | 79 | float m_x, m_y, m_z;//pos in its parent's space 80 | 81 | 82 | 83 | float m_wx, m_wy, m_wz; //pos in world space 84 | 85 | 86 | 87 | //give father's world pos, compute the bone's world pos 88 | 89 | void ComputeWorldPos(float fatherWX, float fatherWY, float fatherWZ) 90 | 91 | { 92 | 93 | m_wx = fatherWX+m_x; 94 | 95 | m_wy = fatherWY+m_y; 96 | 97 | m_wz = fatherWZ+m_z; 98 | 99 | 100 | 101 | if(m_pSibling!=NULL) 102 | 103 | m_pSibling->ComputeWorldPos(fatherWX, fatherWY, fatherWZ); 104 | 105 | 106 | 107 | if(m_pFirstChild!=NULL) 108 | 109 | m_pFirstChild->ComputeWorldPos(m_wx, m_wy, m_wz); 110 | 111 | } 112 | 113 | 114 | 115 | BoneOffset m_boneOffset; 116 | 117 | 118 | 119 | //called after compute world pos when bone loaded but not animated 120 | 121 | void ComputeBoneOffset() 122 | 123 | { 124 | 125 | m_boneOffset.m_offx = -m_wx; 126 | 127 | m_boneOffset.m_offy = -m_wy; 128 | 129 | m_boneOffset.m_offz = -m_wz; 130 | 131 | 132 | 133 | if(m_pSibling!=NULL) 134 | 135 | m_pSibling->ComputeBoneOffset(); 136 | 137 | if(m_pFirstChild!=NULL) 138 | 139 | m_pFirstChild->ComputeBoneOffset(); 140 | 141 | } 142 | 143 | 144 | 145 | void Draw() 146 | 147 | { 148 | 149 | glColor3f(0,0,1.0); 150 | 151 | glPointSize(4); 152 | 153 | glBegin(GL_POINTS); 154 | 155 | glVertex3f(m_wx,m_wy,m_wz); 156 | 157 | glEnd(); 158 | 159 | if(m_pFather!=NULL) 160 | 161 | { 162 | 163 | glBegin(GL_LINES); 164 | 165 | glVertex3f(m_pFather->m_wx,m_pFather->m_wy,m_pFather->m_wz); 166 | 167 | glVertex3f(m_wx,m_wy,m_wz); 168 | 169 | glEnd(); 170 | 171 | } 172 | 173 | 174 | 175 | if(m_pSibling!=NULL) 176 | 177 | m_pSibling->Draw(); 178 | 179 | if(m_pFirstChild!=NULL) 180 | 181 | m_pFirstChild->Draw(); 182 | 183 | 184 | 185 | } 186 | 187 | }; 188 | 189 | 190 | 191 | //-------------------------------------------------------------- 192 | 193 | 194 | 195 | #define MAX_BONE_PER_VERTEX 4 196 | 197 | 198 | 199 | 200 | 201 | class Vertex 202 | 203 | { 204 | 205 | public: 206 | 207 | Vertex():m_boneNum(0) 208 | 209 | { 210 | 211 | } 212 | 213 | 214 | 215 | void ComputeWorldPosByBone(Bone* pBone, float& outX, float& outY, float& outZ) 216 | 217 | { 218 | 219 | //step1: transform vertex from mesh space to bone space 220 | 221 | outX = m_x+pBone->m_boneOffset.m_offx; 222 | 223 | outY = m_y+pBone->m_boneOffset.m_offy; 224 | 225 | outZ = m_z+pBone->m_boneOffset.m_offz; 226 | 227 | 228 | 229 | //step2: transform vertex from bone space to world sapce 230 | 231 | outX += pBone->m_wx; 232 | 233 | outY += pBone->m_wy; 234 | 235 | outZ += pBone->m_wz; 236 | 237 | } 238 | 239 | 240 | 241 | void BlendVertex() 242 | 243 | {//do the vertex blending,get the vertex's pos in world space 244 | 245 | 246 | 247 | m_wX = 0; 248 | 249 | m_wY = 0; 250 | 251 | m_wZ = 0; 252 | 253 | 254 | 255 | for(int i=0; i0) 342 | 343 | delete[] m_vertexs; 344 | 345 | } 346 | 347 | 348 | 349 | void UpdateVertices() 350 | 351 | { 352 | 353 | for(int i=0; iSetFirstChild(g_bone1); 470 | 471 | g_bone1->SetFirstChild(g_bone2); 472 | 473 | g_bone2->SetFirstChild(g_bone31); 474 | 475 | g_bone31->SetSibling(g_bone32); 476 | 477 | } 478 | 479 | 480 | 481 | void deleteBones() 482 | 483 | { 484 | 485 | delete g_boneRoot; 486 | 487 | delete g_bone1; 488 | 489 | delete g_bone2; 490 | 491 | delete g_bone31; 492 | 493 | delete g_bone32; 494 | 495 | } 496 | 497 | 498 | 499 | void animateBones() 500 | 501 | { 502 | 503 | static int dir=-1, dir2=-1; 504 | 505 | //animate bones manually 506 | 507 | 508 | 509 | g_bone1->m_y +=0.00001f*dir; 510 | 511 | 512 | 513 | if(g_bone1->m_y<-0.2 || g_bone1->m_y>0.2) 514 | 515 | dir*=-1; 516 | 517 | 518 | 519 | g_bone32->m_x +=0.00001f*dir2; 520 | 521 | 522 | 523 | if(g_bone32->m_x<0 || g_bone32->m_x>0.2) 524 | 525 | dir2*=-1; 526 | 527 | } 528 | 529 | 530 | 531 | SkinMesh* g_mesh; 532 | 533 | 534 | 535 | 536 | 537 | 538 | 539 | void buildMesh() 540 | 541 | { 542 | 543 | float _meshData[]= 544 | 545 | {//x,y,z 546 | 547 | -0.1,0.05,0, 548 | 549 | 0.1,0.05,0, 550 | 551 | 0.3,0.05,0, 552 | 553 | 0.45,0.06,0, 554 | 555 | 0.6,0.15,0, 556 | 557 | 0.65,0.1,0, 558 | 559 | 560 | 561 | 0.5,0,0, 562 | 563 | 564 | 565 | 0.65,-0.1,0, 566 | 567 | 0.6,-0.15,0, 568 | 569 | 0.45,-0.06,0, 570 | 571 | 0.3,-0.05,0, 572 | 573 | 0.1,-0.05,0, 574 | 575 | -0.1,-0.05,0, 576 | 577 | }; 578 | 579 | 580 | 581 | float _skinInfo[]= 582 | 583 | {//bone_num,bone id(0,1,2,31 or 32), bone weight 1~4, 584 | 585 | 1, 0, -1, -1, -1, 1.0, 0.0, 0.0, 0.0, 586 | 587 | 2, 0, 1, -1, -1, 0.5, 0.5, 0.0, 0.0, 588 | 589 | 2, 1, 2, -1, -1, 0.5, 0.5, 0.0, 0.0, 590 | 591 | 2, 2, 31, -1, -1, 0.3, 0.7, 0.0, 0.0, 592 | 593 | 2, 2, 31, -1, -1, 0.2, 0.8, 0.0, 0.0, 594 | 595 | 1, 31, -1, -1, -1, 1.0, 0.0, 0.0, 0.0, 596 | 597 | 598 | 599 | 2, 31, 32, -1, -1, 0.5, 0.5, 0.0, 0.0, 600 | 601 | 602 | 603 | 1, 32, -1, -1, -1, 1.0, 0.0, 0.0, 0.0, 604 | 605 | 2, 2, 32, -1, -1, 0.2, 0.8, 0.0, 0.0, 606 | 607 | 2, 2, 32, -1, -1, 0.3, 0.7, 0.0, 0.0, 608 | 609 | 2, 1, 2, -1, -1, 0.5, 0.5, 0.0, 0.0, 610 | 611 | 2, 0, 1, -1, -1, 0.5, 0.5, 0.0, 0.0, 612 | 613 | 1, 0, -1, -1, -1, 1.0, 0.0, 0.0, 0.0, 614 | 615 | }; 616 | 617 | 618 | 619 | int vertexNum = sizeof(_meshData)/(sizeof(float)*3); 620 | 621 | g_mesh = new SkinMesh(vertexNum); 622 | 623 | for(int i=0; im_vertexs[i].m_x = _meshData[i*3]; 628 | 629 | g_mesh->m_vertexs[i].m_y = _meshData[i*3+1]; 630 | 631 | g_mesh->m_vertexs[i].m_z = _meshData[i*3+2]; 632 | 633 | } 634 | 635 | 636 | 637 | //set skin info 638 | 639 | for(int i=0; im_vertexs[i].m_boneNum = _skinInfo[i*9]; 644 | 645 | for(int j=0; jm_vertexs[i].m_boneNum; ++j) 646 | 647 | { 648 | 649 | Bone* pBone = g_boneRoot; 650 | 651 | if(_skinInfo[i*9+1+j]==1) 652 | 653 | pBone = g_bone1; 654 | 655 | else if(_skinInfo[i*9+1+j]==2) 656 | 657 | pBone = g_bone2; 658 | 659 | else if(_skinInfo[i*9+1+j]==31) 660 | 661 | pBone = g_bone31; 662 | 663 | else if(_skinInfo[i*9+1+j]==32) 664 | 665 | pBone = g_bone32; 666 | 667 | 668 | 669 | g_mesh->m_vertexs[i].SetBoneAndWeight(j, pBone, _skinInfo[i*9+5+j]); 670 | 671 | } 672 | 673 | } 674 | 675 | 676 | 677 | //compute bone offset 678 | 679 | g_boneRoot->ComputeWorldPos(0, 0, 0); 680 | 681 | g_boneRoot->ComputeBoneOffset(); 682 | 683 | } 684 | 685 | 686 | 687 | void deleteMesh() 688 | 689 | { 690 | 691 | delete g_mesh; 692 | 693 | } 694 | 695 | 696 | 697 | void myInit() 698 | 699 | { 700 | 701 | buildBones(); 702 | 703 | buildMesh(); 704 | 705 | } 706 | 707 | 708 | 709 | void myQuit() 710 | 711 | { 712 | 713 | deleteBones(); 714 | 715 | deleteMesh(); 716 | 717 | } 718 | 719 | 720 | 721 | void myReshape(int width, int height) 722 | 723 | { 724 | 725 | GLfloat h = (GLfloat) height / (GLfloat) width; 726 | 727 | 728 | 729 | glViewport(0, 0, (GLint) width, (GLint) height); 730 | 731 | glMatrixMode(GL_PROJECTION); 732 | 733 | glLoadIdentity(); 734 | 735 | // glFrustum(-1.0, 1.0, -h, h, 5.0, 60.0); 736 | 737 | glFrustum(-1.0, 1.0, -h, h, 1.0, 100.0); 738 | 739 | glMatrixMode(GL_MODELVIEW); 740 | 741 | glLoadIdentity(); 742 | 743 | glTranslatef(0.0, 0.0, -1.0); 744 | 745 | } 746 | 747 | 748 | 749 | void myDisplay(void) 750 | 751 | { 752 | 753 | glClear(GL_COLOR_BUFFER_BIT); 754 | 755 | 756 | 757 | //draw original mesh 758 | 759 | g_mesh->DrawStaticMesh(0,0,0); 760 | 761 | 762 | 763 | //move bones 764 | 765 | animateBones(); 766 | 767 | 768 | 769 | //update all bone's pos in bone tree 770 | 771 | g_boneRoot->ComputeWorldPos(0, 0, 0); 772 | 773 | 774 | 775 | //update vertex pos by bones, using vertex blending 776 | 777 | g_mesh->UpdateVertices(); 778 | 779 | 780 | 781 | //draw deformed mesh 782 | 783 | g_mesh->Draw(); 784 | 785 | 786 | 787 | //draw bone 788 | 789 | g_boneRoot->Draw(); 790 | 791 | 792 | 793 | glFlush(); 794 | 795 | glutSwapBuffers(); 796 | 797 | } 798 | 799 | 800 | 801 | void myIdle(void) 802 | 803 | { 804 | 805 | myDisplay(); 806 | 807 | } 808 | 809 | 810 | 811 | int main(int argc, char *argv[]) 812 | 813 | { 814 | 815 | glutInit(&argc, argv); 816 | 817 | glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); 818 | 819 | glutInitWindowPosition(100, 100); 820 | 821 | glutInitWindowSize(640, 480); 822 | 823 | glutCreateWindow("A simplest skinned mesh DEMO, by happyfirecn@yahoo.com.cn"); 824 | 825 | 826 | 827 | glutDisplayFunc(myDisplay); 828 | 829 | glutReshapeFunc(myReshape); 830 | 831 | glutIdleFunc(myIdle); 832 | 833 | 834 | 835 | myInit(); 836 | 837 | glutMainLoop(); 838 | 839 | myQuit(); 840 | 841 | 842 | 843 | return 0; 844 | 845 | } 846 | --------------------------------------------------------------------------------