//public Color[,] Render(RenderOptions options) //{ // var matrix = new Color[options.Resolution.X, options.Resolution.Y]; // for (var i = 0; i != options.Resolution.X; i++) // { // for (var j = 0; j != options.Resolution.Y; j++) // { // var ray = new Ray() // { // Direction = new Vector3((i - (options.Resolution.X / 2)) * -0.01f, -4, (j - (options.Resolution.Y / 2)) * -0.01f), // Origin = position // }; // matrix[i, j] = RayTrace(ray, 0, 1000); // } // } // return matrix; //} //private Color RayTrace(Ray ray, int maxBounces, float maxLength) //{ // var raycastResult = scene.Ray(ray); // if (raycastResult != null) // return raycastResult.Obj.Material.Color; // return Color.Black; //} //private Color RayTrace(Ray ray, int maxBounces, float maxLength) //{ // var raycastResult = scene.Ray(ray); // if (raycastResult != null) // return new Color((int)((raycastResult.Triangle.Normal.X+1)*127), (int)((raycastResult.Triangle.Normal.Y+1) * 127), (int)((raycastResult.Triangle.Normal.Z+1) * 127)); // return Color.Black; //} //private Color RayTrace(Ray ray, int maxBounces, float maxLength) //{ // var bla = 2500f; // var raycastResult = scene.Ray(ray); // if (raycastResult != null) // return new Color(255 - (int)((bla / 255f) * (raycastResult.Position - ray.Origin).Length), 255 - (int)((bla / 255f) * (raycastResult.Position - ray.Origin).Length), 255 - (int)((bla / 255f) * (raycastResult.Position - ray.Origin).Length)); // return Color.Black; //} private Color RayTrace(Ray ray, int refractBounces, int reflectBounces, float maxLength) { var result = scene.Ray(ray); if (result == null) { return(Color.White); } var color = CalculateLightness(result, maxLength); if (reflectBounces < 0) { return(color); } var reflect = ray.Direction.Reflect(result.Normal); var r = 1 / result.Body.Material.RefractionIndex; var cos1 = -1 * result.Normal * ray.Direction.Normalized; var sin2 = r * (float)Math.Sqrt(1 - cos1 * cos1); var cos2 = (float)Math.Sqrt(1 - sin2 * sin2); var refractIn = r * ray.Direction.Normalized + (r * cos1 - cos2) * result.Normal; if (result.Body.Material.Reflect != null) { color += result.Body.Material.Reflect * RayTrace(new Ray(result.Position, reflect), refractBounces, reflectBounces - 1, maxLength); } if (result.Body.Material.Refract != null && refractBounces >= 0) { color += result.Body.Material.Refract * Refract(result.Body, new Ray(result.Position, refractIn), refractBounces - 1, reflectBounces - 1, maxLength); } return(color); }