Compare commits

...

3 Commits

8 changed files with 4871 additions and 14 deletions

4790
assets/meshes/teddy.obj Normal file

File diff suppressed because it is too large Load Diff

BIN
assets/meshes/teddy.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 547 B

View File

@ -1,7 +1,18 @@
nodes:
- type: mesh
translation: [0, 0, 0]
rotation:
pitch: 45
roll: 45
mesh: meshes/cube.obj
- type: mesh
translation: [1, 0, -10]
rotation:
yaw: 45
mesh: meshes/cube.obj
- type: mesh
translation: [1, 0, -5]
rotation:
yaw: 90
scale: 0.1
mesh: meshes/teddy.obj

View File

@ -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<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)

View File

@ -88,7 +88,7 @@ void SceneRenderer::render(const SceneRendererRenderArgs& args)
-args.camera.position
),
.viewToClip = glm::perspectiveFov(
/* fov = */ glm::radians(90.f),
/* fov = */ args.camera.fovh,
/* width = */ static_cast<float>(args.targetTextureWidth),
/* height = */ static_cast<float>(args.targetTextureHeight),
/* zNear = */ args.camera.zNear,

View File

@ -33,7 +33,7 @@ struct CameraOptions
float pitch = 0.f;
float yaw = 0.f;
float fovh = glm::radians(90.f);
float zNear = 0.f;
float zNear = 0.1f;
float zFar = 100.f;
};

View File

@ -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)

View File

@ -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],