-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStaticMesh.cpp
More file actions
111 lines (105 loc) · 4.33 KB
/
StaticMesh.cpp
File metadata and controls
111 lines (105 loc) · 4.33 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
#include "StaticMesh.h"
std::vector<VkVertexInputBindingDescription> StaticMesh::mVertexInputBindingDescriptions;
std::vector<VkVertexInputAttributeDescription> StaticMesh::mVertexInputAttributeDescriptions;
void StaticMesh::Init() {
mVertexInputBindingDescriptions.resize(1);
mVertexInputBindingDescriptions[0] = {};
mVertexInputBindingDescriptions[0].binding = 0;//0 slot ->
mVertexInputBindingDescriptions[0].inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
mVertexInputBindingDescriptions[0].stride = 4 * 4 * sizeof(float);
mVertexInputAttributeDescriptions.resize(4);
mVertexInputAttributeDescriptions[0].binding = 0;
mVertexInputAttributeDescriptions[0].location = 0;
mVertexInputAttributeDescriptions[0].format = VK_FORMAT_R32G32B32A32_SFLOAT;//vec4
mVertexInputAttributeDescriptions[0].offset = 0;
mVertexInputAttributeDescriptions[1].binding = 0;
mVertexInputAttributeDescriptions[1].location = 1;
mVertexInputAttributeDescriptions[1].format = VK_FORMAT_R32G32B32A32_SFLOAT;//vec4
mVertexInputAttributeDescriptions[1].offset = sizeof(float) * 4;
mVertexInputAttributeDescriptions[2].binding = 0;
mVertexInputAttributeDescriptions[2].location = 2;
mVertexInputAttributeDescriptions[2].format = VK_FORMAT_R32G32B32A32_SFLOAT;//vec4
mVertexInputAttributeDescriptions[2].offset = sizeof(float) * 8;
mVertexInputAttributeDescriptions[3].binding = 0;//slot
mVertexInputAttributeDescriptions[3].location = 3;
mVertexInputAttributeDescriptions[3].format = VK_FORMAT_R32G32B32A32_SFLOAT;//vec4
mVertexInputAttributeDescriptions[3].offset = sizeof(float) * 12;
}
void StaticMesh::SetVertexCount(int inVertexCount) {
mVertexCount = inVertexCount;
mVertexData = new StaticMeshVertexData[inVertexCount];
}
void StaticMesh::SetPosition(int inIndex, float inX, float inY, float inZ, float inW ) {
mVertexData[inIndex].mPosition.x = inX;
mVertexData[inIndex].mPosition.y = inY;
mVertexData[inIndex].mPosition.z = inZ;
mVertexData[inIndex].mPosition.w = inW;
}
void StaticMesh::SetTexcoord(int inIndex, float inX, float inY, float inZ, float inW) {
mVertexData[inIndex].mTexcoord.x = inX;
mVertexData[inIndex].mTexcoord.y = inY;
mVertexData[inIndex].mTexcoord.z = inZ;
mVertexData[inIndex].mTexcoord.w = inW;
}
void StaticMesh::SetNormal(int inIndex, float inX, float inY, float inZ, float inW) {
mVertexData[inIndex].mNormal.x = inX;
mVertexData[inIndex].mNormal.y = inY;
mVertexData[inIndex].mNormal.z = inZ;
mVertexData[inIndex].mNormal.w = inW;
}
void StaticMesh::SetTangent(int inIndex, float inX, float inY, float inZ, float inW) {
mVertexData[inIndex].mTangent.x = inX;
mVertexData[inIndex].mTangent.y = inY;
mVertexData[inIndex].mTangent.z = inZ;
mVertexData[inIndex].mTangent.w = inW;
}
void StaticMesh::InitFromFile(const char* inFilePath) {
FILE* pFile = nullptr;
fopen_s(&pFile,inFilePath, "rb");
if (pFile != nullptr) {
int temp = 0;
fread(&temp, 4, 1, pFile);
mVertexCount = temp;
mVertexData = new StaticMeshVertexData[temp];
fread(mVertexData, 1, sizeof(StaticMeshVertexData) * temp, pFile);
mVBO = GenBufferObject(
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
mVertexData, sizeof(StaticMeshVertexData) * mVertexCount);
while (!feof(pFile)) {
fread(&temp, 4, 1, pFile);
if (feof(pFile)) {
break;
}
char name[256] = {0};
fread(name, 1, temp, pFile);
fread(&temp, 4, 1, pFile);
SubMesh* submesh = new SubMesh;
submesh->mIndexCount = temp;
submesh->mIndexes = new unsigned int[temp];
fread(submesh->mIndexes, 1, sizeof(unsigned int) * temp, pFile);
submesh->mIBO = GenBufferObject(VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, submesh->mIndexes,
sizeof(unsigned int) * submesh->mIndexCount);
mSubMeshes.insert(std::pair<std::string, SubMesh*>(name, submesh));
}
fclose(pFile);
}
}
void StaticMesh::Draw(VkCommandBuffer inCommandBuffer) {
VkDeviceSize offsets[] = { 0 };
vkCmdBindVertexBuffers(inCommandBuffer, 0, 1, &mVBO->mBuffer, offsets);
if (false == mSubMeshes.empty()) {
auto iter = mSubMeshes.begin();
auto iterEnd = mSubMeshes.end();
while (iter!=iterEnd)
{
SubMesh* submesh = iter->second;
vkCmdBindIndexBuffer(inCommandBuffer, submesh->mIBO->mBuffer, 0, VK_INDEX_TYPE_UINT32);
vkCmdDrawIndexed(inCommandBuffer, submesh->mIndexCount, 1, 0, 0, 0);
iter++;
}
}
else {
vkCmdDraw(inCommandBuffer, mVertexCount, 1, 0, 0);
}
}