Jump to content

Screen space to world space


Josh
 Share

Recommended Posts

This took a very long time to figure out. I finally found a formula in Igor's SSLR shader that helped, then I added the camera position to finish it off. This returns the position in world (global) space of the current pixel being rendered in a fragment shader:

vec4 ScreenPositionToWorldPosition()
{
vec2 icoord = vec2(gl_FragCoord.xy/buffersize);
if (isbackbuffer) icoord.y = 1.0f - icoord.y;
float x = icoord.s * 2.0f - 1.0f;
float y = icoord.t * 2.0f - 1.0f;
float z = gl_FragCoord.z;
vec4 posProj = vec4(x,y,z,1.0);
vec4 posView = inverse(projectioncameramatrix) * posProj;
posView /= posView.w;
posView.xyz+=cameraposition;
return posView;
}

  • Upvote 4

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

The division by the w coordinate is beyond my understanding, although I have seen that in a few other algorithms like this. I think that has to do with the projection matrix not using (0,0,0,1) for the right-hand column, which introduces shearing into the equation. I don't know enough about sheared matrices to be able to visualize it like I can an orthogonal matrix.

 

Not just shearing, but the increasing scale of the volume as you move along the z axis...aughhhh!

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...