static void Main(string[] args) { RayObjects = new List<SLRayPrimitive>(); Lights = new List<SLLight>(); ReadScene("scene.urt"); dir = EYE - SPOT; right = UP.Cross(dir); right.Normalize(); screenCenter = dir.Cross(right); screenV = screenCenter * (float)Math.Tan(FOVY * 0.5f); screenU = right * screenV.Magnitude() * ASPECT; WIDTH_DIV_2 = ImageWidth / 2; HEIGHT_DIV_2 = ImageHeight / 2; random = new Random(01478650229); Bitmap outputImage = new Bitmap(ImageWidth, ImageHeight); // add some objects float distance = 20.0f; float offset = distance / 2.0f; for (int i = 0; i < 5; i++) { float x = (float)(random.NextDouble() * distance) - offset; float y = (float)(random.NextDouble() * distance) - offset; float z = (float)(random.NextDouble() * 10.0f + 2.0f); Color c = Color.FromArgb(255, random.Next(255), random.Next(255), random.Next(255)); SLSphere s = new SLSphere(new SLVector3f(x, y, z), (float)(random.NextDouble() + 0.5), c); RayObjects.Add(s); } int dotPeriod = ImageHeight / 10; Stopwatch stopwatch = new Stopwatch(); System.Console.WriteLine("Rendering...\n"); System.Console.WriteLine("|0%---100%|"); stopwatch.Start(); for (int j = 0; j < ImageHeight; j++) { if ((j % dotPeriod) == 0) System.Console.Write("*"); for (int i = 0; i < ImageWidth; i++) { // Go through each pixel to get the color Color c = Render(i, j); outputImage.SetPixel(i, j, c); } } stopwatch.Stop(); TimeSpan ts = stopwatch.Elapsed; string output = ts.Minutes.ToString() + "_" + ts.Seconds.ToString() + ".png"; outputImage.Save(output); }
static void ReadScene(string path) { StreamReader file = new StreamReader(path); string delim = file.ReadLine(); if (delim != "U5") return; // Get resolution string[] res = file.ReadLine().Split(' '); ImageWidth = Int32.Parse(res[0]); ImageHeight = Int32.Parse(res[1]); // Get Eye string[] eye = file.ReadLine().Split(' '); EYE = new SLVector3f(float.Parse(eye[0]), float.Parse(eye[1]), float.Parse(eye[2])); // Get Spot string[] spot = file.ReadLine().Split(' '); SPOT = new SLVector3f(float.Parse(spot[0]), float.Parse(spot[1]), float.Parse(spot[2])); // Get Up string[] up = file.ReadLine().Split(' '); UP = new SLVector3f(float.Parse(up[0]), float.Parse(up[1]), float.Parse(up[2])); // Get aspect string[] viewVol = file.ReadLine().Split(' '); FOVY = float.Parse(viewVol[0]); ASPECT = float.Parse(viewVol[1]); // Read in Ambient Values string[] ambient = file.ReadLine().Split(' '); LaR = float.Parse(ambient[0]); LaG = float.Parse(ambient[1]); LaB = float.Parse(ambient[2]); // Read in spheres while (!file.EndOfStream) { string[] line = file.ReadLine().Split(' '); string del = line[0]; switch(del) { case "l": // LIGHTS SLVector3f pos = new SLVector3f(float.Parse(line[1]), float.Parse(line[2]), float.Parse(line[3])); SLLight l = new SLLight(pos, float.Parse(line[4]), float.Parse(line[5]), float.Parse(line[6])); Lights.Add(l); break; case "s": // SPHERES SLSphere s = new SLSphere(new SLVector3f(float.Parse(line[1]), float.Parse(line[2]), float.Parse(line[3])), float.Parse(line[4]), Color.FromArgb((int)float.Parse(line[5])*255, (int)float.Parse(line[6])*255, (int)float.Parse(line[7])*255)); RayObjects.Add(s); break; case "p": // PLANES float offset; if (float.Parse(line[1]) != 0.0f) offset = float.Parse(line[1]); else if (float.Parse(line[2]) != 0.0f) offset = float.Parse(line[2]); else offset = float.Parse(line[3]); float pR = float.Parse(line[7]) * 255.0f; float pG = float.Parse(line[8]) * 255.0f; float pB = float.Parse(line[9]) * 255.0f; SLPlane p = new SLPlane(new SLVector3f(float.Parse(line[4]), float.Parse(line[5]), float.Parse(line[6])), offset, Color.FromArgb((int)pR, (int)pG, (int)pB)); RayObjects.Add(p); break; } } }