0
\$\begingroup\$

How would I create a matrix that maps coordinates like so:

Near plane

x: [-3,3] -> [-1,1]
y: [-6,2] -> [-1,1]
z: 2 -> -1

Far plane

x: [-4,4] -> [-1,1]
y: [-4,4] -> [-1,1]
z: 0 -> 1

Visual coordinates

Result: result

My current solution is a transformation in a vertex shader like below, but if it's possible to use a matrix I can keep my shaders as is.

vec4 special_projection(vec4 p){
  float ty = 0.25 * p.z;
  float tx = 0.25 * p.x;
  float xz = (tx / 3.0) * (p.z / 2.0);
  float x = tx + xz;
  float y = 0.25 * p.y + ty;
  float z = -0.5 * p.z;
  return vec4(x,y,z,1);
}
\$\endgroup\$
1
\$\begingroup\$

Let's take an initially unknown matrix, and multiply it by one of the points on your near plane:

$$\begin{bmatrix} a & b & c & d\\ e & f & g & h\\ i & j & k & l\\ m & n & o & p \end{bmatrix} \begin{bmatrix} -3\\-6\\2\\1 \end{bmatrix} = \begin{bmatrix} -3a - 6b +2c +d\\ -3e -6f +2g + h\\ -3 i - 6 j + 2k + l\\ -3m -6n +2o + p \end{bmatrix}$$

After the perspective divide, that becomes:

$$\frac 1 {-3m -6n +2o + p}\begin{bmatrix} -3a - 6b +2c +d\\ -3e -6f +2g + h\\ -3 i - 6 j + 2k + l \end{bmatrix} = \begin{bmatrix} -1\\-1\\-1 \end{bmatrix}$$

Here you have 3 equations in 16 unknowns:

$$\begin{align}-3a - 6b +2c +d &= 3m + 6n -2o - p\\ -3e - 6f +2g +h &= 3m + 6n -2o - p\\ -3i - 6j +2k +l &= 3m + 6n -2o - p\\ \\-3a - 6b +2c +d - 3m - 6n +2o + p &= 0\\ -3e - 6f +2g +h - 3m - 6n +2o + p &= 0\\ -3i - 6j +2k +l -3m - 6n +2o + p &=0 \end{align} $$

Now repeat this for each of the corners of your near and far planes, and you'll have \$3 \times 4 \times 2 = 24\$ equations in 16 unknowns. That's enough to plug this system of linear equations into a solver and come up with values for the 16 variables \$a\$ through \$p\$, giving you a matrix that satisfies this mapping.

\$\endgroup\$
2
  • \$\begingroup\$ Here are my equations: pastebin.com/4J3TRSzD. Cannot find any solutions in wolfram alpha or symbolab. Maybe I do not understand, or there exist no solution. \$\endgroup\$ May 31 at 6:23
  • \$\begingroup\$ @user3346597 We can remove the perspective contraction on the y axis by skipping the division by w. Or, correspondingly, by multiplying the y coordinate by w at the end of our vertex shader. That would turn the second equation in the example above into -3d-6f+2g+h = -1. Try applying that to the y row of all of your equations, and then check whether that modified system of linear equations has a solution. \$\endgroup\$
    – DMGregory
    Jun 1 at 1:29

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.