private Light GetLight(MeshObject hitObject, Intersection hitPoint)
        {
            var light = new Light {
                Brightness = AmbientBrightness
            };

            for (var j = 0; j < LightSources.Count; ++j)
            {
                var lightSource  = LightSources[j];
                var shadowRay    = lightSource.GetShadowRay(hitPoint.IntersectionPoint);
                var currentLight = new Light {
                    Brightness = lightSource.Brightness
                };
                hitObject.Material.SurfaceShader.TryGetBrigthness(currentLight, hitPoint, shadowRay, out var shadedBrigthness);
                currentLight.Brightness = shadedBrigthness;

                if (currentLight.Brightness > light.Brightness)
                {
                    light = currentLight;
                }
            }

            return(light);
        }
Exemple #2
0
            public void ProjectToZBuffer(Vector3 v1, Vector3 v2, Vector3 v3, Vector3 n, ZBufferItem[] zbuffer, MeshObject obj)
            {
                var p1 = GetPlaneIntersectionPoint(v1);
                var p2 = GetPlaneIntersectionPoint(v2);
                var p3 = GetPlaneIntersectionPoint(v3);

                //if (p1.X == 0 && p2.X == 0 && p3.X == 0
                //    || p1.Y == 0 && p2.Y == 0 && p3.Y == 0
                //    || p1.X == rayFactory.Width - 1 && p2.X == rayFactory.Width - 1 && p3.X == rayFactory.Width - 1
                //    || p1.Y == rayFactory.Height - 1 && p2.Y == rayFactory.Height - 1 && p3.Y == rayFactory.Height - 1)
                //{
                //    return;
                //}

                var minX = Math.Min(Math.Min(p1.X, p2.X), p3.X);
                var maxX = Math.Max(Math.Max(p1.X, p2.X), p3.X);

                var minY = Math.Min(Math.Min(p1.Y, p2.Y), p3.Y);
                var maxY = Math.Max(Math.Max(p1.Y, p2.Y), p3.Y);

                for (var x = minX; x <= maxX; ++x)
                {
                    for (var y = minY; y <= maxY; ++y)
                    {
                        var ray       = GetCameraRay(x, y);
                        var isSurface = ray.IsSurfaceHit(n);
                        if (!isSurface)
                        {
                            return;
                        }

                        if (ray.TryIntersectTriangle(v1, v2, v3, out var u, out var v, out _))
                        {
                            var d = Vector3.Dot(v1 - ray.StartPoint, n) / Vector3.Dot(n, ray.Direction);
                            var i = y * pixelWidth + x;
                            if (zbuffer[i].Intersection.Distance > d)
                            {
                                zbuffer[i].Intersection.Distance = d;
                                zbuffer[i].Object = obj;
                                zbuffer[i].Intersection.NormalVector = n;
                                zbuffer[i].u = u;
                                zbuffer[i].v = v;
                                zbuffer[i].Intersection.IntersectionPoint = d * ray.Direction + ray.StartPoint;
                            }
                        }
                    }
                }
            }