光线追踪对象文件漫反射着色问题

Ray tracing obj files diffuse shading issue

本文关键字:问题 漫反射 对象 文件 光线追踪      更新时间:2023-10-16

The repository (GitHub)

我的模型的漫反射着色有问题(渲染基元时不会出现此问题。

这里需要注意的有趣之处在于,我认为当您查看左侧反射球体时,阴影看起来很正常(根据观察,我可能错了)。

低聚兔子和三角形

低聚兔子和 2 个反射球

立方体和 2 个反射球

我不确定我做错了什么,因为每次我在构造函数中创建三角形时都会计算法线。我正在使用tinyobjloader来加载我的模型,这是TriangleMesh交叉算法。

FPType TriangleMesh::GetIntersection(const Ray &ray)
{
for(auto &shape : shapes)
{
size_t index_offset = 0;
for(size_t f = 0; f < shape.mesh.num_face_vertices.size(); ++f) // faces (triangles)
{
int fv = shape.mesh.num_face_vertices[f];
tinyobj::index_t &idx0 = shape.mesh.indices[index_offset + 0]; // v0
tinyobj::index_t &idx1 = shape.mesh.indices[index_offset + 1]; // v1
tinyobj::index_t &idx2 = shape.mesh.indices[index_offset + 2]; // v2
Vec3d &v0 = Vec3d(attrib.vertices[3 * idx0.vertex_index + 0], attrib.vertices[3 * idx0.vertex_index + 1], attrib.vertices[3 * idx0.vertex_index + 2]);
Vec3d &v1 = Vec3d(attrib.vertices[3 * idx1.vertex_index + 0], attrib.vertices[3 * idx1.vertex_index + 1], attrib.vertices[3 * idx1.vertex_index + 2]);
Vec3d &v2 = Vec3d(attrib.vertices[3 * idx2.vertex_index + 0], attrib.vertices[3 * idx2.vertex_index + 1], attrib.vertices[3 * idx2.vertex_index + 2]);
Triangle tri(v0, v1, v2);
if(tri.GetIntersection(ray))
return tri.GetIntersection(ray);
index_offset += fv;
}
}
}

三角形交集算法。

FPType Triangle::GetIntersection(const Ray &ray)
{
Vector3d v0v1 = v1 - v0;
Vector3d v0v2 = v2 - v0;
Vector3d pvec = ray.GetDirection().Cross(v0v2);
FPType det = v0v1.Dot(pvec);
// ray and triangle are parallel if det is close to 0
if(abs(det) < BIAS)
return false;
FPType invDet = 1 / det;
FPType u, v;
Vector3d tvec = ray.GetOrigin() - v0;
u = tvec.Dot(pvec) * invDet;
if(u < 0 || u > 1)
return false;
Vector3d qvec = tvec.Cross(v0v1);
v = ray.GetDirection().Dot(qvec) * invDet;
if(v < 0 || u + v > 1)
return false;
FPType t = v0v2.Dot(qvec) * invDet;
if(t < BIAS)
return false;
return t;
}

我认为这是因为当我处理所有对象交集时,三角形网格仅被视为 1 个对象,因此当我尝试获取对象法线时,它只返回 1 个法线:代码

Color Trace(const Vector3d &origin, const Vector3d &direction, const std::vector<std::shared_ptr<Object>> &sceneObjects, const int indexOfClosestObject,
const std::vector<std::shared_ptr<Light>> &lightSources, const int &depth = 0)
{
if(indexOfClosestObject != -1 && depth <= DEPTH) // not checking depth for infinite mirror effect (not a lot of overhead)
{
std::shared_ptr<Object> sceneObject = sceneObjects[indexOfClosestObject];
Vector3d normal = sceneObject->GetNormalAt(origin);

调试的屏幕截图

编辑:我已经解决了这个问题,现在阴影工作正常:https://github.com/MrCappuccino/Tracey/blob/testing/src/TriangleMesh.cpp#L35-L48

如果你迭代所有面并返回你击中的第一个面,你可能会碰到在其他面后面的面,因此不是你想击中的脸,所以你必须测量你的射线的长度并返回最短光线的交点。