public void Init(RTRenderer renderer, IRTCamera cam, int canvasWidth, int canvasHeight, int numSamples) { if (status != STATUS_UNDEFINED) { return; } this.renderer = renderer; this.cam = cam; this.canvasWidth = canvasWidth; this.canvasHeight = canvasHeight; this.numSamples = numSamples; status = STATUS_READY; }
protected virtual void Start() { int canvasWidth = isScreenSize ? Screen.width : 200; int canvasHeight = isScreenSize ? Screen.height : 100; cam = CreateCamera(canvasWidth, canvasHeight); ppmTexture.Init(canvasWidth, canvasHeight); renderingTasksManager = new RenderingTasksManager(); int pixelIndex = 0; RenderingTask task = null; int itemsCount = 0; for (int j = 0; j < ppmTexture.Height; ++j) { for (int i = 0; i < ppmTexture.Width; ++i) { if (multiThreadRendering) { if (itemsCount == 0) { task = new RenderingTask(); task.Init(this, cam, ppmTexture.Width, ppmTexture.Height, numSamples); renderingTasksManager.AddTask(task, RenderingTaskCompleteCB); } task.AddItem(i, j, pixelIndex); ++itemsCount; if (itemsCount == RenderingTask.SIZE) { itemsCount = 0; } ++pixelIndex; } else { Color color = Color.black; for (int s = 0; s < numSamples; ++s) { float u = (i + RTMath.Rnd01()) / ppmTexture.Width; float v = (j + RTMath.Rnd01()) / ppmTexture.Height; RTRay ray = cam.GetRay(u, v); color += GetColor(ray, 0); } color /= numSamples; ppmTexture.WriteAPixel(color); } } } if (!multiThreadRendering) { ppmTexture.Complete(); RenderingComplete(); } else { renderingTasksManager.Start(); } }