} // Camera public void GetFrame(Scene scene, Sampler randomIn, Image image) { RayTracer rayTracer = new RayTracer(scene); int width = image.Width; int height = image.Height; float halfAngle = (float)Math.Tan(viewAngle * 0.5f); #if PARALLEL Parallel.For(0, height, delegate(int y) { var random = randomIn.Clone(); #else for (int y = 0; y < height; ++y) { Random random = randomIn; #endif for (int x = 0; x < width; ++x) { // make image plane displacement vector coefficients float xF = (float)((x + random.GetNextSample()) * 2.0f / width) - 1.0f; float yF = (float)((y + random.GetNextSample()) * 2.0f / height) - 1.0f; // make image plane offset vector Vector offset = (right * xF) + (up * yF * (height / width)); // make sample ray direction, stratified by pixels Vector sampleDirection = (viewDirection + offset * halfAngle).Unitize(); // get radiance from RayTracer Vector radiance = rayTracer.GetRadiance(ViewPosition, sampleDirection, random, null); // add radiance to image image.AddToPixel(x, y, radiance); } } #if PARALLEL ); #endif } }
static double SAVE_PERIOD = 180.0; // seconds #endregion Fields #region Methods /// <summary> /// Program start. Takes one command line parameter to get a filename /// </summary> /// <param name="args"></param> /// <returns></returns> static int Main(string[] args) { int returnValue = -1; // catch everything try { // check for help request if ((args.Length == 0) || (args[0] == "-?") || (args[0] == "--help")) { Console.WriteLine(BANNER_MESSAGE); Console.WriteLine(HELP_MESSAGE); } else { // execute int starttime, lastSaveTime; starttime = lastSaveTime = Environment.TickCount; bool showPNG = false; // default PPM if ((args.Length == 2) && (args[1].ToUpper() == "G")) showPNG = true; Console.WriteLine(BANNER_MESSAGE); // get file names string modelFilePathname = args[0]; string imageFilePathname = Path.GetFileNameWithoutExtension(modelFilePathname); if (showPNG == true) imageFilePathname += ".png"; else imageFilePathname += ".ppm"; // open model file StreamReader modelFile = File.OpenText(modelFilePathname); // check model file format identifier at start of first line string formatId = modelFile.ReadLine(); if (MODEL_FORMAT_ID != formatId) throw new Exception("Invalid model file"); // read frame iterations int iterations = (int)modelFile.ReadFloat(); // create top-level rendering objects with model file Image image = new Image(modelFile); Camera camera = new Camera(modelFile); Scene scene = new Scene(modelFile, camera.ViewPosition); modelFile.Close(); Console.WriteLine("Rendering scene file " + modelFilePathname); Console.WriteLine("Output file will be " + imageFilePathname); var rand = new DotNetSampler(); // todo - option to set seed? // do progressive refinement render loop for (int frameNo = 1; frameNo <= iterations; ++frameNo) { // render a frame camera.GetFrame(scene, rand, image); // display latest frame number Console.CursorLeft = 0; Console.Write("Iteration: {0} of {1}. Elapsed seconds {2}", frameNo, iterations, (Environment.TickCount-lastSaveTime)/1000); // save image every three minutes, and at end if ((frameNo == iterations) || (Environment.TickCount - lastSaveTime > SAVE_PERIOD * 1000)) { lastSaveTime = Environment.TickCount; image.SaveImage(imageFilePathname, frameNo, showPNG); if (frameNo == iterations) Console.WriteLine("\nImage file {0} saved", imageFilePathname); } } Console.WriteLine("\nfinished in {0} secs",(Environment.TickCount - starttime)/1000.0); } returnValue = 1; } // print exception message catch (Exception e) { Console.WriteLine("\n*** execution failed: " + e.Message); } return returnValue; }
public RayTracer(Scene scene) { this.scene = scene; }