private ClosestSphere GetClosestSphereIntersectionInList(Ray ray, List <PotatoSphere> spheres) { PotatoSphere intersectedSphere = null; Vector3 hitPosition = new Vector3(); Vector3 hitNormal = new Vector3(); Vector3 localHitPosition = new Vector3(); Vector3 localHitNormal = new Vector3(); double distance = double.PositiveInfinity; double t = 0.0; //LoopSphereListToGetTheClosets(ray, spheres, ref intersectedSphere, ref hitPosition, ref hitNormal, ref localHitPosition, ref localHitNormal, ref distance, ref t); for (int i = 0; i < spheres.Count; i++) { if (SphereIntersection.Intersect(ray, spheres[i], ref localHitPosition, ref localHitNormal, ref t)) { if (t < distance) { distance = t; intersectedSphere = spheres[i]; hitPosition = localHitPosition; hitNormal = localHitNormal; } } } return(new ClosestSphere(intersectedSphere, hitPosition, hitNormal, distance)); }
public static bool Intersect(Ray ray, PotatoSphere sphere) { bool hit = false; double d = 0; Vector3 co = Vector3.Subtract(ray.Origin, sphere.Position); double a = 1; double b = 2 * Vector3.Dot(ray.Direction, co); double c = co.Length() * co.Length() - sphere.Radius * sphere.Radius; double delta = b * b - 4 * (a * c); if (delta >= 0) { double d1 = (-b - Math.Sqrt(delta)) / (2 * a); double d2 = (-b + Math.Sqrt(delta)) / (2 * a); if (Math.Min(d1, d2) > 0) { d = Math.Min(d1, d2); } else { d = Math.Max(d1, d2); } hit = true; } return(hit); }
private static void CalculatePolynomial(PotatoSphere sphere, Vector3 origin, Vector3 direction, out double a, out double b, out double delta) { Vector3 co = Vector3.Subtract(origin, sphere.Position); a = 1; b = 2 * Vector3.Dot(direction, co); double c = co.Length() * co.Length() - sphere.Radius * sphere.Radius; delta = b * b - 4 * (a * c); }
private ClosestSphere GetClosestSphereIntersectionInScene(Ray ray) { PotatoSphere intersectedSphere = null; Vector3 hitPosition = new Vector3(); Vector3 hitNormal = new Vector3(); Vector3 localHitPosition = new Vector3(); Vector3 localHitNormal = new Vector3(); double distance = double.PositiveInfinity; double t = 0.0; LoopSphereListToGetTheClosets(ray, sceneData.Spheres, ref intersectedSphere, ref hitPosition, ref hitNormal, ref localHitPosition, ref localHitNormal, ref distance, ref t); return(new ClosestSphere(intersectedSphere, hitPosition, hitNormal, distance)); }
public static bool Intersect(Ray ray, PotatoSphere sphere, ref Vector3 hitPosition, ref Vector3 hitNormal, ref double distance) { //bool hit = false; //double a, b, delta = 0; //CalculatePolynomial(sphere, ray.Origin, ray.Direction, out a, out b, out delta); //CalculateIntersection(ref hit, distance, a, b, delta); //hitPosition = ray.Cast(ray.Origin, distance); //hitNormal = Vector3.Normalize(Vector3.Subtract(hitPosition, sphere.Position)); ////if (hit) Console.WriteLine("{0} {1}", hit, distance); //return hit; bool hit = false; double d = 0; Vector3 co = Vector3.Subtract(ray.Origin, sphere.Position); double a = 1; double b = 2 * Vector3.Dot(ray.Direction, co); double c = co.Length() * co.Length() - sphere.Radius * sphere.Radius; double delta = b * b - 4 * (a * c); if (delta >= 0) { double d1 = (-b - Math.Sqrt(delta)) / (2 * a); double d2 = (-b + Math.Sqrt(delta)) / (2 * a); if (Math.Min(d1, d2) > 0) { d = Math.Min(d1, d2); } else { d = Math.Max(d1, d2); } hit = true; } distance = d; hitPosition = ray.Cast(ray.Origin, d); hitNormal = Vector3.Normalize(Vector3.Subtract(hitPosition, sphere.Position)); return(hit); }
private void LoopSphereListToGetTheClosets(Ray ray, List <PotatoSphere> spheres, ref PotatoSphere intersectedSphere, ref Vector3 hitPosition, ref Vector3 hitNormal, ref Vector3 localHitPosition, ref Vector3 localHitNormal, ref double distance, ref double t) { for (int i = 0; i < spheres.Count; i++) { if (SphereIntersection.Intersect(ray, spheres[i], ref localHitPosition, ref localHitNormal, ref t)) { if (t < distance) { distance = t; intersectedSphere = spheres[i]; hitPosition = localHitPosition; hitNormal = localHitNormal; } } } }
private void ProcessSphereUVTexture(PotatoSphere sphere, Vector3 hitNormal) { Vector2 UV = sphere.GetUV(hitNormal, objectRenderTexture); pixelColor = textureManager.GetTextureColor(UV, objectRenderTexturePath); }
//public Color Trace(Ray renderRay, int lightIndex, int depth) //{ // depth -= 1; // //TODO: Rework sphere tracer. // if (objectRender == null) // { // pixelColor = Color.Black; // return pixelColor; // } // //if (depth > 0) // //{ // // Vector3 reflectDirection = ReflectRay(renderRay.Direction, hitNormal); // // renderRay.Set(hitPosition, reflectDirection); // // Trace(renderRay, lightIndex, depth); // //} // //objectRender = GetIntersectionObject(renderRay, out hitPosition, out hitNormal); // //if (objectRender == null) // //{ // // pixelColor = Color.Black; // // return pixelColor; // //} // //pixelColor = objectRender.Color; // SetObjectRenderUVProperties(); // ProcessUVTexture(); // pixelColor = ComputeLight(pixelColor, hitPosition, hitNormal, scene.GetPointLight(lightIndex)); // return pixelColor; //} private void SetSphereUVProperties(PotatoSphere sphere) { objectRenderTexturePath = sphere.GetTexturePath(); objectRenderTexture = textureManager.GetTexture(objectRenderTexturePath); }
public ClosestSphere(PotatoSphere sphere, Vector3 hitPosition, Vector3 hitNormal, double distance) : base(hitPosition, hitNormal, distance, false) { Sphere = sphere; }