⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 winmain.cpp

📁 用DirectX制作高级动画-[Advanced.Animation.with.DirectX]
💻 CPP
📖 第 1 页 / 共 3 页
字号:
    for(j=0;j<NumVertices;j++) {
      fprintf(fp_out, "    %06lf;%06lf;", Vertices[j].u, Vertices[j].v);
      if(j < (NumVertices-1))
        fprintf(fp_out, ",\r\n");
      else
        fprintf(fp_out, ";\r\n");
    }
    fprintf(fp_out, "  }\r\n");

    // Write mesh material list
    fprintf(fp_out, "  MeshMaterialList {\r\n");
    fprintf(fp_out, "    1;\r\n    %lu;\r\n", NumFaces);
    for(j=0;j<NumFaces;j++) {
      fprintf(fp_out, "    0");
      if(j < (NumFaces-1))
        fprintf(fp_out, ",\r\n");
      else
        fprintf(fp_out, ";\r\n");
    }

    // Write material
    fprintf(fp_out, "    Material {\r\n");
    fprintf(fp_out, "      1.000000;1.000000;1.000000;1.000000;;\r\n");
    fprintf(fp_out, "      0.000000;\r\n");
    fprintf(fp_out, "      1.000000;1.000000;1.000000;;\r\n");
    fprintf(fp_out, "      1.000000;1.000000;1.000000;;\r\n");
    fprintf(fp_out, "      TextureFilename {\r\n");
    fprintf(fp_out, "        %c%s%c;\r\n", '"', TextureFilenamePtr, '"');
    fprintf(fp_out, "      }\r\n");
    fprintf(fp_out, "    }\r\n");

    // Close mesh materal list
    fprintf(fp_out, "  }\r\n");

    // Close mesh template data object
    fprintf(fp_out, "}\r\n");
  }

  // Close the input file
  fclose(fp_in);

  // Free used resource
  delete [] Vertices;
  delete [] Indices;
  delete [] mdFrame;
  delete [] mdTextureCoords;
  delete [] mdFaces;

  /////////////////////////////////////////////////
  //
  // Write the animation sets
  //
  /////////////////////////////////////////////////

  // Clear out a buffer for last known mesh sequence's name
  char LastName[16];
  for(DWORD j=0;j<16;j++)
    LastName[j] = (isdigit(MeshNames[j]) && j >= strlen(&MeshNames[0])-2) ? 0:MeshNames[j];

  // Reset # of frames count
  DWORD NumFrames = 0;

  // Go through each mesh (starting at 2nd mesh since 1st already counted)
  for(i=0;i<mdHeader.NumFrames;i++) {

    // Get name of mesh (clean sequence numbers)
    char CleanName[16];
    for(DWORD j=0;j<16;j++)
      CleanName[j] = (isdigit(MeshNames[16*i+j]) && j >= strlen(&MeshNames[16*i])-2) ? 0:MeshNames[16*i+j];

    // Compare names and increase count if matched last name
    if(!strcmp(LastName, CleanName)) {
      NumFrames++;
    } else {

      // Output animation template
      fprintf(fp_out, "MorphAnimationSet %s {\r\n", LastName);
      fprintf(fp_out, "  1;\r\n");  // Always loop
      fprintf(fp_out, "  %lu;\r\n", NumFrames+1);
      for(DWORD k=0;k<NumFrames;k++)
        fprintf(fp_out, "  %lu; %c%s%c;,\r\n", k*MD2FPS, '"', &MeshNames[16*(i-NumFrames+k)], '"');
      fprintf(fp_out, "  %lu; %c%s%c;;\r\n", k*MD2FPS, '"', &MeshNames[16*(i-NumFrames)], '"');
      fprintf(fp_out, "}\r\n");

      // Record last name and go to next animation
      strcpy(LastName, CleanName);
      NumFrames = 1;
    }
  }

  // Check if any frames left to output
  if(NumFrames) {
    // Output animation template
    fprintf(fp_out, "MorphAnimationSet %s {\r\n", LastName);
    fprintf(fp_out, "  1;\r\n");  // Always loop
    fprintf(fp_out, "  %lu;\r\n", NumFrames+1);
    for(DWORD k=0;k<NumFrames;k++)
      fprintf(fp_out, "  %lu; %c%s%c;,\r\n", k*MD2FPS, '"', &MeshNames[16*(i-NumFrames+k)], '"');
    fprintf(fp_out, "  %lu; %c%s%c;;\r\n", k*MD2FPS, '"', &MeshNames[16*(i-NumFrames)], '"');
    fprintf(fp_out, "}\r\n");
  }

  // Close the output file
  fclose(fp_out);

  // Free mesh names 
  delete [] MeshNames;

  // Return success
  return TRUE;
}

BOOL ConvertMS3D(char *SrcFilename, char *DestFilename)
{
  FILE           *fp_in, *fp_out;

  DWORD           i, j, k, Num;
  
  sMS3DHeader     msHeader;

  unsigned short  msNumVertices;
  sMS3DVertex    *msVertices = NULL;

  sMS3DMeshVertex *Vertices;
  unsigned short  *Faces;

  unsigned short  msNumFaces;
  sMS3DFace      *msFaces = NULL;
  unsigned short *Indices = NULL;

  unsigned short  msNumGroups;
  sMS3DGroup     *msGroups = NULL;

  unsigned short  msNumMaterials;
  sMS3DMaterial  *msMaterials = NULL;

  unsigned short  msNumBones;
  sMS3DBone      *msBones = NULL;
  sMS3DBoneContainer *Bones = NULL;

  float           msAnimationFPS;
  float           msCurrentTime;
  int             msTotalFrames;

  // Error checking
  if(!SrcFilename || !DestFilename)
    return FALSE;

  // Open the source file
  if((fp_in = fopen(SrcFilename, "rb"))==NULL)
    return FALSE;

  // Read in the header and make sure it's valid
  fread(&msHeader, 1, sizeof(sMS3DHeader), fp_in);
  if(memcmp(msHeader.Signature, "MS3D000000", 10)) {
    fclose(fp_in);
    return FALSE;
  }

  ///////////////////////////////////////////////
  //
  // Read data into some temporary buffers
  //
  ///////////////////////////////////////////////

  // Read vertex data
  fread(&msNumVertices, 1, sizeof(short), fp_in);
  if(msNumVertices) {
    msVertices = new sMS3DVertex[msNumVertices];
    for(i=0;i<msNumVertices;i++) {
      fread(&msVertices[i], 1, sizeof(sMS3DVertex), fp_in);
    }
  }

  // Read face data
  fread(&msNumFaces, 1, sizeof(short), fp_in);
  if(msNumFaces) {
    msFaces = new sMS3DFace[msNumFaces];
    for(i=0;i<msNumFaces;i++)
      fread(&msFaces[i], 1, sizeof(sMS3DFace), fp_in);
  }

  // Read groups
  fread(&msNumGroups, 1, sizeof(short), fp_in);
  if(msNumGroups) {
    msGroups = new sMS3DGroup[msNumGroups];
    for(i=0;i<msNumGroups;i++) {
      fread(&msGroups[i], 1, sizeof(msGroups[i].Header), fp_in);
      if((Num = msGroups[i].Header.NumFaces)) {
        msGroups[i].Indices = new unsigned short[Num];
        for(j=0;j<Num;j++)
          fread(&msGroups[i].Indices[j], 1, sizeof(short), fp_in);
      }
      fread(&msGroups[i].Material, 1, sizeof(char), fp_in);

      // Change group material to 0 if none assigned (-1)
      if(msGroups[i].Material == -1)
        msGroups[i].Material = 0;
    }
  }

  // Read materials, creating a default one if none in file
  fread(&msNumMaterials, 1, sizeof(short), fp_in);
  if(!msNumMaterials) {
    // Create a single material and color it white
    msNumMaterials = 1;
    msMaterials = new sMS3DMaterial[1];
    ZeroMemory(&msMaterials[0], sizeof(sMS3DMaterial));
    msMaterials[0].Diffuse[0] = msMaterials[0].Diffuse[1] = msMaterials[0].Diffuse[2] = 1.0f;

    // Set all groups to use material #0
    // If there are no materials, set all groups to 
    // use material #0
    if(msNumGroups) {
      for(i=0;i<msNumGroups;i++)
        msGroups[i].Material = 0;
    }
  } else {
    // Read in all materials from file
    msMaterials = new sMS3DMaterial[msNumMaterials];
    for(i=0;i<msNumMaterials;i++)
      fread(&msMaterials[i], 1, sizeof(sMS3DMaterial), fp_in);
  }

  // Read in FPS, current editor time, and # frames
  fread(&msAnimationFPS, 1, sizeof(float), fp_in);
  fread(&msCurrentTime, 1, sizeof(float), fp_in);
  fread(&msTotalFrames, 1, sizeof(int), fp_in);

  // Read in bones, skipping keyframe data
  fread(&msNumBones, 1, sizeof(short), fp_in);
  if(msNumBones) {
    msBones = new sMS3DBone[msNumBones];
    for(i=0;i<msNumBones;i++) {
      fread(&msBones[i], 1, sizeof(msBones[i].Header), fp_in);

      // Allocate key frames (if any)
      if((Num = msBones[i].Header.NumRotFrames)) {
        msBones[i].RotKeyFrames = new sMS3DKeyFrame[Num]();
        for(j=0;j<Num;j++)
          fread(&msBones[i].RotKeyFrames[j], 1, sizeof(sMS3DKeyFrame), fp_in);
      }

      if((Num=msBones[i].Header.NumPosFrames)) {
        msBones[i].PosKeyFrames = new sMS3DKeyFrame[Num]();
        for(j=0;j<Num;j++)
          fread(&msBones[i].PosKeyFrames[j], 1, sizeof(sMS3DKeyFrame), fp_in);
      }
    }
  }

  // Compile a new vertex list based on vertex information from faces
  // Clip out duplicates and build a face list.
  DWORD NumMeshVertices = 0;
  Vertices = new sMS3DMeshVertex[msNumFaces*3]();
  Faces    = new unsigned short[msNumFaces*3];

  // Clear vertex list data
  for(i=0;i<(DWORD)msNumFaces*3;i++) {
    ZeroMemory(&Vertices[i], sizeof(sMS3DMeshVertex));
    Vertices[i].BoneNum = -2;
  }

  for(i=0;i<msNumFaces;i++) {
    for(j=0;j<3;j++) {
      unsigned short Index = msFaces[i].Indices[j];

      // Allocate a vertex structure and fill it w/data
      sMS3DMeshVertex NewVertex;
      NewVertex.x  = msVertices[Index].Pos[0];
      NewVertex.y  = msVertices[Index].Pos[1];
      NewVertex.z  = -msVertices[Index].Pos[2];
      NewVertex.nx = msFaces[i].Normal[j][0];
      NewVertex.ny = msFaces[i].Normal[j][1];
      NewVertex.nz = -msFaces[i].Normal[j][2];
      NewVertex.u  = msFaces[i].u[j];
      NewVertex.v  = msFaces[i].v[j];
      NewVertex.BoneNum = msVertices[Index].Bone;

      // Search existing list and look for match
      DWORD Match = -1;
      for(k=0;k<(DWORD)msNumFaces*3;k++) {
        if(!memcmp(&Vertices[k], &NewVertex, sizeof(sMS3DMeshVertex))) {
          Match = k;
          break;
        }
      }

      // If no match, then find an empty slot to use and reset index
      if(Match == -1) {
        for(k=0;k<(DWORD)msNumFaces*3;k++) {
          if(Vertices[k].BoneNum == -2) {
            Index = (unsigned short)k;
            memcpy(&Vertices[Index], &NewVertex, sizeof(sMS3DMeshVertex));
            NumMeshVertices++;
            break;
          }
        }
      } else {
        // Use matching index number
        Index = (unsigned short)Match;
      }

      // Store index number in face list
      Faces[i*3+j] = Index;
    }
  }

  // Build the frame hierarchy and combine the transformations
  if(msNumBones) {

    // Allocate array for bone data
    Bones = new sMS3DBoneContainer[msNumBones];

    // Store bone data
    for(i=0;i<msNumBones;i++) {

      // Copy over name and clean it of spaces
      strcpy(Bones[i].Name, msBones[i].Header.Name);
      for(j=0;j<strlen(Bones[i].Name);j++) { 
       if(Bones[i].Name[j] == ' ')
         Bones[i].Name[j] = '_';
      }

      // Create transformation matrix
      CreateYawPitchRollRotationMatrix(&Bones[i].matRotation,
                                       msBones[i].Header.Rotation[1],
                                       msBones[i].Header.Rotation[0],
                                       -msBones[i].Header.Rotation[2]);

      D3DXMatrixTranslation(&Bones[i].matTranslation, 
                            msBones[i].Header.Position[0],
                            msBones[i].Header.Position[1],
                            -msBones[i].Header.Position[2]);
      Bones[i].matTransformation = Bones[i].matRotation * Bones[i].matTranslation;
    }

    // Build hierarchy
    for(i=0;i<msNumBones;i++) {
      
      // Match parent
      if(msBones[i].Header.Parent[0]) {

        // Search for parent
        for(j=0;j<msNumBones;j++) {

          // i = current bone, j = searching bone

          // Compare bone names
          if(!stricmp(msBones[i].Header.Parent, msBones[j].Header.Name)) {
            
            // Link sibling
            Bones[i].Sibling = Bones[j].Child;

            // Link child
            Bones[j].Child = &Bones[i];
          }
        }
      }
    }

    // Calculate combined and inverse combined transformations
    D3DXMATRIX matIdentity;
    D3DXMatrixIdentity(&matIdentity);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -