private void AddCube(float cube_half_size, Point3d point, Color color, float specular = 0, float reflecive = 0, float transparent = 0) { var cube = new Polyhedron(); cube.MakeHexahedron(cube_half_size); cube.Translate(point.X, point.Y, point.Z); cube.color = color; cube.specular = specular; cube.reflective = reflecive; cube.transparent = transparent; cube.FindNormals(); polyhedrons.Add(cube); }
private void AddWall(List <Point3d> points, List <float> normal, Color color, float specular = 0, float reflective = 0) { Face f = new Face(points); var wall = new Polyhedron(new List <Face>() { f }); wall.Faces[0].Normal = normal; wall.color = color; wall.specular = specular; wall.reflective = reflective; polyhedrons.Add(wall); }
private PointF IntersectRaySphere(Point3d camera, Point3d D, Polyhedron sphere) { PointF res = new Point(inf, inf); float r = sphere.radius; Point3d OC = Sub(camera, sphere.Center); float k1 = Dot(D, D); float k2 = 2 * Dot(OC, D); float k3 = Dot(OC, OC) - r * r; float discriminant = k2 * k2 - 4 * k1 * k3; if (discriminant < 0) { return(res); } double t1 = (-k2 + Math.Sqrt(discriminant)) / (2 * k1); double t2 = (-k2 - Math.Sqrt(discriminant)) / (2 * k1); res.X = (float)t1; res.Y = (float)t2; return(res); }
private void ClosestIntersection(Point3d camera, Point3d D, float t_min, float t_max, ref Polyhedron closest, ref float closest_t, ref Point3d norm) { closest_t = inf; closest = null; norm = null; foreach (var polyhedron in polyhedrons) { if (polyhedron.is_sphere) { PointF t = IntersectRaySphere(camera, D, polyhedron); if (t.X < closest_t && t_min < t.X && t.X < t_max) { closest_t = t.X; closest = polyhedron; } if (t.Y < closest_t && t_min < t.Y && t.Y < t_max) { closest_t = t.Y; closest = polyhedron; } } else { Point3d norm_res = null; float t = IntersectRay(camera, D, polyhedron, ref norm_res); if (t < closest_t && t_min < t && t < t_max) { closest_t = t; closest = polyhedron; norm = norm_res; } } } if (closest != null && closest.is_sphere) { var point = Sum(camera, Mul(closest_t, D)); norm = Sub(point, closest.Center); } }