How to calculate the position of the representation space from the position of the screen-space without matrix multiplication

I calculate the position of the view space from the position of the screen space inside several light and post-technological shaders. My current code restores the position using the inverse projection matrix:

float depth = depthBuffer.Sample(sampler, uv).x;
float4 temp = mul(float4(uv.x * 2 - 1, (1 - uv.y) * 2 - 1, depth, 1), inverseProjectionMatrix);
float3 viewSpacePosition = temp.xyz / temp.w;

As an optimization, I would like to remove matrix multiplication from my shader and restore the position of the view space using variables such as aspect, near-clipand far-clip.

+4
source share
1 answer

You did not say how you create a perspective projection matrix, which is key information. I am not a D3D expert. Rather, I know OpenGL.

, , , OpenGL gluPerspective. :

gluPerspective matrix

OpenGL. D3D- -, . :

gluPerspective simplified and inverted

Wolfram Alpha ( ):

{{a,0,0,0},{0,b,0,0},{0,0,c,-1},{0,0,d,0}}^-1

:

inverse matrix expression

, [x y z 1], ,

[x/a y/b -1 (z+c)/d]

4d . 3d, x, y, z w:

[(1/a)ux  (1/b)uy  -u]  where u = d / (z+c)

, (1/a) (1/b) . , .

- D3D OpenGL, .

, , , .

+3

Source: https://habr.com/ru/post/1539752/


All Articles