0
\$\begingroup\$

I am having a problem with turning depth to world space position. I am using GLSL.

What could go wrong?

Here is the code:

float x = (uv.x * 2.0) - 1.0;
float y = (uv.y * 2.0) - 1.0;
float z = (depth * 2.0) - 1.0;

vec4 pos_clip = vec4(x, y, z 1.0);

vec4 inverse_pos_view = InvCamProjMatrix * pos_clip;
inverse_pos_view.xyz /= inverse_pos_view.w;

vec4 pos_ws = InvCamViewMatrix * inverse_pos_view;

return pos_ws.xyz;

Thanks.

\$\endgroup\$
1
  • \$\begingroup\$ I don't usually see the w division wedged between two matrix multiplications. Other solutions I've seen multiply by Inverse View Projection all in one go. What format are your depth values in? Is it possible they've been linearized before being sent as input to this code? \$\endgroup\$ – DMGregory Mar 28 at 14:31
1
\$\begingroup\$

I found an answer.

vec4 pos_ws = InvCamViewMatrix * InvCamProjMatrix * pos_clip;
pos_ws.xyz /= pos_ws.w;
\$\endgroup\$
2
  • \$\begingroup\$ You can combine those two matrices into a single one to save a few cycles. \$\endgroup\$ – DMGregory Mar 28 at 14:42
  • \$\begingroup\$ Thanks. Maybe if i will use separate buffer for positions. Should it be faster? For example RG32UI buffer, and pack XYZ in it. For resolution ~1280x960 \$\endgroup\$ – Nick Bane Mar 28 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.