Sam*_*nha 6 c++ opengl raytracing glsl
我试图在片段着色器中编写一个简单的光线追踪器。我有这个函数应该创建一个漫射球体,如下所示:
这是功能:
vec3 GetRayColor(Ray ray)
{
Ray new_ray = ray;
vec3 FinalColor = vec3(1.0f);
bool IntersectionFound = false;
int hit_times = 0;
for (int i = 0; i < RAY_BOUNCE_LIMIT; i++)
{
RayHitRecord ClosestSphere = IntersectSceneSpheres(new_ray, 0.001f, MAX_RAY_HIT_DISTANCE);
if (ClosestSphere.Hit == true)
{
// Get the final ray direction
vec3 R;
R.x = nextFloat(RNG_SEED, -1.0f, 1.0f);
R.y = nextFloat(RNG_SEED, -1.0f, 1.0f);
R.z = nextFloat(RNG_SEED, -1.0f, 1.0f);
vec3 S = normalize(ClosestSphere.Normal) + normalize(R);
S = normalize(S);
new_ray.Origin = ClosestSphere.Point;
new_ray.Direction = S;
hit_times += 1;
IntersectionFound = true;
}
else
{
FinalColor = GetGradientColorAtRay(new_ray);
break;
}
}
if (IntersectionFound)
{
FinalColor /= 2.0f; // Lambertian diffuse only absorbs half the light
FinalColor = FinalColor / float(hit_times);
}
return FinalColor;
}
Run Code Online (Sandbox Code Playgroud)
出于某种原因,它似乎hit_times是恒定的。这个完全相同的代码适用于 CPU 并生成了附加的屏幕截图。
我不确定这是否与 GPU 有关。但是我已经测试了所有其他功能并且它们按预期工作。
这是Normal + RandomVec或S可视化的:
在CPU上完成时完全相同。
这是hit_times在CPU上的可视化
但是在 GPU 上,三个球体都是白色的。这是完整的片段着色器:https : //pastebin.com/3xeA6LtT 这是在 CPU 上运行的代码:https : //pastebin.com/eyLnHYzr
小智 4
所有球体很可能都是白色的,因为函数ClosestSphere.Hit中GetRayColor始终为真。
我认为问题出在你的IntersectSceneSpheres功能上。
在 CPU 代码中,您返回的HitAnything值默认为 false。同时,在片段着色器中,ClosestRecord如果没有任何内容被击中,您将返回保持未初始化的结构。
ClosestRecord.Hit = HitAnything;在函数末尾显式添加IntersectSceneSpheres应该修复它
| 归档时间: |
|
| 查看次数: |
162 次 |
| 最近记录: |