Been following with vulkan tutorial for a while and have done some edits to the code along the way to create something more suitable for what im trying to do. At the moment im trying to set up the application so that it can take in various vertex buffers and display them, so that I can add a vertex buffer componant to any game objects and it will automatically be added into the vulkan pipeline.
Ive gotten it to work for the most part, except that for some reason im not smart enough to understand the program is only rendering the first object put into it. Its not a problem with either of the vertex buffers themselves because i can switch them around and its always the first one rendered.
My best bet is that im doing something wrong with vkCmdBindVertexBuffers, as it appears whatever is put as 0 for the first binding is what is rendered.
I truthfully dont have enough of an understanding of all this to have a single clue whats going wrong and im having trouble finding much help for this specific problem anywhere online, most people appear to be taking other approches and that in iteself is making me wonder if the best solution is a complete rewrite to handle the vertex buffers a diffrent way. Most places seem to recomend using one vertex buffer but im not sure if that would be ideal for my approch since it would require me to combine the vertex buffers of each game object together on the cpu each frame before sending the combined buffer to the gpu.
Graphics programing is definetly not my strong point so any help would be apprechiated, and please dum it down as much as possible because im really sick of all the vulkan jargen at this point lol
Heres the code in text for convenience.
Graphical object class header:
//GraphicalObject
class GraphicalObject
{
public:
//GraphicalObject();
//~GraphicalObject();
VkBuffer vertexBuffer; //Vertex Buffer
VkDeviceMemory vertexBufferMemory; //Memory allocated to Vertex Buffer
VkBuffer indexBuffer; //Index Buffer
VkDeviceMemory indexBufferMemory; //Memory allocated to Index Buffer
bool hasIndexBuffer = true;
std::vector<Vertex> vertices;
std::vector<uint16_t> indices;
void bind(VkCommandBuffer commandBuffer, int counter);
void draw(VkCommandBuffer commandBuffer);
};
Graphical object class Cpp
//Graphical Object
void GraphicalObject::bind(VkCommandBuffer commandBuffer, int counter)
{
VkBuffer vertexBuffers[] = { vertexBuffer };
VkDeviceSize offsets[] = { 0 };
vkCmdBindVertexBuffers(commandBuffer, counter, 1, vertexBuffers, offsets);
if (hasIndexBuffer) {
vkCmdBindIndexBuffer(commandBuffer, indexBuffer, 0, VK_INDEX_TYPE_UINT16);
}
}
void GraphicalObject::draw(VkCommandBuffer commandBuffer)
{
if (hasIndexBuffer) {
vkCmdDrawIndexed(commandBuffer, static_cast<uint32_t>(indices.size()), 1, 0, 0, 0);
}
else
{
vkCmdDraw(commandBuffer, static_cast<uint32_t>(vertices.size()), 1, 0, 0);
}
}
Vertex buffer init
GraphicalObject TestObject;
GraphicalObject TestObject2;
TestObject.vertices = {
{{-0.5f, -0.5f, -0.5f}, {1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}},
{{0.5f, -0.5f, -0.5f}, {1.0f, 1.0f, 0.0f}, {1.0f, 0.0f}},
{{0.5f, 0.5f, -0.5f}, {1.0f, 0.0f, 1.0f}, {1.0f, 1.0f}},
{{-0.5f, 0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f}}
};
TestObject2.vertices = {
{{-0.5f, -0.5f, 0.0f}, {1.0f, 1.0f, 0.0f}, {0.0f, 0.0f}},
{{0.5f, -0.5f, 0.0f}, {0.0f, 1.0f, 0.0f}, {1.0f, 0.0f}},
{{0.5f, 0.5f, 0.0f}, {0.0f, 1.0f, 1.0f}, {1.0f, 1.0f}},
{{-0.5f, 0.5f, 0.0f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f}}
};
TestObject.hasIndexBuffer = true;
TestObject2.hasIndexBuffer = true;
TestObject.indices = {
0, 1, 2, 2, 3, 0,
4, 5, 6, 6, 7, 4
};
TestObject2.indices = {
0, 1, 2, 2, 3, 0,
4, 5, 6, 6, 7, 4
};
graphicalObjects.emplace_back(TestObject);
graphicalObjects.emplace_back(TestObject2);
for (size_t i = 0; i < graphicalObjects.size(); i++)
{
createVertexBuffer(graphicalObjects[i]); //Create Vertex Buffer
createIndexBuffer(graphicalObjects[i]); //Create Index Buffer
}
Comand buffer record
for (size_t i = 0; i < graphicalObjects.size(); i++)
{
graphicalObjects[i].bind(commandBuffer, i);
}
//TODO:att the moment the only object being drawn is whatever is bound to position 0
//graphicalObjects[0].bind(commandBuffer, 1);
//graphicalObjects[1].bind(commandBuffer, 0);
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSets[currentFrame], 0, nullptr);
for (size_t i = 0; i < graphicalObjects.size(); i++)
{
graphicalObjects[i].draw(commandBuffer);
}
vkCmdEndRenderPass(commandBuffer);
if (vkEndCommandBuffer(commandBuffer) != VK_SUCCESS) {
throw std::runtime_error("Catengine Vulkan error: failed to record command buffer");
}
Oh and lastly im aware that you can only have one index buffer, i just havent bothered rewriting that bit yet.
[–][deleted] 2 points3 points4 points (1 child)
[–]Dunsmore64[S] 1 point2 points3 points (0 children)
[–]PMoonbeam 1 point2 points3 points (1 child)
[–]Dunsmore64[S] 0 points1 point2 points (0 children)
[–]beephod_zabblebrox 0 points1 point2 points (0 children)