Пример #1
0
        private static void Main()
        {
            var start    = Tuple.Point(0, 1, 0);
            var velocity = Tuple.Vector(1f, 1.8f, 0f).Normalise() * 11.25f;

            var       p       = new Projectile(start, velocity);
            var       gravity = Tuple.Vector(0, -0.1f, 0);
            var       wind    = Tuple.Vector(-0.01f, 0, 0);
            var       e       = new Environment(gravity, wind);
            const int height  = 500;

            var c  = new Canvas(900, height);
            var c1 = new Color(0, 1, 0);

            while (p.Position.Y <= 500 && p.Position.Y >= 0)
            {
                Console.WriteLine(p.Position);
                c.WriteColor(Convert.ToInt32(Math.Ceiling(p.Position.X)),
                             height - Convert.ToInt32(Math.Ceiling(p.Position.Y)),
                             c1);
                p = Tick(e, p);
            }

            File.WriteAllText("exercise.ppm", c.CreatePPMLines());
        }
Пример #2
0
        static void Main()
        {
            var rayOrigin = Tuple.Point(0, 0, -5);

            const float wallZ        = 10f;
            const float wallSize     = 7f;
            const int   canvasPixels = 100;
            const float pixelSize    = wallSize / canvasPixels;
            const float half         = wallSize / 2;

            var canvas = new Canvas(canvasPixels, canvasPixels);

            var shape = new Sphere {
                Material = new Material {
                    Color = new Color(1, 0.2f, 1)
                }
            };


            var lightPosition = Tuple.Point(-10, 10, -10);
            var lightColor    = new Color(1, 1, 1);
            var light         = new Light(lightPosition, lightColor);

            for (var y = 0; y < canvasPixels; y++)
            {
                var worldY = half - pixelSize * y;

                for (var x = 0; x < canvasPixels; x++)
                {
                    var worldX = -half + pixelSize * x;

                    var position = Tuple.Point(worldX, worldY, wallZ);
                    var r        = new Ray(rayOrigin, (position - rayOrigin).Normalise());

                    var hit = shape.Intersections(r).Hit();
                    if (hit == null)
                    {
                        continue;
                    }

                    var point  = r.Position(hit.T);
                    var normal = hit.Object.NormalAt(point);
                    var color  = hit.Object.Material.Lighting(light, point, r.Direction.Normalise(), normal);

                    Console.WriteLine("Hit");
                    canvas.WriteColor(x, y, color);
                }
            }

            File.WriteAllText("exercise6.ppm", canvas.CreatePPMLines());
        }
Пример #3
0
        static void Main()
        {
            var rayOrigin = Tuple.Point(0, 0, -5);

            const float wallZ        = 10f;
            const float wallSize     = 7f;
            const int   canvasPixels = 100;
            const float pixelSize    = wallSize / canvasPixels;
            const float half         = wallSize / 2;

            var canvas = new Canvas(canvasPixels, canvasPixels);

            var color = new Color(1, 0, 0);
            var shape = new Sphere {
                Transform = Matrix.Scaling(1, 0.5f, 1)
            };

            //var shape = new Sphere {Transform = Matrix.Scaling(0.5f, 1, 1)};
            //var shape = new Sphere {Transform = Matrix.Scaling(0.5f, 1, 1)};
            //var shape = new Sphere {Transform = Matrix.Identity.Rotate_Z(MathF.PI / 4).Scaling(0.5f, 1, 1)};

            //var shape = new Sphere {Transform = Matrix.Shearing(1f, 0, 0,0,0,0).Scaling(0.5f,1,1)};
            for (var y = 0; y < canvasPixels; y++)
            {
                var worldY = half - pixelSize * y;

                for (var x = 0; x < canvasPixels; x++)
                {
                    var worldX = -half + pixelSize * x;

                    var position = Tuple.Point(worldX, worldY, wallZ);
                    var r        = new Ray(rayOrigin, (position - rayOrigin).Normalise());

                    var xs = shape.Intersections(r);

                    Console.WriteLine($"World X {worldX}, WorldY {worldY}, Intersections Count {xs.Count}");
                    if (xs.Hit() == null)
                    {
                        continue;
                    }

                    Console.WriteLine("Hit");
                    canvas.WriteColor(x, y, color);
                }
            }

            File.WriteAllText("exercise5.ppm", canvas.CreatePPMLines());
        }
Пример #4
0
        public static Tuple ComputePoint(int hour)
        {
            var r = Matrix.Identity.Rotate_Y(hour * MathF.PI / 6f);

            return(r * Tuple.Point(0, 0, 1));
        }