Jump to content

Eye position in post-processing shader


Rastar
 Share

Recommended Posts

Hi,

 

for a post effect I need to reconstruct the pixels' camera position from the depth buffer. I am a little confused since Leadwerks seems to be doing some things differently than textbook OpenGL, so a few questions I have:

 

I find this snippet e.g. in the directionallight.shader (and also a little different in klepto's sky shader)

 

screencoord = vec3(((gl_FragCoord.x/buffersize.x)-0.5) * 2.0 * (buffersize.x/buffersize.y),((-gl_FragCoord.y/buffersize.y)+0.5) * 2.0,depthToPosition(depth,camerarange));
 screencoord.x *= screencoord.z / camerazoom;
 screencoord.y *= -screencoord.z / camerazoom;
 screennormal = normalize(screencoord);
 if (!isbackbuffer) screencoord.y *= -1.0;

 

Now I get the depthToPosition function to calculate the z value. However, the x and y coordinates confuse me:

  1. First of all, it seems that Leadwerks uses the y position in the backbuffer inverted compared to the front buffer, therefore those isbackbuffer -> * -1 lines (here and also in other shader parts)?
  2. Is that also the reason that the y coordinate uses 0.5 - gl_FragCoord.y rather than gl_FragCoord.y - 0.5?
  3. The first line basically seems to compute normalized device coordinates, but why is the x coordinate multiplied by buffersize.x/buffersize.y?
  4. This code seems to assume a symmetric FOV with equal FOV values for x and y, correct?

Link to comment
Share on other sites

1. OpenGL Y screen coordinates are the inverse of what you would expect.

2. It worked out that way. See #1.

3. The camera perspective is decided by the vertical space, not the horizontal. This works out better with wide and narrow screen resolutions.

4. Yes, sort of. I mean, if the screen width is wider than the height, which it almost always is, the width will have a wider field of view, but it will be proportional to the vertical FOV and screen height.

 

See the light shaders for pixel position reconstruction (although I think it is relative to the camera).

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