Pino
Pino

Reputation: 629

3d model not rendered (DirectX 12)

I am developing a small program that load 3d models using assimp, but it does not render the model. At first I thought that vertices and indices were not loaded correctly but this is not the case ( I printed on a txt file vertices and indices). I think that the probem might be with the position of the model and camera. The application does not return any error, it runs properly.

Vertex Struct:

struct Vertex {
    XMFLOAT3 position;
    XMFLOAT2 texture;
    XMFLOAT3 normal;
};

Input layout:

D3D12_INPUT_ELEMENT_DESC inputLayout[] =
    {
        { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
        { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
        { "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D12_APPEND_ALIGNED_ELEMENT, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 }
    };

Vertices, texcoords, normals and indices loader:

model = new ModelMesh();
    std::vector<XMFLOAT3> positions;
    std::vector<XMFLOAT3> normals;
    std::vector<XMFLOAT2> texCoords;
    std::vector<unsigned int> indices;
    model->LoadMesh("beast.x", positions, normals,
        texCoords, indices);
    // Create vertex buffer

    if (positions.size() == 0)
    {
        MessageBox(0, L"Vertices vector is empty.",
            L"Error", MB_OK);
    }
    Vertex* vList = new Vertex[positions.size()];
    for (size_t i = 0; i < positions.size(); i++)
    {
        Vertex vert;
        XMFLOAT3 pos = positions[i];
        vert.position = XMFLOAT3(pos.x, pos.y, pos.z);
        XMFLOAT3 norm = normals[i];
        vert.normal = XMFLOAT3(norm.x, norm.y, norm.z);
        XMFLOAT2 tex = texCoords[i];
        vert.texture = XMFLOAT2(tex.x, tex.y);
        vList[i] = vert;
    }

    int vBufferSize = sizeof(vList);

Build of the camera and views:

    XMMATRIX tmpMat = XMMatrixPerspectiveFovLH(45.0f*(3.14f/180.0f), (float)Width / (float)Height, 0.1f, 1000.0f);
    XMStoreFloat4x4(&cameraProjMat, tmpMat);

    // set starting camera state
    cameraPosition = XMFLOAT4(0.0f, 2.0f, -4.0f, 0.0f);
    cameraTarget = XMFLOAT4(0.0f, 0.0f, 0.0f, 0.0f);
    cameraUp = XMFLOAT4(0.0f, 1.0f, 0.0f, 0.0f);

    // build view matrix
    XMVECTOR cPos = XMLoadFloat4(&cameraPosition);
    XMVECTOR cTarg = XMLoadFloat4(&cameraTarget);
    XMVECTOR cUp = XMLoadFloat4(&cameraUp);
    tmpMat = XMMatrixLookAtLH(cPos, cTarg, cUp);
    XMStoreFloat4x4(&cameraViewMat, tmpMat);


    cube1Position = XMFLOAT4(0.0f, 0.0f, 0.0f, 0.0f); 
    XMVECTOR posVec = XMLoadFloat4(&cube1Position); 

    tmpMat = XMMatrixTranslationFromVector(posVec); 
    XMStoreFloat4x4(&cube1RotMat, XMMatrixIdentity()); 
    XMStoreFloat4x4(&cube1WorldMat, tmpMat); 

Update function :

    XMStoreFloat4x4(&cube1WorldMat, worldMat);


    XMMATRIX viewMat = XMLoadFloat4x4(&cameraViewMat); // load view matrix
    XMMATRIX projMat = XMLoadFloat4x4(&cameraProjMat); // load projection matrix
    XMMATRIX wvpMat = XMLoadFloat4x4(&cube1WorldMat) * viewMat * projMat; // create wvp matrix
    XMMATRIX transposed = XMMatrixTranspose(wvpMat); // must transpose wvp matrix for the gpu
    XMStoreFloat4x4(&cbPerObject.wvpMat, transposed); // store transposed wvp matrix in constant buffer

    memcpy(cbvGPUAddress[frameIndex], &cbPerObject, sizeof(cbPerObject));

VERTEX SHADER:

struct VS_INPUT
{
    float4 pos : POSITION;
    float2 tex: TEXCOORD;
    float3 normal : NORMAL;
};

struct VS_OUTPUT
{
    float4 pos: SV_POSITION;
    float2 tex: TEXCOORD;
    float3 normal: NORMAL;
};

cbuffer ConstantBuffer : register(b0)
{
    float4x4 wvpMat;
};

VS_OUTPUT main(VS_INPUT input)
{
    VS_OUTPUT output;
    output.pos = mul(input.pos, wvpMat);
    return output;
}

Hope it is a long code to read but I don't understand what is going wrong with this code. Hope somebody can help me.

Upvotes: 0

Views: 563

Answers (1)

cmaughan
cmaughan

Reputation: 2634

A few things to try/check:

  • Make your background clear color grey. That way, if you are drawing black triangles you will see them.
  • Turn backface culling off in the rendering state, in case your triangles are back to front.
  • Turn depth test off in the rendering state.
  • Turn off alpha blending.
  • You don't show your pixel shader, but try writing a constant color to see if your lighting calculation is broken.
  • Use NVIDIA's nSight tool, or the Visual Studio Graphics debugger to see what your graphics pipeline is doing.

Those are usually the things I try first...

Upvotes: 1

Related Questions