-
Notifications
You must be signed in to change notification settings - Fork 0
/
vkc_entity.cpp
105 lines (77 loc) · 2.82 KB
/
vkc_entity.cpp
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
#include "vkc_entity.h"
/**
* Initialize with empty fields.
*/
VkcEntity::VkcEntity()
{
position = QVector3D(0.0f, 0.0f, 0.0f);
scale = QVector3D(1.0f, 1.0f, 1.0f);
rotation = QQuaternion(1.0f, 0.0f, 0.0f, 0.0f);
dir = 0.1f / 15.0f;
}
/**
* Create the entity.
*/
VkcEntity::VkcEntity(const VkcDevice *device) : VkcEntity()
{
// Load the model.
vertices.append({-1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, -1.0f, 0.0f});
vertices.append({-1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f});
vertices.append({ 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, -1.0f, 0.0f});
vertices.append({ 1.0f, 0.0f, -1.0f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f});
indices = {0, 1, 2, 2, 1, 3};
// Load position, scale and rotation data.
// /@todo
// Create buffer.
buffer.create(vertices.size() * sizeof(VkVertex) + indices.size() * sizeof(uint32_t),
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT, device);
// Map buffer memory to host.
uint8_t *data;
vkMapMemory(device->logical, buffer.memory, 0, VK_WHOLE_SIZE, 0,(void**) &data);
// Copy data to the buffer.
uint32_t offset = 0;
memcpy(data + offset, vertices.data(), vertices.size() * sizeof(VkVertex));
offset += vertices.size() * sizeof(VkVertex);
memcpy(data + offset, indices.data(), indices.size() * sizeof(uint32_t));
// Unmap memory.
vkUnmapMemory(device->logical, buffer.memory);
}
/**
* Destroy the entity.
*/
VkcEntity::~VkcEntity()
{
buffer.destroy();
}
/**
* Register the commands that render the entity.
*/
void VkcEntity::render(VkCommandBuffer commandBuffer, MgBuffer uniformBuffer, QMatrix4x4 vpMatrix, const VkcDevice *device)
{
// Wiggle, wiggle, wiggle.
position += QVector3D(dir, 0.0f, 0.0f);
float pos = position.x();
if(pos > 0.1f || pos < -0.1f)
dir *= -1.0f;
// Calculate model matrix.
QMatrix4x4 modelMatrix;
modelMatrix.translate(position);
modelMatrix.rotate(rotation);
modelMatrix.scale(scale);
// Calculate MVP matrix.
QMatrix4x4 mvpMatrix = vpMatrix * modelMatrix;
// Map uniform buffer memory to host.
void *data = nullptr;
vkMapMemory(device->logical, uniformBuffer.memory, 0, VK_WHOLE_SIZE, 0, &data);
// Copy data to the buffer.
memcpy(data, mvpMatrix.data(), 16 * sizeof(float));
// Unmap memory.
vkUnmapMemory(device->logical, uniformBuffer.memory);
// Bind vertex and index bufffer.
VkDeviceSize vboOffsets[] = {0};
uint32_t iboOffset = vertices.size() * sizeof(VkVertex);
vkCmdBindVertexBuffers(commandBuffer, 0, 1, &buffer.handle, vboOffsets);
vkCmdBindIndexBuffer(commandBuffer, buffer.handle, iboOffset, VK_INDEX_TYPE_UINT32);
// Draw entity.
vkCmdDrawIndexed(commandBuffer, indices.size(), 1, 0, 0, 0);
}