private FreeImageBitmap simpleRender(int width, int height, int aaMode, bool transparentBG, Engine.Color bgColor, RenderTexture renderTexture) { renderTexture.getViewport(0).clear(FrameBufferType.FBT_COLOUR | FrameBufferType.FBT_DEPTH | FrameBufferType.FBT_STENCIL, bgColor); renderTexture.update(); OgrePlugin.PixelFormat format = OgrePlugin.PixelFormat.PF_A8R8G8B8; FreeImageAPI.PixelFormat bitmapFormat = FreeImageAPI.PixelFormat.Format32bppRgb; if (transparentBG) { bitmapFormat = FreeImageAPI.PixelFormat.Format32bppArgb; } FreeImageBitmap bitmap = new FreeImageBitmap(width, height, bitmapFormat); bitmap.copyFromRenderTarget(renderTexture, format); //Resize if aa is active if (aaMode > 1) { int smallWidth = width / aaMode; int smallHeight = height / aaMode; bitmap.Rescale(smallWidth, smallHeight, FREE_IMAGE_FILTER.FILTER_BILINEAR); } return(bitmap); }
private IEnumerable <IdleStatus> gridRender(int width, int height, int backBufferWidth, int backBufferHeight, int aaMode, RenderTexture renderTexture, Camera camera, Camera backgroundCamera, bool transparentBG, Engine.Color bgColor, Action <FreeImageBitmap> renderingCompletedCallback) { float originalLeft, originalRight, originalTop, originalBottom; camera.getFrustumExtents(out originalLeft, out originalRight, out originalTop, out originalBottom); int imageCountWidth = width % backBufferWidth == 0 ? width / backBufferWidth : width / backBufferWidth + 1; int imageCountHeight = height % backBufferHeight == 0 ? height / backBufferHeight : height / backBufferHeight + 1; float gridStepHoriz = (originalRight * 2) / imageCountWidth; float gridStepVert = (originalTop * 2) / imageCountHeight; float bgOriginalLeft = 0, bgOriginalRight = 0, bgOriginalTop = 0, bgOriginalBottom = 0, bgGridStepHoriz = 0, bgGridStepVert = 0; if (backgroundCamera != null) { backgroundCamera.getFrustumExtents(out bgOriginalLeft, out bgOriginalRight, out bgOriginalTop, out bgOriginalBottom); bgGridStepHoriz = (bgOriginalRight * 2) / imageCountWidth; bgGridStepVert = (bgOriginalTop * 2) / imageCountHeight; } int imageStepHoriz = backBufferWidth; int imageStepVert = backBufferHeight; int finalWidth = width / aaMode; int finalHeight = height / aaMode; int imageStepHorizSmall = finalWidth / imageCountWidth; int imageStepVertSmall = finalHeight / imageCountHeight; float left, right, top, bottom; float bgLeft, bgRight, bgTop, bgBottom; int totalSS = imageCountWidth * imageCountHeight; String updateString = "Rendering piece {0} of " + totalSS; OgrePlugin.PixelFormat format = OgrePlugin.PixelFormat.PF_A8R8G8B8; FreeImageAPI.PixelFormat bitmapFormat = FreeImageAPI.PixelFormat.Format32bppRgb; if (transparentBG) { bitmapFormat = FreeImageAPI.PixelFormat.Format32bppArgb; } Rectangle destRect = new Rectangle(); Rectangle srcRect = new Rectangle(0, 0, imageStepHoriz, imageStepVert); FreeImageBitmap fullBitmap = new FreeImageBitmap(finalWidth, finalHeight, bitmapFormat); using (FreeImageBitmap pieceBitmap = new FreeImageBitmap(imageStepHoriz, imageStepVert, bitmapFormat)) { bool aaOn = aaMode > 1; Rectangle scalarRectangle = new Rectangle(); if (aaOn) { scalarRectangle = new Rectangle(0, 0, imageStepHorizSmall, imageStepVertSmall); } for (int i = 0; i < totalSS; ++i) { int y = i / imageCountWidth; int x = i - y * imageCountWidth; left = originalLeft + gridStepHoriz * x; right = left + gridStepHoriz; top = originalTop - gridStepVert * y; bottom = top - gridStepVert; camera.setFrustumExtents(left, right, top, bottom); if (backgroundCamera != null) { bgLeft = bgOriginalLeft + bgGridStepHoriz * x; bgRight = bgLeft + bgGridStepHoriz; bgTop = bgOriginalTop - bgGridStepVert * y; bgBottom = bgTop - bgGridStepVert; backgroundCamera.setFrustumExtents(bgLeft, bgRight, bgTop, bgBottom); } Root.getSingleton().clearEventTimes(); renderTexture.getViewport(0).clear(FrameBufferType.FBT_COLOUR | FrameBufferType.FBT_DEPTH | FrameBufferType.FBT_STENCIL, bgColor); renderTexture.update(); pieceBitmap.copyFromRenderTarget(renderTexture, format); destRect.X = x * imageStepHorizSmall; destRect.Y = y * imageStepVertSmall; destRect.Width = imageStepHorizSmall; destRect.Height = imageStepVertSmall; if (aaOn) { using (FreeImageBitmap scaled = new FreeImageBitmap(pieceBitmap)) { scaled.Rescale(scalarRectangle.Width, scalarRectangle.Height, FREE_IMAGE_FILTER.FILTER_BILINEAR); fullBitmap.Paste(scaled, destRect.X, destRect.Y, int.MaxValue); } } else { fullBitmap.Paste(pieceBitmap, destRect.X, destRect.Y, int.MaxValue); } if (imageRendererProgress != null) { imageRendererProgress.update((uint)(((float)(i + 1) / totalSS) * 100.0f), String.Format(updateString, i + 1)); if (imageRendererProgress.Cancel) { break; } } yield return(IdleStatus.Ok); } } renderTexture.getViewport(0).setOverlaysEnabled(true); renderingCompletedCallback(fullBitmap); yield break; }
/// <summary> /// Write to the given graphics, if the widget texture is not large enough it will be /// resized temporarily and then sized back. So be careful with large destrects. /// </summary> /// <param name="g"></param> /// <param name="destRect"></param> public void writeToGraphics(FreeImageBitmap g, Rectangle destRect) { bool changedSize = false; IntSize2 originalSize = new IntSize2(imageBox.Width, imageBox.Height); float cropRatio = (float)imageBox.Width / destRect.Width; Rectangle srcRect = new Rectangle(0, 0, imageBox.Width, (int)(destRect.Height * cropRatio)); //Make sure the source image is large enough, if not resize. if (originalSize.Width < srcRect.Width || originalSize.Height < srcRect.Height) { imageBox.setSize(srcRect.Width, srcRect.Height); changedSize = true; resized(); renderTexture.update(); } else if (renderOneFrame) { renderTexture.update(); } using (FreeImageBitmap fullBitmap = new FreeImageBitmap(currentTextureWidth, currentTextureHeight, BitmapFormat)) { fullBitmap.copyFromRenderTarget(renderTexture, ogreTextureFormat); //Remove alpha //BitmapDataExtensions.SetAlpha(bmpData, 255); if (srcRect.Height > fullBitmap.Height) { srcRect.Height = fullBitmap.Height; } using (FreeImageBitmap cropped = fullBitmap.Copy(srcRect)) { if (cropped != null) { cropped.Rescale(destRect.Width, destRect.Height, FREE_IMAGE_FILTER.FILTER_BILINEAR); if (ogreTextureFormat == OgrePlugin.PixelFormat.PF_X8R8G8B8) { //Use the api to set the alpha channel to 255, this makes sure we dont carry over the x8 channel from ogre using (var alpha = cropped.GetChannel(FREE_IMAGE_COLOR_CHANNEL.FICC_ALPHA)) { alpha.FillBackground(new RGBQUAD(new FreeImageAPI.Color() { R = 255, G = 255, B = 255, A = 255 })); cropped.SetChannel(alpha, FREE_IMAGE_COLOR_CHANNEL.FICC_ALPHA); } } g.Paste(cropped, destRect.X, destRect.Y, int.MaxValue); } } } if (changedSize) { imageBox.setSize(originalSize.Width, originalSize.Height); resized(); renderTexture.update(); } }