【发布时间】:2015-06-17 05:30:21
【问题描述】:
昨天我发了这个:
我的着色器仍然无法工作,我已经进行了一些调试以试图找到原因。当我运行我的程序并击中一个球体时,会调用 shader.shade(renderable.colour) 并运行以下代码:
public Colour shade(Intersection intersection, Light light){
Vector3D lightDirection = light.location.subtract(intersection.point);
lightDirection.normalise();
Normal normal = intersection.normal;
normal.normalise();
Colour finalColour = new Colour();
float lambCoef = (float) normal.dot(lightDirection);
if(lambCoef>0){
finalColour.r = Math.max(0.0f, diffuseColour.r * lambCoef * light.intensity.r);
finalColour.g = Math.max(0.0f, diffuseColour.g * lambCoef * light.intensity.g);
finalColour.b = Math.max(0.0f, diffuseColour.b * lambCoef * light.intensity.b);
}
return finalColour;
}
我每次都得到不同的 lambCoef 值,但不是很多,例如对于红色球体,对于彼此垂直约 20 像素的像素,我得到: 0.9446402 0.94463843 0.9446326 0.94462925 为了获得我使用的球体的法线:
public Normal getNormalAt(Vector3D point) {
Normal normal = new Normal(point);
normal = normal.subtract(center);
normal = normal.multiply(-1);
normal.normalise();
return normal;
}
这似乎有效。 然后对于我的点和交叉代码,我使用:
public double dot(Vector3D vector){
return x*vector.x + y*vector.y +z*vector.z;
}
public double dot(Point3D point){
return x*point.x + y*point.y +z*point.z;
}
public double dot(Normal normal){
return x*normal.x + y*normal.y +z*normal.z;
}
public Vector3D cross(Vector3D vector) {
Vector3D crossedVector = new Vector3D();
crossedVector.x = y*vector.z - z*vector.y;
crossedVector.y = z*vector.x - x*vector.z;
crossedVector.z = x*vector.y - y*vector.x;
return crossedVector;
}
这似乎也是正确的。 任何帮助都将不胜感激,如果需要,我很乐意提供更多信息。
我现在得到这样的图像:
这是有道理的,因为平面与球体的角度要小得多。不过还是错了。
【问题讨论】:
标签: java shader raytracing