着色器翻转输出图像

发布于 2025-01-19 21:24:53 字数 1649 浏览 0 评论 0原文

我一直在努力找出为什么我的图像有时会在着色器运行并输出结果后翻转的问题。

我将位置图像和法线图像放入着色器中以执行基本的照明计算。位置和法线图像的方向正确,但结果输出似乎最终翻转。我的 SSAO 通道也会发生这种情况,其中 SSAO 通道会翻转图像,但 SSAO 的模糊通道会将图像取消翻转回到正常状态。

我已经利用了

vec2(1.0 - uvCoords.x, uvCoords.y);

虽然这确实将图像反转回正常状态,但它也反转了键盘输入和鼠标控制,因此不是理想的解决方案。

着色器输出到一个四边形,该四边形创建为:

std::vector<Vertex> Quad = {
//  pos                 // color            //texcoords
{{-1.0f, -1.0f, 0.0f}, {1.0f, 0.0f, 0.0f},  {1.0f, 0.0f}},
{{1.0f, -1.0f, 0.0f},  {0.0f, 1.0f, 0.0f},  {0.0f, 0.0f}},
{{1.0f, 1.0f, 0.0f},   {0.0f, 0.0f, 1.0f},  {0.0f, 1.0f}},
{{-1.0f, 1.0f, 0.0f},  {1.0f, 1.0f, 0.0f},  {1.0f, 1.0f}},
{{-1.0f, -1.0f, 0.0f}, {1.0f, 0.0f, 0.0f},  {1.0f, 0.0f}},
{{1.0f, 1.0f, 0.0f},   {0.0f, 0.0f, 1.0f},  {0.0f, 1.0f}},

};

照明通道着色器:

void main() {

    vec3 FragPos = texture(gPosition, uvCoords).rgb;
    vec3 Normal = texture(gNormal, uvCoords).rgb;

    float ambientValue = 0.1;
    vec3 ambient = ambientValue * light.LightColor.xyz;

    vec3 lightingDirection = normalize(light.LightPosition.xyz - FragPos);
    float diffCalc = max(dot(Normal, lightingDirection), 0.0);
    vec3 diffuse = diffCalc * light.LightColor.xyz;

    vec3 result = (ambient + diffuse) * light.ObjectColor.xyz;

    outColor = vec4(result, 1.0);

}

我以正确的方向显示 G 缓冲区法线,以及最终翻转的最终照明通道输出图像: 输入图片此处描述

在此处输入图像描述

I have been struggling to figure out the problem behind why my images sometimes end up flipping once the shader has run and output the result.

I take in position and normal images into my shader to perform basic lighting calculations. The position and normal images are correctly orientated but the resulting output seems to end up flipping. This also occurs with my SSAO pass where the SSAO pass flips the image but the blurring pass for the SSAO will then un-flip the image back to normal.

I have utilised

vec2(1.0 - uvCoords.x, uvCoords.y);

While this does invert the image back to normal, it also inverts keyboard input and mouse control and therefore not the ideal solution.

The shader outputs to a quad which is created as:

std::vector<Vertex> Quad = {
//  pos                 // color            //texcoords
{{-1.0f, -1.0f, 0.0f}, {1.0f, 0.0f, 0.0f},  {1.0f, 0.0f}},
{{1.0f, -1.0f, 0.0f},  {0.0f, 1.0f, 0.0f},  {0.0f, 0.0f}},
{{1.0f, 1.0f, 0.0f},   {0.0f, 0.0f, 1.0f},  {0.0f, 1.0f}},
{{-1.0f, 1.0f, 0.0f},  {1.0f, 1.0f, 0.0f},  {1.0f, 1.0f}},
{{-1.0f, -1.0f, 0.0f}, {1.0f, 0.0f, 0.0f},  {1.0f, 0.0f}},
{{1.0f, 1.0f, 0.0f},   {0.0f, 0.0f, 1.0f},  {0.0f, 1.0f}},

};

Lighting pass shader:

void main() {

    vec3 FragPos = texture(gPosition, uvCoords).rgb;
    vec3 Normal = texture(gNormal, uvCoords).rgb;

    float ambientValue = 0.1;
    vec3 ambient = ambientValue * light.LightColor.xyz;

    vec3 lightingDirection = normalize(light.LightPosition.xyz - FragPos);
    float diffCalc = max(dot(Normal, lightingDirection), 0.0);
    vec3 diffuse = diffCalc * light.LightColor.xyz;

    vec3 result = (ambient + diffuse) * light.ObjectColor.xyz;

    outColor = vec4(result, 1.0);

}

I am showing the G-buffer normal in correct orientation and the resulting lighting pass output image which ends up flipping:
enter image description here

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烙印 2025-01-26 21:24:54

似乎您的X轴在紫外线坐标网中被拨动,

std::vector<Vertex> Quad = {
    //  pos                 // color            //texcoords
   {{-1.0f, -1.0f, 0.0f}, {1.0f, 0.0f, 0.0f},  {1.0f, 0.0f}},
   {{1.0f, -1.0f, 0.0f},  {0.0f, 1.0f, 0.0f},  {0.0f, 0.0f}},
   {{1.0f, 1.0f, 0.0f},   {0.0f, 0.0f, 1.0f},  {0.0f, 1.0f}},
   {{-1.0f, 1.0f, 0.0f},  {1.0f, 1.0f, 0.0f},  {1.0f, 1.0f}},
   {{-1.0f, -1.0f, 0.0f}, {1.0f, 0.0f, 0.0f},  {1.0f, 0.0f}},
   {{1.0f, 1.0f, 0.0f},   {0.0f, 0.0f, 1.0f},  {0.0f, 1.0f}},
};

您可以看到POS X = -1.0F映射到TexCoord 1.0F和POS X 1.0F地图至0.0F,因此它确实将X轴翻转。

Layout should be 
std::vector<Vertex> Quad = {
    //  pos                 // color            //texcoords
   {{-1.0f, -1.0f, 0.0f}, {1.0f, 0.0f, 0.0f},  {0.0f, 0.0f}},
   {{1.0f, -1.0f, 0.0f},  {0.0f, 1.0f, 0.0f},  {1.0f, 0.0f}},
   {{1.0f, 1.0f, 0.0f},   {0.0f, 0.0f, 1.0f},  {1.0f, 1.0f}},
   {{-1.0f, 1.0f, 0.0f},  {1.0f, 1.0f, 0.0f},  {0.0f, 1.0f}},
   {{-1.0f, -1.0f, 0.0f}, {1.0f, 0.0f, 0.0f},  {0.0f, 0.0f}},
   {{1.0f, 1.0f, 0.0f},   {0.0f, 0.0f, 1.0f},  {1.0f, 1.0f}},
};

Seems your x axis in uv coord mesh is flipped

std::vector<Vertex> Quad = {
    //  pos                 // color            //texcoords
   {{-1.0f, -1.0f, 0.0f}, {1.0f, 0.0f, 0.0f},  {1.0f, 0.0f}},
   {{1.0f, -1.0f, 0.0f},  {0.0f, 1.0f, 0.0f},  {0.0f, 0.0f}},
   {{1.0f, 1.0f, 0.0f},   {0.0f, 0.0f, 1.0f},  {0.0f, 1.0f}},
   {{-1.0f, 1.0f, 0.0f},  {1.0f, 1.0f, 0.0f},  {1.0f, 1.0f}},
   {{-1.0f, -1.0f, 0.0f}, {1.0f, 0.0f, 0.0f},  {1.0f, 0.0f}},
   {{1.0f, 1.0f, 0.0f},   {0.0f, 0.0f, 1.0f},  {0.0f, 1.0f}},
};

You can see that pos x = -1.0f maps to texcoord 1.0f and pos x 1.0f maps to 0.0f, so it indeed flips the x axis.

Layout should be 
std::vector<Vertex> Quad = {
    //  pos                 // color            //texcoords
   {{-1.0f, -1.0f, 0.0f}, {1.0f, 0.0f, 0.0f},  {0.0f, 0.0f}},
   {{1.0f, -1.0f, 0.0f},  {0.0f, 1.0f, 0.0f},  {1.0f, 0.0f}},
   {{1.0f, 1.0f, 0.0f},   {0.0f, 0.0f, 1.0f},  {1.0f, 1.0f}},
   {{-1.0f, 1.0f, 0.0f},  {1.0f, 1.0f, 0.0f},  {0.0f, 1.0f}},
   {{-1.0f, -1.0f, 0.0f}, {1.0f, 0.0f, 0.0f},  {0.0f, 0.0f}},
   {{1.0f, 1.0f, 0.0f},   {0.0f, 0.0f, 1.0f},  {1.0f, 1.0f}},
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文