Пример #1
0
int CalCoreModel::loadCoreMesh(const std::string& strFilename, const std::string& strMeshName)
{
  int id = -1;
  std::map<std::string, int>::iterator it = m_meshName.find(strMeshName);
  if (it != m_meshName.end())
  {
    id=(*it).second;

    // the core skeleton has to be loaded already
    if(!m_pCoreSkeleton)
    {
      CalError::setLastError(CalError::INVALID_HANDLE, __FILE__, __LINE__);
      return -1;
    }
    if(m_vectorCoreMesh[id])
    {
      CalError::setLastError(CalError::INDEX_BUILD_FAILED, __FILE__, __LINE__);
      return -1;
    }
    CalCoreMeshPtr pCoreMesh = CalLoader::loadCoreMesh(strFilename);
    if(!pCoreMesh) return -1;
    pCoreMesh->setName(strMeshName);
    m_vectorCoreMesh[id] = pCoreMesh;
  }
  else
  {
    id = loadCoreMesh(strFilename);
    if(id >= 0)
      addMeshName(strMeshName, id);
  }

  return id;
}
Пример #2
0
int CalCoreModel::loadCoreMesh(const std::string& strFilename)
{
  // the core skeleton has to be loaded already
  if(!m_pCoreSkeleton)
  {
    CalError::setLastError(CalError::INVALID_HANDLE, __FILE__, __LINE__);
    return -1;
  }

  // load a new core mesh
  CalCoreMeshPtr pCoreMesh = CalLoader::loadCoreMesh(strFilename);
  if(!pCoreMesh) return -1;

  // add core mesh to this core model
  return addCoreMesh(pCoreMesh.get());
}
Пример #3
0
bool CExporter::ExportMesh(const std::string& strFilename)
{
  // check if a valid interface is set
  if(m_pInterface == 0)
  {
    SetLastError("Invalid handle.", __FILE__, __LINE__);
    return false;
  }

  // build a mesh candidate
  CMeshCandidate meshCandidate;

  // build a skeleton candidate
  CSkeletonCandidate skeletonCandidate;

  // show export wizard sheet
  CMeshExportSheet sheet("Cal3D Mesh Export", m_pInterface->GetMainWnd());
  sheet.SetSkeletonCandidate(&skeletonCandidate);
  sheet.SetMeshCandidate(&meshCandidate);
  sheet.SetWizardMode();
  if(sheet.DoModal() != ID_WIZFINISH) return true;

  // create the core mesh instance
  CalCoreMeshPtr coreMesh = new CalCoreMesh;

  // get the submesh candidate vector
  std::vector<CSubmeshCandidate *>& vectorSubmeshCandidate = meshCandidate.GetVectorSubmeshCandidate();

  // start the progress info
  m_pInterface->StartProgressInfo("Exporting to mesh file...");

  for(size_t submeshCandidateId = 0; submeshCandidateId < vectorSubmeshCandidate.size(); submeshCandidateId++)
  {
    // update the progress info
    m_pInterface->SetProgressInfo(int(100.0f * (float)submeshCandidateId / (float)vectorSubmeshCandidate.size()));

    // get the submesh candidate
    CSubmeshCandidate *pSubmeshCandidate = vectorSubmeshCandidate[submeshCandidateId];

    // get the face vector
    std::vector<CSubmeshCandidate::Face>& vectorFace = pSubmeshCandidate->GetVectorFace();

    // check if the submesh actually contains faces
    if(vectorFace.size() > 0)
    {
      // allocate new core submesh instance
      CalCoreSubmesh *pCoreSubmesh = new CalCoreSubmesh();
      if(pCoreSubmesh == 0)
      {
        SetLastError("Memory allocation failed.", __FILE__, __LINE__);
        m_pInterface->StopProgressInfo();
        return false;
      }

      // set the core material id
      pCoreSubmesh->setCoreMaterialThreadId(pSubmeshCandidate->GetMaterialThreadId());

      // get the vertex candidate vector
      std::vector<CVertexCandidate *>& vectorVertexCandidate = pSubmeshCandidate->GetVectorVertexCandidate();

      // get the spring vector
      std::vector<CSubmeshCandidate::Spring>& vectorSpring = pSubmeshCandidate->GetVectorSpring();

      // reserve memory for all the submesh data
      if(!pCoreSubmesh->reserve(vectorVertexCandidate.size(), pSubmeshCandidate->GetMapCount(), vectorFace.size(), vectorSpring.size()))
      {
        SetLastError(CalError::getLastErrorText(), __FILE__, __LINE__);
        delete pCoreSubmesh;
        m_pInterface->StopProgressInfo();
        return false;
      }

      for(size_t vertexCandidateId = 0; vertexCandidateId < vectorVertexCandidate.size(); vertexCandidateId++)
      {
        // get the vertex candidate
        CVertexCandidate *pVertexCandidate = vectorVertexCandidate[vertexCandidateId];

        CalCoreSubmesh::Vertex vertex;
        pVertexCandidate->GetPosition(vertex.position);
        pVertexCandidate->GetNormal(vertex.normal);
        vertex.collapseId = pVertexCandidate->GetCollapseId();
        vertex.faceCollapseCount = pVertexCandidate->GetFaceCollapseCount();

        // get the texture coordinate vector
        std::vector<CVertexCandidate::TextureCoordinate>& vectorTextureCoordinate = pVertexCandidate->GetVectorTextureCoordinate();

        // set all texture coordinates
        for(size_t textureCoordinateId = 0; textureCoordinateId < vectorTextureCoordinate.size(); textureCoordinateId++)
        {
          CalCoreSubmesh::TextureCoordinate textureCoordinate;
          textureCoordinate.u = vectorTextureCoordinate[textureCoordinateId].u;
          textureCoordinate.v = vectorTextureCoordinate[textureCoordinateId].v;

          // set texture coordinate
          pCoreSubmesh->setTextureCoordinate(pVertexCandidate->GetLodId(), textureCoordinateId, textureCoordinate);
        }

        // get the influence vector
        std::vector<CVertexCandidate::Influence>& vectorInfluence = pVertexCandidate->GetVectorInfluence();

        // reserve memory for the influences in the vertex
        vertex.vectorInfluence.reserve(vectorInfluence.size());
        vertex.vectorInfluence.resize(vectorInfluence.size());

        // set all influences
        for(size_t influenceId = 0; influenceId < vectorInfluence.size(); influenceId++)
        {
          vertex.vectorInfluence[influenceId].boneId = vectorInfluence[influenceId].boneId;
          vertex.vectorInfluence[influenceId].weight = vectorInfluence[influenceId].weight;
        }

        // set vertex in the core submesh instance
        pCoreSubmesh->setVertex(pVertexCandidate->GetLodId(), vertex);

        // set the physical property if there is a spring system
        if(vectorSpring.size() > 0)
        {
          // get the physical property vector
          CVertexCandidate::PhysicalProperty physicalPropertyCandidate;
          pVertexCandidate->GetPhysicalProperty(physicalPropertyCandidate);

          CalCoreSubmesh::PhysicalProperty physicalProperty;
          physicalProperty.weight = physicalPropertyCandidate.weight;

          // set the physical property in the core submesh instance
          pCoreSubmesh->setPhysicalProperty(vertexCandidateId, physicalProperty);

        }
      }

      for(size_t faceId = 0; faceId < vectorFace.size(); faceId++)
      {
        CalCoreSubmesh::Face face;

        // set the vertex ids
        face.vertexId[0] = vectorFace[faceId].vertexLodId[0];
        face.vertexId[1] = vectorFace[faceId].vertexLodId[1];
        face.vertexId[2] = vectorFace[faceId].vertexLodId[2];

        // set face in the core submesh instance
        pCoreSubmesh->setFace(vectorFace[faceId].lodId, face);
      }

      for(size_t springId = 0; springId < vectorSpring.size(); springId++)
      {
        CalCoreSubmesh::Spring spring;

        // set the vertex ids
        spring.vertexId[0] = vectorSpring[springId].vertexId[0];
        spring.vertexId[1] = vectorSpring[springId].vertexId[1];
        spring.springCoefficient = vectorSpring[springId].springCoefficient;
        spring.idleLength = vectorSpring[springId].idleLength;

        // set face in the core submesh instance
        pCoreSubmesh->setSpring(springId, spring);
      }

      // set the LOD step count
      pCoreSubmesh->setLodCount(pSubmeshCandidate->GetLodCount());

      // add the core submesh to the core mesh instance
      coreMesh->addCoreSubmesh(pCoreSubmesh);
    }
  }

  // stop the progress info
  m_pInterface->StopProgressInfo();

  // save core mesh to the file
  if(!CalSaver::saveCoreMesh(strFilename, coreMesh.get()))
  {
    SetLastError(CalError::getLastErrorText(), __FILE__, __LINE__);
    return false;
  }

  return true;
}