作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写一个 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
是从绘制对象的环境中实时渲染的立方体贴图。
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/
我正在编写一个 GLSL 着色器来模拟简单对象的色差。我保持 OpenGL 2.0 兼容,所以我使用内置的 OpenGL 矩阵堆栈。这是简单的顶点着色器: uniform vec3 cameraPos
Actually I'm not sure that Title of my question is 'correct', if you have any idea with it, you coul
我是一名优秀的程序员,十分优秀!