作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在为我的球体设置正确的光照模型时遇到了一些问题。具体来说,我无法正确获得球体的镜面高光。这是我在光线追踪球体时看到的:
其中cr是球体的颜色,ca是光的环境分量,cl是球体的颜色光,n
是球体交点处的法线,l
是光的方向,cp是颜色镜面反射高光,e
是eye/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/
我正在尝试使用 Sfml 编写 2D 游戏。对于那个游戏,我需要一个 Lightengine 和一些代码,这些代码可以为我提供玩家可见的世界区域。由于这两个问题非常吻合(实际上是相同的),我想同时解决
我是一名优秀的程序员,十分优秀!