public unsafe Bitmap Render() { int width = _renderTarget.ColorBuffer.Bitmap.Width; int height = _renderTarget.ColorBuffer.Bitmap.Height; Rectangle imageRectangle = new Rectangle(new Point { X = 0, Y = 0 }, _renderTarget.ColorBuffer.Bitmap.Size); Bitmap result = new Bitmap(width, height, PixelFormat.Format24bppRgb); BitmapData bitmapData = _renderTarget.ColorBuffer.Bitmap.LockBits(imageRectangle, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); BitmapData resultBitmapData = result.LockBits(imageRectangle, ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb); byte *bitmapScan0 = (byte *)bitmapData.Scan0.ToPointer(); byte *resultScan0 = (byte *)resultBitmapData.Scan0.ToPointer(); byte *bitmapPixelPointer; byte *resultPixelPointer; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { double pixelDepth = _renderTarget.DepthBuffer[y, x]; bitmapPixelPointer = bitmapScan0 + y * bitmapData.Stride + x * 3; resultPixelPointer = resultScan0 + y * resultBitmapData.Stride + x * 3; if (pixelDepth >= _focalLengthMin && pixelDepth <= _focalLengthMax) { resultPixelPointer[0] = bitmapPixelPointer[0]; resultPixelPointer[1] = bitmapPixelPointer[1]; resultPixelPointer[2] = bitmapPixelPointer[2]; } else { AverageColor3b color = new AverageColor3b(); int kernelSize = (int)(ScalarHelpers.Saturate(System.Math.Abs(pixelDepth - _focalLength) / 8.0) * _maxKernelSize); if ((kernelSize % 2) == 0) { if (kernelSize > 0) { kernelSize = kernelSize - 1; } else { kernelSize = 1; } } int halfKernelSize = (kernelSize - 1) / 2; Rectangle kernelRectangle = new Rectangle(x - halfKernelSize, y - halfKernelSize, kernelSize, kernelSize); Rectangle intersection = Rectangle.Intersect(imageRectangle, kernelRectangle); for (int yy = intersection.Y; yy < intersection.Bottom; yy++) { for (int xx = intersection.X; xx < intersection.Right; xx++) { bitmapPixelPointer = bitmapScan0 + yy * bitmapData.Stride + xx * 3; color.Add(bitmapPixelPointer); } } resultPixelPointer[0] = color.B; resultPixelPointer[1] = color.G; resultPixelPointer[2] = color.R; } } } _renderTarget.ColorBuffer.Bitmap.UnlockBits(bitmapData); result.UnlockBits(resultBitmapData); return(result); }
public virtual Color GetColor(Vector3d direction, IIntersectionResult intersectionResult, Scene scene) { Color3f result = new Color3f(); Color3f surfaceColor = Material.Texture != null?Material.Texture.GetColor(GetUVCoordinates(intersectionResult), 0) : Material.Color; Color3f reflectionColor = new Color3f(); if (Material.Reflection > 0.0f) { // Reflection Vector3d reflectionVector = VectorHelpers.GetReflectionVector(direction, GetNormal(intersectionResult)); IIntersectionResult reflectionIntersectionResult = scene.GetNearestObjectIntersection(reflectionVector, intersectionResult.Intersection, this); if (reflectionIntersectionResult != null) { reflectionColor = new Color3f(reflectionIntersectionResult.Object.GetColor(direction, reflectionIntersectionResult, scene)); } } if (scene.Lights.Count == 0) { return(surfaceColor.ToColor()); } foreach (AbstractLight light in scene.Lights) { bool lightVisible = true; if (light.Position != null) { Vector3d lightDistanceVector = light.Position - intersectionResult.Intersection; double lightDistance = lightDistanceVector.Length(); foreach (AbstractObject3d o in scene.Objects) { if (o.GetTopParent() == this.GetTopParent()) { continue; } IIntersectionResult lightIntersectionResult = o.Intersection(Vector3d.Normalize(lightDistanceVector), intersectionResult.Intersection); if (lightIntersectionResult != null) { if (lightIntersectionResult.IntersectionDistance < lightDistance) { lightVisible = false; break; } } } } if (!lightVisible) { continue; } Vector3d normal = GetNormal(intersectionResult); Vector3d lightDirection = -light.GetDirection(intersectionResult.Intersection); double distanceSquared = light.GetDistance(intersectionResult.Intersection); distanceSquared *= distanceSquared; double normalDotLightDirection = normal.Dot(lightDirection); double diffuseIntensity = ScalarHelpers.Saturate(normalDotLightDirection); Color3f diffuse = diffuseIntensity * light.DiffuseColor * light.DiffusePower / distanceSquared; Vector3d halfwayVector = (lightDirection - direction).Normalize(); double normalDotHalfwayVector = normal.Dot(halfwayVector); double specularIntensity = System.Math.Pow(ScalarHelpers.Saturate(normalDotHalfwayVector), 16f); Color3f specular = specularIntensity * light.SpecularColor * light.SpecularPower / distanceSquared; //return (surfaceColor * (diffuse + specular)).ToColor(); result += diffuse + specular; } return((((surfaceColor * result) * (1.0 - Material.Reflection)) + (reflectionColor * Material.Reflection)).ToColor()); }