ray tracing lighting
I am trying to implement specular and diffuse lighting for a simple sphere ray tracing application, but I am having problems with my vectors.
I am trying to use the following to update the light, but the generated image looks exactly the same, so I know I am doing something wrong. I assume I am messing up the vectors in some way. Hit is the sphere that has been hit and mindis is the distance to this spheres point. Pir, pig, pib are the rgb for the color.
P3D intersection = ray.position.add(ray.direction).scale(mindis);
P3D l = intersection.sub(light).normalize();
P3D n = hit.center.sub(intersectio开发者_运维百科n).normalize();
double dot = l.dot(n);
P3D f = l.add(n).scale(-2.0 * dot);
double dot2 = f.dot(ray.direction);
pir += dot2 * 20;
pig += dot2 * 20;
pib += dot2 * 20;
Perhaps the first line should be:
P3D intersection = ray.position.add(ray.direction.scale(mindis));
Also
P3D f = l.add(n.scale(-2.0 * dot));
f appears to be the direction that the light bounces off the sphere. This would typically be the opposite direction of the ray, so you probably want
double dot2 = -f.dot(ray.direction);
精彩评论