Fixed/added parsing of translation, rotation and scale from scene.

This commit is contained in:
Patrick 2024-09-30 23:37:17 +02:00
parent 3f62ad3928
commit cbc6c4cf79
3 changed files with 67 additions and 11 deletions

View File

@ -12,6 +12,9 @@ void loadCommonNodeProperties(const YAML::Node& yaml, SceneNode& node)
{ {
const YAML::Node& translationNode = yaml["translation"]; const YAML::Node& translationNode = yaml["translation"];
glm::vec3 translation = {0.f, 0.f, 0.f}; glm::vec3 translation = {0.f, 0.f, 0.f};
glm::vec3 pitchYawRoll = {0.f, 0.f, 0.f};
glm::vec3 scale = {1.f, 1.f, 1.f};
if (translationNode.IsDefined()) if (translationNode.IsDefined())
{ {
mijin::ensure(translationNode.IsSequence() && translationNode.size() == 3 mijin::ensure(translationNode.IsSequence() && translationNode.size() == 3
@ -22,7 +25,45 @@ void loadCommonNodeProperties(const YAML::Node& yaml, SceneNode& node)
translation.z = translationNode[2].as<float>(); translation.z = translationNode[2].as<float>();
} }
node.setTransform(Transform3D::make(translation)); if (const YAML::Node& rotationNode = yaml["rotation"]; rotationNode.IsDefined())
{
mijin::ensure(rotationNode.IsMap(), "Invalid scene YAML: node rotation must be a map.");
if (const YAML::Node& pitchNode = rotationNode["pitch"]; pitchNode.IsDefined())
{
mijin::ensure(pitchNode.IsScalar(), "Invalid scene YAML: node pitch must be a scalar.");
pitchYawRoll[0] = pitchNode.as<float>();
}
if (const YAML::Node& yawNode = rotationNode["yaw"]; yawNode.IsDefined())
{
mijin::ensure(yawNode.IsScalar(), "Invalid scene YAML: yaw pitch must be a scalar.");
pitchYawRoll[1] = yawNode.as<float>();
}
if (const YAML::Node& rollNode = rotationNode["roll"]; rollNode.IsDefined())
{
mijin::ensure(rollNode.IsScalar(), "Invalid scene YAML: node roll must be a scalar.");
pitchYawRoll[2] = rollNode.as<float>();
}
}
if (const YAML::Node& scaleNode = yaml["scale"]; scaleNode.IsDefined())
{
if (scaleNode.IsScalar())
{
scale = glm::vec3(scaleNode.as<float>());
}
else
{
mijin::ensure(scaleNode.IsSequence() && scaleNode.size() == 3
&& scaleNode[0].IsScalar() && scaleNode[1].IsScalar() && scaleNode[2].IsScalar(),
"Invalid scene YAML: node scale must be scalar or sequence of three scalars.");
scale = {
scaleNode[0].as<float>(),
scaleNode[1].as<float>(),
scaleNode[2].as<float>()
};
}
}
node.setTransform(Transform3D::make(translation, glm::quat(glm::radians(pitchYawRoll)), scale));
} }
std::unique_ptr<MeshNode> loadMeshNode(const YAML::Node& yaml) std::unique_ptr<MeshNode> loadMeshNode(const YAML::Node& yaml)

View File

@ -68,7 +68,10 @@ struct Transform3D
inline Transform3D Transform3D::make(const glm::vec3& translation, const glm::quat& rotation, const glm::vec3& scale) inline Transform3D Transform3D::make(const glm::vec3& translation, const glm::quat& rotation, const glm::vec3& scale)
{ {
return Transform3D{glm::translate(glm::mat4_cast(rotation) * glm::scale(glm::mat4(1), scale), translation)}; // return Transform3D{glm::translate(glm::mat4_cast(rotation) * glm::scale(scale), translation)};
return Transform3D{
glm::translate(translation) * glm::mat4_cast(rotation) * glm::scale(scale)
};
} }
void Transform3D::setTranslation(const glm::vec3& translation) void Transform3D::setTranslation(const glm::vec3& translation)

View File

@ -44,21 +44,33 @@ Mesh loadMesh(const mijin::PathReference& path)
for (int vertexIdx = 0; vertexIdx < 3; ++vertexIdx) for (int vertexIdx = 0; vertexIdx < 3; ++vertexIdx)
{ {
const tinyobj::index_t& index = shape.mesh.indices[indexOffset + vertexIdx]; const tinyobj::index_t& index = shape.mesh.indices[indexOffset + vertexIdx];
glm::vec3 normal;
glm::vec2 texcoord;
if (index.normal_index > -1)
{
normal = {
attrib.normals[3 * index.normal_index + 0],
attrib.normals[3 * index.normal_index + 1],
attrib.normals[3 * index.normal_index + 2]
};
}
if (index.texcoord_index > -1)
{
texcoord = {
attrib.texcoords[2 * index.texcoord_index + 0],
1.f - attrib.texcoords[2 * index.texcoord_index + 1] // obj UV is weird
};
}
mesh.vertices.push_back({ mesh.vertices.push_back({
.pos = { .pos = {
attrib.vertices[3 * index.vertex_index + 0], attrib.vertices[3 * index.vertex_index + 0],
attrib.vertices[3 * index.vertex_index + 1], attrib.vertices[3 * index.vertex_index + 1],
attrib.vertices[3 * index.vertex_index + 2] attrib.vertices[3 * index.vertex_index + 2]
}, },
.normal = { .normal = normal,
attrib.normals[3 * index.normal_index + 0], .texcoord = texcoord,
attrib.normals[3 * index.normal_index + 1],
attrib.normals[3 * index.normal_index + 2]
},
.texcoord = {
attrib.texcoords[2 * index.texcoord_index + 0],
1.f - attrib.texcoords[2 * index.texcoord_index + 1] // obj UV is weird
},
.color = { .color = {
attrib.colors[3 * index.vertex_index + 0], attrib.colors[3 * index.vertex_index + 0],
attrib.colors[3 * index.vertex_index + 1], attrib.colors[3 * index.vertex_index + 1],