gpt4 book ai didi

opengl - 具有色差的反射/折射 - 眼睛校正

转载 作者:行者123 更新时间:2023-12-04 15:47:06 26 4
gpt4 key购买 nike

我正在编写一个 GLSL 着色器来模拟简单对象的色差。我保持 OpenGL 2.0 兼容,所以我使用内置的 OpenGL 矩阵堆栈。这是简单的顶点着色器:

uniform vec3 cameraPos;

varying vec3 incident;
varying vec3 normal;

void main(void) {
vec4 position = gl_ModelViewMatrix * gl_Vertex;
incident = position.xyz / position.w - cameraPos;
normal = gl_NormalMatrix * gl_Normal;

gl_Position = ftransform();
}
cameraPos正如人们想象的那样,uniform 是相机在模型空间中的位置。这是片段着色器:
const float etaR = 1.14;
const float etaG = 1.12;
const float etaB = 1.10;
const float fresnelPower = 2.0;
const float F = ((1.0 - etaG) * (1.0 - etaG)) / ((1.0 + etaG) * (1.0 + etaG));

uniform samplerCube environment;

varying vec3 incident;
varying vec3 normal;

void main(void) {
vec3 i = normalize(incident);
vec3 n = normalize(normal);

float ratio = F + (1.0 - F) * pow(1.0 - dot(-i, n), fresnelPower);

vec3 refractR = vec3(gl_TextureMatrix[0] * vec4(refract(i, n, etaR), 1.0));
vec3 refractG = vec3(gl_TextureMatrix[0] * vec4(refract(i, n, etaG), 1.0));
vec3 refractB = vec3(gl_TextureMatrix[0] * vec4(refract(i, n, etaB), 1.0));

vec3 reflectDir = vec3(gl_TextureMatrix[0] * vec4(reflect(i, n), 1.0));

vec4 refractColor;
refractColor.ra = textureCube(environment, refractR).ra;
refractColor.g = textureCube(environment, refractG).g;
refractColor.b = textureCube(environment, refractB).b;

vec4 reflectColor;
reflectColor = textureCube(environment, reflectDir);

vec3 combinedColor = mix(refractColor, reflectColor, ratio);

gl_FragColor = vec4(combinedColor, 1.0);
}
environment是从绘制对象的环境中实时渲染的立方体贴图。

在正常情况下,着色器的行为(我认为)像预期的那样,产生以下结果:

correct shader behavior

然而,当相机围绕其目标旋转 180 度时,它现在从另一侧指向物体,折射/反射图像会像这样扭曲(当然,这会逐渐发生在 0 到 180 度之间的角度) :

incorrect shader behavior

降低/升高相机时会出现类似的伪影;当相机直接位于目标对象上方(在这种情况下指向负 Z)时,它似乎只能 100% 正确运行。

我无法弄清楚着色器中的哪个转换负责这个扭曲的图像,但它应该与 cameraPos 的方式有关。被处理。是什么导致图像以这种方式自行扭曲?

最佳答案

这对我来说看起来很可疑:

vec4 position = gl_ModelViewMatrix * gl_Vertex;
incident = position.xyz / position.w - cameraPos;

是您的 cameraPos在世界空间中定义?您正在从假设的世界空间 position 中减去 View 空间向量 ( cameraPos )向量。您需要在世界空间或 View 空间中进行计算,但不能混合使用它们。

要在世界空间中正确执行此操作,您必须单独上传模型矩阵以获取世界空间事件向量。

关于opengl - 具有色差的反射/折射 - 眼睛校正,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9841863/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com