【问题标题】:Lambertian shader still not working朗伯着色器仍然无法正常工作
【发布时间】:2015-06-17 05:30:21
【问题描述】:

昨天我发了这个:

Lambertian Shader not working

我的着色器仍然无法工作,我已经进行了一些调试以试图找到原因。当我运行我的程序并击中一个球体时,会调用 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


    【解决方案1】:

    +1 用于使用离散的 NormalPointVector 类。在您的 shade() 方法中,在我看来,您并没有考虑到光源到交点的距离。基本上,您希望远离给定灯光的交点接收的光少于靠近灯光的点。您可以通过将灯光的intensity 调整为c / (distance * distance) 系数来为点光源捏造这一点,其中c 是根据经验确定的亮度校正系数(从1 开始,如果结果太暗则向上调整) distance 是点光源和你的交点之间的距离。当我说调整时,我的意思是将该术语添加到 finalColour 计算中,而不是实际更改 light.intensity 的值。

    一旦您开始工作,您可能需要考虑以 PDF 格式对光源进行采样,而不是使用 1/d*d hack。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-02
      • 2018-06-02
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      相关资源
      最近更新 更多