Search code examples
graphicsglslshaderfragment-shaderuv-mapping

Shader flips output image


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


Solution

  • 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}},
    };