public override void InitPath(IPathProcessor buffer) { base.InitPath(buffer); this.scene = pathIntegrator.Scene; this.Radiance = new RgbSpectrum(0f); this.PathState = BDPTSamplerState.InitPath; if (this.connectRays == null) this.connectRays = new ConnectRayInfo[scene.MaxPathDepth * scene.MaxPathDepth]; lightPath = new VertexInfo[MaxLightDepth]; this.Sample = pathIntegrator.Sampler.GetSample(null); lightVertices = 0; //Init eye ray this.EyeThroughput = new RgbSpectrum(1f); pathIntegrator.Scene.Camera.GetRay(Sample.imageX, Sample.imageY, out this.PathRay); this.RayIndex = -1; this.LightRayIndex = -1; this.eyePdf = 1f; //Init light path LightSample ls; var light = scene.Lights[scene.SampleLights(Sample.GetLazyValue())]; light.EvaluatePhoton(scene, Sample.GetLazyValue(), Sample.GetLazyValue(), Sample.GetLazyValue(), Sample.GetLazyValue(), Sample.GetLazyValue(), out ls); LightRay = ls.LightRay; lightPdf = ls.Pdf * (1f / scene.Lights.Length); LightThroughput = (RgbSpectrum)(ls.Spectrum); this.tracedConnectRayCount = 0; this.eyeDepth = 0; this.lightDepth = 0; this.eyeStop = false; this.lightStop = false; }
public BaseBxdf GetBsdf(ref RayData pathRay, ref RayHit hit, ref MediumInfo med, bool fromLight, float u0) { var currentTriangleIndex = (int) hit.Index; bool isLight = scene.IsLight(currentTriangleIndex); var mesh = scene.GetMeshByTriangleIndex(currentTriangleIndex); if (mesh == null) //|| mesh.MeshName.Equals("COL254_01", StringComparison.InvariantCultureIgnoreCase)) { //ii.Color = new RgbSpectrum(1f); //Debugger.Break(); throw new ApplicationException("Invalid triangle index " + currentTriangleIndex + " Mesh not found"); } UV TexCoords; Normal normal = new Normal(), shadeN = new Normal(); mesh.InterpolateTriUV(currentTriangleIndex, hit.U, hit.V, out TexCoords); //normal = -scene.Triangles[currentTriangleIndex].ComputeNormal(scene.Vertices).Normalize(); mesh.InterpolateTriangleNormal((int)hit.Index, hit.U, hit.V, ref normal); //normal = -normal; shadeN = (Normal.Dot(ref pathRay.Dir, ref normal) > 0f) ? -normal : normal; var bsdf = mesh.Material.GetBsdf(ref pathRay, ref hit, ref normal, ref shadeN, ref TexCoords, ref med, fromLight,u0); bsdf.SetLight(isLight); return bsdf; }
public PacketRayData(ref RayData rayData) { maxT = rayData.maxT; Phi = 0; Theta = 0; Org = rayData.Org; Dir = rayData.Dir; }
public override void EvaluateShadow(ShadePointInfo pt, float u0, float u1, float u2, out IColorType radiance, out float pdf, out RayData ray) { var wi = MC.CosineSampleHemisphere(u1, u2); pdf = wi.z * MathLab.INVPI; wi = pt.Frame.ToWorld(ref wi); ray = new RayData(ref pt.HitPoint, ref wi, 1e-4f, float.MaxValue); radiance = Le(ref wi); }
public override void EvaluateIllumination(RayEngineScene scn, float u0, float u1, float u2, float u3, float u4, out RayData ray, out float pdf, out IColorType radiance) { Vector dir = MC.UniformSampleSphere(u0, u1); ray = new RayData(ref Position, ref dir, 1e-4f, 1e4f); pdf = MathLab.INV4PI; radiance = (power.Mul(MathLab.M_PI * 4f)); }
protected BaseBxdf(ref RayHit rh, ref RayData ray, ref Normal ng, ref Normal ns, ref UV texCoord, MaterialInfo mi, SurfaceTextureInfo texData, bool fromLight) { #if VERBOSE if (ng.Length > 1f || ns.Length > 1f) { Console.WriteLine("Normals in bsdf arent normalized"); } #endif this.Init(ref rh, ref ray, ref ng, ref ns, ref texCoord, mi, texData, fromLight); }
public override void EvaluateShadow(ShadePointInfo pt, float u0, float u1, float u2, out IColorType radiance, out float pdf, out RayData ray) { var dir = -(Position - pt.HitPoint); var l2 = dir.Length2(); var l = MathLab.Sqrt(l2); dir.Normalize(); pdf = MC.UniformSpherePdf(); ray = new RayData(ref pt.HitPoint, ref dir, 1e-4f, l - 1e-4f); radiance = power.Mul(1f / l); //float theta = Vector.SphericalTheta(ref dir); //Profile.Evaluate(Vector.SphericalPhi(ref dir) * MathLab.INVTWOPI, theta * MathLab.INVPI); }
public override void GetRay(double x, double y, out Core.Types.RayData ray) { float theta = MathLab.M_PI * ((float)y/Height); float phi = MathLab.M_2PI * ((float)x/Width); Vector dir = new Vector(MathLab.Sin(theta) * MathLab.Cos(phi), MathLab.Cos(theta),MathLab.Sin(theta) * MathLab.Sin(phi)); ray = new RayData() { Org = rayOrigin, Dir = -(dir), minT = clipHither, maxT = clipYon }; }
private void RestartLight() { this.LightRayIndex = -1; float lPdf; LightSample lightSample; var light = scene.Lights[scene.SampleLights(this.Sample.GetLazyValue(), out lPdf)]; light.EvaluatePhoton(scene, this.Sample.GetLazyValue(), this.Sample.GetLazyValue(), this.Sample.GetLazyValue(), this.Sample.GetLazyValue(), this.Sample.GetLazyValue(), out lightSample); LightThroughtput = (RgbSpectrum)(RgbSpectrumInfo)(lightSample.Spectrum) / (lightSample.Pdf); lightWeight = lightSample.Pdf*lPdf; LightRay = lightSample.LightRay; this.currentLightVertex = 0; this.lightDepth = 0; }
public bool NextVertex(ref SurfaceIntersectionData hit, ref RayData pathRay, ref RgbSpectrum throughput, int depth, out float pdf) { var currentTriangleIndex = (int)hit.Hit.Index; var lt = Scene.GetLightByIndex(currentTriangleIndex); var wo = -pathRay.Dir; pdf = 1f; if (hit.Brdf.IsSpecular() || depth == 1) { if (lt != null) { var le = hit.Color * lt.Emittance(ref wo, out pdf); throughput.MAdd(ref throughput, ref le); } } return false; }
public void Sample(VolumeMaterialInfo info, float distance, ref RayData ray, out VolumeHitData vhd) { var pt = ray.Point(distance); var n3d = //NoiseProvider.Instance.Noise(pt.x, pt.y, pt.z); NoiseProvider.Instance.Turbulence(pt.x, pt.y, pt.z, 6); var t3d = NoiseProvider.Instance.Turbulence(pt.x, pt.y, pt.z, 4); // Sample texture info.Density vhd = new VolumeHitData() { IoR = info.MediumInfo.IoR, Emittance = info.Outscattering*t3d, Absorbance = info.Absorbtion*n3d, Scattering = ((info.Inscattering + info.Absorbtion) * t3d) / info.Inscattering, Density = 0.05f//NoiseProvider.Instance.Noise(pt.x,pt.y,pt.z), }; }
public static bool IntersectBox(ref BBox box, ref RayData ray) { float t0 = ray.Min; float t1 = ray.Max; var rayInvDir = ray.InvDirection; //1f /ray.Dir; float invDirX = rayInvDir.x; if (!process_box_coord(ref t0, ref t1, box.Min.x - ray.Org.x, box.Max.x - ray.Org.x, invDirX)) return false; float invDirY = rayInvDir.y; if (!process_box_coord(ref t0, ref t1, box.Min.y - ray.Org.y, box.Max.y - ray.Org.y, invDirY)) return false; float invDirZ = rayInvDir.z; if (!process_box_coord(ref t0, ref t1, box.Min.z - ray.Org.z, box.Max.z - ray.Org.z, invDirZ)) return false; return true; }
private RgbSpectrum EvalRadiance(ref RayData r, int depth) { double t = 0; // distance to intersection int id = 0; // id of intersected object if (!Scene.Intersect(ref r, ref t, ref id) || depth > MaxDepth) { return RgbSpectrum.Black; } var obj = SceneManager.Sph[id]; var x = r.Point((float)t); Normal n = (Normal) (x - obj.p).Normalize(); var nl = Vector.Dot(ref n, ref r.Dir) < 0 ? n : n * -1; var f = obj.c; var e = RgbSpectrum.Black; var p = f.c1 > f.c2 && f.c1 > f.c3 ? f.c1 : f.c2 > f.c3 ? f.c2 : f.c3; // max refl if (obj.refl == 3) { e += f; } if (++depth > MaxDepth) { if (rnd.NextFloat() < p) { f = f*(1f/p); } else { return e; } } var brdf = Brdfs[obj.refl]; Vector dir; float fb; brdf.Sample(ref r.Dir, ref n, rnd.NextFloat(), rnd.NextFloat(), 1f, out dir, out fb); RayData newRay = new RayData(ref x, ref dir); return e + f * fb * EvalRadiance(ref newRay, depth); }
internal void Init(ref RayHit rh, ref RayData ray, ref Normal ng, ref Normal ns, ref UV texCoord, MaterialInfo mi, SurfaceTextureInfo texData, bool fromLight) { if (rh.Miss()) { throw new ArgumentException("RayHit missed geometry!"); } //if (HitPoint == null) //{ HitPoint = new HitPointInfo { Color = RgbSpectrum.Max(ref mi.Kd, ref mi.Ks), HitPoint = ray.Point(rh.Distance), TexCoord = texCoord, FromDirection = -ray.Dir, GeoNormal = ng, ShadingNormal = ns, FromLight = fromLight }; //} //else //{ // HitPoint.HitPoint = ray.Point(rh.Distance); // HitPoint.TexCoord = texCoord; // HitPoint.FromDirection = -ray.Dir; // HitPoint.GeoNormal = ng; // HitPoint.ShadingNormal = ns; // HitPoint.FromLight = fromLight; //}; this.MaterialInfo = mi; this.TexData = texData; if (Frame == null) this.Frame = new ONB(ref HitPoint.GeoNormal); else Frame.SetFromZ(ref HitPoint.GeoNormal); }
protected override bool ShadowRayTest(ref RayHit shadowRayHit, ref RayData shadowRay, out RgbSpectrum attenuation, out bool continueTrace) { var hit = shadowRayHit.Index == 0xffffffffu || scene.IsLight((int)shadowRayHit.Index); attenuation = new RgbSpectrum(1f); continueTrace = false; if (hit) return true; var mesh = scene.GetMeshByTriangleIndex((int)shadowRayHit.Index); var mat = //SurfaceMaterials.CreateMaterial(scene.MaterialProvider.Get(mesh.MaterialName)); scene.MatLib.GetSurfMat(mesh != null ? mesh.MaterialName : "default"); if (mat.TransparentShadows) { continueTrace = true; return true; } if (mat.AlphaTexture != null) { continueTrace = true; this.SurfaceSampler.GetIntersection(ref shadowRay, ref shadowRayHit, ref hitInfo, IntersectionOptions.ResolveTextures); attenuation = (RgbSpectrum) hitInfo.TextureData.Alpha; hit = true; //mat.MaterialData.Kt.Filter() < Sample.GetLazyValue(); } return hit; }
public override sealed void Advance(RayBuffer rayBuffer, SampleBuffer consumer) { int currentTriangleIndex = 0; #if VERBOSE try { #endif if (((PathState == PathTracerPathState.ShadowRaysOnly) || (PathState == PathTracerPathState.NextVertex)) && (tracedShadowRayCount > 0)) { for (int i = 0; i < tracedShadowRayCount; ++i) { RayHit shadowRayHit = rayBuffer.rayHits[secRays[i].ShadowRayIndex]; RgbSpectrum attenuation; bool continueTrace; if (this.ShadowRayTest(ref shadowRayHit, ref secRays[i].ShadowRay, out attenuation, out continueTrace)) { Radiance += attenuation * ((secRays[i].Throughput) / secRays[i].Pdf); pathWeight *= secRays[i].Pdf; } if (continueTrace) { continueRays[contCount++] = secRays[i]; //continueRays.Add(secRays[i]); } } if (PathState == PathTracerPathState.ShadowRaysOnly) { Splat(consumer); return; } Array.Copy(continueRays, secRays, contCount); tracedShadowRayCount = contCount; contCount = 0; } RayHit rayHit = rayBuffer.rayHits[RayIndex]; Vector wo = -PathRay.Dir; depth++; bool missed = rayHit.Index == 0xffffffffu; if (missed || PathState == PathTracerPathState.ShadowRaysOnly || depth > scene.MaxPathDepth) { if (missed) { //Radiance += this.scene.SampleEnvironment(ref wo) * Throughput; var sampledEnvironment = this.scene.SampleEnvironment(ref wo); Radiance.MAdd(ref sampledEnvironment , ref Throughput); } Splat(consumer); return; } //var mesh = scene.GetMeshByTriangleIndex(currentTriangleIndex); //rayHit.Index += (uint)mesh.StartTriangle; // Something was hit if (hitInfo == null) { hitInfo = SurfaceSampler.GetIntersection(ref PathRay, ref rayHit); } else { SurfaceSampler.GetIntersection(ref PathRay, ref rayHit, ref hitInfo); } currentTriangleIndex = (int)rayHit.Index; if (hitInfo == null) { Debugger.Break(); } //If Hit light) if (hitInfo.IsLight) { if (specularBounce || depth == 1) { var lt = scene.GetLightByIndex(currentTriangleIndex); if (lt != null) { float pdf; var le = hitInfo.Color * lt.Emittance(ref wo, out pdf); //Radiance += Throughput * le; Radiance.MAdd(ref Throughput, ref le); } } Splat(consumer); return; } var hitPoint = PathRay.Point(rayHit.Distance); tracedShadowRayCount = 0; var bsdf = hitInfo.MMaterial; if (!hitInfo.TextureData.Alpha.IsBlack()) { Throughput *= (RgbSpectrum.UnitSpectrum() - (RgbSpectrum) hitInfo.TextureData.Alpha); PathRay = new RayData(hitPoint, -wo); return; } if (bsdf.IsDiffuse()) { float lightStrategyPdf = LightSampler.StrategyPdf; //scene.ShadowRaysPerSample/(float)scene.Lights.Length; RgbSpectrum lightTroughtput = Throughput * hitInfo.Color; LightSampler.EvaluateShadow(ref hitPoint, ref hitInfo.Normal, Sample.GetLazyValue(), Sample.GetLazyValue(), Sample.GetLazyValue(), ref ls); for (int index = 0; index < ls.Length; index++) { if (ls[index].Pdf <= 0f) continue; secRays[tracedShadowRayCount].Throughput = ls[index].Spectrum.GetType() == typeof(RgbSpectrumInfo) ? (RgbSpectrum) (RgbSpectrumInfo)ls[index].Spectrum : (RgbSpectrum)ls[index].Spectrum; secRays[tracedShadowRayCount].Pdf = ls[index].Pdf; secRays[tracedShadowRayCount].ShadowRay = ls[index].LightRay; Vector lwi = secRays[tracedShadowRayCount].ShadowRay.Dir; RgbSpectrum fs; hitInfo.MMaterial.f(ref secRays[tracedShadowRayCount].ShadowRay.Dir, ref wo, ref hitInfo.ShadingNormal, ref Throughput, out fs, types: BrdfType.Diffuse); secRays[tracedShadowRayCount].Throughput *= lightTroughtput * Vector.AbsDot(ref hitInfo.Normal, ref lwi) * fs; if (!secRays[tracedShadowRayCount].Throughput.IsBlack()) { #if DEBUG RayDen.Library.Core.Components.Assert.IsNotNaN(secRays[tracedShadowRayCount].Pdf); #endif secRays[tracedShadowRayCount].Pdf /= lightStrategyPdf; LightSample = ls[index]; tracedShadowRayCount++; } } } float fPdf = 0f; var wi = new Vector(); RgbSpectrum f; hitInfo.TextureData.Throughput = Throughput; f = bsdf.Sample_f(ref wo, out wi, ref hitInfo.Normal, ref hitInfo.ShadingNormal, ref Throughput, Sample.GetLazyValue(), Sample.GetLazyValue(), Sample.GetLazyValue() , ref hitInfo.TextureData, out fPdf, out specularBounce); if ((fPdf <= 0.0f) || f.IsBlack()) { if (tracedShadowRayCount > 0) PathState = PathTracerPathState.ShadowRaysOnly; else { Splat(consumer); } return; } CurrentVertices.Add(new PathVertex() { BsdfPdf = fPdf, Throughput = Throughput, GeoNormal = hitInfo.Normal, HitPoint = hitPoint, HitType = PathVertexType.Geometry, IncomingDirection = wi, Material = bsdf, OutgoingDirection = wo, RayDistance = rayHit.Distance, SampleU = rayHit.U, SampleV = rayHit.V, ShadingNormal = hitInfo.ShadingNormal, TexCoords = hitInfo.TexCoords, rrWeight = 1f, }); pathWeight *= fPdf; Throughput *= (f * hitInfo.Color) / fPdf; if (depth > scene.MaxPathDepth) { float prob = Math.Max(Throughput.Filter(), scene.RussianRuletteImportanceCap); if (prob >= Sample.GetLazyValue()) { CurrentVertices[CurrentVertices.Count - 1].rrWeight = 1f/prob; Throughput /= prob; pathWeight *= prob; } else { if (tracedShadowRayCount > 0) PathState = PathTracerPathState.ShadowRaysOnly; else { Splat(consumer); } return; } } PathRay.Org = hitPoint; PathRay.Dir = wi.Normalize(); PathState = PathTracerPathState.NextVertex; #if VERBOSE } catch (Exception ex) { RayDen.Library.Components.SystemComponents.Tracer.TraceLine("Advance path exception"); RayDen.Library.Components.SystemComponents.Tracer.TraceLine("Error triangle {0}", currentTriangleIndex); RayDen.Library.Components.SystemComponents.Tracer.TraceLine(ex.Message); RayDen.Library.Components.SystemComponents.Tracer.TraceLine(ex.StackTrace); } #endif }
private RgbSpectrum EvalRadiance(ref RayData r, int depth) { double t = 0; // distance to intersection int id = 0; // id of intersected object if (!Scene.Intersect(ref r, ref t, ref id) || depth > MaxDepth) { return RgbSpectrum.Black; } var obj = SceneManager.Sph[id]; var x = r.Point((float)t); Normal n = (Normal)(x - obj.p).Normalize(); var nl = Vector.Dot(ref n, ref r.Dir) < 0 ? n : n * -1; var f = obj.c; var e = RgbSpectrum.Black; var p = f.c1 > f.c2 && f.c1 > f.c3 ? f.c1 : f.c2 > f.c3 ? f.c2 : f.c3; // max refl var brdf = Brdfs[obj.refl]; depth++; Vector dir; float fb; RgbSpectrum rad = new RgbSpectrum(); brdf.Sample(ref r.Dir, ref n, rnd.NextFloat(), rnd.NextFloat(), 1f, out dir, out fb); if (id == 8) { return f; } if (brdf.BsdfType.Has(BsdfEvent.Diffuse)) { RayData shr = new RayData(); Scene.GenerateShadowRay(rnd.NextFloat(), rnd.NextFloat(), ref x, ref shr); var l = x - shr.Org; shr.maxT = l.Length; double shd = 1e5f; int tid = 0; if (Scene.Intersect(ref shr, ref shd, ref tid)) { if (tid == 8) { var w = -shr.Dir; rad += (SceneManager.Sph[8].c*Vector.AbsDot(ref nl, ref w)*f*fb)/l.Length; } } else { rad += SceneManager.Sph[8].c*Vector.AbsDot(ref nl, ref shr.Dir)*f/l.Length; } } if (brdf.BsdfType.Has(BsdfEvent.Transmit) && depth < MaxDepth) { RayData newRay = new RayData(ref x, ref dir); rad += fb * EvalRadiance(ref newRay, depth+1); } if (brdf.BsdfType.Has(BsdfEvent.Specular) && depth < MaxDepth) { brdf.Sample(ref r.Dir, ref n, rnd.NextFloat(), rnd.NextFloat(), 1f, out dir, out fb); RayData newRay = new RayData(ref x, ref dir); rad += fb*EvalRadiance(ref newRay, depth+1); } return rad; }
protected override bool ShadowRayTest(ref RayHit shadowRayHit, ref RayData shadowRay, out RgbSpectrum attenuation, out bool continueTrace) { var hit = shadowRayHit.Index == 0xffffffffu || scene.IsLight((int)shadowRayHit.Index); attenuation = new RgbSpectrum(1f); continueTrace = false; if (hit) return true; var mesh = scene.GetMeshByTriangleIndex((int)shadowRayHit.Index); var mat = //SurfaceMaterials.CreateMaterial(scene.MaterialProvider.Get(mesh.MaterialName)); scene.MatLib.GetSurfMat(mesh != null ? mesh.MaterialName : "default"); shadowRayEvent = mat.Type.TypeToEvent(); if (mat.TransparentShadows) { continueTrace = true; return true; } //if (shadowRayEvent.Has(BsdfEvent.Transmit)) //{ // var att = MathLab.Exp(-0.1f*shadowRayHit.Distance); // attenuation *= att + att* hitInfo.TextureData.Transmittance; // continueTrace = true; // return true; //} //if (mat.AlphaTexture != null) //{ // continueTrace = true; // this.SurfaceSampler.GetIntersection(ref shadowRay, ref shadowRayHit, ref hitInfo, IntersectionOptions.ResolveTextures); // attenuation = (RgbSpectrum) hitInfo.TextureData.Alpha; // hit = true; // //mat.MaterialData.Kt.Filter() < Sample.GetLazyValue(); //} return hit; }
public override void GenerateLiRays(IRayEngineScene scn, Sample sample, ref RayData ray, VolumeComputation comp) { comp.Reset(); var scene = (RayEngineScene)(scn); var sigs = sig_s; comp.EmittedLight = lightEmission; float t0, t1; if (!region.Intersect(ray, out t0, out t1)) return; if (sigs.IsBlack() || (scene.Lights.Length < 1)) { float distance = t1 - t0; comp.EmittedLight = lightEmission * distance; } else { // Prepare for volume integration stepping float distance = t1 - t0; var nSamples = MathLab.Ceil2UInt(distance / stepSize); float step = distance / nSamples; RgbSpectrum Tr = new RgbSpectrum(1f); RgbSpectrum Lv = new RgbSpectrum(); var p = ray.Point(t0); float offset = sample.GetLazyValue(); t0 += offset * step; //Vector pPrev; for (var i = 0; i < nSamples; ++i, t0 += step) { //pPrev = p; p = ray.Point(t0); Sigma_s(ref p, out sigs); //sigs = NoiseProvider.Instance.Noise3D(((Vector)p).Normalize()); Lv += lightEmission; if (!sigs.IsBlack() && (scene.Lights.Length) > 0) { // Select the light to sample float lightStrategyPdf; var light = scene.Lights[scene.SampleLights(sample.GetLazyValue(), out lightStrategyPdf)]; // Select a point on the light surface float lightPdf; Normal fakeNorml = new Normal(0f, 0f, 1f); LightSample ls = new LightSample(); light.EvaluateShadow(ref p, ref fakeNorml, sample.GetLazyValue(), sample.GetLazyValue(), sample.GetLazyValue(), ref ls); var lightColor = (RgbSpectrumInfo)(ls.Spectrum); lightPdf = ls.Pdf; comp.Rays[comp.RayCount] = ls.LightRay; if ((lightPdf > 0f) && !lightColor.IsBlack()) { comp.ScatteredLight[comp.RayCount] =(RgbSpectrum)(Tr * sigs * lightColor * MathLab.Exp(-distance) * ((Density(ref p) * step) / (4f * MathLab.M_PI * lightPdf * lightStrategyPdf))); comp.RayCount++; } } comp.EmittedLight = Lv * step; } } }
public void GetRay(double xp, double yp, out IRay cameraRay) { var u = (float)(2.0f * xp / w - 1.0f); var v = (float)(1.0f - 2.0f * yp / h); Vector rdir = mRight * u + mUp * v + this.Look; var rorig = (Position + rdir * 0.1f); rdir.Normalize(); cameraRay = new RayData(rorig, rdir); }
public bool Intersect(ref TriangleDataInfo ti, Point[] meshVertices, ref RayData ray, ref float thit, ref float u, ref float v) { var p0 = meshVertices[ti.v0.VertexIndex]; var p1 = meshVertices[ti.v1.VertexIndex]; var p2 = meshVertices[ti.v2.VertexIndex]; Vector e1, e2, s1; Point.Sub(ref p1, ref p0, out e1); Point.Sub(ref p2, ref p0, out e2); Vector.Cross(ref ray.Dir, ref e2, out s1); var divisor = Vector.Dot(ref s1, ref e1); if (divisor < MathLab.Epsilon) return false; var invDivisor = 1f / divisor; Vector d, s2; Point.Sub(ref ray.Org, ref p0, out d); var b1 = Vector.Dot(ref d, ref s1) * invDivisor; if (b1 < 0f) return false; Vector.Cross(ref d, ref e1, out s2); var b2 = Vector.Dot(ref ray.Dir, ref s2) * invDivisor; if (b2 < 0f) return false; var b0 = 1f - b1 - b2; if (b0 < 0f) return false; var t = Vector.Dot(ref e2, ref s2) * invDivisor; if (t < ray.minT || t > ray.maxT) return false; thit = t; u = b1; v = b2; return true; }
public override void GetRay(double xp, double yp, out RayData ray) { /* float3 ray_origin = eye; float3 ray_direction = d.x*U + d.y*V + W; float3 ray_target = ray_origin + focal_scale * ray_direction; float2 sample = optix::square_to_disk(make_float2(jt.x, jt.y)); ray_origin = ray_origin + aperture_radius * ( sample.x * normalize( U ) + sample.y * normalize( V ) ); ray_direction = normalize(ray_target - ray_origin); */ // Generate raster and camera samples Point Pras = new Point((float)xp, (float)yp, 0); Point Pcamera = UnProject(ref Pras); var u = (float)(2.0f * xp / Width - 1.0f); var v = (float)(1.0f - 2.0f * yp / Height); Vector ray_dir = new Vector(x.x * u + y.x * v + dir.x, x.y * u + y.y * v + dir.y, x.z * u + y.z * v + dir.z); Vector ray_target = (Vector)(Position + ray_dir * focal_scale); Point ray_origin = Position; ray = new RayData(ray_origin, ray_dir, 0f, 1e20f); // Modify ray for depth of field if (lensRadius > 0f) { // Sample point on lens float lensU = 0f, lensV = 0f; float samplelensU = rnd.NextFloat(); float samplelensV = rnd.NextFloat(); MC.ConcentricSampleDisk(samplelensU, samplelensV, ref lensU, ref lensV); lensU *= lensRadius; lensV *= lensRadius; ray_target += lensRadius * (lensU * Vector.Normalize(x) + lensV * Vector.Normalize(y)); // Compute point on plane of focus //float ft = focalDistance / ray.Dir.z; //Point Pfocus = ray.Point(ft); // Update ray for effect of lens ray.Org = ray_origin; ray.Dir = Vector.Normalize(ray_target - (Vector) ray_origin); } //ray->time = Lerp(sample.time, shutterOpen, shutterClose); //CameraToWorld(*ray, ray); //return 1.f; }
public static bool IntersectBox(AABB box, ref RayData ray) { float t0 = ray.Min; float t1 = ray.Max; float hitt0 = t0; float hitt1 = t1; var rayInvDir = ray.InvDirection; //1f /ray.Dir; float invDirX = rayInvDir.x; if (!process_box_coord(ref t0, ref t1, box.Min.x - ray.Org.x, box.Max.x - ray.Org.x, invDirX)) return false; float invDirY = rayInvDir.y; if (!process_box_coord(ref t0, ref t1, box.Min.y - ray.Org.y, box.Max.y - ray.Org.y, invDirY)) return false; float invDirZ = rayInvDir.z; if (!process_box_coord(ref t0, ref t1, box.Min.z - ray.Org.z, box.Max.z - ray.Org.z, invDirZ)) return false; /* for (int i = 0; i < 3; ++i) { // Update interval for _i_th bounding box slab float invRayDir = 1f / ray.Direction[i]; if (!process_box_coord(ref t0, ref t1, min[i] - ray.Origin[i], max[i] - ray.Origin[i], invRayDir)) return false; } * */ //ray.MinT = t0; //ray.MaxT = t1; hitt0 = t0; hitt1 = t1; return true; }
public abstract void EvaluateIllumination(RayEngineScene scn, float u0, float u1, float u2, float u3, float u4, out RayData ray, out float pdf, out IColorType radiance);
public abstract void EvaluateShadow(ShadePointInfo pt, float u0, float u1, float u2, out IColorType radiance, out float pdf, out RayData ray);
public RgbSpectrumInfo Sample(ref Point p, ref Normal N, float u2, float u0, float u1, out RayData shadowRay, out float pdf) { Point samplePoint = new Point(); float b0, b1, b2; scene.Triangles[triangleIndex].Sample(scene.Vertices, u0, u1, out samplePoint, out b0, out b1, out b2); //var sampleN = TriangleNormal; //var N = n; Vector wi = samplePoint - p; //wi.Normalize(); float distanceSquared = wi.Length2(); float distance = MathLab.Sqrt(distanceSquared); wi /= distance; var nw = -wi; float sampleNdotMinusWi = Normal.Dot(ref TriangleNormal, ref nw); float NdotWi = Normal.Dot(ref N, ref wi); if ((sampleNdotMinusWi <= 0f) || (NdotWi <= 0f)) { pdf = 0f; shadowRay = new RayData(); return new RgbSpectrumInfo(0f); } shadowRay = new RayData(ref p, ref wi, MathLab.RAY_EPSILON, distance - MathLab.RAY_EPSILON); pdf = (distanceSquared / (sampleNdotMinusWi * area)); // Using 0.1 instead of 0.0 to cut down fireflies if (pdf <= 0.1f) { pdf = 0f; return new RgbSpectrumInfo(0); } return Gain; }
public override void EvaluateIllumination(RayEngineScene scn, float u0, float u1, float u2, float u3, float u4, out RayData ray, out float pdf, out IColorType radiance) { throw new System.NotImplementedException(); }
public MatteLambertBrdf(ref RayHit rh, ref RayData ray, ref Normal ng, ref Normal ns, ref UV texCoord, MaterialInfo mi, SurfaceTextureInfo texData, bool fromLight) : base(ref rh, ref ray, ref ng, ref ns, ref texCoord, mi, texData, fromLight) { }
public void Sample(float xp, float yp, float u0, float u1, float u2, out IRay r, out float pdf) { Point Pras = new Point(xp, yp, 0); //Point Pcamera = UnProject(ref Pras); var u = 2.0f * xp / Width - 1.0f; var v = 1.0f - 2.0f * yp / Height; Vector ray_dir = new Vector(x.x * u + y.x * v + dir.x, x.y * u + y.y * v + dir.y, x.z * u + y.z * v + dir.z); Vector ray_target = (Vector)(Position + ray_dir * focal_scale); Point ray_origin = Position; var ray = new RayData(ray_origin, ray_dir, 0f, 1e20f); // Modify ray for depth of field if (lensRadius > 0f) { // Sample point on lens float lensU = 0f, lensV = 0f; float samplelensU = rnd.NextFloat(); float samplelensV = rnd.NextFloat(); MC.ConcentricSampleDisk(samplelensU, samplelensV, ref lensU, ref lensV); lensU *= lensRadius; lensV *= lensRadius; ray_target += lensRadius * (lensU * Vector.Normalize(x) + lensV * Vector.Normalize(y)); // Compute point on plane of focus //float ft = focalDistance / ray.Dir.z; //Point Pfocus = ray.Point(ft); // Update ray for effect of lens ray.Org = ray_origin; ray.Dir = Vector.Normalize(ray_target - (Vector)ray_origin); } pdf = 1; r = (IRay)ray; }
public override void GetRay(double x, double y, out RayData ray) { ray = new RayData(); camera.GenerateRay(new UV((float)x, (float)y), out ray.Org, out ray.Dir); }