java - 球体光线追踪 - 镜面高光

标签 java render raytracing lighting

我在为我的球体获取正确的光照模型时遇到一些问题。具体来说,我无法正确获得球体的镜面高光。这是我在光线追踪球体时看到的内容:
Wrong specular highlights

现在,镜面高光应该看起来更像这样:
Correct specular highlights

据我了解,照明模型(对于这些漫射球体)如下所示:
Lighting model

其中cr是球体的颜色,ca是光的环境分量,cl是球体的颜色光,n 是球体交点处的法线,l 是光的方向,cp 是颜色镜面高光,e 是眼睛/Look From,r 是球体表面的反射 vector ,指数中的 p 指的是到 phong 常数/指数(灯光的紧/松程度)。

这是我的过程:

public Color calculateIlluminationModel(Vector normal, Scene scene)
{
    //c = cr * ca + cr * cl * max(0, n \dot l)) + cl * cp * max(0, e \dot r)^p
    Vector lightSourceColor = getColorVector(scene.getLight().getLightColor()); //cl
    Vector diffuseReflectanceColor = getColorVector(getMaterialColor()); //cr
    Vector ambientColor = getColorVector(scene.getLight().getAmbientLightColor()); //ca
    Vector specularHighlightColor = getColorVector(getSpecularHighlight()); //cp
    Vector directionToLight = scene.getLight().getDirectionToLight(); //l
    Vector reflectionVector = normal.multiply(2).multiply(normal.crossProduct(directionToLight)).subtract(directionToLight); //r = 2n(n \dot l) - l

    Vector ambientTerm = diffuseReflectanceColor.multiply(ambientColor);
    double angleBetweenLightAndNormal = directionToLight.dotProduct(normal);
    Vector diffuseTerm = diffuseReflectanceColor.multiply(lightSourceColor).multiply(Math.max(0, angleBetweenLightAndNormal));
    Vector phongTerm = lightSourceColor.multiply(specularHighlightColor).multiply(Math.pow(Math.max(0, scene.getCameraSettings().getLookFrom().dotProduct(reflectionVector)), (double) getPhongConstant()));
    return getVectorColor(ambientTerm.add(diffuseTerm).add(phongTerm));
}

请注意,在本例中,phong 项的眼睛分量是相机的观察方向,即 (0, 0, 1),光线方向为 (1, 0, 0)。

知道为什么我的镜面高光位于球体的顶部而不是面向光线的方向吗?

如果我透露了您需要帮助我的任何重要细节,请告诉我。

最佳答案

反射 vector 应根据表面法线和入射光线方向计算,而不是像现在那样根据光线方向计算。

关于java - 球体光线追踪 - 镜面高光,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40569883/

相关文章:

go - 如何在二叉树中所有节点的先前父级都符合条件的二叉树中搜索(可能有多个)节点?

java - Thymeleaf 显示 HashMap

Java 8 流 API

java - gxt:自定义网格单元格样式

ajax - JSF 和 Ajax 验证 - 如何更新所有消息的消息面板?

game-engine - 表示为法线和偏移的平面?

glsl - 光线追踪立方体内的球体

java - 合并 HDFS 中小于 128MB 的压缩 lzo 文件

java - amp 包含在 url struts 标签中

javascript - React 组件返回时实际发生了什么?