-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStaticMesh.cpp
More file actions
127 lines (98 loc) · 3.94 KB
/
StaticMesh.cpp
File metadata and controls
127 lines (98 loc) · 3.94 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
//
// Copyright (c) 2020-present Caps Collective & contributors
// Originally authored by Jonathan Moallem (@jonjondev) & Aryeh Zinn (@Raelr)
//
// This code is released under an unmodified zlib license.
// For conditions of distribution and use, please see:
// https://opensource.org/licenses/Zlib
//
#include "StaticMesh.h"
#include <resources/PackFile.h>
#include <resources/ResourceSystem.h>
#include <resources/StaticMeshData.h>
#include <utils/Logging.h>
#include "Swapchain.h"
namespace Siege::Vulkan
{
StaticMesh::StaticMesh(unsigned int vertexBufferSize,
unsigned int vertCount,
unsigned int indexBufferSize,
unsigned int idxCount,
unsigned int subMeshCount) :
vertexCount {vertCount},
indexCount {idxCount}
{
CC_ASSERT(vertexCount > 0, "Cannot load in a file with no vertices!")
CC_ASSERT(indexCount > 0, "Cannot load in a file with no indices!")
CC_ASSERT(vertexCount < MAX_VERTICES, "The provided model has too many vertices!")
CC_ASSERT(indexCount < MAX_INDICES, "The provided model has too many indices!")
vertexBuffer = VertexBuffer(vertexBufferSize);
indexBuffer = IndexBuffer(indexBufferSize);
subMeshes = MHArray<SubMesh>(subMeshCount);
materials = MHArray<Material*>(subMeshCount);
}
StaticMesh::StaticMesh(const char* filePath, Material* material)
{
// TODO(Aryeh): How to extract material data from object files?
PackFile* packFile = ResourceSystem::GetInstance().GetPackFile();
std::shared_ptr<StaticMeshData> staticMeshData =
packFile->FindDataDeserialised<StaticMeshData>(filePath);
CC_ASSERT(!staticMeshData->vertices.empty(), "Cannot load in a file with no vertices!")
CC_ASSERT(!staticMeshData->indices.empty(), "Cannot load in a file with no indices!")
CC_ASSERT(staticMeshData->vertices.size() < MAX_VERTICES,
"The provided model has too many vertices!")
CC_ASSERT(staticMeshData->indices.size() < MAX_INDICES,
"The provided model has too many indices!")
vertexCount = staticMeshData->vertices.size();
indexCount = staticMeshData->indices.size();
vertexBuffer = VertexBuffer(sizeof(BaseVertex) * vertexCount);
vertexBuffer.Copy(staticMeshData->vertices.data(), sizeof(BaseVertex) * vertexCount);
indexBuffer = IndexBuffer(sizeof(unsigned int) * indexCount);
indexBuffer.Copy(staticMeshData->indices.data(), sizeof(unsigned int) * indexCount);
subMeshes = MHArray<SubMesh>(1);
materials = MHArray<Material*>(1);
subMeshes.Append({0, 0, 0, vertexCount, indexCount});
materials.Append(material);
}
void StaticMesh::Swap(StaticMesh& other)
{
auto tmpPerFrameVertexBuffers = std::move(vertexBuffer);
auto tmpPerFrameIndexBuffers = std::move(indexBuffer);
auto tmpVertexCount = vertexCount;
auto tmpIndexCount = indexCount;
auto tmpSubmeshes = subMeshes;
auto tmpMaterials = materials;
vertexBuffer = std::move(other.vertexBuffer);
indexBuffer = std::move(other.indexBuffer);
vertexCount = other.vertexCount;
indexCount = other.indexCount;
subMeshes = other.subMeshes;
materials = other.materials;
other.vertexBuffer = std::move(tmpPerFrameVertexBuffers);
other.indexBuffer = std::move(tmpPerFrameIndexBuffers);
other.vertexCount = tmpVertexCount;
other.indexCount = tmpIndexCount;
other.subMeshes = tmpSubmeshes;
other.materials = tmpMaterials;
}
StaticMesh::~StaticMesh()
{
Free();
}
void StaticMesh::Free()
{
vertexBuffer.Free();
indexBuffer.Free();
}
void StaticMesh::Bind(CommandBuffer& commandBuffer, uint64_t offset)
{
vertexBuffer.Bind(commandBuffer, &offset);
}
void StaticMesh::BindIndexed(CommandBuffer& commandBuffer,
uint64_t vertexOffset,
uint64_t indexOffset)
{
Bind(commandBuffer, vertexOffset);
indexBuffer.Bind(commandBuffer, indexOffset);
}
} // namespace Siege::Vulkan