将漫射 material 正确形式 cpu 代码获取到 gpu 着色器
Getting diffuse material right form cpu code to gpu shader
我正在尝试从 raytracing in one weekend
中解开这个函数
vec3 color(const ray& r, hitable *world)
{
hit_record rec;
if(world->hit(r,0.0, MAXFLOAT, rec)){
vec3 target = rec.p + rec.normal + random_in_unit_sphere();
return 0.5*color( ray(rec.p, target-rec.p), world);
}
else{
vec3 unit_direction = unit_vector(r.direction());
float t = 0.5*(unit_direction.y() + 1.0);
return (1.0-t)*vec3(1.0,1.0,1.0) + t*vec3(0.5,0.7,1.0);
}
}
我知道它会发出射线并反弹它直到它没有击中任何东西。
所以我试图在 GLSL 着色器中解开这个递归函数。
vec3 color(ray r, hitableList list)
{
hitRecord rec;
vec3 unitDirection;
float t;
while(hit(r, 0.0, FLT_MAX, rec, list))
{
vec3 target = rec.p + rec.normal;
r = ray(rec.p, target-rec.p);
}
unitDirection = normalize(direction(r));
t = 0.5* (unitDirection.y + 1.);
return (1.-t)*vec3(1.)+t*vec3(0.5,0.7,1.);
}
通常它应该输出这样的漫反射:
但我只得到像这样的反光 material:
请注意,material 具有很强的反光性,可以反射场景中的其他球体。
我查看了代码,有些东西告诉我这是我错误的做法 tail recursive fonction
。
我也没有 return 0.5 *
return 0.5 * color(...)
我不知道该怎么做。
更新
感谢 Jarod42 的 awnser,现在实施了 0.5 *
因素,这解决了 material 未 "properly" 曝光的问题。
但是现在漫反射 material 仍然没有生成,我最终得到了一个金属 Material 完全反射。
要使用 0.5
的因子,您可以这样做:
vec3 color(ray r, hitableList list)
{
hitRecord rec;
vec3 unitDirection;
float t;
float factor = 1.f;
while(hit(r, 0.0, FLT_MAX, rec, list))
{
vec3 target = rec.p + rec.normal;
r = ray(rec.p, target-rec.p);
factor *= 0.5f;
}
unitDirection = normalize(direction(r));
t = 0.5* (unitDirection.y + 1.);
return factor * ((1.-t)*vec3(1.)+t*vec3(0.5,0.7,1.));
}
我正在尝试从 raytracing in one weekend
vec3 color(const ray& r, hitable *world)
{
hit_record rec;
if(world->hit(r,0.0, MAXFLOAT, rec)){
vec3 target = rec.p + rec.normal + random_in_unit_sphere();
return 0.5*color( ray(rec.p, target-rec.p), world);
}
else{
vec3 unit_direction = unit_vector(r.direction());
float t = 0.5*(unit_direction.y() + 1.0);
return (1.0-t)*vec3(1.0,1.0,1.0) + t*vec3(0.5,0.7,1.0);
}
}
我知道它会发出射线并反弹它直到它没有击中任何东西。
所以我试图在 GLSL 着色器中解开这个递归函数。
vec3 color(ray r, hitableList list)
{
hitRecord rec;
vec3 unitDirection;
float t;
while(hit(r, 0.0, FLT_MAX, rec, list))
{
vec3 target = rec.p + rec.normal;
r = ray(rec.p, target-rec.p);
}
unitDirection = normalize(direction(r));
t = 0.5* (unitDirection.y + 1.);
return (1.-t)*vec3(1.)+t*vec3(0.5,0.7,1.);
}
通常它应该输出这样的漫反射:
但我只得到像这样的反光 material:
请注意,material 具有很强的反光性,可以反射场景中的其他球体。
我查看了代码,有些东西告诉我这是我错误的做法 tail recursive fonction
。
我也没有 return
0.5 *
return 0.5 * color(...)
我不知道该怎么做。
更新
感谢 Jarod42 的 awnser,现在实施了 0.5 *
因素,这解决了 material 未 "properly" 曝光的问题。
但是现在漫反射 material 仍然没有生成,我最终得到了一个金属 Material 完全反射。
要使用 0.5
的因子,您可以这样做:
vec3 color(ray r, hitableList list)
{
hitRecord rec;
vec3 unitDirection;
float t;
float factor = 1.f;
while(hit(r, 0.0, FLT_MAX, rec, list))
{
vec3 target = rec.p + rec.normal;
r = ray(rec.p, target-rec.p);
factor *= 0.5f;
}
unitDirection = normalize(direction(r));
t = 0.5* (unitDirection.y + 1.);
return factor * ((1.-t)*vec3(1.)+t*vec3(0.5,0.7,1.));
}