private IntersectInfo IntersectSlab(Ray ray, Vector P1, Vector P2, Vector P3) { Vector N = (P1 - P3).Cross(P2 - P3).Normalize(); double d = N.Dot(P1); IntersectInfo info = new IntersectInfo(); double Vd = N.Dot(ray.Direction); if (Vd == 0) { return(info); // no intersection } double t = -(N.Dot(ray.Position) - d) / Vd; if (t <= 0) { return(info); } Vector hit = ray.Position + ray.Direction * t; if ((hit.x < P1.x || hit.x > P2.x) && (P1.x != P2.x)) { return(info); } if ((hit.y < P3.y || hit.y > P1.y) && (P1.y != P2.y)) { return(info); } //if ((hit.z < P1.z || hit.z > P3.z) && (P1.z != P3.z)) return info; if ((hit.z < P1.z || hit.z > P2.z) && (P1.z != P2.z)) { return(info); } info.Element = this; info.IsHit = true; info.Position = hit; info.Normal = N;// *-1; info.Distance = t; if (Material.HasTexture) { //Vector vecU = new Vector(hit.y - Position.y, hit.z - Position.z, Position.x-hit.x); Vector vecU = new Vector((P1.y + P2.y) / 2 - Position.y, (P1.z + P2.z) / 2 - Position.z, Position.x - (P1.x + P2.x) / 2).Normalize(); Vector vecV = vecU.Cross((P1 + P2) / 2 - Position).Normalize(); double u = info.Position.Dot(vecU); double v = info.Position.Dot(vecV); info.Color = Material.GetColor(u, v); } else { info.Color = Material.GetColor(0, 0); } return(info); }
/// <summary> /// Metoda LookAt /// Umoznuje nastaveni kamery na zadanou polohu a uhel pohledu /// </summary> /// <param name="eye">Vector umisteni kamery</param> /// <param name="focus">Vector s bodem lomu</param> /// <param name="aspect">aspect</param> /// <param name="fovY">Uhel pohledu</param> /// <returns></returns> public static Camera LookAt(Vector eye, Vector focus, double aspect, double fovY) { double zoom = 1.0 / Math.Tan((fovY * 0.5) * (Math.PI / 180.0)); Vector forward = (focus - eye).Normalized * zoom; Vector right = forward.Cross(GlobalUp).Normalized *aspect; Vector up = right.Cross(forward).Normalized; return(new Camera(eye, forward, right, up, fovY)); }
public static Camera Create(Vector pos, Vector lookAt) { Vector forward = Vector.Norm(Vector.Minus(lookAt, pos)); Vector down = new Vector(0, -1, 0); Vector right = Vector.Times(1.5, Vector.Norm(Vector.Cross(forward, down))); Vector up = Vector.Times(1.5, Vector.Norm(Vector.Cross(forward, right))); return(new Camera() { Pos = pos, Forward = forward, Up = up, Right = right }); }
public static Camera Create(Vector pos, Vector lookAt) { Vector forward = Vector.Norm(Vector.Minus(lookAt, pos)); Vector down = new Vector(0, -1, 0); Vector right = Vector.Times(1.5, Vector.Norm(Vector.Cross(forward, down))); Vector up = Vector.Times(1.5, Vector.Norm(Vector.Cross(forward, right))); //Console.WriteLine($"eye {pos.X} {pos.Y} {pos.Z}"); //Console.WriteLine($"lookAt {lookAt.X} {lookAt.Y} {lookAt.Z}"); //Console.WriteLine($"up {up.X} {up.Y} {up.Z}"); return(new Camera() { Pos = pos, Forward = forward, Up = up, Right = right }); }
public override IntersectInfo Intersect(Ray ray) { IntersectInfo info = new IntersectInfo(); double Vd = Position.Dot(ray.Direction); if (Vd == 0) { return(info); // no intersection } double t = -(Position.Dot(ray.Position) + D) / Vd; if (t <= 0) { return(info); } info.Element = this; info.IsHit = true; info.Position = ray.Position + ray.Direction * t; info.Normal = Position;// *-1; info.Distance = t; if (Material.HasTexture) { Vector vecU = new Vector(Position.y, Position.z, -Position.x); Vector vecV = vecU.Cross(Position); double u = info.Position.Dot(vecU); double v = info.Position.Dot(vecV); info.Color = Material.GetColor(u, v); } else { info.Color = Material.GetColor(0, 0); } return(info); }