public override bool scatter(Ray rayIn, ShadeRec sr, out SColor Attenuation, out Ray rayScatter) { Point3D target = (sr.HitPoint + sr.Normal) + Vector3D.RandomInUnitSphere(); rayScatter = new Ray(sr.HitPoint, target - sr.HitPoint); Attenuation = texture.getColor(sr.U, sr.V, sr.HitPoint); return(true); }
//参数:Lambert,Pong,高光指数,颜色 public Material(double kd, double ks, double ka, double ns, SColor matColor) { _kd = kd; _ks = ks; _ka = ka; _ns = ns; _matColor = matColor; _isTexture = false; }
public override bool scatter(Ray rayIn, ShadeRec sr, out SColor Attenuation, out Ray rayScatter) { Vector3D reflectDir = Ray.getReflectDir(rayIn.Direction, sr.Normal); reflectDir.Normalize(); rayScatter = new Ray(sr.HitPoint, reflectDir + fuzz * Vector3D.RandomInUnitSphere()); Attenuation = texture.getColor(sr.U, sr.V, sr.HitPoint); return((rayScatter.Direction * sr.Normal) > 0); }
public override bool scatter(Ray rayIn, ShadeRec sr, out SColor Attenuation, out Ray rayScatter) { Attenuation = new SColor(1, 1, 1); Vector3D outward_normal; Vector3D refracted; Vector3D reflected = Ray.getReflectDir(rayIn.Direction, sr.Normal); double eta, cos, reflect_prob; if (rayIn.Direction * sr.Normal > 0) { outward_normal = -1 * sr.Normal; eta = _RI; cos = _RI * (rayIn.Direction * sr.Normal) / rayIn.Direction.Magnitude(); } else { outward_normal = sr.Normal; eta = 1.0 / _RI; cos = -1.0 * (rayIn.Direction * sr.Normal) / rayIn.Direction.Magnitude(); } if (refract(rayIn.Direction, outward_normal, eta, out refracted)) { reflect_prob = schlick(cos, eta); rayScatter = new Ray(sr.HitPoint, refracted); } else { reflect_prob = 1.0; rayScatter = new Ray(sr.HitPoint, reflected); } if (Form2.random() < reflect_prob) { rayScatter = new Ray(sr.HitPoint, reflected); } else { rayScatter = new Ray(sr.HitPoint, refracted); } return(true); }
SColor Render(Ray ray, int depth) { ShadeRec sr = world.HitAll(ray); Ray scattered = new Ray(); if (sr.IsHit) { SColor emited = sr.HitObjGloMat.emit(sr.U, sr.V, sr.HitPoint); SColor attenuation = new SColor(0, 0, 0); if (depth < 50 && sr.HitObjGloMat.scatter(ray, sr, out attenuation, out scattered)) { return(emited + attenuation * Render(scattered, depth + 1)); } else { return(emited); } } else { return(new SColor(0, 0, 0)); } }
private async Task Build() { Vector3D from = new Vector3D(478, 278, -600), at = new Vector3D(278, 278, 0); int width = 800, height = 400; Camera camera = new Camera(from, at, new Vector3D(0, 1, 0), 45, 1.0 * width / height, 0.0, (from - at).Magnitude()); Bitmap bitmap = new Bitmap(width, height); int sp = 500; timer1.Enabled = true; timer1.Start(); await Task.Run(() => { //world.PerlinSphere(); //world.CornellBox(); //world.BoxWorld(); world.FinalFace(); for (int i = 0; i < width; ++i) { for (int j = 0; j < height; ++j) { SColor clr = new SColor(0, 0, 0); for (int k = 0; k < sp; ++k) { Ray primaryRay = camera.getRay((i + random()) / width, (j + random()) / height); clr += Render(primaryRay, 0); } clr *= 1.0 / sp; clr = new SColor(Math.Sqrt(clr.R), Math.Sqrt(clr.G), Math.Sqrt(clr.B)); bitmap.SetPixel(i, j, clr.RGB255()); } } }); timer1.Stop(); pictureBox1.BackgroundImage = bitmap; }
public ConstantTexture(SColor color) { _color = color; }