Пример #1
0
        /// <summary>
        /// Event occurs when the pixel grid needs to be re-painted.
        /// </summary>
        private void pixelGrid_Paint(object sender, PaintEventArgs e)
        {
            // Initialize required objects
            SyntheticCamera   c      = new SyntheticCamera(_e, _g, _p, 15, 150, 45.0, 1.0, pixelGrid.Grid.Width, pixelGrid.Grid.Height);
            DrawingController drawer = new DrawingController(e.Graphics);
            RayTracer         ray    = new RayTracer(c, 1);

            int superWidth  = pixelGrid.Grid.Width * ray.ResolutionMultiplier;
            int superHeight = pixelGrid.Grid.Height * ray.ResolutionMultiplier;

            // Buffer holds the super width and super height colors to eventually avg and render.
            Color[,] buffer = new Color[superHeight, superWidth];

            // Loop through each pixel and trace a ray through it to find an intersection with the object
            Parallel.For(0, superHeight, (y) =>
            {
                Parallel.For(0, superWidth, (x) =>
                {
                    PointLight pointLight = new PointLight(Color.FromScRgb(1, 1, 1, 1), new Point3D(1000, 1000, 1000));
                    DirectionalLight sun  = new DirectionalLight(Color.FromScRgb(1, 1, 1, 1), new Vector3D(0, 0, -1));
                    buffer[y, x]          = ray.RayTrace(_objects, x, y, pointLight, sun);
                });
            });

            // Calculates the avg's of the super resolution buffer and displys to screen.
            for (int i = 0; i < buffer.GetLength(0); i += ray.ResolutionMultiplier)
            {
                for (int j = 0; j < buffer.GetLength(1); j += ray.ResolutionMultiplier)
                {
                    // Add all the rbg values for each super resolution block of points.
                    float r = 0, g = 0, b = 0;
                    for (int m = 0; m < ray.ResolutionMultiplier; ++m)
                    {
                        for (int n = 0; n < ray.ResolutionMultiplier; ++n)
                        {
                            r += buffer[i + m, j + n].ScR;
                            g += buffer[i + m, j + n].ScG;
                            b += buffer[i + m, j + n].ScB;
                        }
                    }

                    // Avg the block of points to 1 pixel
                    float avgR = (float)(r / Math.Pow(ray.ResolutionMultiplier, 2));
                    float avgG = (float)(g / Math.Pow(ray.ResolutionMultiplier, 2));
                    float avgB = (float)(b / Math.Pow(ray.ResolutionMultiplier, 2));

                    drawer.Color = Color.FromScRgb(1, avgR, avgG, avgB).ToDColor();
                    drawer.DrawPoint(new System.Drawing.Point(j / ray.ResolutionMultiplier, i / ray.ResolutionMultiplier));
                }
            }
        }