Search code examples
c++direct3dassimp

Meshes loaded via Assimp look distorted


So I'm trying to load and render mesh with assimp and DirectX11.(Im loosely following tutorials on youtube) The problem is that it looks weird and distorted. I've checked my meshes - blender and assimp viewer load them correctly. Results of rendering suzanne from obj file: Suzanne from obj file

It looks kinda like the index buffer is wrong but i do not see a mistake when propagating it; Mesh loading code:

struct Vertex
{
   struct
   {
       float x;
       float y;
       float z;
   }Position;

};
Model::Model(const std::string & path)
    {
        Ref<Zephyr::VertexShader> VertexShader = Zephyr::VertexShader::Create("MinimalShaders.hlsl", "VertexShaderMain");

        AddBindable(VertexShader);

        AddBindable(Zephyr::PixelShader::Create("MinimalShaders.hlsl", "PixelShaderMain"));

        AddBindable(Zephyr::Topology::Create(Zephyr::Topology::TopologyType::TriangleList));

        std::vector<Zephyr::InputLayout::InputLayoutElement> InputLayout =
        {
            {"Position",Zephyr::InputLayout::InputDataType::Float3}
        };

        AddBindable(Zephyr::InputLayout::Create(InputLayout, VertexShader));

        Assimp::Importer Imp;
        auto model = Imp.ReadFile(path, aiProcess_JoinIdenticalVertices | aiProcess_Triangulate);

        const auto Mesh = model->mMeshes[0];

        std::vector<Vertex> Vertices;
        Vertices.reserve(Mesh->mNumVertices);

        for (unsigned int i = 0; i < Mesh->mNumVertices; i++)
        {
            Vertex buf;
            buf.Position = { Mesh->mVertices[i].x,Mesh->mVertices[i].y ,Mesh->mVertices[i].z };
            Vertices.push_back(buf);
        }

        std::vector<unsigned short> Indices;
        Indices.reserve(Mesh->mNumFaces * 3);
        

        for (unsigned int i = 0; i < Mesh->mNumFaces; i++)
        {
            const auto & face = Mesh->mFaces[i];

            if (face.mNumIndices != 3)
            {
                Zephyr::Log::Error("More than 3 indices per face ?!"); continue;
            }
                
            Indices.push_back(face.mIndices[0]);
            Indices.push_back(face.mIndices[1]);
            Indices.push_back(face.mIndices[2]);
        }

        AddBindable(Zephyr::VertexBuffer::Create(Vertices));
        BindIndexBuffer(Zephyr::IndexBuffer::Create(Indices));

    }

My shaders are pretty minimal to

cbuffer CBuff
{
    matrix transform;
};

float4 VertexShaderMain(float3 position : Position) : SV_POSITION
{
    return mul(float4(position, 1.0), transform);

}

float4 PixelShaderMain() : SV_TARGET
{
  return float4(1.0,1.0,1.0,1.0);
}

My pipeline renders correctly, f. ex. cubes which are hard coded, but when i try to load cube from file this happens: distorted cube

Assimp opens the file and loads the correct number of vertices. Also number of indices seems to be ok(for cube there are 12 triangles and 36 indices)

Honestly at this point I have no idea what im doing wrong. Am i missing sth obvious?

Thanks in advance


Solution

  • So I've figured it out. The issue was that in some other file i already defined structure named Vertex. That structure contained also uv's so ultimately my vertex buffer ended up being a mess. Silly mistake.