Ray Tracer没有正确渲染球体
问题描述:
我在glsl中写了一个射线追踪器,它应该散射阴影(在这种情况下实际上只是由四个顶点构成的二维形状),但是当代码运行时,我看到球体的轮廓和包含它的正方形,而不仅仅是球体。我非常新的光线追踪,所以我只是想弄清楚这是如何工作的。下面是什么样子(我添加了当使光标在范围内移动了它的反应功能)Ray Tracer没有正确渲染球体
可能。这是什么原因一些图片?下面是一些来自片段着色器的相关代码:
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()
随你怎么在main()
(t < 10000.
)检查命中没有相处(-1)一起报告了一个小姐。
是的,我一直在玩这个游戏,因为当我从''''''''''''''返回10000时,只显示蓝色方块,但是当我返回-1时,我至少可以看到球体的轮廓当我将鼠标移到广场上时。但我更新了代码,以便两者现在匹配。他们的任何其他明显问题是否会导致只显示广场? – loremIpsum1771