问题描述
我正在开发OpenGL应用程序,并且在实现cubemap反射着色器时遇到问题:反射随着相机围绕对象旋转,从任何角度来看都是相同的.
I'm developing OpenGL application and having problem implementing cubemap reflection shader: reflection rotates with camera around the object, it's is same from any point of view.
这是我的顶点着色器:
in vec4 in_Position;
in vec4 in_Normal;
out vec3 ws_coords;
out vec3 normal;
mat4 uniform_ModelViewProjectionMatrix;
mat4 uniform_ModelViewMatrix;
mat4 uniform_ModelMatrix;
mat3 uniform_NormalMatrix;
vec3 uniform_CameraPosition;
...
ws_coords = (uniform_ModelViewMatrix * in_Position).xyz;
normal = normalize(uniform_NormalMatrix * in_Normal);
还有片段:
uniform samplerCube uniform_ReflectionTexture;
...
vec3 normal = normalize(normal);
vec3 reflectedDirection = reflect(normalize(ws_coords), normal);
frag_Color = texture(uniform_ReflectionTexture, reflectedDirection).xyz
我在互联网上找到的所有着色器都存在相同的问题,或者对我产生了奇怪的结果.
All shaders I found over the internet have same issue or producing weird results for me.
我想我需要随着相机旋转来旋转反射方向,但是我不知道该怎么做.在着色器输入中,我具有世界空间摄影机的位置,MVP,MV,M和法线矩阵.
I guess I need to rotate reflected direction with camera rotation but I have no idea how can I do that. On shader input I have world space camera position, MVP, MV, M and Normal matrices.
您能帮我实现考虑到相机方向的着色器吗?
Can you please help me implementing shader, that takes in account camera direction.
推荐答案
这部分对我来说有点奇怪:
This part seems a bit odd to me:
vec3反射方向=反射(normalize(ws_coords),正常);
vec3 reflectedDirection = reflect(normalize(ws_coords), normal);
要反射的第一个参数必须是在世界空间中从像素位置到相机位置的向量.
The first argument to reflect has to be a vector that goes from the pixel position to the camera position, in world space.
我建议您将摄像机放在世界的位置,然后将in_Position带到世界空间(我不知道他们当前在哪个空间),然后从中创建归一化向量.然后使用世界空间法线矢量对其进行反射,并对您的立方体贴图进行采样.
I suggest you have a camera world position, then take your in_Position to world space (I don't know which space they're currently in) and create a normalized vector from that. Then reflect it with a world space normal vector and sample your cubemap.
这篇关于GLSL立方体贴图反射着色器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!