How to deal with perpective projection issue in reflection shader

Started by
1 comment, last by hgoel0974 8 years ago

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.

Code:


#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:

http://i.imgur.com/FUxqgdy.png

With Projection Matrix:

http://i.imgur.com/Q2jsnxC.png

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?

Advertisement

Looks like nearest-neighbor sampling on your reflection map. Change your filter type to linear.

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.

This topic is closed to new replies.

Advertisement