我在glsl中编写了一个光线跟踪器,它应该漫反射一个球体(在这种情况下实际上只是一个由四个顶点组成的2D形状)但是当代码运行时我看到了两个轮廓的轮廓。球体和包含它的正方形而不仅仅是球体。我对光线跟踪非常新,所以我只是试图了解它是如何工作的。下面是一些它看起来像的图像(我添加了使光标在球体内移动时做出反应的功能)
这可能是什么原因?以下是片段着色器中的一些相关代码:
float raySphere(vec3 V, vec3 W, vec4 sph) {
float r = 1.0;
float b = 2.0* dot(V,W);
float c = dot(V, V) - (sph.w * sph.w);
float h = b*b - 4.0*c;
float t = (-b - sqrt(h))/2.0;
if(h <0.0 || t < 0.0 ) return 10000.;
return t;
}
// Diffusely shade a sphere.
// point is the x,y,z position of the surface point.
// sphere is the x,y,z,r definition of the sphere.
// material is the r,g,b color of the sphere.
vec3 shadeSphere(vec3 point, vec4 sphere, vec3 material) {
vec3 color = vec3(0.,0.,0.);
vec3 N = (point - sphere.xyz) / sphere.w;
float diffuse = max(dot(Ldir, N), 0.0);
vec3 ambient = material/5.0;
color = ambient + Lrgb * diffuse * max(0.0, dot(N , Ldir));
return color;
}
void main(void) {
vec2 c = uCursor.xy;
Lrgb = vec3(2.,3.5,4.0);
Ldir = normalize(vec3(c.x, c.y, 1. - 2. * dot(c, c)));
float x = vPosition.x;
float y = vPosition.y;
float z = computeZ(vPosition.xy, 1.0);
// COMPUTE V AND W TO CREATE THE RAY FOR THIS PIXEL,
// USING vPosition.x AND vPosition.y.
vec2 uv = vPosition.xy/uCursor.xy;
//generate a ray
vec3 V, W;
//V = vec3(0.0,1.0,0.0);
//W = normalize(vec3( 2.0,0.0,1.0 ));
V = vec3(0.0, 1.0, 3.0);
W = normalize(vec3((-1.0 + 2.0 )*vec2(1.78,1.0), -1.0));
//SET x,y,z AND r FOR sphere.
sphere = vec4(x,y,z,V + W);
//SET r,g,b FOR material.
vec3 color = vec3(5., 2., 3.);
float t = raySphere(V, W, sphere);
if (t < 10000.)
color = shadeSphere(V + t * W, sphere, material);
//color.r = 0.7;
color = pow(color, vec3(.45,.45,.45)); // Do Gamma correction.
gl_FragColor = vec4(color, 1.); // Set opacity to 1.
}
答案 0 :(得分:1)
raySphere()
报告未命中(-1)的方式以及您在main()
(t < 10000.
)中检查点击的方式并不顺利。