init
parent
2aa0ab76d1
commit
f8212fca5e
|
@ -17,15 +17,106 @@
|
||||||
* If you are looking for a complete glTF implementation, check out https://github.com/SaschaWillems/Vulkan-glTF-PBR/
|
* If you are looking for a complete glTF implementation, check out https://github.com/SaschaWillems/Vulkan-glTF-PBR/
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define TINYGLTF_IMPLEMENTATION
|
||||||
|
#define STB_IMAGE_IMPLEMENTATION
|
||||||
|
#define TINYGLTF_NO_STB_IMAGE_WRITE
|
||||||
|
#ifdef VK_USE_PLATFORM_ANDROID_KHR
|
||||||
|
#define TINYGLTF_ANDROID_LOAD_FROM_ASSETS
|
||||||
|
#endif
|
||||||
|
#include "tiny_gltf.h"
|
||||||
|
|
||||||
#include "homework1.h"
|
#include "vulkanexamplebase.h"
|
||||||
|
|
||||||
glm::mat4 VulkanglTFModel::Node::getLocalMatrix()
|
#define ENABLE_VALIDATION false
|
||||||
|
|
||||||
|
// Contains everything required to render a glTF model in Vulkan
|
||||||
|
// This class is heavily simplified (compared to glTF's feature set) but retains the basic glTF structure
|
||||||
|
class VulkanglTFModel
|
||||||
{
|
{
|
||||||
return glm::translate(glm::mat4(1.0f), translation) * glm::mat4(rotation) * glm::scale(glm::mat4(1.0f), scale) * matrix;
|
public:
|
||||||
}
|
// The class requires some Vulkan objects so it can create it's own resources
|
||||||
|
vks::VulkanDevice* vulkanDevice;
|
||||||
|
VkQueue copyQueue;
|
||||||
|
|
||||||
VulkanglTFModel::~VulkanglTFModel()
|
// The vertex layout for the samples' model
|
||||||
|
struct Vertex {
|
||||||
|
glm::vec3 pos;
|
||||||
|
glm::vec3 normal;
|
||||||
|
glm::vec2 uv;
|
||||||
|
glm::vec3 color;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Single vertex buffer for all primitives
|
||||||
|
struct {
|
||||||
|
VkBuffer buffer;
|
||||||
|
VkDeviceMemory memory;
|
||||||
|
} vertices;
|
||||||
|
|
||||||
|
// Single index buffer for all primitives
|
||||||
|
struct {
|
||||||
|
int count;
|
||||||
|
VkBuffer buffer;
|
||||||
|
VkDeviceMemory memory;
|
||||||
|
} indices;
|
||||||
|
|
||||||
|
// The following structures roughly represent the glTF scene structure
|
||||||
|
// To keep things simple, they only contain those properties that are required for this sample
|
||||||
|
struct Node;
|
||||||
|
|
||||||
|
// A primitive contains the data for a single draw call
|
||||||
|
struct Primitive {
|
||||||
|
uint32_t firstIndex;
|
||||||
|
uint32_t indexCount;
|
||||||
|
int32_t materialIndex;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Contains the node's (optional) geometry and can be made up of an arbitrary number of primitives
|
||||||
|
struct Mesh {
|
||||||
|
std::vector<Primitive> primitives;
|
||||||
|
};
|
||||||
|
|
||||||
|
// A node represents an object in the glTF scene graph
|
||||||
|
struct Node {
|
||||||
|
Node* parent;
|
||||||
|
std::vector<Node*> children;
|
||||||
|
Mesh mesh;
|
||||||
|
glm::mat4 matrix;
|
||||||
|
~Node() {
|
||||||
|
for (auto& child : children) {
|
||||||
|
delete child;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// A glTF material stores information in e.g. the texture that is attached to it and colors
|
||||||
|
struct Material {
|
||||||
|
glm::vec4 baseColorFactor = glm::vec4(1.0f);
|
||||||
|
uint32_t baseColorTextureIndex;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Contains the texture for a single glTF image
|
||||||
|
// Images may be reused by texture objects and are as such separated
|
||||||
|
struct Image {
|
||||||
|
vks::Texture2D texture;
|
||||||
|
// We also store (and create) a descriptor set that's used to access this texture from the fragment shader
|
||||||
|
VkDescriptorSet descriptorSet;
|
||||||
|
};
|
||||||
|
|
||||||
|
// A glTF texture stores a reference to the image and a sampler
|
||||||
|
// In this sample, we are only interested in the image
|
||||||
|
struct Texture {
|
||||||
|
int32_t imageIndex;
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
Model data
|
||||||
|
*/
|
||||||
|
std::vector<Image> images;
|
||||||
|
std::vector<Texture> textures;
|
||||||
|
std::vector<Material> materials;
|
||||||
|
std::vector<Node*> nodes;
|
||||||
|
|
||||||
|
~VulkanglTFModel()
|
||||||
{
|
{
|
||||||
for (auto node : nodes) {
|
for (auto node : nodes) {
|
||||||
delete node;
|
delete node;
|
||||||
|
@ -41,10 +132,6 @@ VulkanglTFModel::~VulkanglTFModel()
|
||||||
vkDestroySampler(vulkanDevice->logicalDevice, image.texture.sampler, nullptr);
|
vkDestroySampler(vulkanDevice->logicalDevice, image.texture.sampler, nullptr);
|
||||||
vkFreeMemory(vulkanDevice->logicalDevice, image.texture.deviceMemory, nullptr);
|
vkFreeMemory(vulkanDevice->logicalDevice, image.texture.deviceMemory, nullptr);
|
||||||
}
|
}
|
||||||
for (Skin skin : skins)
|
|
||||||
{
|
|
||||||
skin.ssbo.destroy();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -53,7 +140,7 @@ VulkanglTFModel::~VulkanglTFModel()
|
||||||
The following functions take a glTF input model loaded via tinyglTF and convert all required data into our own structure
|
The following functions take a glTF input model loaded via tinyglTF and convert all required data into our own structure
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void VulkanglTFModel::loadImages(tinygltf::Model& input)
|
void loadImages(tinygltf::Model& input)
|
||||||
{
|
{
|
||||||
// Images can be stored inside the glTF (which is the case for the sample model), so instead of directly
|
// Images can be stored inside the glTF (which is the case for the sample model), so instead of directly
|
||||||
// loading them from disk, we fetch them from the glTF loader and upload the buffers
|
// loading them from disk, we fetch them from the glTF loader and upload the buffers
|
||||||
|
@ -89,7 +176,7 @@ VulkanglTFModel::~VulkanglTFModel()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void VulkanglTFModel::loadTextures(tinygltf::Model& input)
|
void loadTextures(tinygltf::Model& input)
|
||||||
{
|
{
|
||||||
textures.resize(input.textures.size());
|
textures.resize(input.textures.size());
|
||||||
for (size_t i = 0; i < input.textures.size(); i++) {
|
for (size_t i = 0; i < input.textures.size(); i++) {
|
||||||
|
@ -97,7 +184,7 @@ VulkanglTFModel::~VulkanglTFModel()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void VulkanglTFModel::loadMaterials(tinygltf::Model& input)
|
void loadMaterials(tinygltf::Model& input)
|
||||||
{
|
{
|
||||||
materials.resize(input.materials.size());
|
materials.resize(input.materials.size());
|
||||||
for (size_t i = 0; i < input.materials.size(); i++) {
|
for (size_t i = 0; i < input.materials.size(); i++) {
|
||||||
|
@ -113,478 +200,171 @@ VulkanglTFModel::~VulkanglTFModel()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//glTF nodes loading helper function
|
|
||||||
//rewrite node loader,simplify logic
|
|
||||||
//Search node from parent to children by index
|
|
||||||
VulkanglTFModel::Node* VulkanglTFModel::findNode(Node* parent, uint32_t index)
|
|
||||||
{
|
|
||||||
Node* nodeFound = nullptr;
|
|
||||||
if (parent->index == index)
|
|
||||||
{
|
|
||||||
return parent;
|
|
||||||
}
|
|
||||||
for (auto &child : parent->children)
|
|
||||||
{
|
|
||||||
nodeFound = findNode(child, index);
|
|
||||||
if (nodeFound)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nodeFound;
|
|
||||||
} //iterate vector of nodes to check weather nodes exist or not
|
|
||||||
VulkanglTFModel::Node* VulkanglTFModel::nodeFromIndex(uint32_t index)
|
|
||||||
{
|
|
||||||
Node* nodeFound = nullptr;
|
|
||||||
for (auto& node : nodes)
|
|
||||||
{
|
|
||||||
nodeFound = findNode(node, index);
|
|
||||||
if (nodeFound)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nodeFound;
|
|
||||||
}
|
|
||||||
|
|
||||||
//animation loader
|
void loadNode(const tinygltf::Node& inputNode, const tinygltf::Model& input, VulkanglTFModel::Node* parent, std::vector<uint32_t>& indexBuffer, std::vector<VulkanglTFModel::Vertex>& vertexBuffer)
|
||||||
void VulkanglTFModel::loadAnimations(tinygltf::Model& input)
|
|
||||||
{
|
|
||||||
animations.resize(input.animations.size());
|
|
||||||
|
|
||||||
for (size_t i = 0; i < input.animations.size(); i++)
|
|
||||||
{
|
|
||||||
tinygltf::Animation glTFAnimation = input.animations[i];
|
|
||||||
animations[i].name = glTFAnimation.name;
|
|
||||||
|
|
||||||
animations[i].samplers.resize(glTFAnimation.samplers.size());
|
|
||||||
for (size_t j = 0; j < glTFAnimation.samplers.size(); j++)
|
|
||||||
{
|
|
||||||
tinygltf::AnimationSampler glTFSampler = glTFAnimation.samplers[j];
|
|
||||||
AnimationSampler& dstSampler = animations[i].samplers[j];
|
|
||||||
dstSampler.interpolation = glTFSampler.interpolation;
|
|
||||||
//sample keyframes to input
|
|
||||||
{
|
|
||||||
const tinygltf::Accessor& accessor = input.accessors[glTFSampler.input];
|
|
||||||
const tinygltf::BufferView& bufferView = input.bufferViews[accessor.bufferView];
|
|
||||||
const tinygltf::Buffer& buffer = input.buffers[bufferView.buffer];
|
|
||||||
//data pointer
|
|
||||||
const void* dataptr = &buffer.data[accessor.byteOffset + bufferView.byteOffset];
|
|
||||||
const float* buf = static_cast<const float*>(dataptr);
|
|
||||||
|
|
||||||
for (size_t index = 0; index < accessor.count; index++)
|
|
||||||
{
|
|
||||||
dstSampler.inputs.push_back(buf[index]);
|
|
||||||
}
|
|
||||||
//switch value for correct start and end time
|
|
||||||
for (auto input : animations[i].samplers[j].inputs)
|
|
||||||
{
|
|
||||||
if (input < animations[i].start)
|
|
||||||
{
|
|
||||||
animations[i].start = input;
|
|
||||||
};
|
|
||||||
if (input > animations[i].end)
|
|
||||||
{
|
|
||||||
animations[i].end = input;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//identify accessor type for keyframes to output translate/rotate/scale values
|
|
||||||
{
|
|
||||||
const tinygltf::Accessor& accessor = input.accessors[glTFSampler.input];
|
|
||||||
const tinygltf::BufferView& bufferView = input.bufferViews[accessor.bufferView];
|
|
||||||
const tinygltf::Buffer& buffer = input.buffers[bufferView.buffer];
|
|
||||||
//data pointer
|
|
||||||
const void* dataptr = &buffer.data[accessor.byteOffset + bufferView.byteOffset];
|
|
||||||
|
|
||||||
switch (accessor.type)
|
|
||||||
{
|
|
||||||
case TINYGLTF_TYPE_VEC3:
|
|
||||||
{
|
|
||||||
const glm::vec3* buf = static_cast<const glm::vec3*> (dataptr);
|
|
||||||
for (size_t index = 0; index < accessor.count; index++)
|
|
||||||
{
|
|
||||||
dstSampler.outputsVec4.push_back(glm::vec4(buf[index], 0.0f));
|
|
||||||
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case TINYGLTF_TYPE_VEC4:
|
|
||||||
{
|
|
||||||
const glm::vec4* buf = static_cast<const glm::vec4*>(dataptr);
|
|
||||||
for (size_t index = 0; index < accessor.count; index++)
|
|
||||||
{
|
|
||||||
dstSampler.outputsVec4.push_back(buf[index]);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
{
|
|
||||||
std::cout << "unknown type in accessor" << std::endl;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
animations[i].channels.resize(glTFAnimation.channels.size());
|
|
||||||
for (size_t j = 0; j < glTFAnimation.channels.size(); j++)
|
|
||||||
{
|
|
||||||
tinygltf::AnimationChannel glTFChannel = glTFAnimation.channels[j];
|
|
||||||
AnimationChannel& dstChannel = animations[i].channels[j];
|
|
||||||
dstChannel.path = glTFChannel.target_path;
|
|
||||||
dstChannel.samplerIndex = glTFChannel.sampler;
|
|
||||||
dstChannel.node = nodeFromIndex(glTFChannel.target_node);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
//node loader
|
|
||||||
void VulkanglTFModel::loadNode(const tinygltf::Node& inputNode, const tinygltf::Model& input, VulkanglTFModel::Node* parent, uint32_t nodeIndex, std::vector<uint32_t>& indexBuffer, std::vector<VulkanglTFModel::Vertex>& vertexBuffer)
|
|
||||||
{
|
{
|
||||||
VulkanglTFModel::Node* node = new VulkanglTFModel::Node{};
|
VulkanglTFModel::Node* node = new VulkanglTFModel::Node{};
|
||||||
node->parent = parent;
|
|
||||||
node->matrix = glm::mat4(1.0f);
|
node->matrix = glm::mat4(1.0f);
|
||||||
node->index = nodeIndex;
|
node->parent = parent;
|
||||||
node->skin = inputNode.skin;
|
|
||||||
|
|
||||||
//get distributions of node
|
// Get the local node matrix
|
||||||
if (inputNode.translation.size() == 3)
|
// It's either made up from translation, rotation, scale or a 4x4 matrix
|
||||||
{
|
if (inputNode.translation.size() == 3) {
|
||||||
node->translation = glm::make_vec3(inputNode.translation.data());
|
node->matrix = glm::translate(node->matrix, glm::vec3(glm::make_vec3(inputNode.translation.data())));
|
||||||
}
|
}
|
||||||
if (inputNode.scale.size() == 3)
|
if (inputNode.rotation.size() == 4) {
|
||||||
{
|
|
||||||
node->scale = glm::make_vec3(inputNode.scale.data());
|
|
||||||
}
|
|
||||||
if (inputNode.rotation.size() == 4)
|
|
||||||
{ //rotation is given by quaternion
|
|
||||||
glm::quat q = glm::make_quat(inputNode.rotation.data());
|
glm::quat q = glm::make_quat(inputNode.rotation.data());
|
||||||
node->rotation = glm::mat4(q);
|
node->matrix *= glm::mat4(q);
|
||||||
}
|
}
|
||||||
if (inputNode.matrix.size() == 16)
|
if (inputNode.scale.size() == 3) {
|
||||||
{
|
node->matrix = glm::scale(node->matrix, glm::vec3(glm::make_vec3(inputNode.scale.data())));
|
||||||
|
}
|
||||||
|
if (inputNode.matrix.size() == 16) {
|
||||||
node->matrix = glm::make_mat4x4(inputNode.matrix.data());
|
node->matrix = glm::make_mat4x4(inputNode.matrix.data());
|
||||||
}
|
};
|
||||||
|
|
||||||
//find children of nodes if exists
|
// 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++)
|
loadNode(input.nodes[inputNode.children[i]], input , node, indexBuffer, vertexBuffer);
|
||||||
{
|
|
||||||
VulkanglTFModel::loadNode(input.nodes[inputNode.children[i]], input, node, inputNode.children[i], indexBuffer, vertexBuffer);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
//load meshes in nodes if exists
|
|
||||||
if (inputNode.mesh > -1)
|
// If the node contains mesh data, we load vertices and indices from the buffers
|
||||||
{
|
// In glTF this is done via accessors and buffer views
|
||||||
|
if (inputNode.mesh > -1) {
|
||||||
const tinygltf::Mesh mesh = input.meshes[inputNode.mesh];
|
const tinygltf::Mesh mesh = input.meshes[inputNode.mesh];
|
||||||
for (size_t i = 0; i < mesh.primitives.size(); i++)
|
// Iterate through all primitives of this node's mesh
|
||||||
{
|
for (size_t i = 0; i < mesh.primitives.size(); i++) {
|
||||||
const tinygltf::Primitive& glTFPrimmitive = mesh.primitives[i];
|
const tinygltf::Primitive& glTFPrimitive = mesh.primitives[i];
|
||||||
uint32_t firstIndex = static_cast<uint32_t>(indexBuffer.size());
|
uint32_t firstIndex = static_cast<uint32_t>(indexBuffer.size());
|
||||||
uint32_t vertexStart = static_cast<uint32_t>(vertexBuffer.size());
|
uint32_t vertexStart = static_cast<uint32_t>(vertexBuffer.size());
|
||||||
uint32_t indexCount = 0;
|
uint32_t indexCount = 0;
|
||||||
const float* positionBuffer = nullptr;
|
// Vertices
|
||||||
const float* normalsBuffer = nullptr;
|
|
||||||
const float* texcoordsBuffer = nullptr;
|
|
||||||
const float* jointWeightsBuffer = nullptr;
|
|
||||||
const uint16_t * jointIndicesBuffer = nullptr;
|
|
||||||
size_t vertexCount = 0;
|
|
||||||
bool hasSkin = false;
|
|
||||||
//get buffer by index in primmitive.attributes
|
|
||||||
{
|
{
|
||||||
if (glTFPrimmitive.attributes.find("POSITION") != glTFPrimmitive.attributes.end())
|
const float* positionBuffer = nullptr;
|
||||||
{
|
const float* normalsBuffer = nullptr;
|
||||||
const tinygltf::Accessor& accessor = input.accessors[glTFPrimmitive.attributes.find("POSITION")->second];
|
const float* texCoordsBuffer = nullptr;
|
||||||
const tinygltf::BufferView view = input.bufferViews[accessor.bufferView];
|
size_t vertexCount = 0;
|
||||||
positionBuffer = reinterpret_cast<const float*> (&(input.buffers[view.buffer].data[accessor.byteOffset + view.byteOffset]));
|
|
||||||
|
// Get buffer data for vertex positions
|
||||||
|
if (glTFPrimitive.attributes.find("POSITION") != glTFPrimitive.attributes.end()) {
|
||||||
|
const tinygltf::Accessor& accessor = input.accessors[glTFPrimitive.attributes.find("POSITION")->second];
|
||||||
|
const tinygltf::BufferView& view = input.bufferViews[accessor.bufferView];
|
||||||
|
positionBuffer = reinterpret_cast<const float*>(&(input.buffers[view.buffer].data[accessor.byteOffset + view.byteOffset]));
|
||||||
vertexCount = accessor.count;
|
vertexCount = accessor.count;
|
||||||
}
|
}
|
||||||
if (glTFPrimmitive.attributes.find("NORMAL") != glTFPrimmitive.attributes.end())
|
// Get buffer data for vertex normals
|
||||||
{
|
if (glTFPrimitive.attributes.find("NORMAL") != glTFPrimitive.attributes.end()) {
|
||||||
const tinygltf::Accessor& accessor = input.accessors[glTFPrimmitive.attributes.find("NORMAL")->second];
|
const tinygltf::Accessor& accessor = input.accessors[glTFPrimitive.attributes.find("NORMAL")->second];
|
||||||
const tinygltf::BufferView &view = input.bufferViews[accessor.bufferView];
|
const tinygltf::BufferView& view = input.bufferViews[accessor.bufferView];
|
||||||
normalsBuffer = reinterpret_cast<const float*> (&(input.buffers[view.buffer].data[accessor.byteOffset + view.byteOffset]));
|
normalsBuffer = reinterpret_cast<const float*>(&(input.buffers[view.buffer].data[accessor.byteOffset + view.byteOffset]));
|
||||||
|
|
||||||
}
|
}
|
||||||
if (glTFPrimmitive.attributes.find("TEXCOORD_0") != glTFPrimmitive.attributes.end())
|
// Get buffer data for vertex texture coordinates
|
||||||
{
|
// glTF supports multiple sets, we only load the first one
|
||||||
const tinygltf::Accessor& accessor = input.accessors[glTFPrimmitive.attributes.find("TEXCOORD_0")->second];
|
if (glTFPrimitive.attributes.find("TEXCOORD_0") != glTFPrimitive.attributes.end()) {
|
||||||
const tinygltf::BufferView &view = input.bufferViews[accessor.bufferView];
|
const tinygltf::Accessor& accessor = input.accessors[glTFPrimitive.attributes.find("TEXCOORD_0")->second];
|
||||||
texcoordsBuffer = reinterpret_cast<const float*> (&(input.buffers[view.buffer].data[accessor.byteOffset + view.byteOffset]));
|
const tinygltf::BufferView& view = input.bufferViews[accessor.bufferView];
|
||||||
|
texCoordsBuffer = reinterpret_cast<const float*>(&(input.buffers[view.buffer].data[accessor.byteOffset + view.byteOffset]));
|
||||||
}
|
}
|
||||||
if (glTFPrimmitive.attributes.find("JOINTS_0") != glTFPrimmitive.attributes.end())
|
|
||||||
{
|
|
||||||
const tinygltf::Accessor& accessor = input.accessors[glTFPrimmitive.attributes.find("JOINTS_0")->second];
|
|
||||||
const tinygltf::BufferView &view = input.bufferViews[accessor.bufferView];
|
|
||||||
jointIndicesBuffer = reinterpret_cast<const uint16_t*> (&(input.buffers[view.buffer].data[accessor.byteOffset + view.byteOffset]));
|
|
||||||
|
|
||||||
}
|
// Append data to model's vertex buffer
|
||||||
if (glTFPrimmitive.attributes.find("WEIGHTS_0") != glTFPrimmitive.attributes.end())
|
for (size_t v = 0; v < vertexCount; v++) {
|
||||||
{
|
|
||||||
const tinygltf::Accessor& accessor = input.accessors[glTFPrimmitive.attributes.find("WEIGHTS_0")->second];
|
|
||||||
const tinygltf::BufferView &view = input.bufferViews[accessor.bufferView];
|
|
||||||
jointWeightsBuffer = reinterpret_cast<const float*> (&(input.buffers[view.buffer].data[accessor.byteOffset + view.byteOffset]));
|
|
||||||
|
|
||||||
}
|
|
||||||
hasSkin = (jointIndicesBuffer && jointWeightsBuffer);
|
|
||||||
|
|
||||||
for (size_t v = 0; v < vertexCount; v++)
|
|
||||||
{
|
|
||||||
Vertex vert{};
|
Vertex vert{};
|
||||||
vert.pos = glm::vec4(glm::make_vec3(&positionBuffer[v * 3]), 1.0f);
|
vert.pos = glm::vec4(glm::make_vec3(&positionBuffer[v * 3]), 1.0f);
|
||||||
vert.uv = texcoordsBuffer ? glm::make_vec2(&texcoordsBuffer[v*2]):glm::vec4(0.0f);
|
|
||||||
vert.normal = glm::normalize(glm::vec3(normalsBuffer ? glm::make_vec3(&normalsBuffer[v * 3]) : glm::vec3(0.0f)));
|
vert.normal = glm::normalize(glm::vec3(normalsBuffer ? glm::make_vec3(&normalsBuffer[v * 3]) : glm::vec3(0.0f)));
|
||||||
|
vert.uv = texCoordsBuffer ? glm::make_vec2(&texCoordsBuffer[v * 2]) : glm::vec3(0.0f);
|
||||||
vert.color = glm::vec3(1.0f);
|
vert.color = glm::vec3(1.0f);
|
||||||
vert.jointIndices = hasSkin ? glm::vec4(glm::make_vec4(&jointIndicesBuffer[v * 4])) : glm::vec4(0.0f);
|
|
||||||
vert.jointWeights = hasSkin ? glm::make_vec4(&jointWeightsBuffer[v * 4]) : glm::vec4(0.0f);
|
|
||||||
vertexBuffer.push_back(vert);
|
vertexBuffer.push_back(vert);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Indices
|
||||||
{
|
{
|
||||||
const tinygltf::Accessor& accessor = input.accessors[glTFPrimmitive.indices];
|
const tinygltf::Accessor& accessor = input.accessors[glTFPrimitive.indices];
|
||||||
const tinygltf::BufferView& bufferview = input.bufferViews[accessor.bufferView];
|
const tinygltf::BufferView& bufferView = input.bufferViews[accessor.bufferView];
|
||||||
const tinygltf::Buffer& buffer = input.buffers[bufferview.buffer];
|
const tinygltf::Buffer& buffer = input.buffers[bufferView.buffer];
|
||||||
|
|
||||||
indexCount += static_cast<uint32_t>(accessor.count);
|
indexCount += static_cast<uint32_t>(accessor.count);
|
||||||
|
|
||||||
switch (accessor.componentType)
|
// glTF supports different component types of indices
|
||||||
{
|
switch (accessor.componentType) {
|
||||||
case TINYGLTF_PARAMETER_TYPE_UNSIGNED_INT: {
|
case TINYGLTF_PARAMETER_TYPE_UNSIGNED_INT: {
|
||||||
const uint32_t* buf = reinterpret_cast<const uint32_t*>(&buffer.data[accessor.byteOffset + bufferview.byteOffset]);
|
const uint32_t* buf = reinterpret_cast<const uint32_t*>(&buffer.data[accessor.byteOffset + bufferView.byteOffset]);
|
||||||
for (size_t index = 0; index < accessor.count; index++)
|
for (size_t index = 0; index < accessor.count; index++) {
|
||||||
{
|
|
||||||
indexBuffer.push_back(buf[index] + vertexStart);
|
indexBuffer.push_back(buf[index] + vertexStart);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
case TINYGLTF_PARAMETER_TYPE_UNSIGNED_SHORT:{
|
case TINYGLTF_PARAMETER_TYPE_UNSIGNED_SHORT: {
|
||||||
const uint16_t* buf = reinterpret_cast<const uint16_t*>(&buffer.data[accessor.byteOffset + bufferview.byteOffset]);
|
const uint16_t* buf = reinterpret_cast<const uint16_t*>(&buffer.data[accessor.byteOffset + bufferView.byteOffset]);
|
||||||
for (size_t index = 0; index < accessor.count; index++)
|
for (size_t index = 0; index < accessor.count; index++) {
|
||||||
{
|
|
||||||
indexBuffer.push_back(buf[index] + vertexStart);
|
indexBuffer.push_back(buf[index] + vertexStart);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case TINYGLTF_PARAMETER_TYPE_UNSIGNED_BYTE: {
|
case TINYGLTF_PARAMETER_TYPE_UNSIGNED_BYTE: {
|
||||||
const uint8_t* buf = reinterpret_cast<const uint8_t*>(&buffer.data[accessor.byteOffset + bufferview.byteOffset]);
|
const uint8_t* buf = reinterpret_cast<const uint8_t*>(&buffer.data[accessor.byteOffset + bufferView.byteOffset]);
|
||||||
for (size_t index = 0; index < accessor.count; index++)
|
for (size_t index = 0; index < accessor.count; index++) {
|
||||||
{
|
|
||||||
indexBuffer.push_back(buf[index] + vertexStart);
|
indexBuffer.push_back(buf[index] + vertexStart);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
std::cerr << "index component type" << accessor.componentType << "not supported" << std::endl;
|
std::cerr << "Index component type " << accessor.componentType << " not supported!" << std::endl;
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Primitive primitive{};
|
Primitive primitive{};
|
||||||
primitive.firstIndex = firstIndex;
|
primitive.firstIndex = firstIndex;
|
||||||
primitive.indexCount = indexCount;
|
primitive.indexCount = indexCount;
|
||||||
primitive.materialIndex = glTFPrimmitive.material;
|
primitive.materialIndex = glTFPrimitive.material;
|
||||||
node->mesh.primitives.push_back(primitive);
|
node->mesh.primitives.push_back(primitive);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (parent)
|
|
||||||
{
|
if (parent) {
|
||||||
parent->children.push_back(node);
|
parent->children.push_back(node);
|
||||||
}
|
}
|
||||||
else
|
else {
|
||||||
{
|
|
||||||
nodes.push_back(node);
|
nodes.push_back(node);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// load skins from glTF model
|
|
||||||
void VulkanglTFModel::loadSkins(tinygltf::Model& input)
|
|
||||||
{
|
|
||||||
skins.resize(input.skins.size());
|
|
||||||
std::cout << input.skins.size() << std::endl;
|
|
||||||
|
|
||||||
for (size_t i = 0; i < input.skins.size(); i++)
|
|
||||||
{
|
|
||||||
tinygltf::Skin glTFSkin = input.skins[i];
|
|
||||||
|
|
||||||
skins[i].name = glTFSkin.name;
|
|
||||||
//follow the tree structure,find the root node of skeleton by index
|
|
||||||
skins[i].skeletonRoot = nodeFromIndex(glTFSkin.skeleton);
|
|
||||||
|
|
||||||
//join nodes
|
|
||||||
for (int jointIndex : glTFSkin.joints)
|
|
||||||
{
|
|
||||||
Node* node = nodeFromIndex(jointIndex);
|
|
||||||
if (node)
|
|
||||||
{
|
|
||||||
skins[i].joints.push_back(node);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//get the inverse bind matrices
|
|
||||||
if (glTFSkin.inverseBindMatrices > -1)
|
|
||||||
{
|
|
||||||
const tinygltf::Accessor& accessor = input.accessors[glTFSkin.inverseBindMatrices];
|
|
||||||
const tinygltf::BufferView& bufferview = input.bufferViews[accessor.bufferView];
|
|
||||||
const tinygltf::Buffer& buffer = input.buffers[bufferview.buffer];
|
|
||||||
skins[i].inverseBindMatrices.resize(accessor.count);
|
|
||||||
memcpy(skins[i].inverseBindMatrices.data(), &buffer.data[accessor.byteOffset + bufferview.byteOffset], accessor.count * sizeof(glm::mat4));
|
|
||||||
|
|
||||||
//create a host visible shader buffer to store inverse bind matrices for this skin
|
|
||||||
VK_CHECK_RESULT(vulkanDevice->createBuffer(VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
|
||||||
&skins[i].ssbo,
|
|
||||||
sizeof(glm::mat4) * skins[i].inverseBindMatrices.size(),
|
|
||||||
skins[i].inverseBindMatrices.data()));
|
|
||||||
VK_CHECK_RESULT(skins[i].ssbo.map());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
/*
|
|
||||||
vertex skinning functions
|
|
||||||
*/
|
|
||||||
glm::mat4 VulkanglTFModel::getNodeMatrix(VulkanglTFModel::Node* node)
|
|
||||||
{
|
|
||||||
glm::mat4 nodeMatrix = node->getLocalMatrix();
|
|
||||||
VulkanglTFModel::Node* currentParent = node->parent;
|
|
||||||
while (currentParent)
|
|
||||||
{
|
|
||||||
nodeMatrix = currentParent->getLocalMatrix() * nodeMatrix;
|
|
||||||
currentParent = currentParent->parent;
|
|
||||||
}
|
|
||||||
return nodeMatrix;
|
|
||||||
}
|
|
||||||
|
|
||||||
void VulkanglTFModel::updateJoints(VulkanglTFModel::Node* node)
|
|
||||||
{
|
|
||||||
if (node->skin > -1)
|
|
||||||
{
|
|
||||||
glm::mat4 inversTransform = glm::inverse(getNodeMatrix(node));
|
|
||||||
Skin skin = skins[node->skin];
|
|
||||||
size_t numJoints = (uint32_t)skin.joints.size();
|
|
||||||
std::vector<glm::mat4> jointMatrices(numJoints);
|
|
||||||
for (size_t i = 0; i < numJoints; i++)
|
|
||||||
{
|
|
||||||
jointMatrices[i] = getNodeMatrix(skin.joints[i]) * skin.inverseBindMatrices[i];
|
|
||||||
jointMatrices[i] = inversTransform * jointMatrices[i];
|
|
||||||
}
|
|
||||||
skin.ssbo.copyTo(jointMatrices.data(), jointMatrices.size() * sizeof(glm::mat4));
|
|
||||||
}
|
|
||||||
for (auto& child : node->children)
|
|
||||||
{
|
|
||||||
updateJoints(child);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void VulkanglTFModel::updateAnimation(float deltaTime)
|
|
||||||
{
|
|
||||||
if (activeAnimation > static_cast<uint32_t>(animations.size())-1)
|
|
||||||
{
|
|
||||||
std::cout << "no animation with index" << activeAnimation << std::endl;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
Animation& animation = animations[activeAnimation];
|
|
||||||
animation.currentTime += deltaTime;
|
|
||||||
if (animation.currentTime > animation.end)
|
|
||||||
{
|
|
||||||
animation.currentTime -= animation.end;
|
|
||||||
}
|
|
||||||
for (auto& channel : animation.channels)
|
|
||||||
{
|
|
||||||
|
|
||||||
AnimationSampler& sampler = animation.samplers[channel.samplerIndex];
|
|
||||||
for (size_t i = 0; i < sampler.inputs.size() - 1; i++)
|
|
||||||
{
|
|
||||||
if (sampler.interpolation != "LINEAR")
|
|
||||||
{
|
|
||||||
std::cout << "sample only supports linear interpolaton" << std::endl;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if ((animation.currentTime >= sampler.inputs[i]) && (animation.currentTime <= sampler.inputs[i + 1]))
|
|
||||||
{
|
|
||||||
float a = (animation.currentTime - sampler.inputs[i]) / (sampler.inputs[i + 1] - sampler.inputs[i]);
|
|
||||||
if (channel.path == "translation")
|
|
||||||
{
|
|
||||||
channel.node->translation = glm::mix(sampler.outputsVec4[i], sampler.outputsVec4[i + 1], a);
|
|
||||||
}
|
|
||||||
if (channel.path == "rotation")
|
|
||||||
{
|
|
||||||
//quaternion
|
|
||||||
glm::quat q1;
|
|
||||||
q1.x = sampler.outputsVec4[i].x;
|
|
||||||
q1.y = sampler.outputsVec4[i].y;
|
|
||||||
q1.z = sampler.outputsVec4[i].z;
|
|
||||||
q1.w = sampler.outputsVec4[i].w;
|
|
||||||
|
|
||||||
glm::quat q2;
|
|
||||||
q2.x = sampler.outputsVec4[i].x;
|
|
||||||
q2.y = sampler.outputsVec4[i].y;
|
|
||||||
q2.z = sampler.outputsVec4[i].z;
|
|
||||||
q2.w = sampler.outputsVec4[i].w;
|
|
||||||
|
|
||||||
channel.node->rotation = glm::normalize(glm::slerp(q1, q2, a));
|
|
||||||
|
|
||||||
}
|
|
||||||
if (channel.path == "scale")
|
|
||||||
{
|
|
||||||
channel.node->scale = glm::mix(sampler.outputsVec4[i], sampler.outputsVec4[i + 1], a);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (auto& node : nodes)
|
|
||||||
{
|
|
||||||
updateJoints(node);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
/*
|
/*
|
||||||
glTF rendering functions
|
glTF rendering functions
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Draw a single node including child nodes (if present)
|
// Draw a single node including child nodes (if present)
|
||||||
void VulkanglTFModel::drawNode(VkCommandBuffer commandBuffer, VkPipelineLayout pipelineLayout, VulkanglTFModel::Node node)
|
void drawNode(VkCommandBuffer commandBuffer, VkPipelineLayout pipelineLayout, VulkanglTFModel::Node* node)
|
||||||
{
|
{
|
||||||
if (node.mesh.primitives.size() > 0) {
|
if (node->mesh.primitives.size() > 0) {
|
||||||
// Pass the node's matrix via push constants
|
// Pass the node's matrix via push constants
|
||||||
// Traverse the node hierarchy to the top-most parent to get the final matrix of the current node
|
// Traverse the node hierarchy to the top-most parent to get the final matrix of the current node
|
||||||
glm::mat4 nodeMatrix = node.matrix;
|
glm::mat4 nodeMatrix = node->matrix;
|
||||||
VulkanglTFModel::Node* currentParent = node.parent;
|
VulkanglTFModel::Node* currentParent = node->parent;
|
||||||
while (currentParent) {
|
while (currentParent) {
|
||||||
nodeMatrix = currentParent->matrix * nodeMatrix;
|
nodeMatrix = currentParent->matrix * nodeMatrix;
|
||||||
currentParent = currentParent->parent;
|
currentParent = currentParent->parent;
|
||||||
}
|
}
|
||||||
// Pass the final matrix to the vertex shader using push constants
|
// Pass the final matrix to the vertex shader using push constants
|
||||||
vkCmdPushConstants(commandBuffer, pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(glm::mat4), &nodeMatrix);
|
vkCmdPushConstants(commandBuffer, pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(glm::mat4), &nodeMatrix);
|
||||||
|
for (VulkanglTFModel::Primitive& primitive : node->mesh.primitives) {
|
||||||
//vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 1, 1, &skins[node.skin].descriptorSet, 0, nullptr);
|
if (primitive.indexCount > 0) {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
for (VulkanglTFModel::Primitive& primitive : node.mesh.primitives) {
|
|
||||||
if (primitive.indexCount > 0)
|
|
||||||
{
|
|
||||||
// Get the texture index for this primitive
|
// Get the texture index for this primitive
|
||||||
VulkanglTFModel::Texture texture = textures[materials[primitive.materialIndex].baseColorTextureIndex];
|
VulkanglTFModel::Texture texture = textures[materials[primitive.materialIndex].baseColorTextureIndex];
|
||||||
// Bind the descriptor for the current primitive's texture
|
// Bind the descriptor for the current primitive's texture
|
||||||
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 2, 1, &images[texture.imageIndex].descriptorSet, 0, nullptr);
|
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 1, 1, &images[texture.imageIndex].descriptorSet, 0, nullptr);
|
||||||
vkCmdDrawIndexed(commandBuffer, primitive.indexCount, 1, primitive.firstIndex, 0, 0);
|
vkCmdDrawIndexed(commandBuffer, primitive.indexCount, 1, primitive.firstIndex, 0, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (auto& child : node.children) {
|
for (auto& child : node->children) {
|
||||||
drawNode(commandBuffer, pipelineLayout, *child);
|
drawNode(commandBuffer, pipelineLayout, child);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw the glTF scene starting at the top-level-nodes
|
// Draw the glTF scene starting at the top-level-nodes
|
||||||
void VulkanglTFModel::draw(VkCommandBuffer commandBuffer, VkPipelineLayout pipelineLayout)
|
void draw(VkCommandBuffer commandBuffer, VkPipelineLayout pipelineLayout)
|
||||||
{
|
{
|
||||||
// All vertices and indices are stored in single buffers, so we only need to bind once
|
// All vertices and indices are stored in single buffers, so we only need to bind once
|
||||||
VkDeviceSize offsets[1] = { 0 };
|
VkDeviceSize offsets[1] = { 0 };
|
||||||
|
@ -592,15 +372,43 @@ VulkanglTFModel::~VulkanglTFModel()
|
||||||
vkCmdBindIndexBuffer(commandBuffer, indices.buffer, 0, VK_INDEX_TYPE_UINT32);
|
vkCmdBindIndexBuffer(commandBuffer, indices.buffer, 0, VK_INDEX_TYPE_UINT32);
|
||||||
// Render all nodes at top-level
|
// Render all nodes at top-level
|
||||||
for (auto& node : nodes) {
|
for (auto& node : nodes) {
|
||||||
drawNode(commandBuffer, pipelineLayout, *node);
|
drawNode(commandBuffer, pipelineLayout, node);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
class VulkanExample : public VulkanExampleBase
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
bool wireframe = false;
|
||||||
|
|
||||||
|
VulkanglTFModel glTFModel;
|
||||||
|
|
||||||
VulkanExample::VulkanExample():
|
struct ShaderData {
|
||||||
VulkanExampleBase(ENABLE_VALIDATION)
|
vks::Buffer buffer;
|
||||||
|
struct Values {
|
||||||
|
glm::mat4 projection;
|
||||||
|
glm::mat4 model;
|
||||||
|
glm::vec4 lightPos = glm::vec4(5.0f, 5.0f, -5.0f, 1.0f);
|
||||||
|
glm::vec4 viewPos;
|
||||||
|
} values;
|
||||||
|
} shaderData;
|
||||||
|
|
||||||
|
struct Pipelines {
|
||||||
|
VkPipeline solid;
|
||||||
|
VkPipeline wireframe = VK_NULL_HANDLE;
|
||||||
|
} pipelines;
|
||||||
|
|
||||||
|
VkPipelineLayout pipelineLayout;
|
||||||
|
VkDescriptorSet descriptorSet;
|
||||||
|
|
||||||
|
struct DescriptorSetLayouts {
|
||||||
|
VkDescriptorSetLayout matrices;
|
||||||
|
VkDescriptorSetLayout textures;
|
||||||
|
} descriptorSetLayouts;
|
||||||
|
|
||||||
|
VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION)
|
||||||
{
|
{
|
||||||
title = "homework1";
|
title = "homework1";
|
||||||
camera.type = Camera::CameraType::lookat;
|
camera.type = Camera::CameraType::lookat;
|
||||||
|
@ -610,7 +418,7 @@ VulkanExample::VulkanExample():
|
||||||
camera.setPerspective(60.0f, (float)width / (float)height, 0.1f, 256.0f);
|
camera.setPerspective(60.0f, (float)width / (float)height, 0.1f, 256.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
VulkanExample::~VulkanExample()
|
~VulkanExample()
|
||||||
{
|
{
|
||||||
// Clean up used Vulkan resources
|
// Clean up used Vulkan resources
|
||||||
// Note : Inherited destructor cleans up resources stored in base class
|
// Note : Inherited destructor cleans up resources stored in base class
|
||||||
|
@ -622,26 +430,24 @@ VulkanExample::~VulkanExample()
|
||||||
vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
|
vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
|
||||||
vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.matrices, nullptr);
|
vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.matrices, nullptr);
|
||||||
vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.textures, nullptr);
|
vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.textures, nullptr);
|
||||||
vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.jointMatrices, nullptr);
|
|
||||||
|
|
||||||
|
|
||||||
shaderData.buffer.destroy();
|
shaderData.buffer.destroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
void VulkanExample::getEnabledFeatures()
|
virtual void getEnabledFeatures()
|
||||||
{
|
{
|
||||||
// Fill mode non solid is required for wireframe display
|
// Fill mode non solid is required for wireframe display
|
||||||
if (deviceFeatures.fillModeNonSolid) {
|
if (deviceFeatures.fillModeNonSolid) {
|
||||||
enabledFeatures.fillModeNonSolid = VK_TRUE;
|
enabledFeatures.fillModeNonSolid = VK_TRUE;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
void VulkanExample::buildCommandBuffers()
|
void buildCommandBuffers()
|
||||||
{
|
{
|
||||||
VkCommandBufferBeginInfo cmdBufInfo = vks::initializers::commandBufferBeginInfo();
|
VkCommandBufferBeginInfo cmdBufInfo = vks::initializers::commandBufferBeginInfo();
|
||||||
|
|
||||||
VkClearValue clearValues[2];
|
VkClearValue clearValues[2];
|
||||||
|
clearValues[0].color = defaultClearColor;
|
||||||
clearValues[0].color = { { 0.25f, 0.25f, 0.25f, 1.0f } };;
|
clearValues[0].color = { { 0.25f, 0.25f, 0.25f, 1.0f } };;
|
||||||
clearValues[1].depthStencil = { 1.0f, 0 };
|
clearValues[1].depthStencil = { 1.0f, 0 };
|
||||||
|
|
||||||
|
@ -674,7 +480,7 @@ void VulkanExample::getEnabledFeatures()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void VulkanExample::loadglTFFile(std::string filename)
|
void loadglTFFile(std::string filename)
|
||||||
{
|
{
|
||||||
tinygltf::Model glTFInput;
|
tinygltf::Model glTFInput;
|
||||||
tinygltf::TinyGLTF gltfContext;
|
tinygltf::TinyGLTF gltfContext;
|
||||||
|
@ -696,23 +502,14 @@ void VulkanExample::getEnabledFeatures()
|
||||||
std::vector<uint32_t> indexBuffer;
|
std::vector<uint32_t> indexBuffer;
|
||||||
std::vector<VulkanglTFModel::Vertex> vertexBuffer;
|
std::vector<VulkanglTFModel::Vertex> vertexBuffer;
|
||||||
|
|
||||||
if (fileLoaded)
|
if (fileLoaded) {
|
||||||
{
|
|
||||||
glTFModel.loadImages(glTFInput);
|
glTFModel.loadImages(glTFInput);
|
||||||
glTFModel.loadMaterials(glTFInput);
|
glTFModel.loadMaterials(glTFInput);
|
||||||
glTFModel.loadTextures(glTFInput);
|
glTFModel.loadTextures(glTFInput);
|
||||||
glTFModel.loadSkins(glTFInput);
|
|
||||||
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]];
|
||||||
glTFModel.loadNode(node, glTFInput, nullptr,scene.nodes[i], indexBuffer, vertexBuffer);
|
glTFModel.loadNode(node, glTFInput, nullptr, indexBuffer, vertexBuffer);
|
||||||
}
|
|
||||||
|
|
||||||
glTFModel.loadAnimations(glTFInput);
|
|
||||||
// update joint in nodes
|
|
||||||
for (auto node : glTFModel.nodes)
|
|
||||||
{
|
|
||||||
glTFModel.updateJoints(node);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -777,7 +574,6 @@ void VulkanExample::getEnabledFeatures()
|
||||||
©Region);
|
©Region);
|
||||||
|
|
||||||
copyRegion.size = indexBufferSize;
|
copyRegion.size = indexBufferSize;
|
||||||
|
|
||||||
vkCmdCopyBuffer(
|
vkCmdCopyBuffer(
|
||||||
copyCmd,
|
copyCmd,
|
||||||
indexStaging.buffer,
|
indexStaging.buffer,
|
||||||
|
@ -794,12 +590,12 @@ void VulkanExample::getEnabledFeatures()
|
||||||
vkFreeMemory(device, indexStaging.memory, nullptr);
|
vkFreeMemory(device, indexStaging.memory, nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void VulkanExample::loadAssets()
|
void loadAssets()
|
||||||
{
|
{
|
||||||
loadglTFFile(getAssetPath() + "models/CesiumMan/glTF/CesiumMan.gltf");
|
loadglTFFile(getAssetPath() + "buster_drone/busterDrone.gltf");
|
||||||
}
|
}
|
||||||
|
|
||||||
void VulkanExample::setupDescriptors()
|
void setupDescriptors()
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
This sample uses separate descriptor sets (and layouts) for the matrices and materials (textures)
|
This sample uses separate descriptor sets (and layouts) for the matrices and materials (textures)
|
||||||
|
@ -809,43 +605,24 @@ void VulkanExample::getEnabledFeatures()
|
||||||
vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1),
|
vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1),
|
||||||
// One combined image sampler per model image/texture
|
// One combined image sampler per model image/texture
|
||||||
vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, static_cast<uint32_t>(glTFModel.images.size())),
|
vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, static_cast<uint32_t>(glTFModel.images.size())),
|
||||||
//initialize descriptor pool size for skin
|
|
||||||
vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,static_cast<uint32_t>(glTFModel.skins.size())),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// One set for matrices and one per model image/texture
|
// One set for matrices and one per model image/texture
|
||||||
const uint32_t maxSetCount = static_cast<uint32_t>(glTFModel.images.size()) + static_cast<uint32_t>(glTFModel.skins.size())+1;
|
const uint32_t maxSetCount = static_cast<uint32_t>(glTFModel.images.size()) + 1;
|
||||||
VkDescriptorPoolCreateInfo descriptorPoolInfo = vks::initializers::descriptorPoolCreateInfo(poolSizes, maxSetCount);
|
VkDescriptorPoolCreateInfo descriptorPoolInfo = vks::initializers::descriptorPoolCreateInfo(poolSizes, maxSetCount);
|
||||||
VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool));
|
VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool));
|
||||||
|
|
||||||
// Descriptor set layouts
|
|
||||||
VkDescriptorSetLayoutBinding setLayoutBinding{};
|
|
||||||
VkDescriptorSetLayoutCreateInfo descriptorSetLayoutCI = vks::initializers::descriptorSetLayoutCreateInfo(&setLayoutBinding, 1);
|
|
||||||
|
|
||||||
// Descriptor set layout for passing matrices
|
// Descriptor set layout for passing matrices
|
||||||
setLayoutBinding = vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT, 0);
|
VkDescriptorSetLayoutBinding setLayoutBinding = vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT, 0);
|
||||||
|
VkDescriptorSetLayoutCreateInfo descriptorSetLayoutCI = vks::initializers::descriptorSetLayoutCreateInfo(&setLayoutBinding, 1);
|
||||||
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorSetLayoutCI, nullptr, &descriptorSetLayouts.matrices));
|
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorSetLayoutCI, nullptr, &descriptorSetLayouts.matrices));
|
||||||
|
|
||||||
// Descriptor set layout for passing material textures
|
// Descriptor set layout for passing material textures
|
||||||
setLayoutBinding = vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT, 0);
|
setLayoutBinding = vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT, 0);
|
||||||
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorSetLayoutCI, nullptr, &descriptorSetLayouts.textures));
|
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorSetLayoutCI, nullptr, &descriptorSetLayouts.textures));
|
||||||
|
// Pipeline layout using both descriptor sets (set 0 = matrices, set 1 = material)
|
||||||
//Descriptor set layout for passing skin joint matrices
|
std::array<VkDescriptorSetLayout, 2> setLayouts = { descriptorSetLayouts.matrices, descriptorSetLayouts.textures };
|
||||||
setLayoutBinding = vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, VK_SHADER_STAGE_VERTEX_BIT, 0);
|
|
||||||
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorSetLayoutCI, nullptr, &descriptorSetLayouts.jointMatrices));
|
|
||||||
|
|
||||||
// Pipeline layout using three descriptor sets (set 0 = matrices, set 1 = joint matrices, set 3 = material)
|
|
||||||
std::array<VkDescriptorSetLayout, 3> setLayouts =
|
|
||||||
{
|
|
||||||
descriptorSetLayouts.matrices,
|
|
||||||
descriptorSetLayouts.jointMatrices,
|
|
||||||
descriptorSetLayouts.textures
|
|
||||||
};
|
|
||||||
VkPipelineLayoutCreateInfo pipelineLayoutCI= vks::initializers::pipelineLayoutCreateInfo(setLayouts.data(), static_cast<uint32_t>(setLayouts.size()));
|
VkPipelineLayoutCreateInfo pipelineLayoutCI= vks::initializers::pipelineLayoutCreateInfo(setLayouts.data(), static_cast<uint32_t>(setLayouts.size()));
|
||||||
|
|
||||||
// We will use push constants to push the local matrices of a primitive to the vertex shader
|
// We will use push constants to push the local matrices of a primitive to the vertex shader
|
||||||
VkPushConstantRange pushConstantRange = vks::initializers::pushConstantRange(VK_SHADER_STAGE_VERTEX_BIT, sizeof(glm::mat4), 0);
|
VkPushConstantRange pushConstantRange = vks::initializers::pushConstantRange(VK_SHADER_STAGE_VERTEX_BIT, sizeof(glm::mat4), 0);
|
||||||
|
|
||||||
// Push constant ranges are part of the pipeline layout
|
// Push constant ranges are part of the pipeline layout
|
||||||
pipelineLayoutCI.pushConstantRangeCount = 1;
|
pipelineLayoutCI.pushConstantRangeCount = 1;
|
||||||
pipelineLayoutCI.pPushConstantRanges = &pushConstantRange;
|
pipelineLayoutCI.pPushConstantRanges = &pushConstantRange;
|
||||||
|
@ -856,16 +633,6 @@ void VulkanExample::getEnabledFeatures()
|
||||||
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSet));
|
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSet));
|
||||||
VkWriteDescriptorSet writeDescriptorSet = vks::initializers::writeDescriptorSet(descriptorSet, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &shaderData.buffer.descriptor);
|
VkWriteDescriptorSet writeDescriptorSet = vks::initializers::writeDescriptorSet(descriptorSet, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &shaderData.buffer.descriptor);
|
||||||
vkUpdateDescriptorSets(device, 1, &writeDescriptorSet, 0, nullptr);
|
vkUpdateDescriptorSets(device, 1, &writeDescriptorSet, 0, nullptr);
|
||||||
|
|
||||||
//Descriptor sets for skin joint matrices
|
|
||||||
for (auto& skin : glTFModel.skins)
|
|
||||||
{
|
|
||||||
const VkDescriptorSetAllocateInfo allocInfo = vks::initializers::descriptorSetAllocateInfo(descriptorPool, &descriptorSetLayouts.jointMatrices, 1);
|
|
||||||
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &skin.descriptorSet));
|
|
||||||
VkWriteDescriptorSet writeDescriptorSet = vks::initializers::writeDescriptorSet(skin.descriptorSet, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 0, &skin.ssbo.descriptor);
|
|
||||||
vkUpdateDescriptorSets(device, 1, &writeDescriptorSet, 0, nullptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Descriptor sets for materials
|
// Descriptor sets for materials
|
||||||
for (auto& image : glTFModel.images) {
|
for (auto& image : glTFModel.images) {
|
||||||
const VkDescriptorSetAllocateInfo allocInfo = vks::initializers::descriptorSetAllocateInfo(descriptorPool, &descriptorSetLayouts.textures, 1);
|
const VkDescriptorSetAllocateInfo allocInfo = vks::initializers::descriptorSetAllocateInfo(descriptorPool, &descriptorSetLayouts.textures, 1);
|
||||||
|
@ -873,11 +640,9 @@ void VulkanExample::getEnabledFeatures()
|
||||||
VkWriteDescriptorSet writeDescriptorSet = vks::initializers::writeDescriptorSet(image.descriptorSet, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 0, &image.texture.descriptor);
|
VkWriteDescriptorSet writeDescriptorSet = vks::initializers::writeDescriptorSet(image.descriptorSet, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 0, &image.texture.descriptor);
|
||||||
vkUpdateDescriptorSets(device, 1, &writeDescriptorSet, 0, nullptr);
|
vkUpdateDescriptorSets(device, 1, &writeDescriptorSet, 0, nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void VulkanExample::preparePipelines()
|
void preparePipelines()
|
||||||
{
|
{
|
||||||
VkPipelineInputAssemblyStateCreateInfo inputAssemblyStateCI = vks::initializers::pipelineInputAssemblyStateCreateInfo(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, 0, VK_FALSE);
|
VkPipelineInputAssemblyStateCreateInfo inputAssemblyStateCI = vks::initializers::pipelineInputAssemblyStateCreateInfo(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, 0, VK_FALSE);
|
||||||
VkPipelineRasterizationStateCreateInfo rasterizationStateCI = vks::initializers::pipelineRasterizationStateCreateInfo(VK_POLYGON_MODE_FILL, VK_CULL_MODE_BACK_BIT, VK_FRONT_FACE_COUNTER_CLOCKWISE, 0);
|
VkPipelineRasterizationStateCreateInfo rasterizationStateCI = vks::initializers::pipelineRasterizationStateCreateInfo(VK_POLYGON_MODE_FILL, VK_CULL_MODE_BACK_BIT, VK_FRONT_FACE_COUNTER_CLOCKWISE, 0);
|
||||||
|
@ -897,9 +662,6 @@ void VulkanExample::getEnabledFeatures()
|
||||||
vks::initializers::vertexInputAttributeDescription(0, 1, VK_FORMAT_R32G32B32_SFLOAT, offsetof(VulkanglTFModel::Vertex, normal)),// Location 1: Normal
|
vks::initializers::vertexInputAttributeDescription(0, 1, VK_FORMAT_R32G32B32_SFLOAT, offsetof(VulkanglTFModel::Vertex, normal)),// Location 1: Normal
|
||||||
vks::initializers::vertexInputAttributeDescription(0, 2, VK_FORMAT_R32G32B32_SFLOAT, offsetof(VulkanglTFModel::Vertex, uv)), // Location 2: Texture coordinates
|
vks::initializers::vertexInputAttributeDescription(0, 2, VK_FORMAT_R32G32B32_SFLOAT, offsetof(VulkanglTFModel::Vertex, uv)), // Location 2: Texture coordinates
|
||||||
vks::initializers::vertexInputAttributeDescription(0, 3, VK_FORMAT_R32G32B32_SFLOAT, offsetof(VulkanglTFModel::Vertex, color)), // Location 3: Color
|
vks::initializers::vertexInputAttributeDescription(0, 3, VK_FORMAT_R32G32B32_SFLOAT, offsetof(VulkanglTFModel::Vertex, color)), // Location 3: Color
|
||||||
vks::initializers::vertexInputAttributeDescription(0, 4, VK_FORMAT_R32G32B32_SFLOAT, offsetof(VulkanglTFModel::Vertex,jointIndices)), // Location 4 : jointIndices
|
|
||||||
vks::initializers::vertexInputAttributeDescription(0, 5, VK_FORMAT_R32G32B32_SFLOAT, offsetof(VulkanglTFModel::Vertex,jointWeights)), //Location 5 : jointWeights
|
|
||||||
|
|
||||||
};
|
};
|
||||||
VkPipelineVertexInputStateCreateInfo vertexInputStateCI = vks::initializers::pipelineVertexInputStateCreateInfo();
|
VkPipelineVertexInputStateCreateInfo vertexInputStateCI = vks::initializers::pipelineVertexInputStateCreateInfo();
|
||||||
vertexInputStateCI.vertexBindingDescriptionCount = static_cast<uint32_t>(vertexInputBindings.size());
|
vertexInputStateCI.vertexBindingDescriptionCount = static_cast<uint32_t>(vertexInputBindings.size());
|
||||||
|
@ -936,7 +698,7 @@ void VulkanExample::getEnabledFeatures()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepare and initialize uniform buffer containing shader uniforms
|
// Prepare and initialize uniform buffer containing shader uniforms
|
||||||
void VulkanExample::prepareUniformBuffers()
|
void prepareUniformBuffers()
|
||||||
{
|
{
|
||||||
// Vertex shader uniform buffer block
|
// Vertex shader uniform buffer block
|
||||||
VK_CHECK_RESULT(vulkanDevice->createBuffer(
|
VK_CHECK_RESULT(vulkanDevice->createBuffer(
|
||||||
|
@ -951,15 +713,15 @@ void VulkanExample::getEnabledFeatures()
|
||||||
updateUniformBuffers();
|
updateUniformBuffers();
|
||||||
}
|
}
|
||||||
|
|
||||||
void VulkanExample::updateUniformBuffers()
|
void updateUniformBuffers()
|
||||||
{
|
{
|
||||||
shaderData.values.projection = camera.matrices.perspective;
|
shaderData.values.projection = camera.matrices.perspective;
|
||||||
shaderData.values.model = camera.matrices.view;
|
shaderData.values.model = camera.matrices.view;
|
||||||
|
shaderData.values.viewPos = camera.viewPos;
|
||||||
memcpy(shaderData.buffer.mapped, &shaderData.values, sizeof(shaderData.values));
|
memcpy(shaderData.buffer.mapped, &shaderData.values, sizeof(shaderData.values));
|
||||||
}
|
}
|
||||||
|
|
||||||
void VulkanExample::prepare()
|
void prepare()
|
||||||
{
|
{
|
||||||
VulkanExampleBase::prepare();
|
VulkanExampleBase::prepare();
|
||||||
loadAssets();
|
loadAssets();
|
||||||
|
@ -970,24 +732,20 @@ void VulkanExample::getEnabledFeatures()
|
||||||
prepared = true;
|
prepared = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void VulkanExample::render()
|
virtual void render()
|
||||||
{
|
{
|
||||||
renderFrame();
|
renderFrame();
|
||||||
if (camera.updated) {
|
if (camera.updated) {
|
||||||
updateUniformBuffers();
|
updateUniformBuffers();
|
||||||
}
|
}
|
||||||
if (!paused)
|
|
||||||
{
|
|
||||||
glTFModel.updateAnimation(frameTimer);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void VulkanExample::viewChanged()
|
virtual void viewChanged()
|
||||||
{
|
{
|
||||||
updateUniformBuffers();
|
updateUniformBuffers();
|
||||||
}
|
}
|
||||||
|
|
||||||
void VulkanExample::OnUpdateUIOverlay(vks::UIOverlay *overlay)
|
virtual void OnUpdateUIOverlay(vks::UIOverlay *overlay)
|
||||||
{
|
{
|
||||||
if (overlay->header("Settings")) {
|
if (overlay->header("Settings")) {
|
||||||
if (overlay->checkBox("Wireframe", &wireframe)) {
|
if (overlay->checkBox("Wireframe", &wireframe)) {
|
||||||
|
@ -995,6 +753,6 @@ void VulkanExample::getEnabledFeatures()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
VULKAN_EXAMPLE_MAIN()
|
VULKAN_EXAMPLE_MAIN()
|
||||||
|
|
|
@ -1,226 +0,0 @@
|
||||||
|
|
||||||
#include <assert.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#define GLM_FORCE_RADIANS
|
|
||||||
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
|
|
||||||
#include <glm/glm.hpp>
|
|
||||||
#include <glm/gtc/matrix_transform.hpp>
|
|
||||||
#include <glm/gtc/type_ptr.hpp>
|
|
||||||
|
|
||||||
#define TINYGLTF_IMPLEMENTATION
|
|
||||||
#define STB_IMAGE_IMPLEMENTATION
|
|
||||||
#define TINYGLTF_NO_STB_IMAGE_WRITE
|
|
||||||
|
|
||||||
#ifdef VK_USE_PLATFORM_ANDROID_KHR
|
|
||||||
#define TINYGLTF_ANDROID_LOAD_FROM_ASSETS
|
|
||||||
#endif
|
|
||||||
#include "tiny_gltf.h"
|
|
||||||
|
|
||||||
#include "vulkanexamplebase.h"
|
|
||||||
|
|
||||||
#define ENABLE_VALIDATION false
|
|
||||||
|
|
||||||
|
|
||||||
// Contains everything required to render a glTF model in Vulkan
|
|
||||||
// This class is heavily simplified (compared to glTF's feature set) but retains the basic glTF structure
|
|
||||||
class VulkanglTFModel
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
// The class requires some Vulkan objects so it can create it's own resources
|
|
||||||
vks::VulkanDevice* vulkanDevice;
|
|
||||||
VkQueue copyQueue;
|
|
||||||
|
|
||||||
// The vertex layout for the samples' model
|
|
||||||
struct Vertex {
|
|
||||||
glm::vec3 pos;
|
|
||||||
glm::vec3 normal;
|
|
||||||
glm::vec2 uv;
|
|
||||||
glm::vec3 color;
|
|
||||||
glm::vec3 jointIndices;
|
|
||||||
glm::vec3 jointWeights;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Single vertex buffer for all primitives
|
|
||||||
struct Vertices {
|
|
||||||
VkBuffer buffer;
|
|
||||||
VkDeviceMemory memory;
|
|
||||||
} vertices;
|
|
||||||
|
|
||||||
// Single index buffer for all primitives
|
|
||||||
struct Indices {
|
|
||||||
int count;
|
|
||||||
VkBuffer buffer;
|
|
||||||
VkDeviceMemory memory;
|
|
||||||
} indices;
|
|
||||||
|
|
||||||
// The following structures roughly represent the glTF scene structure
|
|
||||||
// To keep things simple, they only contain those properties that are required for this sample
|
|
||||||
struct Node;
|
|
||||||
|
|
||||||
// A primitive contains the data for a single draw call
|
|
||||||
struct Primitive {
|
|
||||||
uint32_t firstIndex;
|
|
||||||
uint32_t indexCount;
|
|
||||||
int32_t materialIndex;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Contains the node's (optional) geometry and can be made up of an arbitrary number of primitives
|
|
||||||
struct Mesh {
|
|
||||||
std::vector<Primitive> primitives;
|
|
||||||
};
|
|
||||||
|
|
||||||
// A node represents an object in the glTF scene graph
|
|
||||||
struct Node {
|
|
||||||
Node* parent;
|
|
||||||
uint32_t index;
|
|
||||||
std::vector<Node*> children;
|
|
||||||
Mesh mesh;
|
|
||||||
glm::vec3 translation{};
|
|
||||||
glm::vec3 scale{ 1.0f };
|
|
||||||
glm::quat rotation{};
|
|
||||||
int32_t skin = -1;
|
|
||||||
glm::mat4 getLocalMatrix();
|
|
||||||
glm::mat4 matrix;
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
// A glTF material stores information in e.g. the texture that is attached to it and colors
|
|
||||||
struct Material {
|
|
||||||
glm::vec4 baseColorFactor = glm::vec4(1.0f);
|
|
||||||
uint32_t baseColorTextureIndex;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Contains the texture for a single glTF image
|
|
||||||
// Images may be reused by texture objects and are as such separated
|
|
||||||
struct Image {
|
|
||||||
vks::Texture2D texture;
|
|
||||||
// We also store (and create) a descriptor set that's used to access this texture from the fragment shader
|
|
||||||
VkDescriptorSet descriptorSet;
|
|
||||||
};
|
|
||||||
|
|
||||||
// A glTF texture stores a reference to the image and a sampler
|
|
||||||
// In this sample, we are only interested in the image
|
|
||||||
struct Texture {
|
|
||||||
int32_t imageIndex;
|
|
||||||
};
|
|
||||||
|
|
||||||
// structure of skin
|
|
||||||
struct Skin {
|
|
||||||
std::string name;
|
|
||||||
Node* skeletonRoot = nullptr;
|
|
||||||
std::vector<glm::mat4> inverseBindMatrices;
|
|
||||||
std::vector<Node*> joints;
|
|
||||||
vks::Buffer ssbo;
|
|
||||||
VkDescriptorSet descriptorSet;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct AnimationSampler
|
|
||||||
{
|
|
||||||
std::string interpolation;
|
|
||||||
std::vector<float> inputs;
|
|
||||||
std::vector<glm::vec4> outputsVec4;
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
struct AnimationChannel
|
|
||||||
{
|
|
||||||
std::string path;
|
|
||||||
Node* node;
|
|
||||||
uint32_t samplerIndex;
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Animation
|
|
||||||
{
|
|
||||||
std::string name;
|
|
||||||
std::vector<AnimationSampler> samplers;
|
|
||||||
std::vector<AnimationChannel> channels;
|
|
||||||
|
|
||||||
float start = std::numeric_limits<float>::max();
|
|
||||||
float end = std::numeric_limits<float>::min();
|
|
||||||
float currentTime = 0.0f;
|
|
||||||
};
|
|
||||||
|
|
||||||
/*
|
|
||||||
Model data
|
|
||||||
*/
|
|
||||||
std::vector<Image> images;
|
|
||||||
std::vector<Texture> textures;
|
|
||||||
std::vector<Material> materials;
|
|
||||||
std::vector<Node*> nodes;
|
|
||||||
std::vector<Skin> skins;
|
|
||||||
std::vector<Animation> animations;
|
|
||||||
|
|
||||||
uint32_t activeAnimation = 0;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//VulkanglTFModel();
|
|
||||||
~VulkanglTFModel();
|
|
||||||
void loadImages(tinygltf::Model& input);
|
|
||||||
void loadTextures(tinygltf::Model& input);
|
|
||||||
void loadMaterials(tinygltf::Model& input);
|
|
||||||
Node* findNode(Node* parent, uint32_t index);
|
|
||||||
Node* nodeFromIndex(uint32_t index);
|
|
||||||
void loadSkins(tinygltf::Model& input);
|
|
||||||
void loadAnimations(tinygltf::Model& input);
|
|
||||||
void loadNode(const tinygltf::Node& inputNode, const tinygltf::Model& input, VulkanglTFModel::Node* parent, uint32_t nodeIndex, std::vector<uint32_t>& indexBuffer, std::vector<VulkanglTFModel::Vertex>& vertexBuffer);
|
|
||||||
glm::mat4 getNodeMatrix(VulkanglTFModel::Node* node);
|
|
||||||
void updateJoints(VulkanglTFModel::Node* node);
|
|
||||||
void updateAnimation(float deltaTime);
|
|
||||||
void drawNode(VkCommandBuffer commandBuffer, VkPipelineLayout pipelineLayout, VulkanglTFModel::Node node);
|
|
||||||
void draw(VkCommandBuffer commandBuffer, VkPipelineLayout pipelineLayout);
|
|
||||||
};
|
|
||||||
|
|
||||||
class VulkanExample : public VulkanExampleBase
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
bool wireframe = false;
|
|
||||||
|
|
||||||
VulkanglTFModel glTFModel;
|
|
||||||
|
|
||||||
struct ShaderData {
|
|
||||||
vks::Buffer buffer;
|
|
||||||
struct Values {
|
|
||||||
glm::mat4 projection;
|
|
||||||
glm::mat4 model;
|
|
||||||
glm::vec4 lightPos = glm::vec4(5.0f, 5.0f, 5.0f, 1.0f);
|
|
||||||
glm::vec4 viewPos;
|
|
||||||
} values;
|
|
||||||
} shaderData;
|
|
||||||
|
|
||||||
struct Pipelines {
|
|
||||||
VkPipeline solid;
|
|
||||||
VkPipeline wireframe = VK_NULL_HANDLE;
|
|
||||||
} pipelines;
|
|
||||||
|
|
||||||
VkPipelineLayout pipelineLayout;
|
|
||||||
|
|
||||||
|
|
||||||
struct DescriptorSetLayouts {
|
|
||||||
VkDescriptorSetLayout matrices;
|
|
||||||
VkDescriptorSetLayout textures;
|
|
||||||
VkDescriptorSetLayout jointMatrices;
|
|
||||||
} descriptorSetLayouts;
|
|
||||||
|
|
||||||
VkDescriptorSet descriptorSet;
|
|
||||||
|
|
||||||
VulkanExample();
|
|
||||||
~VulkanExample();
|
|
||||||
void loadglTFFile(std::string filename);
|
|
||||||
virtual void getEnabledFeatures();
|
|
||||||
void buildCommandBuffers();
|
|
||||||
void loadAssets();
|
|
||||||
void setupDescriptors();
|
|
||||||
void preparePipelines();
|
|
||||||
void prepareUniformBuffers();
|
|
||||||
void updateUniformBuffers();
|
|
||||||
void prepare();
|
|
||||||
virtual void render();
|
|
||||||
virtual void viewChanged();
|
|
||||||
virtual void OnUpdateUIOverlay(vks::UIOverlay* overlay);
|
|
||||||
};
|
|
Loading…
Reference in New Issue