0

I was looking on the internet for some Assimp sample but without success.

I have the following struct:

struct VertexTextureNormal
{
    XMFLOAT3 Position;
    XMFLOAT2 TexCoord;
    XMFLOAT3 Normal;
};

Can anyone please post actual code that fills an array of vertex and indices?

1 Answer 1

2
        DWORD nIndices;
        DWORD *pIndices;

        if (pAIMesh->HasFaces())
        {
            aiFace *pAIFaces;

            pAIFaces = pAIMesh->mFaces;
            nIndices = pAIMesh->mNumFaces * 3;

            pIndices = new DWORD[nIndices];

            for (DWORD i = 0; i < pAIMesh->mNumFaces; i++)
            {
                if (pAIFaces[i].mNumIndices != 3)
                {
                    aiReleaseImport(pScene);
                    delete[] pIndices;
                    return E_FAIL;
                }

                for (DWORD j = 0; j < 3; j++)
                {
                    pIndices[i * 3 + j] = pAIFaces[i].mIndices[j];
                }
            }
        }

        if (pAIMesh->HasPositions())
        {
            DWORD nVertices;
            CVertex *pVertices;

            nVertices = pAIMesh->mNumVertices;

            pVertices = new CVertex[nVertices];

            for (DWORD i = 0; i < nVertices; i++)
            {
                pVertices[i].vPos = XMFLOAT3(pAIMesh->mVertices[i].x, pAIMesh->mVertices[i].y, pAIMesh->mVertices[i].z);

                XMStoreFloat3(&pVertices[i].vPos, XMVector3TransformCoord(XMLoadFloat3(&pVertices[i].vPos), mLocalWorld));
            }

            if (pAIMesh->HasNormals())
            {
                for (DWORD i = 0; i < nVertices; i++)
                {
                    XMVECTOR xvNormal = XMLoadFloat3((XMFLOAT3 *)&pAIMesh->mNormals[i]);

                    xvNormal = XMVector3Normalize(xvNormal);

                    XMStoreFloat3(&pVertices[i].vNormal, xvNormal);
                }
            }

            if (pAIMesh->HasTextureCoords(0))
            {
                for (DWORD i = 0; i < nVertices; i++)
                {
                    pVertices[i].vTexCoords = XMFLOAT2(pAIMesh->mTextureCoords[0][i].x, pAIMesh->mTextureCoords[0][i].y);
                }
            }

            pMesh->m_pVertices = pVertices;
            pMesh->m_dwNumVertices = nVertices;
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.