Advertisement Jump to content
Sign in to follow this  

How to deal with perpective projection issue in reflection shader

This topic is 1016 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was playing around with some shader code when I ended up with something that seemed to be working rather well as a reflection shader. However it has artifacts near the camera due to the transformation by the projection matrix, however, if the transformation is not there, while the artifact is gone, the reflections are no longer perspective correct. I'm not sure how to deal with this.



#version 430 core

in vec2 UV;

layout(location = 0) out vec4 reflection;

uniform sampler2D colorMap;
uniform sampler2D normData;
uniform sampler2D specularData;
uniform sampler2D worldData;
uniform sampler2D envMap;
uniform sampler2D depthMap;
uniform vec3 EyePos;
uniform mat4 View;
uniform mat4 Projection;

void main(){
    vec3 n = 2.0 * texture2D(normData, UV).xyz - 1.0;
    n = normalize(n);
    vec3 worldCoord = texture2D(worldData, UV).rgb;
    vec3 v = EyePos - worldCoord;
    v = normalize(-v);
    float depth = texture2D(depthMap, UV).r;
	vec4 vis = vec4(0);
    vec3 refNorm = (Projection * View * vec4(reflect(v, n), 0)).rgb;
    refNorm = 0.5 * refNorm + 0.5;
    vis = texture2D(colorMap, refNorm.xy);
    reflection = vis;

Without Projection matrix:


With Projection Matrix:


The artifact kind of gets hidden as I start to move faster, but it'd still be nice to get rid of it. Just had the thought while writing the question, is the artifact possibly due to missing information?

Edited by HimanshuGoel97

Share this post

Link to post
Share on other sites

I'm sorry, I should have been more clear, I was referring to the circle like area where the reflection is suddenly different around the ship.

The changing filter type idea did give me the idea to see if that was due to missing information by disabling texture wrapping, which did eliminate the issue, sort of, it replaced the circle with stretching, which works better.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!