diff --git a/private/sdl_gpu_test/scene/loader.cpp b/private/sdl_gpu_test/scene/loader.cpp index 21a9625..7fbb24f 100644 --- a/private/sdl_gpu_test/scene/loader.cpp +++ b/private/sdl_gpu_test/scene/loader.cpp @@ -12,6 +12,9 @@ void loadCommonNodeProperties(const YAML::Node& yaml, SceneNode& node) { const YAML::Node& translationNode = yaml["translation"]; 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()) { mijin::ensure(translationNode.IsSequence() && translationNode.size() == 3 @@ -22,7 +25,45 @@ void loadCommonNodeProperties(const YAML::Node& yaml, SceneNode& node) translation.z = translationNode[2].as(); } - 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(); + } + 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(); + } + 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(); + } + } + if (const YAML::Node& scaleNode = yaml["scale"]; scaleNode.IsDefined()) + { + if (scaleNode.IsScalar()) + { + scale = glm::vec3(scaleNode.as()); + } + 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(), + scaleNode[1].as(), + scaleNode[2].as() + }; + } + } + + node.setTransform(Transform3D::make(translation, glm::quat(glm::radians(pitchYawRoll)), scale)); } std::unique_ptr loadMeshNode(const YAML::Node& yaml) diff --git a/private/sdl_gpu_test/scene/transform3d.hpp b/private/sdl_gpu_test/scene/transform3d.hpp index c0653c7..b3773d5 100644 --- a/private/sdl_gpu_test/scene/transform3d.hpp +++ b/private/sdl_gpu_test/scene/transform3d.hpp @@ -68,7 +68,10 @@ struct Transform3D 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) diff --git a/private/sdl_gpu_test/util/mesh.cpp b/private/sdl_gpu_test/util/mesh.cpp index c2850dc..f7be770 100644 --- a/private/sdl_gpu_test/util/mesh.cpp +++ b/private/sdl_gpu_test/util/mesh.cpp @@ -44,21 +44,33 @@ Mesh loadMesh(const mijin::PathReference& path) for (int vertexIdx = 0; vertexIdx < 3; ++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({ .pos = { attrib.vertices[3 * index.vertex_index + 0], attrib.vertices[3 * index.vertex_index + 1], attrib.vertices[3 * index.vertex_index + 2] }, - .normal = { - attrib.normals[3 * index.normal_index + 0], - 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 - }, + .normal = normal, + .texcoord = texcoord, .color = { attrib.colors[3 * index.vertex_index + 0], attrib.colors[3 * index.vertex_index + 1],