View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# [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.

3 replies to this topic

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.

### #2HappyCoder  Members

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.

My current game project Platform RPG

### #3HappyCoder  Members

Posted 03 July 2014 - 02:55 PM

EDIT: Double posted for some reason

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

My current game project Platform RPG

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.