【发布时间】:2017-08-16 16:46:09
【问题描述】:
我的模型的漫反射着色存在问题(渲染基元时不会出现。
这里要注意的是,我认为当你看左边的反射球时,阴影看起来是正常的(根据观察,我可能错了)。
Low poly bunny and 2 reflective spheres
我不确定我做错了什么,因为每次我在构造函数中创建一个三角形时都会计算法线。我正在使用 tinyobjloader 加载模型,这里是 the TriangleMesh intersection algorithm。
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;
}
}
}
The Triangle Intersection algorithm.
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 个法线:code
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
【问题讨论】:
-
偷偷编辑!只是不要立即从
TriangleMesh::GetIntersection返回,而是计算你的命中距离,如果距离小于你当前保存的命中,只记住你当前的命中并在最后返回。 -
@Darklighter 你的意思是我应该循环遍历所有三角形并且只返回最近的一个inside TriangleMesh::GetIntersection?
-
c++ 中有没有办法查看一个对象是三角形网格还是三角形?
-
是的,否则你不会得到你真正想要的交叉点,不是吗?为什么要知道一个对象是
TriangleMesh还是Triangle? (通常不会,不)
标签: c++ graphics 3d rendering raytracing