0
\$\begingroup\$

I have been following "Vulkan Tuturial" on GitHub as a reference on how to get basic objects rendering. In their tutorial, they create the command buffers at the start of the application after they have created a objects vertex and index buffers, then define that they want to draw it. Then one per frame they submit the command. That part I'm fine with.

I have been attempting to make it so I can send draw commands during the rendering loop, rather than just define one object that should be rendered and call that command every frame, the reason is so I can send custom uniform buffers with each command, to draw them in different locations on the screen.

Unfortunately, when I try and pass a diffrent uniform buffer for each draw call, only the last uniform buffer is applied.

Render loop

while (engine.Running())
{
    engine.PreRender();

    engine.BindMesh(mesh);
    engine.UpdateUniformBuffer(0.0f);
    engine.Draw();
    engine.UpdateUniformBuffer(90.0f);
    engine.Draw();

    engine.PostRender();
}

UpdateUniformBuffer

void Engine::UpdateUniformBuffer(float rot)
{
    static auto startTime = std::chrono::high_resolution_clock::now();

    auto currentTime = std::chrono::high_resolution_clock::now();
    float time = std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - startTime).count() / 1000.0f;

    UniformBufferObject ubo = {};
    ubo.model = glm::rotate(glm::mat4(), rot, glm::vec3(0.0f, 0.0f, 1.0f));
    ubo.view = glm::lookAt(glm::vec3(2.0f, 2.0f, 2.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f));
    ubo.proj = glm::perspective(glm::radians(45.0f), window.swap_chain_extent.width / (float)window.swap_chain_extent.height, 0.1f, 10.0f);
    // Since GLM was made for OpenGL we need to flip the frame
    ubo.proj[1][1] *= -1;

    void* data;
    vkMapMemory(vulkan.device, vulkan.uniform_buffer.buffer_memory, 0, sizeof(ubo), 0, &data);
    memcpy(data, &ubo, sizeof(ubo));
    vkUnmapMemory(vulkan.device, vulkan.uniform_buffer.buffer_memory);
}

Draw function

vkCmdDrawIndexed(
        vulkan.command_buffers[i],
        static_cast<uint32_t>(indices.size()),
        1,
        0,
        0,
        0
    );
\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

I have managed to solve this issue using two uniform buffers, one for static data (data that does not change per frame like the projection matrix and the view matrix) and one for dynamic data (this is the model matrix).

I ended up using this small guide as the backbone of the solution. Link to Guide

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .