You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// TODO Lab: 1.03 Using `tinyobjloader` implement `load_obj`, `allocate_buffers`, `compute_normal`, `fill_vertex_data`, and `fill_buffers` methods of `cg::world::model` class
20
+
// Парсинг OBJ/MTL
21
+
tinyobj::attrib_t attrib;
22
+
std::vector<tinyobj::shape_t> shapes;
23
+
std::vector<tinyobj::material_t> materials;
24
+
std::string warn, err;
25
+
26
+
std::filesystem::path base = model_path.parent_path();
// TODO Lab: 1.03 Using `tinyobjloader` implement `load_obj`, `allocate_buffers`, `compute_normal`, `fill_vertex_data`, and `fill_buffers` methods of `cg::world::model` class
54
+
vertex_buffers.clear();
55
+
index_buffers.clear();
56
+
textures.clear();
57
+
vertex_buffers.reserve(shapes.size());
58
+
index_buffers.reserve(shapes.size());
59
+
textures.reserve(shapes.size());
60
+
61
+
for (constauto& s : shapes)
62
+
{
63
+
// Оценить верхнюю границу количества вершин по количеству индексов
64
+
// (будет один к одному после развёртки индексов).
// TODO Lab: 1.03 Using `tinyobjloader` implement `load_obj`, `allocate_buffers`, `compute_normal`, `fill_vertex_data`, and `fill_buffers` methods of `cg::world::model` class
30
-
return float3{};
81
+
// Вычисление геометрической нормали треугольника по 3 вершинам (позициям)
82
+
// index_offset указывает на начало треугольника в mesh.indices (индекс a).
83
+
const tinyobj::index_t ia = mesh.indices[index_offset + 0];
// TODO Lab: 1.03 Using `tinyobjloader` implement `load_obj`, `allocate_buffers`, `compute_normal`, `fill_vertex_data`, and `fill_buffers` methods of `cg::world::model` class
114
+
// Позиция
115
+
{
116
+
size_t vi = static_cast<size_t>(idx.vertex_index);
117
+
vertex.position = float3{
118
+
attrib.vertices[3 * vi + 0],
119
+
attrib.vertices[3 * vi + 1],
120
+
attrib.vertices[3 * vi + 2]
121
+
};
122
+
}
123
+
124
+
// Нормаль: берем из атрибутов, если есть; иначе — скомпьютенную
125
+
if (idx.normal_index >= 0) {
126
+
size_t ni = static_cast<size_t>(idx.normal_index);
127
+
vertex.normal = float3{
128
+
attrib.normals[3 * ni + 0],
129
+
attrib.normals[3 * ni + 1],
130
+
attrib.normals[3 * ni + 2]
131
+
};
132
+
} else {
133
+
vertex.normal = computed_normal;
134
+
}
135
+
136
+
// UV: если есть
137
+
if (idx.texcoord_index >= 0) {
138
+
size_t ti = static_cast<size_t>(idx.texcoord_index);
// TODO Lab: 1.03 Using `tinyobjloader` implement `load_obj`, `allocate_buffers`, `compute_normal`, `fill_vertex_data`, and `fill_buffers` methods of `cg::world::model` class
152
+
for (size_t s = 0; s < shapes.size(); ++s)
153
+
{
154
+
constauto& shape = shapes[s];
155
+
constauto& mesh = shape.mesh;
156
+
157
+
// Привязанные ресурсы для шейпа
158
+
auto vb = vertex_buffers[s];
159
+
auto ib = index_buffers[s];
160
+
161
+
// Индексная запись линейная: на каждый tinyobj индекс — уникальная вершина в нашем VB
162
+
size_t write = 0;
163
+
for (size_t f = 0; f < mesh.num_face_vertices.size(); ++f)
0 commit comments