Ray Tracer 无法正确渲染球体

Ray Tracer not rendering sphere properly

我在 glsl 中编写了一个光线追踪器,它应该对一个球体进行漫反射着色(在这种情况下,它实际上只是一个由四个顶点组成的二维形状)但是当代码运行时,我看到了两个轮廓球体和包含它的正方形,而不仅仅是球体。我对光线追踪还很陌生,所以我只是想了解它是如何工作的。这是它的一些图片(我添加了当光标在球体内移动时做出反应的功能)

这可能是什么原因造成的?以下是片段着色器的一些相关代码:

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.
   }

raySphere() 报告未命中 (-1) 的方式与您在 main() (t < 10000.) 中检查命中的方式不一致。