【发布时间】:2022-01-24 09:40:52
【问题描述】:
我正在尝试关注Ray Tracing in one Weekend tutorial,但我的法线看起来不像我期望的那样。
float hit_sphere(Sphere sphere, Ray r){
vec3 oc = r.origin - sphere.center;
float a = dot(r.direction,r.direction);
float b = 2.0 * dot(oc, r.direction);
float c = dot(oc, oc) - sphere.radius * sphere.radius;
float discriminant = b * b - 4 * a * c;
if(discriminant > 0){
return -1;
}
else{
return (-b -sqrt(discriminant))/(2.0 * a);
}
}
vec3 at(Ray ray, float p){
return ray.origin + p * ray.direction;
}
void main()
{
vec3 camera_origin = vec3(0,0,2);
vec2 st = gl_FragCoord.xy/vec2(x, y);
Ray r = Ray(camera_origin, normalize(vec3(st.x - 0.5, st.y - 0.5, 1.0)));
Sphere sphere = {vec3(0,0,0),0.5};
float p = hit_sphere(sphere, r);
if(p < 0.0){
vec3 N = normalize(at(r, p) - sphere.center);
FragColor = vec4(N.x + 1, N.y + 1, N.z + 1, 1);
}
else{
// FragColor = vec4(st.xy, 1.0, 1.0);
FragColor = vec4(1 - st.y+0.7, 1 - st.y+0.7,1 - st.y+0.9, 1.0);
}
}
请注意,每个正常颜色通道中的+ 1 是为了更明显地发现色差。
这就是我的法线的样子。
虽然这不是我所期望的这些法线的样子。 它们应该是这样的(不完全像这样,但很接近)
是什么错误或监督问题导致了这种情况。
【问题讨论】:
标签: glsl fragment-shader raytracing