-
Notifications
You must be signed in to change notification settings - Fork 2k
/
vertexattributes.h
140 lines (121 loc) · 3.29 KB
/
vertexattributes.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* Vulkan Example - Passing vertex attributes using interleaved and separate buffers
*
* Copyright (C) 2022-2023 by Sascha Willems - www.saschawillems.de
*
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
*/
#define TINYGLTF_IMPLEMENTATION
#define STB_IMAGE_IMPLEMENTATION
#define TINYGLTF_NO_STB_IMAGE_WRITE
#define TINYGLTF_NO_STB_IMAGE
#define TINYGLTF_NO_EXTERNAL_IMAGE
#ifdef VK_USE_PLATFORM_ANDROID_KHR
#define TINYGLTF_ANDROID_LOAD_FROM_ASSETS
#endif
#include "tiny_gltf.h"
#include "vulkanexamplebase.h"
struct PushConstBlock {
glm::mat4 nodeMatrix;
uint32_t alphaMask;
float alphaMaskCutoff;
};
struct Material {
glm::vec4 baseColorFactor = glm::vec4(1.0f);
uint32_t baseColorTextureIndex;
uint32_t normalTextureIndex;
std::string alphaMode = "OPAQUE";
float alphaCutOff;
VkDescriptorSet descriptorSet;
};
struct Image {
vks::Texture2D texture;
};
struct Texture {
int32_t imageIndex;
};
// Layout for the interleaved vertex attributes
struct Vertex {
glm::vec3 pos;
glm::vec3 normal;
glm::vec2 uv;
glm::vec4 tangent;
};
struct Primitive {
uint32_t firstIndex;
uint32_t indexCount;
int32_t materialIndex;
};
struct Mesh {
std::vector<Primitive> primitives;
};
struct Node;
struct Node {
Node* parent;
std::vector<Node> children;
Mesh mesh;
glm::mat4 matrix;
};
std::vector<Node> nodes;
class VulkanExample : public VulkanExampleBase
{
public:
enum VertexAttributeSettings { interleaved, 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<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
struct SeparateVertexBuffers {
vks::Buffer pos, normal, uv, tangent;
} separateVertexBuffers;
// Single vertex buffer for all primitives
vks::Buffer interleavedVertexBuffer;
// Index buffer for all primitives of the scene
vks::Buffer indices;
struct ShaderData {
vks::Buffer buffer;
struct Values {
glm::mat4 projection;
glm::mat4 view;
glm::vec4 lightPos = glm::vec4(0.0f, 2.5f, 0.0f, 1.0f);
glm::vec4 viewPos;
} values;
} shaderData;
struct Pipelines {
VkPipeline vertexAttributesInterleaved;
VkPipeline vertexAttributesSeparate;
} pipelines;
VkPipelineLayout pipelineLayout;
struct DescriptorSetLayouts {
VkDescriptorSetLayout matrices;
VkDescriptorSetLayout textures;
} descriptorSetLayouts;
VkDescriptorSet descriptorSet;
struct Scene {
std::vector<Image> images;
std::vector<Texture> textures;
std::vector<Material> materials;
} scene;
VulkanExample();
~VulkanExample();
virtual void getEnabledFeatures();
void buildCommandBuffers();
void uploadVertexData();
void loadglTFFile(std::string filename);
void loadAssets();
void setupDescriptors();
void preparePipelines();
void prepareUniformBuffers();
void updateUniformBuffers();
void prepare();
void loadSceneNode(const tinygltf::Node& inputNode, const tinygltf::Model& input, Node* parent);
void drawSceneNode(VkCommandBuffer commandBuffer, Node node);
virtual void render();
virtual void OnUpdateUIOverlay(vks::UIOverlay* overlay);
};