4

I have a 3x3 transformation matrix for 2D homogeneous coordinates:

a b c
d e f
g h i

I'd like to pass this to OpenGL (using glMultMatrix) in a 2D application, but OpenGL takes 4x4 matrices for 3D homogeneous coordinates. I'd like all coordinates transformed by my 4x4 matrix to end up with x and y the same as for the 3x3 matrix and with z=0.

I've tried to work it out. For a vector x, y, 1 I'd end up with the transformed vector ax + by + c, dx + ey + f, gx + hy + i, so that means for a vector x, y, 0, 1, I'd want to end up with the tranformed vector ax + by + c, dx + ey + f, 0, ?. One matrix that would do this is (as far as I can tell):

a b 0 c
d e 0 f
0 0 1 0
0 0 0 1

Is this correct? Does it work? I don't think this is the only matrix that will give the result I'm looking for, but I don't quite understand what should or shouldn't go in the third and fourth rows (and the fourth column).

Vegard
  • 2,081
  • 1
  • 17
  • 26

1 Answers1

5

If you want the z-coordinate to be 0, you have to pass a zero-row. Also, include the perspective part in the last row:

a b 0 c
d e 0 f
0 0 0 0
g h 0 i
Nico Schertler
  • 32,049
  • 4
  • 39
  • 70
  • Ah, thanks! If the vector I'm transforming has z=`0` already, I can leave the third row as `0 0 1 0` to make z in the same as z out, right? – Vegard Jul 30 '15 at 12:24
  • 3
    Why not keep the 3rd row [0 0 1 0] leaving z unchanged? Identity would be to keep it this way. A zero vector in the 3rd row would mean a destructive projection transform. I understand that even if a point had z ≠ 0 it'll be made 0 while the other one will leave it unchanged. But is there any other reason other than that you had in mind? Just curious. – legends2k Jul 30 '15 at 14:13
  • @legends2k No, it's really just because OP wanted all z-components to be 0 (as far as I understood). – Nico Schertler Jul 30 '15 at 14:32