public RTF.Canvas DrawRedCircle(RTF.Shapes.Sphere s)
        {
            var    rayOrigin   = RTF.PointType.Point(0, 0, -5);
            double wallZ       = 10;
            double wallSize    = 7;
            var    canvasPixel = 100;
            double pixelSize   = (double)wallSize / (double)canvasPixel;
            double half        = wallSize / 2;

            var canvas = new RTF.Canvas(canvasPixel, canvasPixel, new RTF.Color(0, 0, 0));
            var color  = new RTF.Color(255, 0, 0);
            var shape  = s;

            for (int y = 0; y < canvasPixel; y++)
            {
                var worldY = half - pixelSize * y;
                for (int x = 0; x < canvasPixel; x++)
                {
                    var worldX   = -half + pixelSize * x;
                    var position = RTF.PointType.Point(worldX, worldY, wallZ);

                    var r  = new RTF.Ray(rayOrigin, (position - rayOrigin).Normalize());
                    var xs = shape.Intersect(r);

                    var hit = RTF.Intersection.Hit(xs);

                    if (hit != null)
                    {
                        canvas.WritePixel(x, y, color);
                    }
                }
            }

            return(canvas);
        }
        public void ConstructPPMHeader()
        {
            string expected = "P3\n5 3\n255";
            var    canvas   = new RTF.Canvas(5, 3);

            Assert.Equal(expected, string.Join("\n", canvas.CreatePPMHeader()));
        }
        private RTCf.Canvas GenerateProjectile(int width, int height)
        {
            var canvas = new RTCf.Canvas(width, height, black);

            var m   = double.Parse(Magnetude.Text);
            var vel = new Tuple <double, double, double>(
                double.Parse(VelocityX.Text),
                double.Parse(VelocityY.Text),
                double.Parse(VelocityZ.Text));

            var proj = new obj.Projectile(
                RTCf.PointType.Point(0, 1, 0),
                RTCf.PointType.Vector(vel.Item1, vel.Item2, vel.Item3).Normalize() * m);
            var env = new obj.Environment(
                RTCf.PointType.Vector(0, -0.1, 0),
                RTCf.PointType.Vector(-0.01, 0, 0));

            canvas.WritePixel((int)proj.Position.X, (height - 1) - (int)proj.Position.Y, red);

            foreach (var coord in obj.Projectile.GetTick(env, proj))
            {
                canvas.WritePixel((int)coord.X, (height - 1) - (int)coord.Y, red);
            }

            return(canvas);
        }
        public void CreateCanvas()
        {
            var c = new RTF.Canvas(10, 20);

            Assert.Equal(10, c.Width);
            Assert.Equal(20, c.Height);
            Assert.True(c.EveryPixelsAre(new RTF.Color(0, 0, 0)));
        }
        public void WritingPixelToCanvas()
        {
            var c   = new RTF.Canvas(10, 20);
            var red = new RTF.Color(1, 0, 0);

            c.WritePixel(2, 3, red);

            Assert.Equal(red, c.PixelAt(2, 3));
        }
Exemple #6
0
        public RTF.Canvas CreateCircle(RTF.Shapes.Sphere s)
        {
            var lightPos = RTF.PointType.Point(-10, 10, -10);
            var light    = new RTF.Light(lightPos, RTF.Color.White);

            var    rayOrigin   = RTF.PointType.Point(0, 0, -5);
            double wallZ       = 10;
            double wallSize    = 7;
            var    canvasPixel = 100;
            double pixelSize   = wallSize / canvasPixel;
            double half        = wallSize / 2;

            var canvas = new RTF.Canvas(canvasPixel, canvasPixel, RTF.Color.Black);

            var shape = s;

            shape.Material.Color = RTF.Color.Magenta;

            for (int y = 0; y < canvasPixel; y++)
            {
                var worldY = half - pixelSize * y;
                for (int x = 0; x < canvasPixel; x++)
                {
                    var worldX   = -half + pixelSize * x;
                    var position = RTF.PointType.Point(worldX, worldY, wallZ);

                    var r  = new RTF.Ray(rayOrigin, (position - rayOrigin).Normalize());
                    var xs = shape.Intersect(r);

                    var hit = RTF.Intersection.Hit(xs);

                    if (hit != null)
                    {
                        var point  = RTH.Transformations.Position(r, hit.T);
                        var normal = hit.Object.NormalAt(point);
                        var eye    = -r.Direction;

                        var color = RTF.Light.Lighting(
                            (hit.Object as RTF.Shapes.Sphere).Material,
                            hit.Object,
                            light,
                            point,
                            eye,
                            normal,
                            false);

                        canvas.WritePixel(x, y, color);
                    }
                }
            }

            return(canvas);
        }
        private void GenerateButton_Click(object sender, RoutedEventArgs e)
        {
            var clock = CreateClock();

            var width  = int.Parse(WidthCanvas.Text);
            var height = int.Parse(HeightCanvas.Text);

            RTF.Canvas canvas = ConvertPointsToCanvas(clock, width, height);

            string filename = $"{FolderPath.Text}\\Clock[{WidthCanvas.Text}x{HeightCanvas.Text}].ppm";

            canvas.SaveAsPPMFile(filename);
        }
        public void RenderingWorldWithCamera()
        {
            var w = RTF.World.Default();
            var c = new RTF.Camera(11, 11, Math.PI / 2);

            var from = RTF.PointType.Point(0, 0, -5);
            var to   = RTF.PointType.Point(0, 0, 0);
            var up   = RTF.PointType.Vector(0, 1, 0);

            c.Transform = RTH.Transformations.ViewTransform(from, to, up);

            RTF.Canvas image = RTF.Canvas.Render(c, w);

            var exp = new RTF.Color(0.38066, 0.47583, 0.2855);

            CustomAssert.Equal(exp, image.PixelAt(5, 5), 5);
        }
        public void ConstructPPMPixelData()
        {
            var expected = "255 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" +
                           "0 0 0 0 0 0 0 128 0 0 0 0 0 0 0\n" +
                           "0 0 0 0 0 0 0 0 0 0 0 0 0 0 255";

            var c  = new RTF.Canvas(5, 3);
            var c1 = new RTF.Color(1.5, 0, 0);
            var c2 = new RTF.Color(0, 0.5, 0);
            var c3 = new RTF.Color(-0.5, 0, 1);

            c.WritePixel(0, 0, c1);
            c.WritePixel(2, 1, c2);
            c.WritePixel(4, 2, c3);

            Assert.Equal(expected, string.Join("\n", c.CreatePPMCanvas()));
        }
        public RTF.Canvas ConvertPointsToCanvas(List <RTF.PointType> points, int width, int height)
        {
            RTF.Canvas c      = new RTF.Canvas(width, height, new RTF.Color(0, 0, 0));
            var        radius = (3d / 8d) * width;

            var color = new RTF.Color(255, 255, 255);

            foreach (var point in points)
            {
                int x = (int)((radius * point.X) + width / 2);
                int y = (int)((radius * point.Z) + height / 2);

                c.WritePixel(x, y, color);
            }

            return(c);
        }
        private void GenerateButton_Click(object sender, RoutedEventArgs e)
        {
            var watch = System.Diagnostics.Stopwatch.StartNew();

            GenerateButton.IsEnabled = false;
            var canvasSize = new Tuple <int, int>(int.Parse(WidthCanvas.Text), int.Parse(HeightCanvas.Text));

            RTCf.Canvas canvas = GenerateProjectile(canvasSize.Item1, canvasSize.Item2);


            watch.Stop();
            var    elapsedMs = watch.ElapsedMilliseconds;
            string filename  = $"{FolderPath.Text}\\{FileName.Text}[{WidthCanvas.Text}x{HeightCanvas.Text}][{VelocityX.Text},{VelocityY.Text},{VelocityZ.Text}][{Magnetude.Text}] [{elapsedMs}ms].ppm";

            canvas.SaveAsPPMFile(filename);
            GenerateButton.IsEnabled = true;
        }