Tyrrrz's picture

Matrix transformation

I have projection, view and model matrices. When i pass it to the shader, the objects get rendered correctly.
However, i also want to transform my vertices with the same matrices on CPU from time to time (to perform collision checks).

My code is as follows:

                Matrix4 mat = ModelMatrix * Camera.ViewMatrix * Camera.ProjectionMatrix;
                Vector2[] vert = (Vector2[])Entity.Structure.Vertices.Clone();
                for (int i = 0; i < vert.Length; i++)
                    Vector4 trans = new Vector4(vert[i]);
                    trans = Vector4.Transform(trans, mat);
                    vert[i] = new Vector2(trans.X, trans.Y);
                return vert;

The output vertices are incorrect though. I'm also multiplying the matrices in inverse order to account for OpenTK transposition. What am i doing wrong?


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
winterhell's picture

I may be wrong, but can you try settings trans.W =1 before transforming it?

Tyrrrz's picture

I forgot about that, thanks.
But despite that, it still transforms it incorrectly. If i invert the matrix multiplication order, it becomes somewhat better (in regards to what im expecting) but still not there.

Tyrrrz's picture

Lol, why was i multiplying by View and Projection matrix too? That has nothing to do with vertex transformation, dumb me.

winterhell's picture

So you didnt want to do screen-space culling after all. Huh.