1
\$\begingroup\$

I'm converting view coordinates to texture coordinates for both my shadow maps and Screen space reflections. I keep seeing this conversion in examples:

            var T = new Matrix
            {
                M11 = 0.5f,
                M22 = -0.5f,
                M33 = 1.0f,
                M41 = 0.5f,
                M42 = 0.5f,
                M44 = 1.0f
            };

            Matrix m = view * projection * T;

What is T and why does this "work"? I'm saying "work" because I not satisfied with the result but the problem may not lie here.

I would like to use and inverted view and projection and then adjust the coordinates from -1,1 to 0,1. But that doesn't work at all. What have I misunderstood?

\$\endgroup\$
3
\$\begingroup\$

It's transforming the range from [-1 : 1] to [0 : 1] and inverting the y/u component for texture coords.

The standard formula is:

u = (x / 2) + 0.5

v = (y / -2) + 0.5.

There's a negative 2 here because tex coords are often vertically inverted. That is, the top left coord is (0,0). See here.

T = [ 0.5, 0.0, 0.0, 0.0 ] [ 0.0, -0.5, 0.0, 0.0 ] [ 0.0, 0.0, 1.0, 0.0 ] [ 0.5, 0.5, 0.0, 1.0 ]

M11 and M22 are the scaling for the X and Y coords, and inversion of the Y component. The range changes from [-1 : 1] to [-0.5 : 0.5] in X and [-1 : 1] to [0.5 : -0.5] in Y.

M41 and M42 is translation required to go from [-0.5 : 0.5] to [0 : 1] in X and [0.5, -0.5] to [1 : 0] in Y.

\$\endgroup\$

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.