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);
        }
Beispiel #2
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();
        }