示例#1
0
        private RGBA[,] Draw()
        {
            var camera    = _cameraProvider.Get();
            var config    = _configProvider.Get();
            var matrix    = new RGBA[config.Height, config.Width];
            var lightning = _lightningProvider.Get();

            Console.WriteLine("Started render");
            Parallel.For(0, config.Width, x =>
            {
                Parallel.For(0, config.Height, y =>
                {
                    var ray             = _rayProvider.Get(x, y);
                    var lastHitDistance = float.MaxValue;
                    var allHit          = _objContainer.GetObjectsByRay(ray);

                    Parallel.ForEach(allHit, obj =>
                    {
                        if (obj.HitBy(ray, camera.Position))
                        {
                            var hitDistance = (camera.Position - obj.MidPoint).Length;

                            if (lastHitDistance > hitDistance)
                            {
                                lastHitDistance = hitDistance;
                                matrix[y, x]    = lightning.Illuminate(obj);
                            }
                        }
                    });
                });
            });
            Console.WriteLine("Finished render");

            return(matrix);
        }
示例#2
0
        public Vector3 Get(int x, int y)
        {
            var camera = _cameraProvider.Get();
            var config = _config.Get();

            var angle       = MathF.Tan(config.Fov * MathF.PI / 180);
            var aspectRatio = (float)config.Width / config.Height;

            var projectedX = (2 * (x + 0.5f) / config.Width - 1f) * angle * aspectRatio;
            var projectedY = (2 * ((y + 0.5f) / config.Height) - 1) * angle;

            var direction = camera.Direction + new Vector3(projectedX, 0, projectedY) - camera.Position;

            return(direction.Norm());
        }
示例#3
0
        public Vector3 Get(int x, int y)
        {
            var camera = _cameraProvider.Get();
            var config = _config.Get();

            var xNorm = (x - config.Width / 2) / (float)config.Width * 2;
            var yNorm = -(y - config.Height / 2) / (float)config.Height * 2;

            var vec = new Vector3(xNorm, 0, yNorm);

            vec -= camera.Position;
            vec += camera.Direction;


            return(vec.Norm());
        }