Jump to content
  • Advertisement
Sign in to follow this  
link102

[glsl] converting screen coordinates to view coordinates and back again

This topic is 1542 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

For the past 3 days I've been trying to get ssao working without much result. I've narrowed the problem down to the way I'm converting screen coordinates to view coordinates and back again. Below is the code I use to test the conversion.
 
Attached are the results of the shader and the depth buffer. [removed for now, please check back later]
 
Anyone able to point out what's going wrong here?
Are the screenToViewSpace & viewToScreenSpace functions correct?

#version 420

in vec2 vTexCoord;
in vec3 vNormal;

layout(binding = 0) uniform sampler2D uTexDiffuse;
layout(binding = 2) uniform sampler2D uTexDepth;

uniform mat4 projectionMatrix;
uniform mat4 invProjectionMatrix;

out vec4 out_fragColor;

vec3 screenToViewSpace(in vec2 texCoord, in float depth) {	
	vec4 position = vec4(texCoord, depth, 1.0);
	
	// unpack
	position.xyz = position.xyz * 2.0 - 1.0;
	
	// unproject
	position = invProjectionMatrix * position;
	position /= position.w;
	
	return position.xyz;
}

vec2 viewToScreenSpace(in vec3 unprojectedPosition) {
	vec4 position = vec4(unprojectedPosition, 0.0);
	
	// project
	position = projectionMatrix * position;
	position /= position.w;
	
	// pack
	position = position * 0.5 + 0.5;
	
	return position.xy;
}

void main() {
	float depth = texture(uTexDepth, vTexCoord).r;
	
	if(vTexCoord.x < 0.5) {
		// convert to view space, then back to screen space
		out_fragColor.xy = viewToScreenSpace(screenToViewSpace(vTexCoord, depth));
	} else {
		out_fragColor.xy = vTexCoord;
	}
}
Edited by link102

Share this post


Link to post
Share on other sites
Advertisement

You are changing the range of the depth value

    vec4 position = vec4(texCoord, depth, 1.0);
    // unpack
    position.xyz = position.xyz * 2.0 - 1.0;
Edited by HappyCoder

Share this post


Link to post
Share on other sites

You are changing the range of the depth value

    vec4 position = vec4(texCoord, depth, 1.0);
    // unpack
    position.xyz = position.xyz * 2.0 - 1.0;


It's working now. I used the method (poorly) described here and it magically worked: http://bassser.tumblr.com/post/11626074256/reconstructing-position-from-depth-buffer

I'm still struggling a bit with the names of all the coordinate spaces, but I do know that I wasn't passing the correct coordinate space to the inverse projection matrix. I believe it wants NDC space coordinates and I was passing it screen space so I had to divide by w first or something like that, I'm stil looking into it. Edited by link102

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!