Code cleanup

This commit is contained in:
Sascha Willems 2022-01-21 21:07:16 +01:00
parent 6b0bc52a1b
commit aad5a7dd81
2 changed files with 19 additions and 19 deletions

View file

@ -8,7 +8,7 @@
#include "vertexattributes.h" #include "vertexattributes.h"
void VulkanExample::loadSceneNode(const tinygltf::Node& inputNode, const tinygltf::Model& input, Node* parent, std::vector<uint32_t>& indexBuffer, std::vector<Vertex>& vertexBuffer) void VulkanExample::loadSceneNode(const tinygltf::Node& inputNode, const tinygltf::Model& input, Node* parent)
{ {
Node node{}; Node node{};
@ -32,7 +32,7 @@ void VulkanExample::loadSceneNode(const tinygltf::Node& inputNode, const tinyglt
// Load node's children // Load node's children
if (inputNode.children.size() > 0) { if (inputNode.children.size() > 0) {
for (size_t i = 0; i < inputNode.children.size(); i++) { for (size_t i = 0; i < inputNode.children.size(); i++) {
loadSceneNode(input.nodes[inputNode.children[i]], input, &node, indexBuffer, vertexBuffer); loadSceneNode(input.nodes[inputNode.children[i]], input, &node);
} }
} }
@ -93,10 +93,10 @@ void VulkanExample::loadSceneNode(const tinygltf::Node& inputNode, const tinyglt
vertexBuffer.push_back(vert); vertexBuffer.push_back(vert);
// Append separate attributes // Append separate attributes
vertexAttributes.pos.push_back(glm::make_vec3(&positionBuffer[v * 3])); vertexAttributeBuffers.pos.push_back(glm::make_vec3(&positionBuffer[v * 3]));
vertexAttributes.normal.push_back(glm::normalize(glm::vec3(normalsBuffer ? glm::make_vec3(&normalsBuffer[v * 3]) : glm::vec3(0.0f)))); vertexAttributeBuffers.normal.push_back(glm::normalize(glm::vec3(normalsBuffer ? glm::make_vec3(&normalsBuffer[v * 3]) : glm::vec3(0.0f))));
vertexAttributes.tangent.push_back(tangentsBuffer ? glm::make_vec4(&tangentsBuffer[v * 4]) : glm::vec4(0.0f)); vertexAttributeBuffers.tangent.push_back(tangentsBuffer ? glm::make_vec4(&tangentsBuffer[v * 4]) : glm::vec4(0.0f));
vertexAttributes.uv.push_back(texCoordsBuffer ? glm::make_vec2(&texCoordsBuffer[v * 2]) : glm::vec3(0.0f)); vertexAttributeBuffers.uv.push_back(texCoordsBuffer ? glm::make_vec2(&texCoordsBuffer[v * 2]) : glm::vec3(0.0f));
} }
@ -174,6 +174,7 @@ VulkanExample::~VulkanExample()
separateVertexBuffers.pos.destroy(); separateVertexBuffers.pos.destroy();
separateVertexBuffers.tangent.destroy(); separateVertexBuffers.tangent.destroy();
separateVertexBuffers.uv.destroy(); separateVertexBuffers.uv.destroy();
interleavedVertexBuffer.destroy();
for (Image image : scene.images) { for (Image image : scene.images) {
vkDestroyImageView(vulkanDevice->logicalDevice, image.texture.view, nullptr); vkDestroyImageView(vulkanDevice->logicalDevice, image.texture.view, nullptr);
vkDestroyImage(vulkanDevice->logicalDevice, image.texture.image, nullptr); vkDestroyImage(vulkanDevice->logicalDevice, image.texture.image, nullptr);
@ -306,7 +307,7 @@ void VulkanExample::loadglTFFile(std::string filename)
const tinygltf::Scene& scene = glTFInput.scenes[0]; const tinygltf::Scene& scene = glTFInput.scenes[0];
for (size_t i = 0; i < scene.nodes.size(); i++) { for (size_t i = 0; i < scene.nodes.size(); i++) {
const tinygltf::Node node = glTFInput.nodes[scene.nodes[i]]; const tinygltf::Node node = glTFInput.nodes[scene.nodes[i]];
loadSceneNode(node, glTFInput, nullptr, indexBuffer, vertexBuffer); loadSceneNode(node, glTFInput, nullptr);
} }
uploadVertexData(); uploadVertexData();
@ -350,10 +351,10 @@ void VulkanExample::uploadVertexData()
We create multiple separate buffers for each of the vertex attributes (position, normals, etc.) We create multiple separate buffers for each of the vertex attributes (position, normals, etc.)
*/ */
std::array<vks::Buffer, 4> stagingBuffers; std::array<vks::Buffer, 4> stagingBuffers;
createStagingBuffer(stagingBuffers[0], vertexAttributes.pos.data(), vertexAttributes.pos.size() * sizeof(vertexAttributes.pos[0])); createStagingBuffer(stagingBuffers[0], vertexAttributeBuffers.pos.data(), vertexAttributeBuffers.pos.size() * sizeof(vertexAttributeBuffers.pos[0]));
createStagingBuffer(stagingBuffers[1], vertexAttributes.normal.data(), vertexAttributes.normal.size() * sizeof(vertexAttributes.normal[0])); createStagingBuffer(stagingBuffers[1], vertexAttributeBuffers.normal.data(), vertexAttributeBuffers.normal.size() * sizeof(vertexAttributeBuffers.normal[0]));
createStagingBuffer(stagingBuffers[2], vertexAttributes.uv.data(), vertexAttributes.uv.size() * sizeof(vertexAttributes.uv[0])); createStagingBuffer(stagingBuffers[2], vertexAttributeBuffers.uv.data(), vertexAttributeBuffers.uv.size() * sizeof(vertexAttributeBuffers.uv[0]));
createStagingBuffer(stagingBuffers[3], vertexAttributes.tangent.data(), vertexAttributes.tangent.size() * sizeof(vertexAttributes.tangent[0])); createStagingBuffer(stagingBuffers[3], vertexAttributeBuffers.tangent.data(), vertexAttributeBuffers.tangent.size() * sizeof(vertexAttributeBuffers.tangent[0]));
createDeviceBuffer(separateVertexBuffers.pos, stagingBuffers[0].size); createDeviceBuffer(separateVertexBuffers.pos, stagingBuffers[0].size);
createDeviceBuffer(separateVertexBuffers.normal, stagingBuffers[1].size); createDeviceBuffer(separateVertexBuffers.normal, stagingBuffers[1].size);

View file

@ -67,13 +67,6 @@ struct Node {
glm::mat4 matrix; glm::mat4 matrix;
}; };
// Only used at loading time
struct VertexAttributes {
std::vector<glm::vec2> uv;
std::vector<glm::vec3> pos, normal;
std::vector<glm::vec4> tangent;
} vertexAttributes;
std::vector<Node> nodes; std::vector<Node> nodes;
class VulkanExample : public VulkanExampleBase class VulkanExample : public VulkanExampleBase
@ -82,8 +75,14 @@ public:
enum VertexAttributeSettings { interleaved, separate }; enum VertexAttributeSettings { interleaved, separate };
VertexAttributeSettings vertexAttributeSettings = separate; VertexAttributeSettings vertexAttributeSettings = separate;
// Used to store indices and vertices from glTF to be uploaded to the GPU
std::vector<uint32_t> indexBuffer; std::vector<uint32_t> indexBuffer;
std::vector<Vertex> vertexBuffer; std::vector<Vertex> vertexBuffer;
struct VertexAttributes {
std::vector<glm::vec2> uv;
std::vector<glm::vec3> pos, normal;
std::vector<glm::vec4> tangent;
} vertexAttributeBuffers;
// Buffers for the separate vertex attributes // Buffers for the separate vertex attributes
// @todo: rename // @todo: rename
@ -137,7 +136,7 @@ public:
void prepareUniformBuffers(); void prepareUniformBuffers();
void updateUniformBuffers(); void updateUniformBuffers();
void prepare(); void prepare();
void loadSceneNode(const tinygltf::Node& inputNode, const tinygltf::Model& input, Node* parent, std::vector<uint32_t>& indexBuffer, std::vector<Vertex>& vertexBuffer); void loadSceneNode(const tinygltf::Node& inputNode, const tinygltf::Model& input, Node* parent);
void drawSceneNode(VkCommandBuffer commandBuffer, Node node); void drawSceneNode(VkCommandBuffer commandBuffer, Node node);
virtual void render(); virtual void render();
virtual void OnUpdateUIOverlay(vks::UIOverlay* overlay); virtual void OnUpdateUIOverlay(vks::UIOverlay* overlay);