-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirstApp.cpp
More file actions
165 lines (142 loc) · 5.47 KB
/
FirstApp.cpp
File metadata and controls
165 lines (142 loc) · 5.47 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "FirstApp.hpp"
#include "SuzumeModel.hpp"
#include "SuzumePipeline.hpp"
#include "SuzumeSwapChain.hpp"
#include "glm/fwd.hpp"
#include "glm/gtc/constants.hpp"
#include <array>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <stdexcept>
#include <vulkan/vulkan_core.h>
#define GLM_FORCE_RADIANS
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
namespace Suzume {
struct SuzumePushConstantData {
glm::mat4 transform{1.f};
alignas(16) glm::vec3 color{};
};
FirstApp::FirstApp() {
loadGameObjects();
createPipelineLayout();
createPipeline();
}
FirstApp::~FirstApp() {
vkDestroyPipelineLayout(suzumeDevice.device(), pipelineLayout, nullptr);
}
void FirstApp::run() {
while (!suzumeWindow.shouldClose()) {
glfwPollEvents();
if (auto commandBuffer = suzumeRenderer.beginFrame()) {
suzumeRenderer.beginSwapChainRenderPass(commandBuffer);
renderGameObjects(commandBuffer);
suzumeRenderer.endSwapChainRenderPass(commandBuffer);
suzumeRenderer.endFrame();
}
}
vkDeviceWaitIdle(suzumeDevice.device());
}
// temporary helper function, creates a 1x1x1 cube centered at offset
std::unique_ptr<SuzumeModel> createCubeModel(SuzumeDevice &device, glm::vec3 offset) {
std::vector<SuzumeModel::Vertex> vertices{
// left face (coral pink gradient)
{{-.5f, -.5f, -.5f}, {1.0f, .4f, .5f}},
{{-.5f, .5f, .5f}, {1.0f, .6f, .7f}},
{{-.5f, -.5f, .5f}, {1.0f, .5f, .6f}},
{{-.5f, -.5f, -.5f}, {1.0f, .4f, .5f}},
{{-.5f, .5f, -.5f}, {1.0f, .5f, .6f}},
{{-.5f, .5f, .5f}, {1.0f, .6f, .7f}},
// right face (mint green gradient)
{{.5f, -.5f, -.5f}, {.3f, .9f, .7f}},
{{.5f, .5f, .5f}, {.5f, 1.0f, .8f}},
{{.5f, -.5f, .5f}, {.4f, .95f, .75f}},
{{.5f, -.5f, -.5f}, {.3f, .9f, .7f}},
{{.5f, .5f, -.5f}, {.4f, .95f, .75f}},
{{.5f, .5f, .5f}, {.5f, 1.0f, .8f}},
// top face (cyan gradient)
{{-.5f, -.5f, -.5f}, {.2f, .8f, 1.0f}},
{{.5f, -.5f, .5f}, {.4f, .9f, 1.0f}},
{{-.5f, -.5f, .5f}, {.3f, .85f, 1.0f}},
{{-.5f, -.5f, -.5f}, {.2f, .8f, 1.0f}},
{{.5f, -.5f, -.5f}, {.3f, .85f, 1.0f}},
{{.5f, -.5f, .5f}, {.4f, .9f, 1.0f}},
// bottom face (magenta gradient)
{{-.5f, .5f, -.5f}, {.9f, .2f, .6f}},
{{.5f, .5f, .5f}, {1.0f, .4f, .8f}},
{{-.5f, .5f, .5f}, {.95f, .3f, .7f}},
{{-.5f, .5f, -.5f}, {.9f, .2f, .6f}},
{{.5f, .5f, -.5f}, {.95f, .3f, .7f}},
{{.5f, .5f, .5f}, {1.0f, .4f, .8f}},
// front face (electric blue gradient)
{{-.5f, -.5f, 0.5f}, {.1f, .4f, 1.0f}},
{{.5f, .5f, 0.5f}, {.3f, .6f, 1.0f}},
{{-.5f, .5f, 0.5f}, {.2f, .5f, 1.0f}},
{{-.5f, -.5f, 0.5f}, {.1f, .4f, 1.0f}},
{{.5f, -.5f, 0.5f}, {.2f, .5f, 1.0f}},
{{.5f, .5f, 0.5f}, {.3f, .6f, 1.0f}},
// back face (purple gradient)
{{-.5f, -.5f, -0.5f}, {.6f, .2f, .9f}},
{{.5f, .5f, -0.5f}, {.8f, .4f, 1.0f}},
{{-.5f, .5f, -0.5f}, {.7f, .3f, .95f}},
{{-.5f, -.5f, -0.5f}, {.6f, .2f, .9f}},
{{.5f, -.5f, -0.5f}, {.7f, .3f, .95f}},
{{.5f, .5f, -0.5f}, {.8f, .4f, 1.0f}},
};
for (auto &v : vertices) {
v.position += offset;
}
return std::make_unique<SuzumeModel>(device, vertices);
}
void FirstApp::loadGameObjects() {
std::shared_ptr<SuzumeModel> suzumeModel =
createCubeModel(suzumeDevice, {.0f, .0f, .0f});
auto cube = SuzumeGameObject::createGameObject();
cube.model = suzumeModel;
cube.transform.translation = {.0f, .0f, .5f};
cube.transform.scale = {.5f, .5f, .5f};
gameObjects.push_back(std::move(cube));
}
void FirstApp::createPipelineLayout() {
VkPushConstantRange pushConstantRange{};
pushConstantRange.stageFlags =
VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT;
pushConstantRange.offset = 0;
pushConstantRange.size = sizeof(SuzumePushConstantData);
VkPipelineLayoutCreateInfo pipelineLayoutInfo{};
pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
pipelineLayoutInfo.setLayoutCount = 0;
pipelineLayoutInfo.pSetLayouts = nullptr;
pipelineLayoutInfo.pushConstantRangeCount = 1;
pipelineLayoutInfo.pPushConstantRanges = &pushConstantRange;
if (vkCreatePipelineLayout(suzumeDevice.device(), &pipelineLayoutInfo,
nullptr, &pipelineLayout) != VK_SUCCESS) {
throw std::runtime_error("failed to create pipeline layout!");
}
}
void FirstApp::createPipeline() {
PipelineConfigInfo pipelineConfig{};
SuzumePipeline::defaultPipelineConfigInfo(pipelineConfig);
pipelineConfig.renderPass = suzumeRenderer.getSwapChainRenderPass();
pipelineConfig.pipelineLayout = pipelineLayout;
suzumePipeline = std::make_unique<SuzumePipeline>(
suzumeDevice, "shaders/SuzumeShader.vert.spv",
"shaders/SuzumeShader.frag.spv", pipelineConfig);
}
void FirstApp::renderGameObjects(VkCommandBuffer commandBuffer) {
suzumePipeline->bind(commandBuffer);
for (auto &obj : gameObjects) {
obj.transform.rotation.y =
glm::mod(obj.transform.rotation.y + 0.01f, glm::two_pi<float>());
obj.transform.rotation.x =
glm::mod(obj.transform.rotation.x + 0.005f, glm::two_pi<float>());
SuzumePushConstantData push{};
push.color = obj.color;
push.transform = obj.transform.mat4();
vkCmdPushConstants(commandBuffer, pipelineLayout,
VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT,
0, sizeof(SuzumePushConstantData), &push);
obj.model->bind(commandBuffer);
obj.model->draw(commandBuffer);
}
}
} // namespace Suzume