Exemplo n.º 1
0
    private unsafe void RenderLoop(int iterations)
    {
        // Create a ray tracer, and create a reference to "sphere2" that we are going to bounce
        var packetTracer = new Packet256Tracer(_width, _height);
        var scene        = packetTracer.DefaultScene;
        var sphere2      = (SpherePacket256)scene.Things[0]; // The first item is assumed to be our sphere
        var baseY        = sphere2.Radiuses;

        sphere2.Centers.Ys = sphere2.Radiuses;

        // Timing determines how fast the ball bounces as well as diagnostics frames/second info
        var renderingTime = new Stopwatch();
        var totalTime     = Stopwatch.StartNew();

        // Keep rendering until the iteration count is hit
        for (_frames = 0; _frames < iterations; _frames++)
        {
            // Or the rendering task has been canceled
            if (_cancellation.IsCancellationRequested)
            {
                break;
            }

            // Get the next buffer
            var rgbBuffer = _freeBuffers.GetObject();

            // Determine the new position of the sphere based on the current time elapsed
            float dy2 = 0.8f * MathF.Abs(MathF.Sin((float)(totalTime.ElapsedMilliseconds * Math.PI / 3000)));
            sphere2.Centers.Ys = Avx.Add(baseY, Vector256.Create(dy2));

            // Render the scene
            renderingTime.Reset();
            renderingTime.Start();
            ParallelOptions options = new ParallelOptions
            {
                MaxDegreeOfParallelism = _degreeOfParallelism,
                CancellationToken      = _cancellation.Token
            };
            fixed(int *ptr = rgbBuffer)
            {
                packetTracer.RenderVectorized(scene, ptr);
            }

            renderingTime.Stop();

            _framesPerSecond = (1000.0 / renderingTime.ElapsedMilliseconds);
            _freeBuffers.PutObject(rgbBuffer);
        }
    }
Exemplo n.º 2
0
    private unsafe void RenderTo(string fileName, bool wirteToFile)
    {
        var       packetTracer = new Packet256Tracer(_width, _height);
        var       scene        = packetTracer.DefaultScene;
        var       rgb          = new int[_width * 3 * _height];
        Stopwatch stopWatch    = new Stopwatch();

        stopWatch.Start();
        fixed(int *ptr = rgb)
        {
            packetTracer.RenderVectorized(scene, ptr);
        }

        stopWatch.Stop();
        TimeSpan ts          = stopWatch.Elapsed;
        string   elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                             ts.Hours, ts.Minutes, ts.Seconds,
                                             ts.Milliseconds / 10);

        Console.WriteLine("RunTime " + elapsedTime);

        if (wirteToFile)
        {
            using (var file = new System.IO.StreamWriter(fileName))
            {
                file.WriteLine("P3");
                file.WriteLine(_width + " " + _height);
                file.WriteLine("255");

                for (int i = 0; i < _height; i++)
                {
                    for (int j = 0; j < _width; j++)
                    {
                        // Each pixel has 3 fields (RGB)
                        int pos = (i * _width + j) * 3;
                        file.Write(rgb[pos] + " " + rgb[pos + 1] + " " + rgb[pos + 2] + " ");
                    }
                    file.WriteLine();
                }
            }
        }
    }