-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyProject.cpp
More file actions
511 lines (419 loc) · 15 KB
/
MyProject.cpp
File metadata and controls
511 lines (419 loc) · 15 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
// This has been adapted from the Vulkan tutorial
#include "MyProject.hpp"
const std::string MODEL_PATH = "models/Terrain.obj";
const std::string TEXTURE_PATH = "textures/mapa.jpg";
const std::string MODEL_PATH_2 = "models/drone.obj";
const std::string TEXTURE_PATH_2 = "textures/espana.jpg";
// Position limits
static const float field_width = 30;
static const float field_length = 30;
static const float border_left = field_width / 2;
static const float border_right = (-1) * (field_width / 2);
static const float border_top = (-1) * (field_length / 2);
static const float border_bottom = field_length / 2;
static const float border_floor = 0;
static const float border_ceil = 8;
// Orientation Limits
static const float max_roll = glm::radians(45.0f);
static const float max_pitch = glm::radians(45.0f);
static const float max_yaw = glm::radians(45.0f);
static const float radius_drone = 2.5;
// Drone position and orientation
glm::vec3 global_pos_drone = glm::vec3(0, 0, 0);
float droneYaw = 0.0;
float dronePitch = 0.0;
float droneRoll = 0.0;
//glm::vec3 drone_orientation = glm::vec3(droneYaw, dronePitch, droneYaw);
// Top view
bool seenCenter = true;
float deltaHeight = 4.0f;
// Follow drone
bool seenFollow = false;
float followerDist = 0.10;
static glm::vec3 FollowerPos = global_pos_drone;
// Drone camera view
bool seenDrone = false;
//Extra view
bool seenExtra = false;
// Camera position
glm::vec3 cameraPos = global_pos_drone;
glm::mat3 CamDir = glm::mat3(1.0f);
glm::vec3 CamPos = glm::vec3(0.0f, 0.0f, 0.0f);
// The uniform buffer object used in this example
struct UniformBufferObject {
alignas(16) glm::mat4 model;
alignas(16) glm::mat4 view;
alignas(16) glm::mat4 proj;
};
struct GlobalUniformBufferObject {
alignas(16) glm::vec3 lightDir;
alignas(16) glm::vec4 lightColor;
alignas(16) glm::vec3 eyePos;
};
// MAIN !
class droneSimulator : public BaseProject {
protected:
// Here you list all the Vulkan objects you need:
// Descriptor Layouts [what will be passed to the shaders]
DescriptorSetLayout DSL1;
DescriptorSetLayout DSL2;
// Pipelines [Shader couples]
Pipeline P1;
Pipeline P2;
// Models, textures and Descriptors (values assigned to the uniforms)
Model M1;
Texture T1;
DescriptorSet DS1;
Model M2;
Texture T2;
DescriptorSet DS2;
// Here you set the main application parameters
void setWindowParameters() {
// window size, titile and initial background
windowWidth = 800;
windowHeight = 600;
windowTitle = "Drone Simulator";
initialBackgroundColor = {0.529f, 0.808f, 0.922f, 1.0f};
// Descriptor pool sizes - 2 models loaded (terrain and drone)
uniformBlocksInPool = 2;
texturesInPool = 2;
setsInPool = 2;
}
// Here you load and setup all your Vulkan objects
void localInit() {
// Descriptor Layouts [what will be passed to the shaders]
DSL1.init(this, {
// this array contains the binding:
// first element : the binding number
// second element : the time of element (buffer or texture)
// third element : the pipeline stage where it will be used
{0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT},
{1, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT}
});
// Pipelines [Shader couples]
P1.init(this, "shaders/vert.spv", "shaders/frag.spv", {&DSL1});
// Models, textures and Descriptors (values assigned to the uniforms)
M1.init(this, MODEL_PATH, 1);
T1.init(this, TEXTURE_PATH);
DS1.init(this, &DSL1, {
// the second parameter, is a pointer to the Uniform Set Layout of this set
// the last parameter is an array, with one element per binding of the set.
// first elmenet : the binding number
// second element : UNIFORM or TEXTURE (an enum) depending on the type
// third element : only for UNIFORMs, the size of the corresponding C++ object
// fourth element : only for TEXTUREs, the pointer to the corresponding texture object
{0, UNIFORM, sizeof(UniformBufferObject), nullptr},
{1, TEXTURE, 0, &T1}
});
// MODEL 2
DSL2.init(this, {{0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT},
{1, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT}});
P2.init(this, "shaders/vert.spv", "shaders/frag.spv", {&DSL2});
M2.init(this, MODEL_PATH_2, 2);
T2.init(this, TEXTURE_PATH_2);
DS2.init(this, &DSL2, {
{0, UNIFORM, sizeof(UniformBufferObject), nullptr},
{1, TEXTURE, 0, &T2}
});
}
// Here you destroy all the objects you created!
void localCleanup() {
DS1.cleanup();
T1.cleanup();
M1.cleanup();
P1.cleanup();
DSL1.cleanup();
// MODEL 2
DS2.cleanup();
T2.cleanup();
M2.cleanup();
P2.cleanup();
DSL2.cleanup();
}
//Creation of the command buffer:
void populateCommandBuffer(VkCommandBuffer commandBuffer, int currentImage) {
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS,
P1.graphicsPipeline);
VkBuffer vertexBuffers[] = {M1.vertexBuffer};
// property .vertexBuffer of models, contains the VkBuffer handle to its vertex buffer
VkDeviceSize offsets[] = {0};
vkCmdBindVertexBuffers(commandBuffer, 0, 1, vertexBuffers, offsets);
// property .indexBuffer of models, contains the VkBuffer handle to its index buffer
vkCmdBindIndexBuffer(commandBuffer, M1.indexBuffer, 0,
VK_INDEX_TYPE_UINT32);
// property .pipelineLayout of a pipeline contains its layout.
// property .descriptorSets of a descriptor set contains its elements.
vkCmdBindDescriptorSets(commandBuffer,
VK_PIPELINE_BIND_POINT_GRAPHICS,
P1.pipelineLayout, 0, 1, &DS1.descriptorSets[currentImage],
0, nullptr);
// property .indices.size() of models, contains the number of triangles * 3 of the mesh.
vkCmdDrawIndexed(commandBuffer,
static_cast<uint32_t>(M1.indices.size()), 1, 0, 0, 0);
// MODELO 2
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, P2.graphicsPipeline);
VkBuffer vertexBuffers2[] = {M2.vertexBuffer};
VkDeviceSize offsets2[] = {0};
vkCmdBindVertexBuffers(commandBuffer, 0, 1, vertexBuffers2, offsets2);
vkCmdBindIndexBuffer(commandBuffer, M2.indexBuffer, 0, VK_INDEX_TYPE_UINT32);
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, P2.pipelineLayout, 0, 1, &DS2.descriptorSets[currentImage], 0, nullptr);
vkCmdDrawIndexed(commandBuffer, static_cast<uint32_t>(M2.indices.size()), 1, 0, 0, 0);
}
// Update the uniforms
void updateUniformBuffer(uint32_t currentImage) {
static auto startTime = std::chrono::high_resolution_clock::now();
auto currentTime = std::chrono::high_resolution_clock::now();
float time = std::chrono::duration<float, std::chrono::seconds::period>(currentTime - startTime).count();
float lastTime = 0.0f;
float deltaT = time - lastTime;
lastTime = time;
// updates global uniforms
GlobalUniformBufferObject gubo{};
gubo.lightDir = glm::vec3(cos(glm::radians(135.0f)), sin(glm::radians(135.0f)), 0.0f);
gubo.lightColor = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f);
gubo.eyePos = CamPos;
UniformBufferObject ubo_terrain{};
ubo_terrain.model =glm::rotate(glm::mat4(1.0f),
glm::radians(90.0f),
glm::vec3(0.0f, 0.0f, 1.0f));
ubo_terrain.view = glm::lookAt(glm::vec3(2.0f, 2.0f, 2.0f),
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(0.0f, 0.0f, 1.0f));
ubo_terrain.proj = glm::perspective(glm::radians(45.0f),
swapChainExtent.width / (float) swapChainExtent.height,
0.1f, 10.0f);
ubo_terrain.proj[1][1] *= -1;
// Drone
UniformBufferObject ubo_drone{};
ubo_drone.model = glm::scale(glm::mat4(1.0f), glm::vec3(0.1, 0.1, 0.1)) * glm::translate(glm::mat4(1.0f), global_pos_drone) * glm::rotate(glm::mat4(1.0f), glm::radians(90.0f), glm::vec3(1.0f, 0.0f, 0.0f));
ubo_drone.view = glm::lookAt(glm::vec3(0.0f, 2.0f, 3.0f),
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(0.0f, 0.0f, 1.0f));
ubo_drone.proj = glm::perspective(glm::radians(45.0f),
swapChainExtent.width / (float)swapChainExtent.height,
0.1f, 10.0f);
ubo_drone.proj[1][1] *= -1;
void* data;
// CAMERA UPDATE
setCameraMode(window, seenCenter, seenDrone, seenFollow, seenExtra);
// Update drone position and orientation
getDroneInput(window);
// Top view
if (seenCenter) //GLFW_KEY_I
{
cameraPos = global_pos_drone + glm::vec3(0.0f, 0.0f, deltaHeight);
ubo_terrain.view = LookAtMat(cameraPos, global_pos_drone , 0.0f);
ubo_drone.model *= LookInDirMat(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(droneRoll, dronePitch, droneYaw));
ubo_drone.view = LookAtMat(glm::vec3(0.0f, 0.0f, deltaHeight), glm::vec3(0.0f) , 0.0f); // Works quite well
}
// Follow drone
else if (seenFollow) //GLFW_KEY_0
{
cameraPos = global_pos_drone + glm::vec3(0.0f, deltaHeight, deltaHeight/2);
ubo_terrain.view = LookInDirMat(cameraPos, glm::vec3(droneRoll, dronePitch, droneYaw));
ubo_drone.view = LookAtMat(global_pos_drone, global_pos_drone, 0.0f);
}
// Drone camera
else if (seenDrone) //GLFW_KEY_P
{
cameraPos = global_pos_drone;
ubo_terrain.view = LookInDirMat(cameraPos, glm::vec3(droneRoll, dronePitch, droneYaw));
ubo_drone.view = LookAtMat(cameraPos, global_pos_drone, 0.0f);
}
//Extra view
else if (seenExtra) //GLFW_KEY_L
{
cameraPos = global_pos_drone + glm::vec3(0.0f, 0.0f, deltaHeight);
ubo_terrain.view = LookAtMat(cameraPos, global_pos_drone , 0.0f);
ubo_drone.model *= LookInDirMat(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(droneRoll, dronePitch, droneYaw));
ubo_drone.view = glm::lookAt(glm::vec3(2.0f, 0.0f, 1.0f),
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(0.0f, 0.0f, 1.0f));
}
if(glfwGetKey(window, GLFW_KEY_ESCAPE)){ cleanup(); }
// Update uniforms
//MODEL 1
vkMapMemory(device, DS1.uniformBuffersMemory[0][currentImage], 0,
sizeof(ubo_terrain), 0, &data);
memcpy(data, &ubo_terrain, sizeof(ubo_terrain));
vkUnmapMemory(device, DS1.uniformBuffersMemory[0][currentImage]);
//MODEL 2
vkMapMemory(device, DS2.uniformBuffersMemory[0][currentImage], 0,
sizeof(ubo_drone), 0, &data);
memcpy(data, &ubo_drone, sizeof(ubo_drone));
vkUnmapMemory(device, DS2.uniformBuffersMemory[0][currentImage]);
}
void getDroneInput(GLFWwindow *window){
static auto startTime = std::chrono::high_resolution_clock::now();
auto currentTime = std::chrono::high_resolution_clock::now();
float time = std::chrono::duration<float, std::chrono::seconds::period>(currentTime - startTime).count();
float lastTime = 0.0f;
float deltaT = time - lastTime;
lastTime = time;
const float ROT_SPEED = glm::radians(.009f);
const float MOVE_SPEED = 0.01;
if(glfwGetKey(window, GLFW_KEY_LEFT)) {
droneYaw += deltaT * ROT_SPEED;
}
if(glfwGetKey(window, GLFW_KEY_RIGHT)) {
droneYaw -= deltaT * ROT_SPEED;
}
if(glfwGetKey(window, GLFW_KEY_UP)) {
dronePitch += deltaT * ROT_SPEED;
}
if(glfwGetKey(window, GLFW_KEY_DOWN)) {
dronePitch -= deltaT * ROT_SPEED;
}
if(glfwGetKey(window, GLFW_KEY_Q)) {
droneRoll -= deltaT * ROT_SPEED;
}
if(glfwGetKey(window, GLFW_KEY_E)) {
droneRoll += deltaT * ROT_SPEED;
}
ensureOrientationLimits();
if(glfwGetKey(window, GLFW_KEY_A)) {
global_pos_drone -= MOVE_SPEED * glm::vec3(glm::rotate(glm::mat4(1.0f), 0.0f,
glm::vec3(0.0f, 1.0f, 0.0f)) * glm::vec4(1,0,0,1)) * deltaT;
}
if(glfwGetKey(window, GLFW_KEY_D)) {
global_pos_drone += MOVE_SPEED * glm::vec3(glm::rotate(glm::mat4(1.0f), 0.0f,
glm::vec3(0.0f, 1.0f, 0.0f)) * glm::vec4(1,0,0,1)) * deltaT;
}
if(glfwGetKey(window, GLFW_KEY_S)) {
global_pos_drone -= MOVE_SPEED * glm::vec3(glm::rotate(glm::mat4(1.0f), 0.0f,
glm::vec3(0.0f, 1.0f, 0.0f)) * glm::vec4(0,1,0,1)) * deltaT;
}
if(glfwGetKey(window, GLFW_KEY_W)) {
global_pos_drone += MOVE_SPEED * glm::vec3(glm::rotate(glm::mat4(1.0f), 0.0f,
glm::vec3(0.0f, 1.0f, 0.0f)) * glm::vec4(0,1,0,1)) * deltaT;
}
if(glfwGetKey(window, GLFW_KEY_F)){
global_pos_drone -= MOVE_SPEED * glm::vec3(glm::rotate(glm::mat4(1.0f), 0.0f,
glm::vec3(0.0f, 1.0f, 0.0f)) * glm::vec4(0,0,1,1)) * deltaT;
}
if(glfwGetKey(window, GLFW_KEY_R)){
global_pos_drone += MOVE_SPEED * glm::vec3(glm::rotate(glm::mat4(1.0f), droneYaw,
glm::vec3(0.0f, 1.0f, 0.0f)) * glm::vec4(0,0,1,1)) * deltaT;
}
ensurePosLimits();
}
void ensurePosLimits(){
if (global_pos_drone.x + radius_drone > border_left)
{
global_pos_drone.x = border_left - radius_drone;
}
else if (global_pos_drone.y + radius_drone > border_bottom)
{
global_pos_drone.y = border_bottom - radius_drone;
}
else if (global_pos_drone.x - radius_drone < border_right)
{
global_pos_drone.x = border_right + radius_drone;
}
else if (global_pos_drone.y - radius_drone < border_top)
{
global_pos_drone.y = border_top + radius_drone;
}
else if (global_pos_drone.z < border_floor)
{
global_pos_drone.z = border_floor;
}
else if (global_pos_drone.z > border_ceil)
{
global_pos_drone.z = border_ceil;
}
else
{
return;
}
}
void ensureOrientationLimits(){
if (droneRoll > max_roll)
{
droneRoll = max_roll;
}
else if (droneRoll < -max_roll)
{
droneRoll = -max_roll;
}
else if (droneYaw > max_yaw)
{
droneYaw = max_yaw;
}
else if (droneYaw < -max_yaw)
{
droneYaw = -max_yaw;
}
else if (dronePitch > max_pitch)
{
dronePitch = max_pitch;
}
else if (dronePitch < -max_pitch)
{
dronePitch = -max_pitch;
}
else
{
return;
}
}
void setCameraMode(GLFWwindow *window, bool &seenCenter, bool &seenDrone, bool &seenFollow, bool &seenExtra){
if (glfwGetKey(window, GLFW_KEY_I))
{ // Top view
seenCenter = true;
seenFollow = false;
seenDrone = false;
seenExtra = false;
}
if (glfwGetKey(window, GLFW_KEY_O))
{ // follow drone
seenCenter = false;
seenFollow = true;
seenDrone = false;
seenExtra = false;
}
if (glfwGetKey(window, GLFW_KEY_P))
{ // camera drone
seenCenter = false;
seenFollow = false;
seenDrone = true;
seenExtra = false;
}
if (glfwGetKey(window, GLFW_KEY_L))
{ // camera drone
seenCenter = false;
seenFollow = false;
seenDrone = false;
seenExtra = true;
}
}
glm::mat4 I = glm::mat4(1.0f); // Identity matrix
glm::vec3 xAxis = glm::vec3(1,0,0); // x axis
glm::vec3 yAxis = glm::vec3(0,1,0); // y axis
glm::vec3 zAxis = glm::vec3(0,0,1); // z axis
glm::mat4 LookInDirMat(glm::vec3 Pos, glm::vec3 Angs) {
glm::mat4 T = glm::translate(I, -Pos);
glm::mat4 R_y = glm::rotate(I, -Angs.x, yAxis);
glm::mat4 R_x = glm::rotate(I, -Angs.y, xAxis);
glm::mat4 R_z = glm::rotate(I, -Angs.z, zAxis);
glm::mat4 M_V = R_z*R_x*R_y*T;
return M_V;
}
glm::mat4 LookAtMat(glm::vec3 Pos, glm::vec3 aim, float Roll) {
glm::mat4 R = glm::rotate(I, glm::radians(Roll), yAxis);
glm::vec3 u = yAxis;
glm::mat4 M_V = R*glm::lookAt(Pos, aim, u);
return M_V;
}
};
// Main
int main() {
droneSimulator app;
try {
app.run();
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}