Exemplo n.º 1
0
        /// <summary>
        /// Redraws the whole image.
        /// </summary>
        private void RenderImage()
        {
            Cursor.Current = Cursors.WaitCursor;

            EnableRendering(false);

            width = ImageWidth;
            if (width <= 0)
            {
                width = panel1.Width;
            }
            height = ImageHeight;
            if (height <= 0)
            {
                height = panel1.Height;
            }
            superSampling = (int)numericSupersampling.Value;
            outputImage   = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

            IRayScene scene = FormSupport.getScene(textParam.Text);  // scene prototype

            IImageFunction imf = FormSupport.getImageFunction(scene);

            imf.Width  = width;
            imf.Height = height;

            IRenderer rend = FormSupport.getRenderer(imf);

            rend.Width        = width;
            rend.Height       = height;
            rend.Adaptive     = 0;
            rend.ProgressData = progress;
            progress.Continue = true;

            // animation:
            ITimeDependent sc = scene as ITimeDependent;

            if (sc != null)
            {
                sc.Time = (double)numTime.Value;
            }

            MT.InitThreadData();
            Stopwatch sw = new Stopwatch();

            sw.Start();

            rend.RenderRectangle(outputImage, 0, 0, width, height);

            sw.Stop();
            labelElapsed.Text = string.Format("Elapsed: {0:f1}s", 1.0e-3 * sw.ElapsedMilliseconds);

            pictureBox1.Image = outputImage;

            EnableRendering(true);

            Cursor.Current = Cursors.Default;
        }
Exemplo n.º 2
0
        public Form1(string[] args)
        {
            singleton = this;
            InitializeComponent();

            // Init rendering params:
            string name;

            FormSupport.InitializeParams(args, out name);
            Text += " (rev: " + rev + ") '" + name + '\'';

            buttonRes.Text = FormResolution.GetLabel(ref ImageWidth, ref ImageHeight);
        }
Exemplo n.º 3
0
        public Form1(string[] args)
        {
            singleton = this;
            InitializeComponent();

            // Init rendering params:
            string name;

            FormSupport.InitializeParams(args, out name);
            if (!string.IsNullOrEmpty(sceneFileName))
            {
                sceneFileName    = Path.GetFullPath(sceneFileName);
                buttonScene.Text = sceneFileName;
            }
            Text += " (rev: " + rev + ") '" + name + '\'';

            buttonRes.Text = FormResolution.GetLabel(ref ImageWidth, ref ImageHeight);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Main animation rendering thread.
        /// Initializes worker threads and collects the results.
        /// </summary>
        protected void RenderAnimation()
        {
            Cursor.Current = Cursors.WaitCursor;

            int threads = Environment.ProcessorCount;
            int t; // thread ordinal number

            WorkerThreadInit[] wti = new WorkerThreadInit[threads];

            for (t = 0; t < threads; t++)
            {
                IRayScene      sc  = FormSupport.getScene(textParam.Text);
                IImageFunction imf = FormSupport.getImageFunction(sc);
                imf.Width  = width;
                imf.Height = height;

                IRenderer r = FormSupport.getRenderer(imf);
                r.Width        = width;
                r.Height       = height;
                r.Adaptive     = 0;   // turn off adaptive bitmap synthesis completely (interactive preview not needed)
                r.ProgressData = progress;

                wti[t] = new WorkerThreadInit(r, sc as ITimeDependent, imf as ITimeDependent, width, height);
            }

            initQueue();
            sem = new Semaphore(0, 10 * threads);

            // pool of working threads:
            Thread[] pool = new Thread[threads];
            for (t = 0; t < threads; t++)
            {
                pool[t] = new Thread(new ParameterizedThreadStart(RenderWorker));
            }
            for (t = threads; --t >= 0;)
            {
                pool[t].Start(wti[t]);
            }

            // loop for collection of computed frames:
            int        frames             = 0;
            int        lastDisplayedFrame = -1;
            const long DISPLAY_GAP        = 10000L;
            long       lastDisplayedTime  = -DISPLAY_GAP;
            Stopwatch  sw = new Stopwatch();

            sw.Start();

            while (true)
            {
                sem.WaitOne();              // wait until a frame is finished

                lock ( progress )           // regular finish, escape, user break?
                {
                    if (!progress.Continue ||
                        time >= end &&
                        frames >= frameNumber)
                    {
                        break;
                    }
                }

                // there could be a frame to process:
                Result r;
                lock ( queue )
                {
                    if (queue.Count == 0)
                    {
                        continue;
                    }
                    r = queue.Dequeue();
                }

                // GUI progress indication:
                double seconds = 1.0e-3 * sw.ElapsedMilliseconds;
                double fps     = ++frames / seconds;
                SetText(string.Format(CultureInfo.InvariantCulture, "Frames (mt{0}): {1}  ({2:f0} s, {3:f2} fps)",
                                      threads, frames, seconds, fps));
                if (r.frameNumber > lastDisplayedFrame &&
                    sw.ElapsedMilliseconds > lastDisplayedTime + DISPLAY_GAP)
                {
                    lastDisplayedFrame = r.frameNumber;
                    lastDisplayedTime  = sw.ElapsedMilliseconds;
                    SetImage((Bitmap)r.image.Clone());
                }

                // save the image file:
                string fileName = string.Format("out{0:0000}.png", r.frameNumber);
                r.image.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);
                r.image.Dispose();
            }

            for (t = 0; t < threads; t++)
            {
                pool[t].Join();
                pool[t] = null;
            }

            Cursor.Current = Cursors.Default;

            StopAnimation();
        }
Exemplo n.º 5
0
        /// <summary>
        /// Main animation rendering thread.
        /// Initializes worker threads and collects the results.
        /// </summary>
        protected void RenderAnimation()
        {
            Cursor.Current = Cursors.WaitCursor;

            int    threads = Environment.ProcessorCount;
            int    t; // thread ordinal number
            int    superSampling = (int)numericSupersampling.Value;
            double minTime       = (double)numFrom.Value;
            double maxTime       = (double)numTo.Value;
            double fps           = (double)numFps.Value;

            WorkerThreadInit[] wti = new WorkerThreadInit[threads];

            // 1. preprocessing - compute simulation, animation data, etc.
            FormSupport.getScene(
                true,
                out _, out _,
                ref ActualWidth,
                ref ActualHeight,
                ref superSampling,
                ref minTime,
                ref maxTime,
                ref fps,
                textParam.Text);

            for (t = 0; t < threads; t++)
            {
                // 2. initialize data for regular frames (using the pre-computed context).
                IRayScene sc = FormSupport.getScene(
                    false,
                    out IImageFunction imf,
                    out IRenderer rend,
                    ref ActualWidth,
                    ref ActualHeight,
                    ref superSampling,
                    ref minTime,
                    ref maxTime,
                    ref fps,
                    textParam.Text);

                if (t == 0)
                {
                    // Update GUI.
                    if (ImageWidth > 0) // preserving default (form-size) resolution
                    {
                        ImageWidth  = ActualWidth;
                        ImageHeight = ActualHeight;
                        UpdateResolutionButton();
                    }
                    UpdateSupersampling(superSampling);
                    UpdateAnimationTiming(minTime, maxTime, fps);
                }

                if (sc is ITimeDependent sca)
                {
                    sc = (IRayScene)sca.Clone();
                }

                // IImageFunction.
                if (imf == null) // not defined in the script
                {
                    imf = FormSupport.getImageFunction(sc);
                }
                else
                if (imf is RayCasting imfray)
                {
                    imfray.Scene = sc;
                }
                imf.Width  = ActualWidth;
                imf.Height = ActualHeight;

                // IRenderer.
                if (rend == null) // not defined in the script
                {
                    rend = FormSupport.getRenderer(superSampling);
                }
                rend.ImageFunction = imf;
                rend.Width         = ActualWidth;
                rend.Height        = ActualHeight;
                rend.Adaptive      = 0; // turn off adaptive bitmap synthesis completely (interactive preview not needed)
                rend.ProgressData  = progress;

                wti[t] = new WorkerThreadInit(rend, sc as ITimeDependent, imf as ITimeDependent, ActualWidth, ActualHeight);
            }

            // Update animation timing.
            time = minTime;
            end  = maxTime;
            if (end <= time)
            {
                end = time + 1.0;
            }

            dt          = (fps > 0.0) ? 1.0 / fps : 25.0;
            end        += 0.5 * dt;
            frameNumber = 0;

            initQueue();
            sem = new Semaphore(0, 10 * threads);

            // Pool of working threads.
            Thread[] pool = new Thread[threads];
            for (t = 0; t < threads; t++)
            {
                pool[t] = new Thread(new ParameterizedThreadStart(RenderWorker));
            }
            for (t = threads; --t >= 0;)
            {
                pool[t].Start(wti[t]);
            }

            // Loop for collection of computed frames.
            int        frames             = 0;
            int        lastDisplayedFrame = -1;
            const long DISPLAY_GAP        = 10000L;
            long       lastDisplayedTime  = -DISPLAY_GAP;
            Stopwatch  sw = new Stopwatch();

            sw.Start();

            while (true)
            {
                sem.WaitOne();            // wait until a frame is finished

                lock (progress)           // regular finish, escape, user break?
                {
                    if (!progress.Continue ||
                        time >= end &&
                        frames >= frameNumber)
                    {
                        break;
                    }
                }

                // There could be a frame to process.
                Result r;
                lock (queue)
                {
                    if (queue.Count == 0)
                    {
                        continue;
                    }
                    r = queue.Dequeue();
                }

                // GUI progress indication:
                double seconds = 1.0e-3 * sw.ElapsedMilliseconds;
                double cfps    = ++frames / seconds;
                SetText(string.Format(CultureInfo.InvariantCulture, "Frames (mt{0}): {1}  ({2:f0} s, {3:f2} fps)",
                                      threads, frames, seconds, cfps));
                if (r.frameNumber > lastDisplayedFrame &&
                    sw.ElapsedMilliseconds > lastDisplayedTime + DISPLAY_GAP)
                {
                    lastDisplayedFrame = r.frameNumber;
                    lastDisplayedTime  = sw.ElapsedMilliseconds;
                    SetImage((Bitmap)r.image.Clone());
                }

                // Save the image file.
                string fileName = string.Format("out{0:0000}.png", r.frameNumber);
                r.image.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);
                r.image.Dispose();
            }

            for (t = 0; t < threads; t++)
            {
                pool[t].Join();
                pool[t] = null;
            }

            Cursor.Current = Cursors.Default;

            StopAnimation();
        }
Exemplo n.º 6
0
        /// <summary>
        /// Redraws the whole image.
        /// </summary>
        private void RenderImage()
        {
            Cursor.Current = Cursors.WaitCursor;

            EnableRendering(false);

            ActualWidth = ImageWidth;
            if (ActualWidth <= 0)
            {
                ActualWidth = panel1.Width;
            }
            ActualHeight = ImageHeight;
            if (ActualHeight <= 0)
            {
                ActualHeight = panel1.Height;
            }

            superSampling = (int)numericSupersampling.Value;
            double minTime = (double)numFrom.Value;
            double maxTime = (double)numTo.Value;
            double fps     = (double)numFps.Value;

            // 1. preprocessing - compute simulation, animation data, etc.
            _ = FormSupport.getScene(
                true,
                out _, out _,
                ref ActualWidth,
                ref ActualHeight,
                ref superSampling,
                ref minTime,
                ref maxTime,
                ref fps,
                textParam.Text);

            // 2. compute regular frame (using the pre-computed context).
            IRayScene scene = FormSupport.getScene(
                false,
                out IImageFunction imf,
                out IRenderer rend,
                ref ActualWidth,
                ref ActualHeight,
                ref superSampling,
                ref minTime,
                ref maxTime,
                ref fps,
                textParam.Text);

            // Update GUI.
            if (ImageWidth > 0) // preserving default (form-size) resolution
            {
                ImageWidth  = ActualWidth;
                ImageHeight = ActualHeight;
                UpdateResolutionButton();
            }
            UpdateSupersampling(superSampling);
            UpdateAnimationTiming(minTime, maxTime, fps);

            // IImageFunction.
            if (imf == null) // not defined in the script
            {
                imf = FormSupport.getImageFunction(scene);
            }
            else
            if (imf is RayCasting imfrc)
            {
                imfrc.Scene = scene;
            }
            imf.Width  = ActualWidth;
            imf.Height = ActualHeight;

            // IRenderer.
            if (rend == null)  // not defined in the script
            {
                rend = FormSupport.getRenderer(superSampling);
            }
            rend.ImageFunction = imf;
            rend.Width         = ActualWidth;
            rend.Height        = ActualHeight;
            rend.Adaptive      = 0;
            rend.ProgressData  = progress;
            progress.Continue  = true;

            // Animation time has to be set.
            if (scene is ITimeDependent sc)
            {
                sc.Time = (double)numTime.Value;
            }

            // Output image.
            outputImage = new Bitmap(ActualWidth, ActualHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

            MT.InitThreadData();
            Stopwatch sw = new Stopwatch();

            sw.Start();

            rend.RenderRectangle(outputImage, 0, 0, ActualWidth, ActualHeight);

            sw.Stop();
            labelElapsed.Text = string.Format("Elapsed: {0:f1}s", 1.0e-3 * sw.ElapsedMilliseconds);

            pictureBox1.Image = outputImage;

            EnableRendering(true);

            Cursor.Current = Cursors.Default;
        }