-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathmain.cpp
More file actions
176 lines (143 loc) · 5.73 KB
/
main.cpp
File metadata and controls
176 lines (143 loc) · 5.73 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
166
167
168
169
170
171
172
173
174
175
176
#include <vulkan/vulkan.h>
#include "Instance.h"
#include "Window.h"
#include "Renderer.h"
#include "Camera.h"
#include "Scene.h"
#include "Image.h"
Device* device;
SwapChain* swapChain;
Renderer* renderer;
Camera* camera;
namespace {
void resizeCallback(GLFWwindow* window, int width, int height) {
if (width == 0 || height == 0) return;
vkDeviceWaitIdle(device->GetVkDevice());
swapChain->Recreate();
renderer->RecreateFrameResources();
}
bool leftMouseDown = false;
bool rightMouseDown = false;
double previousX = 0.0;
double previousY = 0.0;
void mouseDownCallback(GLFWwindow* window, int button, int action, int mods) {
if (button == GLFW_MOUSE_BUTTON_LEFT) {
if (action == GLFW_PRESS) {
leftMouseDown = true;
glfwGetCursorPos(window, &previousX, &previousY);
}
else if (action == GLFW_RELEASE) {
leftMouseDown = false;
}
} else if (button == GLFW_MOUSE_BUTTON_RIGHT) {
if (action == GLFW_PRESS) {
rightMouseDown = true;
glfwGetCursorPos(window, &previousX, &previousY);
}
else if (action == GLFW_RELEASE) {
rightMouseDown = false;
}
}
}
void mouseMoveCallback(GLFWwindow* window, double xPosition, double yPosition) {
if (leftMouseDown) {
double sensitivity = 0.5;
float deltaX = static_cast<float>((previousX - xPosition) * sensitivity);
float deltaY = static_cast<float>((previousY - yPosition) * sensitivity);
camera->UpdateOrbit(deltaX, deltaY, 0.0f);
previousX = xPosition;
previousY = yPosition;
} else if (rightMouseDown) {
double deltaZ = static_cast<float>((previousY - yPosition) * 0.05);
camera->UpdateOrbit(0.0f, 0.0f, deltaZ);
previousY = yPosition;
}
}
}
int main() {
static constexpr char* applicationName = "Vulkan Grass Rendering";
InitializeWindow(1000, 720, applicationName);
unsigned int glfwExtensionCount = 0;
const char** glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
Instance* instance = new Instance(applicationName, glfwExtensionCount, glfwExtensions);
VkSurfaceKHR surface;
if (glfwCreateWindowSurface(instance->GetVkInstance(), GetGLFWWindow(), nullptr, &surface) != VK_SUCCESS) {
throw std::runtime_error("Failed to create window surface");
}
instance->PickPhysicalDevice({ VK_KHR_SWAPCHAIN_EXTENSION_NAME }, QueueFlagBit::GraphicsBit | QueueFlagBit::TransferBit | QueueFlagBit::ComputeBit | QueueFlagBit::PresentBit, surface);
VkPhysicalDeviceFeatures deviceFeatures = {};
deviceFeatures.tessellationShader = VK_TRUE;
deviceFeatures.fillModeNonSolid = VK_TRUE;
deviceFeatures.samplerAnisotropy = VK_TRUE;
device = instance->CreateDevice(QueueFlagBit::GraphicsBit | QueueFlagBit::TransferBit | QueueFlagBit::ComputeBit | QueueFlagBit::PresentBit, deviceFeatures);
swapChain = device->CreateSwapChain(surface, 5);
camera = new Camera(device, 640.f / 480.f);
VkCommandPoolCreateInfo transferPoolInfo = {};
transferPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
transferPoolInfo.queueFamilyIndex = device->GetInstance()->GetQueueFamilyIndices()[QueueFlags::Transfer];
transferPoolInfo.flags = 0;
VkCommandPool transferCommandPool;
if (vkCreateCommandPool(device->GetVkDevice(), &transferPoolInfo, nullptr, &transferCommandPool) != VK_SUCCESS) {
throw std::runtime_error("Failed to create command pool");
}
VkImage grassImage;
VkDeviceMemory grassImageMemory;
Image::FromFile(device,
transferCommandPool,
"images/grass.jpg",
VK_FORMAT_R8G8B8A8_UNORM,
VK_IMAGE_TILING_OPTIMAL,
VK_IMAGE_USAGE_SAMPLED_BIT,
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
grassImage,
grassImageMemory
);
float planeDim = 25.f;
float halfWidth = planeDim * 0.5f;
Model* plane = new Model(device, transferCommandPool,
{
{ { -halfWidth, 0.0f, halfWidth }, { 1.0f, 0.0f, 0.0f },{ 1.0f, 0.0f } },
{ { halfWidth, 0.0f, halfWidth }, { 0.0f, 1.0f, 0.0f },{ 0.0f, 0.0f } },
{ { halfWidth, 0.0f, -halfWidth }, { 0.0f, 0.0f, 1.0f },{ 0.0f, 1.0f } },
{ { -halfWidth, 0.0f, -halfWidth }, { 1.0f, 1.0f, 1.0f },{ 1.0f, 1.0f } }
},
{ 0, 1, 2, 2, 3, 0 }
);
plane->SetTexture(grassImage);
Blades* blades = new Blades(device, transferCommandPool, planeDim);
vkDestroyCommandPool(device->GetVkDevice(), transferCommandPool, nullptr);
Scene* scene = new Scene(device);
scene->AddModel(plane);
scene->AddBlades(blades);
renderer = new Renderer(device, swapChain, scene, camera);
glfwSetWindowSizeCallback(GetGLFWWindow(), resizeCallback);
glfwSetMouseButtonCallback(GetGLFWWindow(), mouseDownCallback);
glfwSetCursorPosCallback(GetGLFWWindow(), mouseMoveCallback);
int start = GetTickCount();
int frame = 0;
while (!ShouldQuit()) {
glfwPollEvents();
scene->UpdateTime();
renderer->Frame();
frame++;
if (frame == 500)
{
int total = GetTickCount() - start;
printf("ms per frame%f\n", float(total / 500.0));
}
}
vkDeviceWaitIdle(device->GetVkDevice());
vkDestroyImage(device->GetVkDevice(), grassImage, nullptr);
vkFreeMemory(device->GetVkDevice(), grassImageMemory, nullptr);
delete scene;
delete plane;
delete blades;
delete camera;
delete renderer;
delete swapChain;
delete device;
delete instance;
DestroyWindow();
return 0;
}