|
楼主 |
发表于 2008-12-2 17:15:24
|
显示全部楼层
//-------------------------------------------------------------- Bone* g_boneRoot; Bone* g_bone1, *g_bone2, *g_bone31, *g_bone32; void buildBones() { g_boneRoot = new Bone(0, 0, 0); g_bone1 = new Bone(0.2, 0, 0); g_bone2 = new Bone(0.2, 0, 0); g_bone31 = new Bone(0.2, 0.1, 0); g_bone32 = new Bone(0.2, -0.1, 0); g_boneRoot->SetFirstChild(g_bone1); g_bone1->SetFirstChild(g_bone2); g_bone2->SetFirstChild(g_bone31); g_bone31->SetSibling(g_bone32); } void deleteBones() { delete g_boneRoot; delete g_bone1; delete g_bone2; delete g_bone31; delete g_bone32; } void animateBones() { static int dir=-1, dir2=-1; //animate bones manually g_bone1->m_y +=0.00001f*dir; if(g_bone1->m_y<-0.2 || g_bone1->m_y>0.2) dir*=-1; g_bone32->m_x +=0.00001f*dir2; if(g_bone32->m_x<0 || g_bone32->m_x>0.2) dir2*=-1; } SkinMesh* g_mesh; void buildMesh() { float _meshData[]= {//x,y,z -0.1,0.05,0, 0.1,0.05,0, 0.3,0.05,0, 0.45,0.06,0, 0.6,0.15,0, 0.65,0.1,0, 0.5,0,0, 0.65,-0.1,0, 0.6,-0.15,0, 0.45,-0.06,0, 0.3,-0.05,0, 0.1,-0.05,0, -0.1,-0.05,0, }; float _skinInfo[]= {//bone_num,bone id(0,1,2,31 or 32), bone weight 1~4, 1, 0, -1, -1, -1, 1.0, 0.0, 0.0, 0.0, 2, 0, 1, -1, -1, 0.5, 0.5, 0.0, 0.0, 2, 1, 2, -1, -1, 0.5, 0.5, 0.0, 0.0, 2, 2, 31, -1, -1, 0.3, 0.7, 0.0, 0.0, 2, 2, 31, -1, -1, 0.2, 0.8, 0.0, 0.0, 1, 31, -1, -1, -1, 1.0, 0.0, 0.0, 0.0, 2, 31, 32, -1, -1, 0.5, 0.5, 0.0, 0.0, 1, 32, -1, -1, -1, 1.0, 0.0, 0.0, 0.0, 2, 2, 32, -1, -1, 0.2, 0.8, 0.0, 0.0, 2, 2, 32, -1, -1, 0.3, 0.7, 0.0, 0.0, 2, 1, 2, -1, -1, 0.5, 0.5, 0.0, 0.0, 2, 0, 1, -1, -1, 0.5, 0.5, 0.0, 0.0, 1, 0, -1, -1, -1, 1.0, 0.0, 0.0, 0.0, }; int vertexNum = sizeof(_meshData)/(sizeof(float)*3); g_mesh = new SkinMesh(vertexNum); for(int i=0; i<vertexNum; ++i) { g_mesh->m_vertexs.m_x = _meshData[i*3]; g_mesh->m_vertexs.m_y = _meshData[i*3+1]; g_mesh->m_vertexs.m_z = _meshData[i*3+2]; } //set skin info for(int i=0; i<vertexNum; ++i) { g_mesh->m_vertexs.m_boneNum = _skinInfo[i*9]; for(int j=0; j<g_mesh->m_vertexs.m_boneNum; ++j) { Bone* pBone = g_boneRoot; if(_skinInfo[i*9+1+j]==1) pBone = g_bone1; else if(_skinInfo[i*9+1+j]==2) pBone = g_bone2; else if(_skinInfo[i*9+1+j]==31) pBone = g_bone31; else if(_skinInfo[i*9+1+j]==32) pBone = g_bone32; g_mesh->m_vertexs.SetBoneAndWeight(j, pBone, _skinInfo[i*9+5+j]); } } //compute bone offset g_boneRoot->ComputeWorldPos(0, 0, 0); g_boneRoot->ComputeBoneOffset(); } void deleteMesh() { delete g_mesh; } void myInit() { buildBones(); buildMesh(); } void myQuit() { deleteBones(); deleteMesh(); } void myReshape(int width, int height) { GLfloat h = (GLfloat) height / (GLfloat) width; glViewport(0, 0, (GLint) width, (GLint) height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); // glFrustum(-1.0, 1.0, -h, h, 5.0, 60.0); glFrustum(-1.0, 1.0, -h, h, 1.0, 100.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0, 0.0, -1.0); } void myDisplay(void) { glClear(GL_COLOR_BUFFER_BIT); //draw original mesh g_mesh->DrawStaticMesh(0,0,0); //move bones animateBones(); //update all bone's pos in bone tree g_boneRoot->ComputeWorldPos(0, 0, 0); //update vertex pos by bones, using vertex blending g_mesh->UpdateVertices(); //draw deformed mesh g_mesh->Draw(); //draw bone g_boneRoot->Draw(); glFlush(); glutSwapBuffers(); } void myIdle(void) { myDisplay(); } int main(int argc, char *argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); glutInitWindowPosition(100, 100); glutInitWindowSize(640, 480); glutCreateWindow("A simplest skinned mesh DEMO, by happyfirecn@yahoo.com.cn"); glutDisplayFunc(myDisplay); glutReshapeFunc(myReshape); glutIdleFunc(myIdle); myInit(); glutMainLoop(); myQuit(); return 0; }
|
|