ViewPort.Project

Aug 5, 2010 at 3:25 PM

Hi, i didn't find ViewPort.Project method so i implemented it by myself, i hope someone, not only me, will find this useful :) I would be very much appreciated if you'll add this or your own implementation to source.

here is the code:

        public Vector Project(Vector source, Matrix projection, Matrix view, Matrix world)
        {
            Matrix matrix = (world * view) * projection;
            Vector vector = Vector.TransformNormal(source, matrix);
            float a = (((source.X * matrix[0, 3]) + (source.Y * matrix[1, 3])) + (source.Z * matrix[2, 3])) + matrix[3, 3];
            
            if (!WithinEpsilon(a, 1f))
            {
                vector = (Vector)(vector / (a));
            }

            vector.X = (((vector.X + 1f) * 0.5f) * Width);
            vector.Y = (((-vector.Y + 1f) * 0.5f) * Height);
            vector.Z = (vector.Z * (MaxDepth - MinDepth)) + MinDepth;

            return vector;
        }

 

Coordinator
Aug 5, 2010 at 4:12 PM

Excellent. I can sure add it, thanks for the contribution - would it be OK if I changed the signature a bit? The projection and view matrix is accessible in the Viewport.

 

Aug 5, 2010 at 4:21 PM

Sure, no problem. Thank you.

Coordinator
Aug 5, 2010 at 4:36 PM
Implemented and pushed :)