Kot*_*oto 19 c++ 3d graphics raytracing
我一直在研究我的光线跟踪器.我添加了反射和多线程支持.目前我正在努力增加折射,但它只有一半工作.

如您所见,有一个中心球体(没有镜面反射高光),一个反射球体(右侧)和一个折射球体(左侧).我很高兴反思,看起来非常好.对于折射它有点工作......光被折射并且球体的所有阴影都在球体中可见(折射率1.4),但是有一个外部黑色环.
编辑:当我增加球体的折射率时,显然黑色环变大,因此球体变小.相反,当降低折射率时,球体变大,黑色环变小......直到折射率设为1,环完全消失.IOR = 1.9
IOR = 1.1
IOR = 1.00001
有趣的是,在IOR = 1时,球体失去透明度并变白.

我认为我涵盖了全部内部反思,这不是问题所在.
现在的代码:我正在使用operator |for dot产品,因此(vec|vec)是一个点积和operator ~反转向量.物体,包括ligths和spheres都存储在Object **objects;.光线跟踪功能
Colour raytrace(const Ray &r, const int &depth)
{
//first find the nearest intersection of a ray with an object
Colour finalColour = skyBlue *(r.getDirection()|Vector(0,0,-1)) * SKY_FACTOR;
double t, t_min = INFINITY;
int index_nearObj = -1;
for(int i = 0; i < objSize; i++)
{
if(!dynamic_cast<Light *>(objects[i]))//skip light src
{
t = objects[i]->findParam(r);
if(t > 0 && t < t_min)
{
t_min = t;
index_nearObj = i;
}
}
}
//no intersection
if(index_nearObj < 0)
return finalColour;
Vector intersect = r.getOrigin() + r.getDirection()*t_min;
Vector normal = objects[index_nearObj]->NormalAtIntersect(intersect);
Colour objectColor = objects[index_nearObj]->getColor();
Ray rRefl, rRefr; //reflected and refracted Ray
Colour refl = finalColour, refr = finalColour; //reflected and refracted colours
double reflectance = 0, transmittance = 0;
if(objects[index_nearObj]->isReflective() && depth < MAX_TRACE_DEPTH)
{
//handle reflection
rRefl = objects[index_nearObj]->calcReflectingRay(r, intersect, normal);
refl = raytrace(rRefl, depth + 1);
reflectance = 1;
}
if(objects[index_nearObj]->isRefractive() && depth < MAX_TRACE_DEPTH)
{
//handle transmission
rRefr = objects[index_nearObj]->calcRefractingRay(r, intersect, normal, reflectance, transmittance);
refr = raytrace(rRefr, depth + 1);
}
Ray rShadow; //shadow ray
bool shadowed;
double t_light = -1;
Colour localColour;
Vector tmpv;
//get material properties
double ka = 0.2; //ambient coefficient
double kd; //diffuse coefficient
double ks; //specular coefficient
Colour ambient = ka * objectColor; //ambient component
Colour diffuse, specular;
double brightness;
localColour = ambient;
//look if the object is in shadow or light
//do this by casting a ray from the obj and
// check if there is an intersection with another obj
for(int i = 0; i < objSize; i++)
{
if(dynamic_cast<Light *>(objects[i])) //if object is a light
{
//for each light
shadowed = false;
//create Ray to light
tmpv = objects[i]->getPosition() - intersect;
rShadow = Ray(intersect + (!tmpv) * BIAS, tmpv);
t_light = objects[i]->findParam(rShadow);
if(t_light < 0) //no imtersect, which is quite impossible
continue;
//then we check if that Ray intersects one object that is not a light
for(int j = 0; j < objSize; j++)
{
if(!dynamic_cast<Light *>(objects[j]) && j != index_nearObj)//if obj is not a light
{
t = objects[j]->findParam(rShadow);
//if it is smaller we know the light is behind the object
//--> shadowed by this light
if (t >= 0 && t < t_light)
{
// Set the flag and stop the cycle
shadowed = true;
break;
}
}
}
if(!shadowed)
{
rRefl = objects[index_nearObj]->calcReflectingRay(rShadow, intersect, normal);
//reflected ray from ligh src, for ks
kd = maximum(0.0, (normal|rShadow.getDirection()));
if(objects[index_nearObj]->getShiny() <= 0)
ks = 0;
else
ks = pow(maximum(0.0, (r.getDirection()|rRefl.getDirection())), objects[index_nearObj]->getShiny());
diffuse = kd * objectColor;// * objects[i]->getColour();
specular = ks * objects[i]->getColor();
brightness = 1 /(1 + t_light * DISTANCE_DEPENDENCY_LIGHT);
localColour += brightness * (diffuse + specular);
}
}
}
finalColour = localColour + (transmittance * refr + reflectance * refl);
return finalColour;
}
Run Code Online (Sandbox Code Playgroud)
现在计算折射Ray的函数,我使用了几个不同的资源站点,每个站点都有类似的算法.这是迄今为止我能做的最好的事情.它可能只是我看不到的一个小细节......
Ray Sphere::calcRefractingRay(const Ray &r, const Vector &intersection,Vector &normal, double & refl, double &trans)const
{
double n1, n2, n;
double cosI = (r.getDirection()|normal);
if(cosI > 0.0)
{
n1 = 1.0;
n2 = getRefrIndex();
normal = ~normal;//invert
}
else
{
n1 = getRefrIndex();
n2 = 1.0;
cosI = -cosI;
}
n = n1/n2;
double sinT2 = n*n * (1.0 - cosI * cosI);
double cosT = sqrt(1.0 - sinT2);
//fresnel equations
double rn = (n1 * cosI - n2 * cosT)/(n1 * cosI + n2 * cosT);
double rt = (n2 * cosI - n1 * cosT)/(n2 * cosI + n2 * cosT);
rn *= rn;
rt *= rt;
refl = (rn + rt)*0.5;
trans = 1.0 - refl;
if(n == 1.0)
return r;
if(cosT*cosT < 0.0)//tot inner refl
{
refl = 1;
trans = 0;
return calcReflectingRay(r, intersection, normal);
}
Vector dir = n * r.getDirection() + (n * cosI - cosT)*normal;
return Ray(intersection + dir * BIAS, dir);
}
Run Code Online (Sandbox Code Playgroud)
编辑:我也改变了折射率.从
if(cosI > 0.0)
{
n1 = 1.0;
n2 = getRefrIndex();
normal = ~normal;
}
else
{
n1 = getRefrIndex();
n2 = 1.0;
cosI = -cosI;
}
Run Code Online (Sandbox Code Playgroud)
至
if(cosI > 0.0)
{
n1 = getRefrIndex();
n2 = 1.0;
normal = ~normal;
}
else
{
n1 = 1.0;
n2 = getRefrIndex();
cosI = -cosI;
}
Run Code Online (Sandbox Code Playgroud)
然后我得到这个,几乎相同(仍然是颠倒的)折射率为1!
反思计算:
Ray Sphere::calcReflectingRay(const Ray &r, const Vector &intersection, const Vector &normal)const
{
Vector rdir = r.getDirection();
Vector dir = rdir - 2 * (rdir|normal) * normal;
return Ray(intersection + dir*BIAS, dir);
//the Ray constructor automatically normalizes directions
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:如何修复外部黑圈?哪个版本是正确的?
非常感谢帮助:)
这是使用g ++ 4.8.2在Linux上编译的.
Jer*_*fin 14
警告:以下是猜测,不确定.我必须更详细地查看代码,以确定发生了什么以及为什么.
也就是说,在我看来,你的原始代码基本上是模拟凹透镜而不是凸透镜.

凸透镜基本上是放大镜,将来自相对较小区域的光线聚焦在平面上:

这也说明了更正后的代码显示倒置图像的原因.来自一侧顶部的光线投射到另一侧的底部(反之亦然).
回到凹透镜:凹透镜是一个缩小透镜,显示镜头前方的广角图像:

如果你看这里的右下角,它会显示我怀疑的问题:特别是在高折射率下,试图进入镜头的光线与镜头本身的边缘相交.对于比这更宽的所有角度,您通常会看到一个黑色环,因为镜头的前缘充当阴影以防止光线进入.
增加折射率会增加黑色环的宽度,因为光更多地弯曲,因此边缘处的较大部分与透镜的外边缘相交.
如果您关心如何使用广角相机镜头来避免这种情况,通常的方法是使用弯月形镜头,至少对于前部元件:

这不是万能药,但至少可以防止入射光线与前透镜元件的外边缘相交.根据镜头需要覆盖的角度究竟有多宽,它的弯月度通常比这更少(并且在某些情况下它会是平凹的)但是你得到了一般的想法.
最后的警告:当然,所有这些都是手绘的,并且只是为了给出一般的想法,而不是(例如)反映任何特定镜头的设计,具有任何特定折射率的元素等.
| 归档时间: |
|
| 查看次数: |
6332 次 |
| 最近记录: |