3D vector projection to 2D screen problems.

Jun 17 2008 7:20 PM
I am developing a function for converting 3D vector data to 2D data using 3D projection. I am using the information found on http://en.wikipedia.org/wiki/3D_projection. I am having some problems with it. They are:
Need to implement clipping regions
Need to change LookAt location....not implemented
It just acts kinda fishy...
Here is my code:
        /// <summary>
        /// Converts the 3D vector to a 2D vector. Uses Camera data.
        /// </summary>
        /// <returns>Returns a Vector2D vector.</returns>
        public Vector2D ConvertToVector2D() {

            Vector3D vector = this;                            // 3D Vector to convert
            Vector2D vectorResult = new Vector2D();            // 2D Vector to store result
            Vector3D cameraPosition = pC;                    // Camera Position (default = 0, 0, 50)
            Vector3D translation = new Vector3D();            // Temporary vector to store translation for projection
            Vector3D theta = new Vector3D(pTheta.X * (Math.PI / 180),
                                          pTheta.Y * (Math.PI / 180),
                                          pTheta.Z * (Math.PI / 180)); // Camera angle / Translate degress to radians
            Vector3D viewScreen = pE;                        // Projection screen location

            // Extracted from inline algorithms for debugging purposes
            double sinX = Math.Sin(theta.X);
            double cosX = Math.Cos(theta.X);
            double sinY = Math.Sin(theta.Y);
            double cosY = Math.Cos(theta.Y);
            double sinZ = Math.Sin(theta.Z);
            double cosZ = Math.Cos(theta.Z);

            // Translation of coordinates: taken from http://en.wikipedia.org/wiki/3D_projection
            translation.X = cosY * (sinZ * (vector.Y - cameraPosition.Y) + cosZ * (vector.X - cameraPosition.X))
                            - sinY * (vector.Z - cameraPosition.Z);
            translation.Y = sinX * (cosY * (vector.Z - cameraPosition.Z) + sinY * (sinZ * (vector.Y - cameraPosition.Y) + cosZ * (vector.X - cameraPosition.X)))
                            + cosX * (cosZ * (vector.Y - cameraPosition.Y) - sinZ * (vector.X - cameraPosition.X));
            translation.Z = cosX * (cosY * (vector.Z - cameraPosition.Z) + sinY * (sinZ * (vector.Y - cameraPosition.Y) + cosZ * (vector.X - cameraPosition.X)))
                            - sinX * (cosZ * (vector.Y - cameraPosition.Y) - sinZ * (vector.X - cameraPosition.X));
           

            // Project translated coordinates to 2D screen
            // Prevent div by zero
            double Zprojection;
            if(translation.Z == 0) {
                Zprojection = 0;
            }
            else {
                Zprojection = (viewScreen.Z / translation.Z);
            }

            vectorResult.X = (Math.Round(translation.X, 6) - viewScreen.X) * Zprojection;
            vectorResult.Y = (Math.Round(translation.Y, 6) - viewScreen.Y) * Zprojection;


            return vectorResult;
        }

Answers (5)