public static bool TestRaySphere(Ray r, Sphere s) { Vector3 m = r.Pos - s.Center; float c = Vector3.Dot(m, m) - s.Radius * s.Radius; if (c <= 0.0f) { return true; } float b = Vector3.Dot(m, r.Dir); if (b > 0.0f) { return false; } float disc = b * b - c; if (disc < 0.0f) { return false; } return true; }
public static bool TestRaySphere(Ray r, Sphere s) { Vector3 m = r.Pos - s.Center; float c = Vector3.Dot(m, m) - s.Radius * s.Radius; if (c <= 0.0f) { return(true); } float b = Vector3.Dot(m, r.Dir); if (b > 0.0f) { return(false); } float disc = b * b - c; if (disc < 0.0f) { return(false); } return(true); }
public override RGBColor TraceRay(Ray ray, int depth) { ShadeRec shadeRec = world.IntersectAllObjects(ray); if (shadeRec.HasHitObject) return shadeRec.Color; return world.BackgroundColor; }
public static bool TestRaySphere(Ray r, Sphere s, out float t, out Vector3 q) { t = float.MaxValue; q = new Vector3(); Vector3 m = r.Pos - s.Center; float b = Vector3.Dot(m, r.Dir); float c = Vector3.Dot(m, m) - s.Radius * s.Radius; if (c > 0.0f && b > 0.0f) { return(false); } float discr = b * b - c; if (discr < 0.0f) { return(false); } t = -b - (float)Math.Sqrt(discr); if (t < 0.0f) { t = 0.0f; } q = r.Pos + r.Dir * t; return(true); }
public static bool TestRaySphere(Ray r, Sphere s, out float t, out Vector3 q) { t = float.MaxValue; q = new Vector3(); Vector3 m = r.Pos - s.Center; float b = Vector3.Dot(m, r.Dir); float c = Vector3.Dot(m, m) - s.Radius * s.Radius; if (c > 0.0f && b > 0.0f) { return false; } float discr = b * b - c; if (discr < 0.0f) { return false; } t = -b - (float)Math.Sqrt(discr); if (t < 0.0f) { t = 0.0f; } q = r.Pos + r.Dir * t; return true; }
public override void RenderScene(World world) { //Stopwatch sw = Stopwatch.StartNew(); //while (true) //{ // sw.Restart(); RGBColor L = new RGBColor(); ViewPlane viewPlane = (ViewPlane) world.ViewPlane.Clone(); Ray ray = new Ray(); int depth = 0; Point2d samplePoint = new Point2d(); Point2d pixelPoint = new Point2d(); viewPlane.PixelSize /= zoom; ray.Origin = Eye; Bitmap bitmap = new Bitmap(viewPlane.HRes, viewPlane.VRes); for (int r = 0; r < viewPlane.VRes; r++) { for (int c = 0; c < viewPlane.HRes; c++) { L = new RGBColor(0, 0, 0); for (int i = 0; i < viewPlane.Sampler.NumSamples; i++) { samplePoint = viewPlane.Sampler.SampleUnitSquare(); pixelPoint.X = viewPlane.PixelSize*(c - 0.5*viewPlane.HRes + samplePoint.X); pixelPoint.Y = viewPlane.PixelSize*(r - 0.5*viewPlane.VRes + samplePoint.Y); ray.Direction = GetRayDirection(pixelPoint); L += world.Tracer.TraceRay(ray, depth); } L /= viewPlane.Sampler.NumSamples; L *= eposureTime; L.MaxToOne(); bitmap.SetPixel(c, viewPlane.VRes - r - 1, Color.FromArgb((int) (L.R*255), (int) (L.G*255), (int) (L.B*255))); } Console.Out.WriteLine("r = {0}", r); } bitmap.Save("TracedImage.bmp", ImageFormat.Bmp); //Console.Out.WriteLine("sw = {0}", sw.ElapsedMilliseconds); }
public override RGBColor TraceRay(Ray ray, int depth) { ShadeRec shadeRec = world.IntersectAllObjects(ray); if (shadeRec.HasHitObject) { shadeRec.Ray = ray; return shadeRec.Material.Shade(shadeRec); } else { return world.BackgroundColor; } }
public override RGBColor TraceRay(Ray ray, int depth) { ShadeRec shadeRec = new ShadeRec(world); //not used double t; // not used if (world.Sphere.Intersect(ray, out t, shadeRec)) { return new RGBColor(1, 0, 0); } else { return new RGBColor(0, 0, 0); } }
public override RGBColor TraceRay(Ray ray, int depth) { ShadeRec shadeRec = new ShadeRec(world); //not used double t; // not used if (world.Sphere.Intersect(ray, out t, shadeRec)) { return new RGBColor((float) (shadeRec.Normal.X + 1)/2, (float) (shadeRec.Normal.Y + 1)/2, (float) (shadeRec.Normal.Z + 1)/2); } else { return new RGBColor(0, 0, 0); } }
public override bool Intersect(Ray ray, out double tmin, ShadeRec shadeRec) { double t = Vector3d.Dot((Point - ray.Origin), Normal) / Vector3d.Dot(ray.Direction, Normal); if (t>kEpsilon && !double.IsInfinity(t)) { tmin = t; shadeRec.Normal = Normal; shadeRec.Hitpoint = ray.Origin + t*ray.Direction; return true; } else { tmin = 0; return false; } }
public override bool Intersect(Ray ray, out double tmin, ShadeRec shadeRec) { double t = 0; Vector3d temp = ray.Origin - Center; double a = Vector3d.Dot(ray.Direction, ray.Direction); double b = Vector3d.Dot(temp, ray.Direction)*2; double c = Vector3d.Dot(temp, temp) - Radius*Radius; double disc = b*b - 4*a*c; tmin = 0; if (disc < 0) { return false; } else { //TODO: what if denom == 0? double e = Math.Sqrt(disc); double denom = 2*a; //smaller root t = (-b - e)/denom; if (t>kEpsilon) { tmin = t; shadeRec.Normal = (temp + t*ray.Direction)/Radius; shadeRec.Hitpoint = ray.Origin + t*ray.Direction; return true; } //larger root t = (-b + e) / denom; if (t > kEpsilon) { tmin = t; shadeRec.Normal = (temp + t * ray.Direction) / Radius; shadeRec.Hitpoint = ray.Origin + t * ray.Direction; return true; } //no valid intersection return false; } }
private void InitializeEyeRays(Camera camera) { eyeRays = new Ray[width * height]; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { Vector3 source1 = new Vector3(i, j, 0f); Vector3 source2 = new Vector3(i, j, 1f); Vector3 unproj1 = GraphicsDevice.Viewport.Unproject(source1, cam.Proj, cam.View, cam.World); Vector3 unproj2 = GraphicsDevice.Viewport.Unproject(source2, cam.Proj, cam.View, cam.World); Ray r = new Ray(); r.Pos = unproj1; r.Dir = Vector3.Normalize(unproj2 - unproj1); eyeRays[(j * width) + i] = r; } } }
private void RayTrace() { Vector3 q = Vector3.Zero; float t; Stack <IntermediateLightData> ildStack = new Stack <IntermediateLightData>(iterations); for (int j = 0; j < width; j++) { for (int i = 0; i < height; i++) { ildStack.Clear(); Vector3 final = Vector3.Zero; //The final color for this pixel int index = j * width + i; //The index of this pixel in our array Ray r = eyeRays[index]; for (int k = 0; k < iterations; k++) { //Find r's intersect float minT = float.MaxValue; Sphere minSphere = new Sphere(); Vector3 hitPoint = Vector3.Zero; for (int m = 0; m < spheres.Length; m++) { bool hit = CollisionHelper.TestRaySphere(r, spheres[m], out t, out q); if (hit && t < minT) { minT = t; minSphere = spheres[m]; hitPoint = q; } } //If we didn't intersect anything, push black on the stack and break out of the loop if (minT >= float.MaxValue) { ildStack.Push(new IntermediateLightData(Vector3.Zero, 0.0f)); break; } else { //Else we intersected something, namely minSphere //Sum up the light contribution, and push our color value on the stack Vector3 lightSum = Vector3.Zero; Vector3 normal = Vector3.Normalize(hitPoint - minSphere.Center); for (int n = 0; n < lights.Length; n++) { bool inShadow = false; Vector3 lightDir = lights[n].Pos - hitPoint; //If the surface isn't facing the light to begin with... if (Vector3.Dot(normal, lightDir) <= 0) { inShadow = true; break; } if (CollisionHelper.PointInSphere(lights[n].Pos, minSphere)) { inShadow = true; break; } //Check to see if we can reach the light float pointToLightSquared = lightDir.LengthSquared(); lightDir.Normalize(); Ray pointToLight = new Ray(hitPoint + normal * 0.001f, lightDir); float tTest = float.MaxValue; Vector3 newHitPoint = new Vector3(); for (int z = 0; z < spheres.Length; z++) { bool intersects = CollisionHelper.TestRaySphere(pointToLight, spheres[z], out tTest, out newHitPoint); //If we intersected a sphere before we hit the light, we're in shadow if (intersects && tTest * tTest < pointToLightSquared) { inShadow = true; break; } } if (!inShadow) { float attenuation = (hitPoint - lights[n].Pos).LengthSquared(); attenuation = Vector3.Dot(normal, pointToLight.Dir); lightSum += lights[n].Color.ToVector3() * attenuation; } } ildStack.Push(new IntermediateLightData(minSphere.Color.ToVector3() * ambientCoefficient + lightSum, minSphere.ReflectanceFactor)); Vector3 newDir = Vector3.Reflect(r.Dir, normal); newDir.Normalize(); r = new Ray(hitPoint + normal * 0.001f, newDir); pixels[index] = new Color(new Vector4(minSphere.Color.ToVector3() * ambientCoefficient + lightSum, 1)); } } final = ildStack.Pop().Color; //ildStack now has our color data while (ildStack.Count > 0) { IntermediateLightData ild = ildStack.Pop(); final = ild.Color + (ild.ReflectanceFraction * final); } Color finalColor = Color.FromNonPremultiplied(new Vector4(final, 1f)); pixels[index] = Color.FromNonPremultiplied(new Vector4(final, 1f)); } } }
private void RayTrace() { Vector3 q = Vector3.Zero; float t; Stack<IntermediateLightData> ildStack = new Stack<IntermediateLightData>(iterations); for (int j = 0; j < width; j++) { for (int i = 0; i < height; i++) { ildStack.Clear(); Vector3 final = Vector3.Zero; //The final color for this pixel int index = j * width + i; //The index of this pixel in our array Ray r = eyeRays[index]; for (int k = 0; k < iterations; k++) { //Find r's intersect float minT = float.MaxValue; Sphere minSphere = new Sphere(); Vector3 hitPoint = Vector3.Zero; for (int m = 0; m < spheres.Length; m++) { bool hit = CollisionHelper.TestRaySphere(r, spheres[m], out t, out q); if (hit && t < minT) { minT = t; minSphere = spheres[m]; hitPoint = q; } } //If we didn't intersect anything, push black on the stack and break out of the loop if (minT >= float.MaxValue) { ildStack.Push(new IntermediateLightData(Vector3.Zero, 0.0f)); break; } else { //Else we intersected something, namely minSphere //Sum up the light contribution, and push our color value on the stack Vector3 lightSum = Vector3.Zero; Vector3 normal = Vector3.Normalize(hitPoint - minSphere.Center); for(int n = 0; n < lights.Length; n++) { bool inShadow = false; Vector3 lightDir = lights[n].Pos - hitPoint; //If the surface isn't facing the light to begin with... if (Vector3.Dot(normal, lightDir) <= 0) { inShadow = true; break; } if (CollisionHelper.PointInSphere(lights[n].Pos, minSphere)) { inShadow = true; break; } //Check to see if we can reach the light float pointToLightSquared = lightDir.LengthSquared(); lightDir.Normalize(); Ray pointToLight = new Ray(hitPoint + normal * 0.001f, lightDir); float tTest = float.MaxValue; Vector3 newHitPoint = new Vector3(); for (int z = 0; z < spheres.Length; z++) { bool intersects = CollisionHelper.TestRaySphere(pointToLight, spheres[z], out tTest, out newHitPoint); //If we intersected a sphere before we hit the light, we're in shadow if (intersects && tTest * tTest < pointToLightSquared) { inShadow = true; break; } } if (!inShadow) { float attenuation = (hitPoint - lights[n].Pos).LengthSquared(); attenuation = Vector3.Dot(normal, pointToLight.Dir); lightSum += lights[n].Color.ToVector3() * attenuation; } } ildStack.Push(new IntermediateLightData(minSphere.Color.ToVector3() * ambientCoefficient + lightSum, minSphere.ReflectanceFactor)); Vector3 newDir = Vector3.Reflect(r.Dir, normal); newDir.Normalize(); r = new Ray(hitPoint + normal * 0.001f, newDir); pixels[index] = new Color(new Vector4(minSphere.Color.ToVector3() * ambientCoefficient + lightSum, 1)); } } final = ildStack.Pop().Color; //ildStack now has our color data while (ildStack.Count > 0) { IntermediateLightData ild = ildStack.Pop(); final = ild.Color + (ild.ReflectanceFraction * final); } Color finalColor = Color.FromNonPremultiplied(new Vector4(final, 1f)); pixels[index] = Color.FromNonPremultiplied(new Vector4(final, 1f)); } } }
private void InitializeEyeRays(Camera camera) { eyeRays = new Ray[width * height]; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { Vector3 source1 = new Vector3(i, j, 0f); Vector3 source2 = new Vector3(i, j, 1f); Vector3 unproj1 = GraphicsDevice.Viewport.Unproject(source1, cam.Proj, cam.View, cam.World); Vector3 unproj2 = GraphicsDevice.Viewport.Unproject(source2, cam.Proj, cam.View, cam.World); Ray r = new Ray(); r.Pos = unproj1; r.Dir = Vector3.Normalize(unproj2-unproj1); eyeRays[(j * width) + i] = r; } } }
public virtual RGBColor TraceRay(Ray ray,double tmin,int depth) { return new RGBColor(); }
public virtual RGBColor TraceRay(Ray ray) { return new RGBColor(); }
public virtual bool Intersect(Ray ray, out double tmin, ShadeRec shadeRec) { tmin = 0; return false; }