Example #1
0
        /// <inheritdoc />
        public override double GenerateRay(CameraSample sample, out Ray ray)
        {
            //ProfilePhase prof(Prof::GenerateCameraRay);
            // Compute raster and camera sample positions
            Point3D pFilm   = new Point3D(sample.FilmPoint.X, sample.FilmPoint.Y, 0.0);
            Point3D pCamera = RasterToCamera.AtPoint(pFilm);

            ray = new Ray(new Point3D(0.0, 0.0, 0.0), pCamera.ToVector3D().Normalize());
            // Modify ray for depth of field
            if (LensRadius > 0.0)
            {
                // Sample point on lens
                Point2D pLens = LensRadius * Sampling.ConcentricSampleDisk(sample.LensPoint);

                // Compute point on plane of focus
                double  ft     = FocalDistance / ray.Direction.Z;
                Point3D pFocus = ray.AtPoint(ft);

                // Update ray for effect of lens
                ray.Origin    = new Point3D(pLens.X, pLens.Y, 0.0);
                ray.Direction = (pFocus - ray.Origin).ToVector3D().Normalize();
            }
            ray.Time   = PbrtMath.Lerp(sample.Time, ShutterOpen, ShutterClose);
            ray.Medium = Medium;
            ray        = CameraToWorld.ExecuteTransform(ray);
            return(1.0);
        }
Example #2
0
        /// <inheritdoc />
        public override void Pdf_We(Ray ray, out double pdfPos, out double pdfDir)
        {
            // Interpolate camera matrix and fail if $\w{}$ is not forward-facing
            CameraToWorld.Interpolate(ray.Time, out Transform c2w);
            double cosTheta = ray.Direction.Dot(c2w.ExecuteTransform(new Vector3D(0.0, 0.0, 1.0)));

            if (cosTheta <= 0)
            {
                pdfPos = pdfDir = 0.0;
                return;
            }

            // Map ray $(\p{}, \w{})$ onto the raster grid
            Point3D pFocus  = ray.AtPoint((LensRadius > 0.0 ? FocalDistance : 1.0) / cosTheta);
            Point3D pRaster = RasterToCamera.Inverse().ExecuteTransform(c2w.Inverse().ExecuteTransform(pFocus));

            // Return zero probability for out of bounds points
            Bounds2I sampleBounds = Film.GetSampleBounds();

            if (pRaster.X < sampleBounds.MinPoint.X || pRaster.X >= sampleBounds.MaxPoint.X ||
                pRaster.Y < sampleBounds.MinPoint.Y || pRaster.Y >= sampleBounds.MaxPoint.Y)
            {
                pdfPos = pdfDir = 0.0;
                return;
            }

            // Compute lens area of perspective camera
            double lensArea = LensRadius != 0.0 ? (Math.PI * LensRadius * LensRadius) : 1.0;

            pdfPos = 1.0 / lensArea;
            pdfDir = 1.0 / (_a * cosTheta * cosTheta * cosTheta);
        }
Example #3
0
        /// <inheritdoc />
        public override Spectrum Sample_Wi(
            Interaction it,
            Point2D u,
            out Vector3D wi,
            out double pdf,
            out Point2D pRaster,
            out VisibilityTester vis)
        {
            // Uniformly sample a lens interaction _lensIntr_
            Point2D     pLens      = LensRadius * Sampling.ConcentricSampleDisk(u);
            Point3D     pLensWorld = CameraToWorld.ExecuteTransform(it.Time, new Point3D(pLens.X, pLens.Y, 0.0));
            Interaction lensIntr   = new Interaction(pLensWorld, it.Time, new MediumInterface(Medium));

            lensIntr.N = (CameraToWorld.ExecuteTransform(it.Time, new Point3D(0.0, 0.0, 1.0))).ToVector3D().ToNormal3D();

            // Populate arguments and compute the importance value
            vis = new VisibilityTester(it, lensIntr);
            wi  = (lensIntr.P - it.P).ToVector3D();
            double dist = wi.Length();

            wi /= dist;

            // Compute PDF for importance arriving at _ref_

            // Compute lens area of perspective camera
            double lensArea = LensRadius != 0.0 ? (Math.PI * LensRadius * LensRadius) : 1.0;

            pdf = (dist * dist) / (lensIntr.N.AbsDot(wi) * lensArea);
            return(We(lensIntr.SpawnRay(-wi), out pRaster));
        }
Example #4
0
        public override float GenerateRayDifferential(CameraSample sample, out RayDifferential ray)
        {
            // Generate raster and camera samples
            Point Pras    = new Point(sample.ImageX, sample.ImageY, 0);
            Point Pcamera = RasterToCamera.TransformPoint(ref Pras);

            ray = new RayDifferential(new Point(0, 0, 0), Vector.Normalize((Vector)Pcamera), 0.0f);
            // Modify ray for depth of field
            if (LensRadius > 0.0f)
            {
                // Sample point on lens
                float lensU, lensV;
                MonteCarloUtilities.ConcentricSampleDisk(sample.LensU, sample.LensV, out lensU, out lensV);
                lensU *= LensRadius;
                lensV *= LensRadius;

                // Compute point on plane of focus
                float ft     = FocalDistance / ray.Direction.Z;
                Point Pfocus = ray.Evaluate(ft);

                // Update ray for effect of lens
                ray.Origin    = new Point(lensU, lensV, 0.0f);
                ray.Direction = Vector.Normalize(Pfocus - ray.Origin);
            }
            // Compute offset rays for _PerspectiveCamera_ ray differentials
            ray.RxOrigin    = ray.RyOrigin = ray.Origin;
            ray.RxDirection = Vector.Normalize((Vector)Pcamera + _dxCamera);
            ray.RyDirection = Vector.Normalize((Vector)Pcamera + _dyCamera);
            ray.Time        = sample.Time;
            ray             = CameraToWorld.TransformRayDifferential(ray);
            return(1.0f);
        }
Example #5
0
            public TransformBufferLayout(Matrix cameraToClipSpace, Matrix worldToCamera, Matrix objectToWorld)
            {
                Matrix clipSpaceToCamera = cameraToClipSpace;

                clipSpaceToCamera.Invert();
                Matrix cameraToWorld = worldToCamera;

                cameraToWorld.Invert();
                Matrix worldToObject = objectToWorld;

                worldToObject.Invert();

                CameraToClipSpace = cameraToClipSpace;
                ClipSpaceToCamera = clipSpaceToCamera;
                WorldToCamera     = worldToCamera;
                CameraToWorld     = cameraToWorld;
                WorldToClipSpace  = Matrix.Multiply(worldToCamera, cameraToClipSpace);
                ClipSpaceToWorld  = Matrix.Multiply(clipSpaceToCamera, cameraToWorld);
                ObjectToWorld     = objectToWorld;
                WorldToObject     = worldToObject;
                ObjectToCamera    = Matrix.Multiply(objectToWorld, worldToCamera);
                ObjectToClipSpace = Matrix.Multiply(ObjectToCamera, cameraToClipSpace);

                // transpose all as mem layout in hlsl constant buffer is row based
                CameraToClipSpace.Transpose();
                ClipSpaceToCamera.Transpose();
                WorldToCamera.Transpose();
                CameraToWorld.Transpose();
                WorldToClipSpace.Transpose();
                ClipSpaceToWorld.Transpose();
                ObjectToWorld.Transpose();
                WorldToObject.Transpose();
                ObjectToCamera.Transpose();
                ObjectToClipSpace.Transpose();
            }
Example #6
0
        // float GenerateRay()
        public override float GenerateRay(CameraSample sample, out Ray ray)
        {
            Point3 Pras    = new Point3(sample.imageX, sample.imageY);
            Point3 Pcamera = RasterToCamera.Caculate(Pras);

            ray = new Ray(Pcamera, new Vector(0, 0, 1), 0, LR.INFINITY);//正交相机方向固定

            #region modify ray for depth of filed
            if (lensRadius > 0.0f)
            {
                // Sample point on lens
                float lensU = 0, lensV = 0;

                LR.ConcentricSampleDisk(sample.lensU, sample.lensV, ref lensU, ref lensV);
                lensU *= lensRadius;
                lensV *= lensRadius;

                // Compute point on plane of focus
                float  ft     = focalDistance / ray.d.z;
                Point3 Pfocus = ray.Transfer(ft);

                // Update ray for effect of lens
                ray.o = new Point3(lensU, lensV, 0.0f);
                ray.d = LR.Normalize(Pfocus - ray.o);
            }
            #endregion
            ray.time = sample.time;
            ray      = CameraToWorld.Caculate(ray);
            return(1.0f);
        }
Example #7
0
        /// <inheritdoc />
        public override double GenerateRayDifferential(CameraSample sample, out RayDifferential ray)
        {
            //ProfilePhase prof(Prof::GenerateCameraRay);
            // Compute raster and camera sample positions
            Point3D  pFilm   = new Point3D(sample.FilmPoint.X, sample.FilmPoint.Y, 0.0);
            Point3D  pCamera = RasterToCamera.ExecuteTransform(pFilm);
            Vector3D dir     = new Vector3D(pCamera.X, pCamera.Y, pCamera.Z).Normalize();

            ray = new RayDifferential(new Point3D(0.0, 0.0, 0.0), dir);
            // Modify ray for depth of field
            if (LensRadius > 0.0)
            {
                // Sample point on lens
                Point2D pLens = LensRadius * Sampling.ConcentricSampleDisk(sample.LensPoint);

                // Compute point on plane of focus
                double  ft     = FocalDistance / ray.Direction.Z;
                Point3D pFocus = ray.AtPoint(ft);

                // Update ray for effect of lens
                ray.Origin    = new Point3D(pLens.X, pLens.Y, 0.0);
                ray.Direction = (pFocus - ray.Origin).ToVector3D().Normalize();
            }

            // Compute offset rays for _PerspectiveCamera_ ray differentials
            if (LensRadius > 0.0)
            {
                // Compute _PerspectiveCamera_ ray differentials accounting for lens

                // Sample point on lens
                Point2D  pLens  = LensRadius * Sampling.ConcentricSampleDisk(sample.LensPoint);
                Vector3D dx     = (pCamera.ToVector3D() + _dxCamera).Normalize();
                double   ft     = FocalDistance / dx.Z;
                Point3D  pFocus = new Point3D(0.0, 0.0, 0.0) + (ft * dx).ToPoint3D();
                ray.RxOrigin    = new Point3D(pLens.X, pLens.Y, 0.0);
                ray.RxDirection = (pFocus - ray.RxOrigin).ToVector3D().Normalize();

                Vector3D dy = (pCamera.ToVector3D() + _dyCamera).Normalize();
                ft              = FocalDistance / dy.Z;
                pFocus          = new Point3D(0.0, 0.0, 0.0) + (ft * dy).ToPoint3D();
                ray.RyOrigin    = new Point3D(pLens.X, pLens.Y, 0.0);
                ray.RyDirection = (pFocus - ray.RyOrigin).ToVector3D().Normalize();
            }
            else
            {
                ray.RxOrigin    = ray.RyOrigin = ray.Origin;
                ray.RxDirection = (pCamera.ToVector3D() + _dxCamera).Normalize();
                ray.RyDirection = (pCamera.ToVector3D() + _dyCamera).Normalize();
            }
            ray.Time   = PbrtMath.Lerp(sample.Time, ShutterOpen, ShutterClose);
            ray.Medium = Medium;
            ray        = new RayDifferential(CameraToWorld.ExecuteTransform(ray))
            {
                HasDifferentials = true
            };
            return(1.0);
        }
Example #8
0
        /// <inheritdoc />
        public override Spectrum We(Ray ray, out Point2D pRaster2)
        {
            // Interpolate camera matrix and check if $\w{}$ is forward-facing
            CameraToWorld.Interpolate(ray.Time, out Transform c2w);
            double cosTheta = ray.Direction.Dot(c2w.ExecuteTransform(new Vector3D(0.0, 0.0, 1.0)));

            if (cosTheta <= 0.0)
            {
                pRaster2 = null;
                return(Spectrum.Create(0.0));
            }

            // Map ray $(\p{}, \w{})$ onto the raster grid
            Point3D pFocus  = ray.AtPoint((LensRadius > 0.0 ? FocalDistance : 1.0) / cosTheta);
            Point3D pRaster = RasterToCamera.ExecuteTransform(c2w.Inverse().ExecuteTransform(pFocus));

            //Point3D pRaster = Inverse(RasterToCamera)(Inverse(c2w.Inverse)(pFocus));

            // Return raster position if requested
            pRaster2 = new Point2D(pRaster.X, pRaster.Y);

            // Return zero importance for out of bounds points
            Bounds2I sampleBounds = Film.GetSampleBounds();

            if (pRaster.X < sampleBounds.MinPoint.X || pRaster.Z >= sampleBounds.MaxPoint.X ||
                pRaster.Y < sampleBounds.MinPoint.Y || pRaster.Y >= sampleBounds.MaxPoint.Y)
            {
                return(Spectrum.Create(0.0));
            }

            // Compute lens area of perspective camera
            double lensArea = LensRadius != 0.0 ? (Math.PI * LensRadius * LensRadius) : 1.0;

            // Return importance for point on image plane
            double cos2Theta = cosTheta * cosTheta;

            return(Spectrum.Create(1.0 / (_a * lensArea * cos2Theta * cos2Theta)));
        }
Example #9
0
        public override float GenerateRayDifferential(CameraSample sample, out RayDifferential ray)
        {
            Point3 Pras    = new Point3(sample.imageX, sample.imageY);
            Point3 Pcamera = RasterToCamera.Caculate(Pras);

            ray = new RayDifferential(Pcamera, new Vector(0, 0, 1), 0, 1.0f / 0);

            #region Modify ray for depth of field
            if (lensRadius > 0.0)
            {
                // Sample point on lens
                float lensU = 0, lensV = 0;
                LR.ConcentricSampleDisk(sample.lensU, sample.lensV, ref lensU, ref lensV);
                lensU *= lensRadius;
                lensV *= lensRadius;

                // Compute point on plane of focus
                float  ft     = focalDistance / ray.d.z;
                Point3 Pfocus = ray.Transfer(ft);

                // Update ray for effect of lens
                ray.o = new Point3(lensU, lensV, 0.0f);
                ray.d = LR.Normalize(Pfocus - ray.o);
            }
            #endregion
            ray.time = sample.time;

            #region Compute ray differentials for _OrthoCamera_
            if (lensRadius > 0)
            {
                // Compute _OrthoCamera_ ray differentials with defocus blur

                // Sample point on lens
                float lensU = 0, lensV = 0;
                LR.ConcentricSampleDisk(sample.lensU, sample.lensV, ref lensU, ref lensV);
                lensU *= lensRadius;
                lensV *= lensRadius;

                float ft = focalDistance / ray.d.z;

                Point3 pFocus = Pcamera + dxCamera + (ft * new Vector(0, 0, 1));
                ray.rxOrigin    = new Point3(lensU, lensV, 0.0f);
                ray.rxDirection = LR.Normalize(pFocus - ray.rxOrigin);

                pFocus          = Pcamera + dyCamera + (ft * new Vector(0, 0, 1));
                ray.ryOrigin    = new Point3(lensU, lensV, 0.0f);
                ray.ryDirection = LR.Normalize(pFocus - ray.ryOrigin);
            }
            else
            {
                ray.rxOrigin    = ray.o + dxCamera;
                ray.ryOrigin    = ray.o + dyCamera;
                ray.rxDirection = ray.ryDirection = ray.d;
            }
            #endregion

            ray.hasDifferentials = true;
            ray = CameraToWorld.Caculate(ray);

            return(1.0f);
        }