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

📄 hardwaremodel.cpp

📁 Cal3D实现虚拟角色 Cal3D实现虚拟角色
💻 CPP
📖 第 1 页 / 共 2 页
字号:
  *  * @return The number of vertex.  *****************************************************************************/int CalHardwareModel::getVertexCount(){    if( m_selectedHardwareMesh >= 0 && m_selectedHardwareMesh < int(m_vectorHardwareMesh.size()))  {    return m_vectorHardwareMesh[m_selectedHardwareMesh].vertexCount;  }  return 0;   }/*****************************************************************************//** Returns the number of bone.  *  * This function returns the number of bone in the selected hardware mesh instance.  *  * @return The number of bone.  *****************************************************************************/int CalHardwareModel::getBoneCount(){    if( m_selectedHardwareMesh >= 0 && m_selectedHardwareMesh < int(m_vectorHardwareMesh.size()))  {    return m_vectorHardwareMesh[m_selectedHardwareMesh].m_vectorBonesIndices.size();  }  return 0;   }/*****************************************************************************//** Returns the base vertex index.  *  * This function returns the base vertex index of the selected hardware mesh instance.  *  * @return a integer with the base vertex index.  *****************************************************************************/int CalHardwareModel::getBaseVertexIndex(){    if( m_selectedHardwareMesh >= 0 && m_selectedHardwareMesh < int(m_vectorHardwareMesh.size()))  {    return m_vectorHardwareMesh[m_selectedHardwareMesh].baseVertexIndex;  }  return 0;   }/*****************************************************************************//** Returns the start index.  *  * This function returns the start index of the selected hardware mesh instance.  *  * @return a integer with the start index.  *****************************************************************************/int CalHardwareModel::getStartIndex(){    if( m_selectedHardwareMesh >= 0 && m_selectedHardwareMesh < int(m_vectorHardwareMesh.size()))  {    return m_vectorHardwareMesh[m_selectedHardwareMesh].startIndex;  }  return 0;   } /*****************************************************************************//** Selects a hardware mesh for rendering data queries.  *  * This function selects a hardware mesh for further rendering data queries.  *  * @param meshId The ID of the hardware mesh that should be used for further rendering  *               data queries.  *  * @return One of the following values:  *         \li \b true if successful  *         \li \b false if an error happend  *****************************************************************************/bool CalHardwareModel::selectHardwareMesh(size_t meshId){  if( meshId < m_vectorHardwareMesh.size())   {    m_selectedHardwareMesh=meshId;        return true;  }  return false;   }/*****************************************************************************//** Returns the number of faces in the hardware model instance.  *  * This function returns the number of faces in the hardware model instance.  *  * @return The number of faces.  *****************************************************************************/int CalHardwareModel::getTotalFaceCount(){  return m_totalFaceCount;}/*****************************************************************************//** Returns the number of vertices in the hardware model instance.  *  * This function returns the number of vertices in the hardware model instance.  *  * @return The number of vertices.  *****************************************************************************/int CalHardwareModel::getTotalVertexCount(){  return m_totalVertexCount;}/*****************************************************************************//** Provides access to a specified map user data.** This function returns the user data stored in the specified map of the* material of the selected hardware mesh.** @param mapId The ID of the map.** @return One of the following values:*         \li the user data stored in the specified map*         \li \b 0 if an error happend*****************************************************************************/Cal::UserData CalHardwareModel::getMapUserData(int mapId){    if( m_selectedHardwareMesh >= 0 && m_selectedHardwareMesh < int(m_vectorHardwareMesh.size()))  {    if(m_vectorHardwareMesh[m_selectedHardwareMesh].pCoreMaterial==0)      return 0;        // get the map vector        std::vector<CalCoreMaterial::Map>& vectorMap = m_vectorHardwareMesh[m_selectedHardwareMesh].pCoreMaterial->getVectorMap();                // check if the map id is valid        if((mapId < 0) || (mapId >= (int)vectorMap.size()))        {      CalError::setLastError(CalError::INVALID_HANDLE, __FILE__, __LINE__);      return 0;        }                return vectorMap[mapId].userData;  }  return 0;}/*****************************************************************************//** Compute the information needed to use the hardware model .** This function Compute the information needed to use the hardware model,* it fill vertex buffers with the model data** @param baseVertexIndex The base vertex Index.* @param startIndex The start index.* @param maxBonesPerMesh The maximun of bone by hardware mesh** @return One of the following values:*         \li \b true if succeed*         \li \b false if an error happend*****************************************************************************/bool CalHardwareModel::load(int baseVertexIndex, int startIndex,int maxBonesPerMesh){  if(m_pVertexBuffer==NULL ||  m_pNormalBuffer ==NULL|| m_pWeightBuffer ==NULL || m_pMatrixIndexBuffer ==NULL)  {    CalError::setLastError(CalError::INVALID_HANDLE, __FILE__, __LINE__);    return false;     }  int mapId;  for(mapId = 0; mapId < m_textureCoordNum; mapId++)  {    if(m_pTextureCoordBuffer[mapId]==NULL)    {      CalError::setLastError(CalError::INVALID_HANDLE, __FILE__, __LINE__);      return false;             }    }     m_vectorVertexIndiceUsed.resize(50000);  int vertexCount=baseVertexIndex;  int faceIndexCount = startIndex;                // unused.  //CalCoreSkeleton * pCoreSkeleton = m_pCoreModel->getCoreSkeleton();  //std::vector< CalCoreBone *>& vectorBone = pCoreSkeleton->getVectorCoreBone();  // if unspecified, fill with all core mesh ids  if(m_coreMeshIds.empty())  {    for(int coreMeshId = 0; coreMeshId < m_pCoreModel->getCoreMeshCount(); coreMeshId++)      m_coreMeshIds.push_back(coreMeshId);  }      for(std::vector<int>::iterator meshIdIt = m_coreMeshIds.begin();meshIdIt != m_coreMeshIds.end(); meshIdIt++)  {    int meshId = *meshIdIt;    CalCoreMesh *pCoreMesh = m_pCoreModel->getCoreMesh(meshId);    int submeshCount= pCoreMesh->getCoreSubmeshCount();    int submeshId;    for(submeshId = 0 ;submeshId < submeshCount ; submeshId++)    {           CalCoreSubmesh *pCoreSubmesh = pCoreMesh->getCoreSubmesh(submeshId);            std::vector<CalCoreSubmesh::Vertex>& vectorVertex = pCoreSubmesh->getVectorVertex();      std::vector<CalCoreSubmesh::Face>& vectorFace = pCoreSubmesh->getVectorFace();                        // unused.      //std::vector< std::vector<CalCoreSubmesh::TextureCoordinate> >& vectorTex = pCoreSubmesh->getVectorVectorTextureCoordinate();            CalHardwareMesh hardwareMesh;      hardwareMesh.meshId = meshId;      hardwareMesh.submeshId = submeshId;            hardwareMesh.baseVertexIndex=vertexCount;           hardwareMesh.startIndex=faceIndexCount;      hardwareMesh.m_vectorBonesIndices.clear();                      hardwareMesh.vertexCount=0;      hardwareMesh.faceCount=0;                 int startIndex=hardwareMesh.startIndex;            int faceId;           for( faceId =0 ;faceId<pCoreSubmesh->getFaceCount();faceId++)      {        if(canAddFace(hardwareMesh,vectorFace[faceId],vectorVertex,maxBonesPerMesh))        {          m_pIndexBuffer[startIndex+hardwareMesh.faceCount*3]=   addVertex(hardwareMesh,vectorFace[faceId].vertexId[0],pCoreSubmesh,maxBonesPerMesh);          m_pIndexBuffer[startIndex+hardwareMesh.faceCount*3+1]= addVertex(hardwareMesh,vectorFace[faceId].vertexId[1],pCoreSubmesh,maxBonesPerMesh);          m_pIndexBuffer[startIndex+hardwareMesh.faceCount*3+2]= addVertex(hardwareMesh,vectorFace[faceId].vertexId[2],pCoreSubmesh,maxBonesPerMesh);          hardwareMesh.faceCount++;        }        else        {          vertexCount+=hardwareMesh.vertexCount;          faceIndexCount+=hardwareMesh.faceCount*3;          hardwareMesh.pCoreMaterial= m_pCoreModel->getCoreMaterial(pCoreSubmesh->getCoreMaterialThreadId());                    m_vectorHardwareMesh.push_back(hardwareMesh);                    hardwareMesh.baseVertexIndex=vertexCount;          hardwareMesh.startIndex=faceIndexCount;                    hardwareMesh.m_vectorBonesIndices.clear();          hardwareMesh.vertexCount=0;           hardwareMesh.faceCount=0;                    startIndex=hardwareMesh.startIndex;                    m_pIndexBuffer[startIndex+hardwareMesh.faceCount*3]=   addVertex(hardwareMesh,vectorFace[faceId].vertexId[0],pCoreSubmesh,maxBonesPerMesh);          m_pIndexBuffer[startIndex+hardwareMesh.faceCount*3+1]= addVertex(hardwareMesh,vectorFace[faceId].vertexId[1],pCoreSubmesh,maxBonesPerMesh);          m_pIndexBuffer[startIndex+hardwareMesh.faceCount*3+2]= addVertex(hardwareMesh,vectorFace[faceId].vertexId[2],pCoreSubmesh,maxBonesPerMesh);          hardwareMesh.faceCount++;                 }      }            vertexCount+=hardwareMesh.vertexCount;      faceIndexCount+=hardwareMesh.faceCount*3;      hardwareMesh.pCoreMaterial= m_pCoreModel->getCoreMaterial(pCoreSubmesh->getCoreMaterialThreadId());            m_vectorHardwareMesh.push_back(hardwareMesh);          }  }      m_vectorVertexIndiceUsed.clear();  m_totalFaceCount=0;  m_totalVertexCount=0;  for(size_t hardwareMeshId = 0; hardwareMeshId <  m_vectorHardwareMesh.size(); hardwareMeshId++)  {    m_totalFaceCount+=m_vectorHardwareMesh[hardwareMeshId].faceCount;    m_totalVertexCount+=m_vectorHardwareMesh[hardwareMeshId].vertexCount;  }        return true;}  bool CalHardwareModel::canAddFace(CalHardwareMesh &hardwareMesh, CalCoreSubmesh::Face & face,std::vector<CalCoreSubmesh::Vertex>& vectorVertex, int maxBonesPerMesh){  size_t boneCount=hardwareMesh.m_vectorBonesIndices.size();    for(unsigned faceIndex=0;faceIndex<3;faceIndex++)  {    for(size_t influenceIndex=0;influenceIndex< vectorVertex[face.vertexId[faceIndex]].vectorInfluence.size();influenceIndex++)    {      unsigned boneIndex=0;      while(boneIndex< hardwareMesh.m_vectorBonesIndices.size()         && hardwareMesh.m_vectorBonesIndices[boneIndex]!=vectorVertex[face.vertexId[faceIndex]].vectorInfluence[influenceIndex].boneId)        boneIndex++;            if(boneIndex==hardwareMesh.m_vectorBonesIndices.size())        boneCount++;    }  }    /// @todo Change maxBonesPerMesh to a size_t?        if(int(boneCount)>maxBonesPerMesh)    return false;    return true;  }int CalHardwareModel::addVertex(CalHardwareMesh &hardwareMesh, int indice, CalCoreSubmesh *pCoreSubmesh, int maxBonesPerMesh){  int i=0;    while(i< hardwareMesh.vertexCount && m_vectorVertexIndiceUsed[i]!=indice)    i++;    if(i != hardwareMesh.vertexCount)    return i;    std::vector<CalCoreSubmesh::Vertex>& vectorVertex = pCoreSubmesh->getVectorVertex();  std::vector< std::vector<CalCoreSubmesh::TextureCoordinate> >& vectorvectorTextureCoordinate = pCoreSubmesh->getVectorVectorTextureCoordinate();  std::vector< std::vector<CalCoreSubmesh::TangentSpace> >& vectorvectorTangentSpace = pCoreSubmesh->getVectorVectorTangentSpace();  m_vectorVertexIndiceUsed[hardwareMesh.vertexCount]=indice;    memcpy(&m_pVertexBuffer[(hardwareMesh.baseVertexIndex+i)*m_vertexStride],&vectorVertex[indice].position,sizeof(CalVector));  memcpy(&m_pNormalBuffer[(hardwareMesh.baseVertexIndex+i)*m_normalStride],&vectorVertex[indice].normal,sizeof(CalVector));  /// @todo change m_textureCoordName to size_t?  size_t mapId;  for(mapId = 0; int(mapId) < m_textureCoordNum; mapId++)  {    if( vectorvectorTextureCoordinate.size() > mapId)      memcpy(&m_pTextureCoordBuffer[mapId][(hardwareMesh.baseVertexIndex+i)*m_textureCoordStride[mapId]],&vectorvectorTextureCoordinate[mapId][indice],sizeof(CalCoreSubmesh::TextureCoordinate));    else      memset(&m_pTextureCoordBuffer[mapId][(hardwareMesh.baseVertexIndex+i)*m_textureCoordStride[mapId]],0,sizeof(CalCoreSubmesh::TextureCoordinate));      }  for(mapId = 0; mapId < 8 ; mapId++)  {    if(m_pTangentSpaceBuffer[mapId] != NULL)    {      if(vectorvectorTangentSpace.size() > mapId && pCoreSubmesh->isTangentsEnabled(mapId))        memcpy(&m_pTangentSpaceBuffer[mapId][(hardwareMesh.baseVertexIndex+i)*m_tangentSpaceStride[mapId]],&vectorvectorTangentSpace[mapId][indice],sizeof(CalCoreSubmesh::TangentSpace));      else        memset(&m_pTangentSpaceBuffer[mapId][(hardwareMesh.baseVertexIndex+i)*m_tangentSpaceStride[mapId]],0,sizeof(CalCoreSubmesh::TangentSpace));    }  }    for(size_t l=0 ; l<4 ; l++)  {    if(l < vectorVertex[indice].vectorInfluence.size())    {       int BoneId = vectorVertex[indice].vectorInfluence[l].boneId;      float newBoneId = (float)addBoneIndice(hardwareMesh,BoneId,maxBonesPerMesh);          memcpy(&m_pWeightBuffer[(hardwareMesh.baseVertexIndex+i)*m_weightStride+l * sizeof(float) ], &vectorVertex[indice].vectorInfluence[l].weight ,sizeof(float));           memcpy(&m_pMatrixIndexBuffer[(hardwareMesh.baseVertexIndex+i)*m_matrixIndexStride+l * sizeof(float) ], &newBoneId ,sizeof(float));          }    else    {      memset(&m_pWeightBuffer[(hardwareMesh.baseVertexIndex+i)*m_weightStride+l * sizeof(float) ], 0 ,sizeof(float));           memset(&m_pMatrixIndexBuffer[(hardwareMesh.baseVertexIndex+i)*m_matrixIndexStride+l * sizeof(float) ], 0 ,sizeof(float));    }  }  hardwareMesh.vertexCount++;  return i;}int CalHardwareModel::addBoneIndice(CalHardwareMesh &hardwareMesh, int Indice, int maxBonesPerMesh){   size_t i=0;  while(i< hardwareMesh.m_vectorBonesIndices.size()  && hardwareMesh.m_vectorBonesIndices[i]!=Indice)    i++;  if( i != hardwareMesh.m_vectorBonesIndices.size())    return i;  /// @todo change maxBonesPerMesh to size_t?        if(int(hardwareMesh.m_vectorBonesIndices.size())<maxBonesPerMesh)  {    hardwareMesh.m_vectorBonesIndices.push_back(Indice);    return i;  }  else   {    return -1;  }}

⌨️ 快捷键说明

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