Support glTF meshes without UVs

This commit is contained in:
Bailey Harrison 2024-01-31 15:53:30 +00:00
parent a686371339
commit 69802afb50
3 changed files with 23 additions and 14 deletions

View File

@ -136,6 +136,9 @@ static void CreateNodes(engine::Scene& app_scene, const tg::Scene& gl_scene, con
const size_t norm_bytestride = norm_accessor.ByteStride(norm_bufferview);
const tg::Buffer& norm_buffer = gl_model.buffers.at(norm_bufferview.buffer);
std::vector<engine::Vertex> vertices(pos_accessor.count);
if (prim.attributes.contains("TEXCOORD_0")) {
const tg::Accessor& uv_accessor = gl_model.accessors.at(prim.attributes.at("TEXCOORD_0"));
if (uv_accessor.componentType != TINYGLTF_COMPONENT_TYPE_FLOAT) throw std::runtime_error("UV att. must be float!");
if (uv_accessor.type != 2) throw std::runtime_error("UV att. dim. must be 2!");
@ -144,18 +147,20 @@ static void CreateNodes(engine::Scene& app_scene, const tg::Scene& gl_scene, con
const size_t uv_bytestride = uv_accessor.ByteStride(uv_bufferview);
const tg::Buffer& uv_buffer = gl_model.buffers.at(uv_bufferview.buffer);
for (size_t i = 0; i < vertices.size(); ++i) {
vertices[i].uv = *reinterpret_cast<const glm::vec2*>(&uv_buffer.data[uv_byteoffset + uv_bytestride * i]);
}
}
bool has_tangents = false;
if (prim.attributes.contains("TANGENT")) {
has_tangents = true;
}
std::vector<engine::Vertex> vertices(pos_accessor.count);
// copy everything except tangents
// copy everything except tangents and uvs
for (size_t i = 0; i < vertices.size(); ++i) {
vertices[i].pos = *reinterpret_cast<const glm::vec3*>(&pos_buffer.data[pos_byteoffset + pos_bytestride * i]);
vertices[i].norm = *reinterpret_cast<const glm::vec3*>(&norm_buffer.data[norm_byteoffset + norm_bytestride * i]);
vertices[i].uv = *reinterpret_cast<const glm::vec2*>(&uv_buffer.data[uv_byteoffset + uv_bytestride * i]);
}
if (has_tangents) {
@ -176,12 +181,14 @@ static void CreateNodes(engine::Scene& app_scene, const tg::Scene& gl_scene, con
engine::Vertex* vertices;
const uint32_t* indices;
size_t count;
std::vector<uint32_t> new_indices;
};
MeshData meshData{};
meshData.vertices = vertices.data();
meshData.indices = indices->data();
meshData.count = indices->size();
meshData.new_indices.reserve(meshData.count);
SMikkTSpaceInterface mts_interface{};
mts_interface.m_getNumFaces = [](const SMikkTSpaceContext* pContext) -> int {

BIN
test/res/models/walls.glb Normal file

Binary file not shown.

View File

@ -175,7 +175,7 @@ void PlayGame(GameSettings settings)
engine::Entity wall2 = scene2->CreateEntity("wall2", pivot, glm::vec3{-50.0f, -50.0f, 0.0f});
auto wall_renderable = scene2->AddComponent<engine::MeshRenderableComponent>(wall2);
wall_renderable->mesh = GenCuboidMesh(app.renderer()->GetDevice(), 100.0f, 100.0f, 0.1f, 100.0f);
wall_renderable->material = std::make_unique<engine::Material>(app.renderer(), app.GetResource<engine::Shader>("builtin.standard"));
wall_renderable->material = std::make_unique<engine::Material>(app.renderer(), app.GetResource<engine::Shader>("builtin.fancy"));
std::shared_ptr<engine::Texture> albedo_texture =
engine::LoadTextureFromFile(app.GetResourcePath("textures/brickwall_albedo.jpg"), engine::Texture::Filtering::kAnisotropic, app.renderer());
@ -198,8 +198,10 @@ void PlayGame(GameSettings settings)
scene2->GetComponent<engine::TransformComponent>(teapot)->scale *= 10.0f;
auto teapot2 = engine::util::LoadGLTF(*scene2, app.GetResourcePath("models/teapot.glb"));
scene2->GetComponent<engine::TransformComponent>(teapot2)->scale *= 10.0f;
scene2->GetComponent<engine::TransformComponent>(teapot2)->position.y += 10.0f;
scene2->GetComponent<engine::TransformComponent>(teapot2)->rotation = glm::angleAxis(glm::half_pi<float>(), glm::vec3{1.0f, 0.0f, 0.0f});
scene2->GetComponent<engine::TransformComponent>(teapot2)->position.y += 5.0f;
scene2->GetComponent<engine::TransformComponent>(teapot2)->rotation = glm::angleAxis(glm::pi<float>(), glm::vec3{ 0.0f, 0.0f, 1.0f });
scene2->GetComponent<engine::TransformComponent>(teapot2)->rotation *= glm::angleAxis(glm::half_pi<float>(), glm::vec3{1.0f, 0.0f, 0.0f});
auto walls = engine::util::LoadGLTF(*scene2, app.GetResourcePath("models/walls.glb"));
}
my_scene->GetSystem<CameraControllerSystem>()->next_scene_ = scene2;