Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 link102   Members   -  Reputation: 252

Like
0Likes
Like

Posted 03 July 2014 - 10:25 AM

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, 04 July 2014 - 08:35 AM.


Sponsor:

#2 HappyCoder   Members   -  Reputation: 2884

Like
0Likes
Like

Posted 03 July 2014 - 02:54 PM

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, 03 July 2014 - 02:56 PM.


#3 HappyCoder   Members   -  Reputation: 2884

Like
0Likes
Like

Posted 03 July 2014 - 02:55 PM

EDIT: Double posted for some reason


Edited by HappyCoder, 03 July 2014 - 02:56 PM.


#4 link102   Members   -  Reputation: 252

Like
0Likes
Like

Posted 04 July 2014 - 12:08 PM

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, 04 July 2014 - 12:08 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS