U1 Resource::CreateHardwareBuff( MeshHDBuffer* pOutBuff,CalCoreMesh* pInMesh ){ std::vector<ABuff> vertexData; std::vector<UInt> indexData; SInt iMeshCount = pInMesh->getCoreSubmeshCount(); SInt iVertexCount = 0; SInt iIndexCount = 0; //首先计算顶点数 和 索引数 for(SInt i=0;i<iMeshCount;i++){ CalCoreSubmesh* pSubMesh = pInMesh->getCoreSubmesh(i); iVertexCount += pSubMesh->getVectorVertex().size(); iIndexCount += pSubMesh->getFaceCount()*3; } if( iVertexCount == 0 || iIndexCount == 0) { return false; } //分配空间 vertexData.resize(iVertexCount); indexData.resize(iIndexCount); SInt iV = 0; SInt iI = 0; for(SInt i=0;i<iMeshCount;i++){ CalCoreSubmesh* pSubMesh = pInMesh->getCoreSubmesh(i); //计算切线 pSubMesh->enableTangents(0,true); std::vector<CalCoreSubmesh::Vertex>& lstVector = pSubMesh->getVectorVertex(); std::vector<std::vector<CalCoreSubmesh::TangentSpace> >& lstTangent = pSubMesh->getVectorVectorTangentSpace(); std::vector<std::vector<CalCoreSubmesh::TextureCoordinate> >& lstTexcoord = pSubMesh->getVectorVectorTextureCoordinate(); std::vector<CalCoreSubmesh::Face>& lstFace = pSubMesh->getVectorFace(); SInt iFaceCount = lstFace.size(); //索引缓冲 for(SInt j=0;j<iFaceCount;j++){ indexData[iI] = lstFace[j].vertexId[0] + iV; indexData[iI+1] = lstFace[j].vertexId[1] + iV; indexData[iI+2] = lstFace[j].vertexId[2] + iV; iI += 3; } //顶点缓冲数据 for(SInt j=0;j<iVertexCount;j++){ CalCoreSubmesh::Vertex& ver = lstVector[j]; CalCoreSubmesh::TangentSpace tang = lstTangent[0][j]; CalCoreSubmesh::TextureCoordinate uv = lstTexcoord[0][j]; //位置 vertexData[iV].Position = Float3(ver.position.x,ver.position.y,ver.position.z); SInt iBoneCount = ver.vectorInfluence.size(); for(SInt k=0;k<4;k++){ if(k<iBoneCount){ vertexData[iV].BoneWeight[k] = ver.vectorInfluence[k].weight*255.0f; //如果只传递自己需要的矩阵 //骨骼索引变成0~n vertexData[iV].BoneIndex[k] = ver.vectorInfluence[k].boneId; }else{ vertexData[iV].BoneWeight[k] = 0; vertexData[iV].BoneIndex[k] = 0; } } //法线 vertexData[iV].Normal = Float3(ver.normal.x,ver.normal.y,ver.normal.z); //UV vertexData[iV].UV.x = uv.u; vertexData[iV].UV.y = uv.v; //切线 vertexData[iV].Tangent.x = tang.tangent.x; vertexData[iV].Tangent.y = tang.tangent.y; vertexData[iV].Tangent.z = tang.tangent.z; iV++; } AString strName = m_strProductName + pInMesh->getName(); Render::Vertex::IDeclare::Info dInfo; dInfo.SetPNTT_Animation(); pOutBuff->pVertexDeclare = Render::System::GetSingleton()->CreateProduct<Render::Vertex::IDeclare>("PNTTANIM",&dInfo); Render::Buffer::Info vInfo; vInfo.SetVertexBuffer(iVertexCount,52); vInfo.InitData = &vertexData[0]; pOutBuff->pVertexBuff = Render::System::GetSingleton()->CreateProduct<Render::Buffer>(strName+"VB",&vInfo); vInfo.SetIndexBuffer32(iFaceCount*3); vInfo.InitData = &indexData[0]; pOutBuff->pIndexBuff = Render::System::GetSingleton()->CreateProduct<Render::Buffer>(strName+"IB",&vInfo); } return true; }
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; }