-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDrawable.cpp
213 lines (180 loc) · 6.67 KB
/
Drawable.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
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
#include "Drawable.h"
#define TINYOBJLOADER_IMPLEMENTATION
#include <tiny_obj_loader.h>
namespace MelonRenderer {
bool Drawable::LoadMeshData(DeviceMemoryManager& memoryManager, const std::string& path)
{
tinyobj::attrib_t attributes;
std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials;
std::string warnings, errors;
if (!tinyobj::LoadObj(&attributes, &shapes, &materials, &warnings, &errors, path.c_str(), "models/"))
{
Logger::Log(warnings + errors);
//return false;
}
if (materials.size())
{
for (int i = 0; i < materials.size(); i++)
{
WaveFrontMaterial material = {};
material.ambient = glm::make_vec3(materials[i].ambient);
material.diffuse = glm::make_vec3(materials[i].diffuse);
material.specular = glm::make_vec3(materials[i].specular);
material.transmittance = glm::make_vec3(materials[i].transmittance);
material.emission = glm::make_vec3(materials[i].emission);
material.shininess = materials[i].shininess;
material.indexOfRefraction = materials[i].ior;
material.dissolve = materials[i].dissolve;
material.illum = materials[i].illum;
if (materials[i].diffuse_texname.empty())
{
material.textureId = memoryManager.CreateTextureID("textureDefault.jpg");
}
else
{
//lookup if texture already exists, create if not, return texture id
material.textureId = memoryManager.CreateTextureID(materials[i].diffuse_texname.c_str());
}
m_materials.emplace_back(material);
}
}
else
{
//default material
WaveFrontMaterial material = {};
material.textureId = memoryManager.CreateTextureID("textureDefault.jpg");
m_materials.emplace_back(material);
}
//to make use of indices, we need to ignore duplicates
std::unordered_map<Vertex, uint32_t> uniqueVertices;
for (const auto& shape : shapes) {
uint32_t faceID = 0;
uint32_t indexCount = 0;
for (const auto& index : shape.mesh.indices) {
Vertex vertex = {};
vertex.posX = attributes.vertices[3 * index.vertex_index];
vertex.posY = attributes.vertices[3 * index.vertex_index + 1];
vertex.posZ = attributes.vertices[3 * index.vertex_index + 2];
if (!attributes.normals.empty())
{
vertex.normalX = attributes.normals[3 * index.normal_index];
vertex.normalY = attributes.normals[3 * index.normal_index + 1];
vertex.normalZ = attributes.normals[3 * index.normal_index + 2];
}
if (!attributes.texcoords.empty() && index.texcoord_index >= 0)
{
vertex.u = attributes.texcoords[2 * index.texcoord_index + 0];
vertex.v = attributes.texcoords[2 * index.texcoord_index + 1]; //flip the coord with 1.f - ?
}
else
{
vertex.u = 0.f;
vertex.v = 0.f;
}
vertex.matID = shape.mesh.material_ids[faceID];
indexCount++;
if (indexCount == 3) //every 3 vertices, one face
{
faceID++;
indexCount = 0;
}
if (uniqueVertices.count(vertex) == 0) {
uniqueVertices[vertex] = static_cast<uint32_t>(m_vertices.size());
m_vertices.push_back(vertex);
}
m_indices.push_back(uniqueVertices[vertex]);
}
}
// some objs do not come with normals
if (attributes.normals.empty())
{
for (size_t i = 0; i < m_indices.size(); i += 3)
{
Vertex& v0 = m_vertices[m_indices[i + 0]];
Vertex& v1 = m_vertices[m_indices[i + 1]];
Vertex& v2 = m_vertices[m_indices[i + 2]];
glm::vec3 normal = glm::normalize(glm::cross(
(vec3(v1.posX, v1.posY, v1.posZ) - vec3(v0.posX, v0.posY, v0.posZ)),
(vec3(v2.posX, v2.posY, v2.posZ) - vec3(v0.posX, v0.posY, v0.posZ))));
v0.normalX = normal.x;
v0.normalY = normal.y;
v0.normalZ = normal.z;
v1.normalX = normal.x;
v1.normalY = normal.y;
v1.normalZ = normal.z;
v2.normalX = normal.x;
v2.normalY = normal.y;
v2.normalZ = normal.z;
}
}
return true;
}
bool Drawable::Init(DeviceMemoryManager& memoryManager)
{
uint32_t vertexBufferSize = sizeof(cube_vertex_data);
if (!memoryManager.CreateOptimalBuffer(m_vertexBuffer, m_vertexBufferMemory, cube_vertex_data, vertexBufferSize,
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT))
{
Logger::Log("Could not create vertex buffer.");
return false;
}
uint32_t indexBufferSize = sizeof(cube_index_data);
if (!memoryManager.CreateOptimalBuffer(m_indexBuffer, m_indexBufferMemory, cube_index_data, indexBufferSize,
VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT))
{
Logger::Log("Could not create index buffer.");
return false;
}
//default cube material
WaveFrontMaterial material = {};
m_materials.emplace_back(material);
uint32_t materialBuffersize = m_materials.size() * sizeof(WaveFrontMaterial);
if (!memoryManager.CreateOptimalBuffer(m_materialBuffer, m_materialBufferMemory, m_materials.data(), materialBuffersize,
VK_BUFFER_USAGE_STORAGE_BUFFER_BIT))
{
Logger::Log("Could not create material buffer.");
return false;
}
m_vertexCount = sizeof(cube_vertex_data) / sizeof(Vertex);
m_indexCount = sizeof(cube_index_data) / sizeof(uint32_t);
return true;
}
bool Drawable::Init(DeviceMemoryManager& memoryManager, const std::string& path)
{
LoadMeshData(memoryManager, path);
uint32_t vertexBufferSize = sizeof(Vertex) * m_vertices.size();
if (!memoryManager.CreateOptimalBuffer(m_vertexBuffer, m_vertexBufferMemory, m_vertices.data(), vertexBufferSize,
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT))
{
Logger::Log("Could not create vertex buffer.");
return false;
}
uint32_t indexBufferSize = sizeof(uint32_t) * m_indices.size();
if (!memoryManager.CreateOptimalBuffer(m_indexBuffer, m_indexBufferMemory, m_indices.data(), indexBufferSize,
VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT))
{
Logger::Log("Could not create index buffer.");
return false;
}
uint32_t materialBuffersize = m_materials.size() * sizeof(WaveFrontMaterial);
if (!memoryManager.CreateOptimalBuffer(m_materialBuffer, m_materialBufferMemory, m_materials.data(), materialBuffersize,
VK_BUFFER_USAGE_STORAGE_BUFFER_BIT))
{
Logger::Log("Could not create material buffer.");
return false;
}
m_vertexCount = m_vertices.size();
m_indexCount = m_indices.size();
return true;
}
void Drawable::Fini()
{
vkFreeMemory(Device::Get().m_device, m_indexBufferMemory, nullptr);
vkFreeMemory(Device::Get().m_device, m_vertexBufferMemory, nullptr);
vkFreeMemory(Device::Get().m_device, m_materialBufferMemory, nullptr);
vkDestroyBuffer(Device::Get().m_device, m_indexBuffer, nullptr);
vkDestroyBuffer(Device::Get().m_device, m_vertexBuffer, nullptr);
vkDestroyBuffer(Device::Get().m_device, m_materialBuffer, nullptr);
}
}