/** * Read mesh data from ASSIMP mesh to an internal mesh representation that can be used to generate Vulkan buffers * * @param meshEntry Pointer to the target MeshEntry strucutre for the mesh data * @param paiMesh ASSIMP mesh to get the data from * @param pScene Scene file of the ASSIMP mesh */ void InitMesh(MeshEntry *meshEntry, const aiMesh* paiMesh, const aiScene* pScene) { meshEntry->MaterialIndex = paiMesh->mMaterialIndex; aiColor3D pColor(0.f, 0.f, 0.f); pScene->mMaterials[paiMesh->mMaterialIndex]->Get(AI_MATKEY_COLOR_DIFFUSE, pColor); aiVector3D Zero3D(0.0f, 0.0f, 0.0f); for (unsigned int i = 0; i < paiMesh->mNumVertices; i++) { aiVector3D* pPos = &(paiMesh->mVertices[i]); aiVector3D* pNormal = &(paiMesh->mNormals[i]); aiVector3D* pTexCoord = (paiMesh->HasTextureCoords(0)) ? &(paiMesh->mTextureCoords[0][i]) : &Zero3D; aiVector3D* pTangent = (paiMesh->HasTangentsAndBitangents()) ? &(paiMesh->mTangents[i]) : &Zero3D; aiVector3D* pBiTangent = (paiMesh->HasTangentsAndBitangents()) ? &(paiMesh->mBitangents[i]) : &Zero3D; Vertex v( glm::vec3(pPos->x, -pPos->y, pPos->z), glm::vec2(pTexCoord->x , pTexCoord->y), glm::vec3(pNormal->x, pNormal->y, pNormal->z), glm::vec3(pTangent->x, pTangent->y, pTangent->z), glm::vec3(pBiTangent->x, pBiTangent->y, pBiTangent->z), glm::vec3(pColor.r, pColor.g, pColor.b) ); dim.max.x = fmax(pPos->x, dim.max.x); dim.max.y = fmax(pPos->y, dim.max.y); dim.max.z = fmax(pPos->z, dim.max.z); dim.min.x = fmin(pPos->x, dim.min.x); dim.min.y = fmin(pPos->y, dim.min.y); dim.min.z = fmin(pPos->z, dim.min.z); meshEntry->Vertices.push_back(v); } dim.size = dim.max - dim.min; uint32_t indexBase = static_cast<uint32_t>(meshEntry->Indices.size()); for (unsigned int i = 0; i < paiMesh->mNumFaces; i++) { const aiFace& Face = paiMesh->mFaces[i]; if (Face.mNumIndices != 3) continue; meshEntry->Indices.push_back(indexBase + Face.mIndices[0]); meshEntry->Indices.push_back(indexBase + Face.mIndices[1]); meshEntry->Indices.push_back(indexBase + Face.mIndices[2]); } }
void Mesh::initMesh( unsigned int _meshIndex, const aiMesh* _aiMesh, std::vector<ngl::Vec3>& o_positions, std::vector<ngl::Vec3>& o_normals, std::vector<ngl::Vec2>& o_texCoords, std::vector<VertexBoneData>& o_bones, std::vector<unsigned int>& o_indices ) { ngl::Vec3 Zero3D(0.0f, 0.0f, 0.0f); // Populate the vertex attribute vectors for (unsigned int i = 0 ; i < _aiMesh->mNumVertices ; ++i) { ngl::Vec3 pos = AIU::aiVector3DToNGLVec3(_aiMesh->mVertices[i]); ngl::Vec3 normal = AIU::aiVector3DToNGLVec3(_aiMesh->mNormals[i]); ngl::Vec3 tex = _aiMesh->HasTextureCoords(0) ? AIU::aiVector3DToNGLVec3(_aiMesh->mTextureCoords[0][i]) : Zero3D; o_positions.push_back(pos); o_normals.push_back(normal); o_texCoords.push_back(ngl::Vec2(tex.m_x,tex.m_y) ); } loadBones(_meshIndex, _aiMesh, o_bones); // Populate the index buffer for (unsigned int i = 0 ; i < _aiMesh->mNumFaces ; ++i) { const aiFace& Face = _aiMesh->mFaces[i]; o_indices.push_back(Face.mIndices[0]); o_indices.push_back(Face.mIndices[1]); o_indices.push_back(Face.mIndices[2]); } }