public Matrix GetGizmoModificationMatrix()
        {
            Matrix matrix = ModificationMatrix.GetIdentityMatrix(4);

            matrix = matrix.Multiply(ModificationMatrix.GetRotateXMatrix(MathHelp.DegreesToRadians(AngleX)));
            matrix = matrix.Multiply(ModificationMatrix.GetRotateYMatrix(MathHelp.DegreesToRadians(AngleY)));
            matrix = matrix.Multiply(ModificationMatrix.GetRotateZMatrix(MathHelp.DegreesToRadians(AngleZ)));
            return(matrix);
        }
Example #2
0
        public void RotatePositionUpDown(double angle)
        {
            Matrix rotateMatrix = ModificationMatrix.GetMoveMatrix(-Target.X, -Target.Y, -Target.Z);

            rotateMatrix = rotateMatrix.Multiply(Position.GetProjectiveCoordinates());
            Position.Set(rotateMatrix[0, 0], rotateMatrix[1, 0], rotateMatrix[2, 0]);

            angle += MathHelp.RadiansToDegrees(Position.Theta);
            angle  = Math.Min(Math.Max(angle, MIN_THETA), MAX_THETA);
            Position.SetTheta(MathHelp.DegreesToRadians(angle));
            AngleTheta = 180 - (int)angle;

            rotateMatrix = ModificationMatrix.GetMoveMatrix(Target.X, Target.Y, Target.Z);
            rotateMatrix = rotateMatrix.Multiply(Position.GetProjectiveCoordinates());
            Position.Set(rotateMatrix[0, 0], rotateMatrix[1, 0], rotateMatrix[2, 0]);
        }
Example #3
0
        public void RotateTargetLeftRight(double angle)
        {
            Matrix rotateMatrix = ModificationMatrix.GetMoveMatrix(-Position.X, -Position.Y, -Position.Z);

            rotateMatrix = rotateMatrix.Multiply(Target.GetProjectiveCoordinates());
            Target.Set(rotateMatrix[0, 0], rotateMatrix[1, 0], rotateMatrix[2, 0]);

            angle += MathHelp.RadiansToDegrees(Target.Phi);
            angle %= 360;
            angle  = angle < 0 ? 360 + angle : angle;

            Target.SetPhi(MathHelp.DegreesToRadians(angle));
            AnglePhi = (int)angle;

            rotateMatrix = ModificationMatrix.GetMoveMatrix(Position.X, Position.Y, Position.Z);
            rotateMatrix = rotateMatrix.Multiply(Target.GetProjectiveCoordinates());
            Target.Set(rotateMatrix[0, 0], rotateMatrix[1, 0], rotateMatrix[2, 0]);
        }
Example #4
0
        public Matrix GetCameraMatrix()
        {
            Matrix matrixInverted = ModificationMatrix.GetMoveMatrix(-Position.X, -Position.Y, -Position.Z);

            Vector3D N = new Vector3D(Target.X - Position.X, Target.Y - Position.Y, Target.Z - Position.Z);
            Vector3D V = new Vector3D(0, 1, 0);
            Vector3D U = V ^ N;

            V = U ^ N;

            U.Normalize();
            V.Normalize();
            N.Normalize();

            cameraMatrix = ModificationMatrix.GetUVNProjectionMatrix(
                U.X, U.Y, U.Z,
                V.X, V.Y, V.Z,
                N.X, N.Y, N.Z);
            cameraMatrix = cameraMatrix.Multiply(matrixInverted);

            return(cameraMatrix);
        }
Example #5
0
        public void PaintObjects()
        {
            FastBitmap bitmap = new FastBitmap(new Bitmap(Camera.FrameWidth, Camera.FrameHeight));

            double[,] buffer = new double[Camera.FrameWidth, Camera.FrameHeight];
            for (int i = 0; i < Camera.FrameWidth; ++i)
            {
                for (int j = 0; j < Camera.FrameHeight; ++j)
                {
                    buffer[i, j] = double.MaxValue;
                }
            }

            Matrix cameraMatrix = Camera.GetCameraMatrix();

            if (PanObject == null)
            {
                if (IsGizmoVisible && IsVisibleForCamera(new Point3D(0, 0, 0), GIZMO_SIZE, cameraMatrix))
                {
                    DrawGizmo(bitmap, buffer, ModificationMatrix.GetIdentityMatrix(4), new Point3D(0, 0, 0), cameraMatrix);
                }

                Matrix lightMatrix = ModificationMatrix.GetIdentityMatrix(4);
                foreach (Vector3D light in Lights)
                {
                    Point3D lightPosition = new Point3D(light);
                    if (IsVisibleForCamera(lightPosition, lightPosition.Length, cameraMatrix))
                    {
                        Sphere lightSphere = new Sphere(lightPosition, 3, 4, Color.Yellow);
                        foreach (Face face in lightSphere.Faces)
                        {
                            List <Point3D> points = new List <Point3D>();
                            foreach (Point3D point in face.Points)
                            {
                                points.Add(point.Clone());
                            }
                            DrawLines(bitmap, buffer, points, lightSphere.Color, lightMatrix, lightPosition, lightMatrix, lightPosition, cameraMatrix);
                        }
                    }
                }
            }

            foreach (SceneObject sceneObject in Objects)
            {
                if (IsVisibleForCamera(sceneObject.BasePoint, sceneObject.MaxLength, cameraMatrix) && (PanObject == null || PanObject.Equals(sceneObject)))
                {
                    Matrix objectMatrix            = sceneObject.GetModificationMatrix();
                    Matrix gizmoModificationMatrix = sceneObject.GetGizmoModificationMatrix();
                    if (Mode == MODE.WIREFRAME && IsGizmoVisible)
                    {
                        DrawGizmo(bitmap, buffer, gizmoModificationMatrix, sceneObject.BasePoint, cameraMatrix);
                    }
                    foreach (ScenePrimitive scenePrimitive in sceneObject.ScenePrimitives)
                    {
                        Matrix primitiveMatrix = scenePrimitive.GetModificationMatrix();
                        foreach (Face face in scenePrimitive.Primitive.Faces)
                        {
                            List <Point3D> points = new List <Point3D>();
                            foreach (Point3D point in face.Points)
                            {
                                points.Add(point.Clone());
                            }

                            if (Mode == MODE.WIREFRAME)
                            {
                                DrawLines(bitmap, buffer, points, scenePrimitive.Primitive.Color, primitiveMatrix, scenePrimitive.Primitive.BasePoint, objectMatrix, sceneObject.BasePoint, cameraMatrix);
                            }
                            else
                            {
                                ApplyMatrix(points, primitiveMatrix, scenePrimitive.Primitive.BasePoint, objectMatrix, sceneObject.BasePoint);

                                double brightness = CalculateLight(new Face(new Point3D[] { points[0], points[1], points[2] }));
                                Color  color      = Color.FromArgb(
                                    (int)(scenePrimitive.Primitive.Color.R * brightness),
                                    (int)(scenePrimitive.Primitive.Color.G * brightness),
                                    (int)(scenePrimitive.Primitive.Color.B * brightness)
                                    );

                                ConvertLocalToCamera(points, cameraMatrix);
                                ConvertCameraToScreen(points);

                                Triangle(new Vector3D(points[0]), new Vector3D(points[1]), new Vector3D(points[2]), color, bitmap, buffer);
                            }
                        }
                    }
                }
            }

            FastBitmap bufferImage = new FastBitmap(new Bitmap(canvas.ClientSize.Width, canvas.ClientSize.Height));
            double     minimum     = double.MaxValue;
            double     maximum     = double.MinValue;

            for (int i = 0; i < Camera.FrameWidth; ++i)
            {
                for (int j = 0; j < Camera.FrameHeight; ++j)
                {
                    minimum = Math.Min(minimum, Math.Max(buffer[i, j], double.MinValue));
                    maximum = Math.Max(maximum, Math.Min(buffer[i, j], double.MaxValue));
                }
            }

            canvas.Image = bitmap.GetBitmap();
        }