public void RenderImage(Texture image) { try { if (_lbuf == null || _widthPx != image.Width || _heightPx != image.Height) { _lbuf = new WideColor[image.Height, image.Width]; _widthPx = image.Width; _heightPx = image.Height; _scene.Camera.SetScreenDimensions(_widthPx, _heightPx); } int max = ExecuteTasks(); double scale = 255.0 / max * BrigthnessScale; for (int i = 0; i < _heightPx; i++) { for (int j = 0; j < _widthPx; j++) { WideColor color = _lbuf[i, j]; image[i, j] = (color * scale).To8Bit(); } } } catch (Exception e) { Log.Info("Exception in renderImage:"); Log.Info(e); Shutdown = true; } }
protected override WideColor ShootRay(Shapes.Ray ray, int level) { if (level == MaxRecursionDepth) { return(Color.Red.ToWide()); } //Find nearest Hit CollisionDetails c = Scene.FindNearestHit(ray); //No hit if (c.Obj == null || double.IsInfinity(c.Distance)) { // TODO: actually use the scene sky here! return(Color.White.ToWide()); } //Calculate hit position Vect3 hitPoint = ray.Origin + ray.Direction * c.Distance; //Get color and normal at hitpoint WideColor color = c.Obj.GetColorAt(hitPoint).ToWide(); Vect3 normal = c.Obj.GetNormalAt(hitPoint); //Check if anything is blocking direct sunlight (go where the sunlight comes from) Vect3 lrDir = Scene.AmbientLightDirection * -1; var lightRay = new Shapes.Ray { Origin = hitPoint, Direction = lrDir }; CollisionDetails lc = Scene.FindNearestHit(lightRay); //if nothing blocks the sun's light, add ambient and diffuse light, otherwise ambient only double lightScale = 0; if (lc.Obj == null) { lightScale = normal * Scene.AmbientLightDirection; } lightScale = AmbientLightIntensity + DiffuseLightIntensity * (lightScale < 0 ? -lightScale : 0); return(color * lightScale); }
/** * Renders the part <code>id</code> of <code>splitCount</code> of the image to ibuf. * * @param id a number from <code>0</code> to <code>splitCount-1</code> */ private int RenderImagePart(int id) { int splitCount = _threadCount * SplitMultiplier; int slotHeight = _heightPx / splitCount; int from = slotHeight * id; int to = slotHeight * (id + 1); if (id == splitCount - 1) { to = _heightPx; } BackwardRayTracer logic = GetLogic(); Camera camera = _scene.Camera; var baseDirection = camera.ViewPaneEdge - camera.Position; var vertAdd = camera.ViewPaneHeightVector / (_heightPx - 1); var horzAdd = camera.ViewPaneWidthVector / (_widthPx - 1); int localMaxBrightness = 0; for (int i = from; i < to; i++) { for (int j = 0; j < _widthPx; j++) { var ray = new Shapes.Ray { Origin = camera.Position, Direction = (baseDirection + vertAdd * i + horzAdd * j).Normalize() }; WideColor color = logic.Shoot(ray); ushort localBrightness = color.GetMax(); if (localBrightness > localMaxBrightness) { localMaxBrightness = localBrightness; } _lbuf[i, j] = color; } } return(localMaxBrightness); }