forked from SaschaWillems/Vulkan
-
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathgears.cpp
More file actions
139 lines (119 loc) · 5.36 KB
/
gears.cpp
File metadata and controls
139 lines (119 loc) · 5.36 KB
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
/*
* Vulkan Example - Animated gears using multiple uniform buffers
*
* Copyright (C) 2016 by Sascha Willems - www.saschawillems.de
*
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
*/
#include <vulkanExampleBase.h>
#include <vulkanGear.h>
class VulkanExample : public vkx::ExampleBase {
using Parent = vkx::ExampleBase;
public:
struct {
vk::Pipeline solid;
} pipelines;
std::vector<VulkanGear> gears;
vks::model::VertexLayout vertexLayout{ {
vks::model::VERTEX_COMPONENT_POSITION,
vks::model::VERTEX_COMPONENT_NORMAL,
vks::model::VERTEX_COMPONENT_COLOR,
} };
vk::PipelineLayout pipelineLayout;
vk::DescriptorSetLayout descriptorSetLayout;
VulkanExample() {
timerSpeed *= 0.25f;
camera.translate(glm::vec3(0.0f, 0.0f, -12.0f * zoomSpeed));
title = "Vulkan Example - Gears";
}
~VulkanExample() {
// Clean up used Vulkan resources
// Note : Inherited destructor cleans up resources stored in base class
device.destroyPipeline(pipelines.solid);
device.destroyPipelineLayout(pipelineLayout);
device.destroyDescriptorSetLayout(descriptorSetLayout);
gears.clear();
}
void updateDrawCommandBuffer(const vk::CommandBuffer& cmdBuffer) override {
cmdBuffer.setViewport(0, vks::util::viewport(size));
cmdBuffer.setScissor(0, vks::util::rect2D(size));
cmdBuffer.bindPipeline(vk::PipelineBindPoint::eGraphics, pipelines.solid);
for (auto& gear : gears) {
gear.draw(cmdBuffer, pipelineLayout);
}
}
void prepareVertices() {
// Gear definitions
std::vector<float> innerRadiuses = { 1.0f, 0.5f, 1.3f };
std::vector<float> outerRadiuses = { 4.0f, 2.0f, 2.0f };
std::vector<float> widths = { 1.0f, 2.0f, 0.5f };
std::vector<int32_t> toothCount = { 20, 10, 10 };
std::vector<float> toothDepth = { 0.7f, 0.7f, 0.7f };
std::vector<glm::vec3> colors = { glm::vec3(1.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.2f), glm::vec3(0.0f, 0.0f, 1.0f) };
std::vector<glm::vec3> positions = { glm::vec3(-3.0, 0.0, 0.0), glm::vec3(3.1, 0.0, 0.0), glm::vec3(-3.1, -6.2, 0.0) };
std::vector<float> rotationSpeeds = { 1.0f, -2.0f, -2.0f };
std::vector<float> rotationStarts = { 0.0f, -9.0f, -30.0f };
gears.resize(positions.size());
for (int32_t i = 0; i < gears.size(); ++i) {
gears[i].generate(context, innerRadiuses[i], outerRadiuses[i], widths[i], toothCount[i], toothDepth[i], colors[i], positions[i], rotationSpeeds[i],
rotationStarts[i]);
}
}
void setupDescriptorPool() {
// One UBO for each gears
std::vector<vk::DescriptorPoolSize> poolSizes{
vk::DescriptorPoolSize(vk::DescriptorType::eUniformBuffer, 3),
};
descriptorPool = device.createDescriptorPool({ {}, 3, (uint32_t)poolSizes.size(), poolSizes.data() });
}
void setupDescriptorSetLayout() {
std::vector<vk::DescriptorSetLayoutBinding> setLayoutBindings = { // Binding 0 : Vertex shader uniform buffer
{ 0, vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex }
};
descriptorSetLayout = device.createDescriptorSetLayout({ {}, (uint32_t)setLayoutBindings.size(), setLayoutBindings.data() });
pipelineLayout = device.createPipelineLayout({ {}, 1, &descriptorSetLayout });
}
void setupDescriptorSets() {
for (auto& gear : gears) {
gear.setupDescriptorSet(descriptorPool, descriptorSetLayout);
}
}
void preparePipelines() {
// Solid rendering pipeline
vks::pipelines::GraphicsPipelineBuilder pipelineBuilder{ device, pipelineLayout, renderPass };
pipelineBuilder.rasterizationState.frontFace = vk::FrontFace::eClockwise;
pipelineBuilder.rasterizationState.cullMode = vk::CullModeFlagBits::eNone;
pipelineBuilder.colorBlendState.blendAttachmentStates.resize(1);
pipelineBuilder.depthStencilState = { true };
// Load shaders
pipelineBuilder.loadShader(getAssetPath() + "shaders/gears/gears.vert.spv", vk::ShaderStageFlagBits::eVertex);
pipelineBuilder.loadShader(getAssetPath() + "shaders/gears/gears.frag.spv", vk::ShaderStageFlagBits::eFragment);
pipelineBuilder.vertexInputState.appendVertexLayout(vertexLayout);
pipelines.solid = pipelineBuilder.create(context.pipelineCache);
}
void updateUniformBuffers() {
glm::mat4 perspective = glm::perspective(glm::radians(60.0f), (float)size.width / (float)size.height, 0.001f, 256.0f);
for (auto& gear : gears) {
gear.updateUniformBuffer(perspective, camera.matrices.view, timer * 360.0f);
}
}
void prepare() override {
Parent::prepare();
prepareVertices();
setupDescriptorSetLayout();
preparePipelines();
setupDescriptorPool();
setupDescriptorSets();
updateUniformBuffers();
buildCommandBuffers();
prepared = true;
}
void update(float delta) override {
Parent::update(delta);
if (!paused) {
updateUniformBuffers();
}
}
void viewChanged() override { updateUniformBuffers(); }
};
RUN_EXAMPLE(VulkanExample)