【发布时间】:2016-03-31 04:37:17
【问题描述】:
我正在制作双向路径追踪器,但遇到了一些麻烦。
要清楚: 1) 单点光源 2)所有物体都是漫反射的 3)所有物体都是球体,甚至是墙壁(它们非常大) 4) 没有失重
光发射是一个 3D 矢量。球体的 BRDF 是一个 3D 矢量。硬编码。
在下面的主函数中,我生成 EyePath 和 LightPath,然后将它们连接起来。至少我会尝试。
在这篇文章中,我将讨论主要功能,然后是 EyePath,然后是 LightPath。 EyePath 和 Light 设置好后,会出现关于连接的功能。
- 第一个问题:
- 第一个光点的生成好不好?
- 我需要根据光源的发射来计算这个点吗?还是只是排放?该行在我填充顶点结构的位置被注释。
- 我需要翻译 fromlight 吗?为了把它放在球体上
下面的代码是在 main 函数中采样的。在它上面有两个遍历所有像素的 for 循环。 Camera.o 是眼睛。 CameraRayDir 是当前像素的方向。
//The path light starting point is at the same position as the light
Ray fromLight(Vec(0, 24.3, 0), Vec());
Sphere light = spheres[7];
#define PDF 0.15915494309 // 1 / (2 * PI)
for(int i = 0; i < samps; ++i)
{
std::vector<Vertices> PathEye;
std::vector<Vertices> PathLight;
Vec cameraRayDir = cx * (double(x) / w - .5) + cy * (double(y) / h - .5) + camera.d;
Ray rayEye(camera.o, cameraRayDir.norm());
// Hemisphere oriented towards the top
fromLight.d = generateRayInHemisphere(fromLight.o,Vec(0,1,0)).d;
double f = clamp(n.dot(fromLight.d.norm()));
Vertices vert;
vert.d = fromLight.d;
vert.x = fromLight.o;
vert.id = 7;
vert.cos = f;
vert.n = Vec(0,1,0).norm();
// this one ?
//vert.couleur = spheres[7].e * f / PDF;
// Or this one ?
vert.couleur = spheres[7].e;
PathLight.push_back(vert);
int sizeEye = generateEyePath(PathEye, rayEye, maxDepth);
int sizeLight = generateLightPath(PathLight, fromLight, maxDepth);
for (int s = 0; s < sizeLight; ++s)
{
for (int t = 1; t < sizeEye; ++t)
{
int depth = t + s - 1;
if ((s == 0 && t == 0) || depth < 0 || depth > maxDepth)
continue;
pixelValue = pixelValue + connectPaths(PathEye, PathLight, s, t);
}
}
}
对于 EyePath,我与几何体相交,然后根据与灯光的距离计算照明。如果点在阴影中,则颜色为黑色。
第二个问题:对于眼路和直接照明,计算好不好?我在许多代码中看到,人们甚至在直接照明下也使用 pdf。但我只使用点光源和球体。
int generateEyePath(std::vector<Vertices>& v, Ray eye, int maxDepth)
{
double t;
int id = 0;
Vertices vert;
int RussianRoulette;
while(v.size() <= maxDepth)
{
if(distribRREye(generatorRREye) < 10)
break;
// Intersect all the geometry
// id is the id of the intersected geometry in an array
intersect(eye, t, id);
const Sphere& obj = spheres[id];
// Intersection point
Vec x = eye.o + eye.d * t;
// normal
Vec n = (x - obj.p).norm();
Vec direction = light.p - x;
// Shadow ray
Ray RaytoLight = Ray(x, direction.norm());
const float distance = direction.length();
// shadow
const bool visibility = intersect(RaytoLight, t, id);
const Sphere &lumiere = spheres[id];
float degree = clamp(n.dot((lumiere.p - x).norm()));
// If the intersected geometry is not a light, then in shadow
if(lumiere.e.x == 0)
{
vert.couleur = Vec();
}
else // else we compute the colour
// obj.c is the brdf, lumiere.e is the emission
vert.couleur = (obj.c).mult(lumiere.e / (distance * distance)) * degree;
vert.x = x;
vert.id = id;
vert.n = n;
vert.d = eye.d.normn();
vert.cos = degree;
v.push_back(vert);
eye = generateRayInHemisphere(x,n);
}
return v.size();
}
对于LightPath,对于给定的点,我根据前一个和此时的值计算它。就像在公共路径追踪中一样。\n
第三个问题:颜色计算好不好?
int generateLightPath(std::vector<Vertices>& v, Ray fromLight, int maxDepth)
{
double t;
int id = 0;
Vertices vert;
Vec previous;
while(v.size() <= maxDepth)
{
if(distribRRLight(generatorRRLight) < 10)
break;
previous = v.back().couleur;
intersect(fromLight, t, id);
// intersected geometry
const Sphere& obj = spheres[id];
// Intersection point
Vec x = fromLight.o + fromLight.d * t;
// normal
Vec n = (x - obj.p).norm();
double f = clamp(n.dot(fromLight.d.norm()));
// obj.c is the brdf
vert.couleur = previous.mult(((obj.c / M_PI) * f) / PDF);
vert.x = x;
vert.id = id;
vert.n = n;
vert.d = fromLight.d.norm();
vert.cos = f;
v.push_back(vert);
fromLight = generateRayInHemisphere(x,n);
}
return v.size();
}
目前我得到了这个结果。
一旦 EyePath 和 LightPath 都好,连接功能就来了。
谢谢大家
【问题讨论】:
标签: rendering raytracing