示例#1
0
 /// <summary>
 /// An async image render. This will hijack the idle handler's onIdle to
 /// render the image. You supply a callback that will be called when the
 /// image completes rendering. This funciton will return immediately.
 /// </summary>
 /// <param name="properties"></param>
 /// <param name="renderingCompletedCallback"></param>
 public void renderImageAsync(ImageRendererProperties properties, Action <FreeImageBitmap> renderingCompletedCallback)
 {
     idleHandler.runTemporaryIdle(renderImage(properties, (product) =>
     {
         renderingCompletedCallback(product);
     }));
 }
示例#2
0
        /// <summary>
        /// A synchronous image render. It will happen on the calling thread like normal.
        /// </summary>
        /// <param name="properties"></param>
        /// <returns></returns>
        public FreeImageBitmap renderImage(ImageRendererProperties properties)
        {
            FreeImageBitmap          image   = null;
            IEnumerable <IdleStatus> process = renderImage(properties, (product) =>
            {
                image = product;
            });
            IEnumerator <IdleStatus> runner = process.GetEnumerator();

            while (runner.MoveNext())
            {
                ;
            }
            return(image);
        }
示例#3
0
        public MedicalStateController(ImageRenderer imageRenderer, MedicalController medicalController)
        {
            this.imageRenderer = imageRenderer;

            imageProperties        = new ImageRendererProperties();
            imageProperties.Width  = 100;
            imageProperties.Height = 100;
            imageProperties.UseWindowBackgroundColor  = false;
            imageProperties.CustomBackgroundColor     = BACK_COLOR;
            imageProperties.AntiAliasingMode          = 2;
            imageProperties.UseActiveViewportLocation = true;
            imageProperties.OverrideLayers            = false;
            imageProperties.TransparentBackground     = true;
            imageProperties.ShowBackground            = false;
            imageProperties.ShowWatermark             = false;
            imageProperties.ShowUIUpdates             = false;

            this.medicalController = medicalController;
        }
示例#4
0
        private IEnumerable <IdleStatus> renderImage(ImageRendererProperties properties, Action <FreeImageBitmap> renderingCompletedCallback)
        {
            if (imageRendererProgress != null)
            {
                imageRendererProgress.Visible = properties.ShowUIUpdates;
                imageRendererProgress.Cancel  = false;
                imageRendererProgress.update(0, "Rendering Image");
            }

            FreeImageBitmap bitmap      = null;
            SceneViewWindow sceneWindow = sceneViewController.ActiveWindow;

            if (sceneWindow != null)
            {
                if (ImageRenderStarted != null)
                {
                    ImageRenderStarted.Invoke(this, EventArgs.Empty);
                }

                //Background color
                Engine.Color backgroundColor = properties.CustomBackgroundColor;
                if (properties.UseWindowBackgroundColor)
                {
                    backgroundColor = sceneWindow.BackColor;
                }

                //Size (with AA)
                //int width = properties.Width * properties.AntiAliasingMode;
                //int height = properties.Height * properties.AntiAliasingMode;

                //Camera position
                Vector3 cameraPosition = sceneWindow.Translation;
                Vector3 cameraLookAt   = sceneWindow.LookAt;
                if (!properties.UseActiveViewportLocation)
                {
                    cameraPosition = properties.CameraPosition;
                    cameraLookAt   = properties.CameraLookAt;
                }

                //Turn off background if needed
                if (background != null)
                {
                    background.setVisible(properties.ShowBackground);
                }

                //Layer override
                String activeTransparencyState = TransparencyController.ActiveTransparencyState;
                if (properties.OverrideLayers)
                {
                    TransparencyController.ActiveTransparencyState = TransparencyStateName;
                    if (properties.LayerState != null)
                    {
                        properties.LayerState.instantlyApply();
                    }
                    else
                    {
                        Log.Warning("ImageRenderer was told to override layers but no layer state was provided.");
                    }
                }

                TransparencyController.applyTransparencyState(TransparencyController.ActiveTransparencyState);

                //Render
                IEnumerable <IdleStatus> process = createRender(properties.Width, properties.Height, properties.AntiAliasingMode, properties.ShowWatermark, properties.TransparentBackground, backgroundColor, sceneWindow.Camera, cameraPosition, cameraLookAt, sceneWindow.MinNearDistance, sceneWindow.NearPlaneWorld, sceneWindow.NearFarLength, properties,
                                                                (product) =>
                {
                    bitmap = product;
                });
                foreach (IdleStatus idleStatus in process)
                {
                    yield return(idleStatus);
                }

                //Turn off layer override
                if (properties.OverrideLayers)
                {
                    TransparencyController.ActiveTransparencyState = activeTransparencyState;
                }

                //Reactivate background
                if (background != null)
                {
                    background.setVisible(true);
                }

                if (ImageRenderCompleted != null)
                {
                    ImageRenderCompleted.Invoke(this, EventArgs.Empty);
                }
            }

            if (imageRendererProgress != null && properties.ShowUIUpdates)
            {
                imageRendererProgress.Visible = false;
            }

            renderingCompletedCallback(bitmap);
            yield break;
        }
示例#5
0
        private IEnumerable <IdleStatus> createRender(int finalWidth, int finalHeight, int aaMode, bool showWatermark, bool transparentBG, Engine.Color backColor, Camera cloneCamera, Vector3 position, Vector3 lookAt, float minNearDistance, float nearPlaneWorld, float nearFarLength, ImageRendererProperties properties, Action <FreeImageBitmap> renderingCompletedCallback)
        {
            FreeImageBitmap  bitmap       = null;
            OgreSceneManager sceneManager = controller.CurrentScene.getDefaultSubScene().getSimElementManager <OgreSceneManager>();

            if (sceneManager != null)
            {
                bool doGridRender;
                int  backBufferWidth;
                int  backBufferHeight;

                using (TexturePtr texture = createOgreTexture(finalWidth, finalHeight, aaMode, out doGridRender, out backBufferWidth, out backBufferHeight))
                {
                    if (texture != null)
                    {
                        using (HardwarePixelBufferSharedPtr pixelBuffer = texture.Value.getBuffer())
                        {
                            RenderTexture renderTexture = pixelBuffer.Value.getRenderTarget();
                            Camera        camera        = sceneManager.SceneManager.createCamera("__PictureCamera");
                            camera.setLodBias(cloneCamera.getLodBias());
                            camera.setUseRenderingDistance(cloneCamera.getUseRenderingDistance());
                            camera.setNearClipDistance(cloneCamera.getNearClipDistance());
                            camera.setFarClipDistance(cloneCamera.getFarClipDistance());
                            camera.setPolygonMode(cloneCamera.getPolygonMode());
                            camera.setRenderingDistance(cloneCamera.getRenderingDistance());
                            camera.setProjectionType(cloneCamera.getProjectionType());
                            camera.setFOVy(cloneCamera.getFOVy());

                            camera.setAutoAspectRatio(false);
                            camera.setAspectRatio((float)finalWidth / finalHeight);

                            SceneNode node = sceneManager.SceneManager.createSceneNode("__PictureCameraNode");
                            node.attachObject(camera);
                            node.setPosition(position);
                            sceneManager.SceneManager.getRootSceneNode().addChild(node);
                            camera.lookAt(lookAt);
                            Viewport viewport = renderTexture.addViewport(camera, 1, 0.0f, 0.0f, 1.0f, 1.0f);

                            if (properties.UseIncludePoint)
                            {
                                Matrix4x4 viewMatrix       = camera.getViewMatrix();
                                Matrix4x4 projectionMatrix = camera.getProjectionMatrix();
                                float     aspect           = camera.getAspectRatio();
                                float     fovy             = camera.getFOVy() * 0.5f;

                                float   distance  = SceneViewWindow.computeOffsetToIncludePoint(viewMatrix, projectionMatrix, properties.IncludePoint, aspect, fovy);
                                Vector3 direction = (position - lookAt).normalized();
                                node.setPosition(position - (direction * distance));
                                camera.lookAt(lookAt);
                            }

                            if (transparentBG)
                            {
                                backColor.a = 0.0f;
                            }

                            ViewportBackground bgViewport = null;
                            if (background != null)
                            {
                                bgViewport = new ViewportBackground("ImageRenderer", 0, background, renderTexture, false);
                                bgViewport.BackgroundColor = backColor;
                                bgViewport.Camera.setAutoAspectRatio(false);
                                bgViewport.Camera.setAspectRatio((float)finalWidth / finalHeight);
                            }
                            viewport.setBackgroundColor(backColor);
                            viewport.setOverlaysEnabled(false);
                            viewport.setClearEveryFrame(false);

                            if (properties.CustomizeCameraPosition != null)
                            {
                                properties.CustomizeCameraPosition(camera, viewport);
                            }

                            float near = CameraPositioner.computeNearClipDistance(camera.getDerivedPosition().length(), minNearDistance, nearPlaneWorld);
                            camera.setNearClipDistance(near);
                            camera.setFarClipDistance(near + nearFarLength);

                            if (doGridRender)
                            {
                                IEnumerable <IdleStatus> process = gridRender(finalWidth * aaMode, finalHeight * aaMode, backBufferWidth, backBufferHeight, aaMode, renderTexture, camera, bgViewport != null ? bgViewport.Camera : null, transparentBG, backColor,
                                                                              (product) =>
                                {
                                    bitmap = product;
                                });
                                foreach (IdleStatus idleStatus in process)
                                {
                                    yield return(idleStatus);
                                }
                            }
                            else
                            {
                                bitmap = simpleRender(backBufferWidth, backBufferHeight, aaMode, transparentBG, backColor, renderTexture);
                            }

                            if (showWatermark && LoadLogo != null)
                            {
                                using (FreeImageBitmap logo = LoadLogo())
                                {
                                    float imageFinalHeight = bitmap.Height * 0.0447f;
                                    float scale            = imageFinalHeight / logo.Height;
                                    float imageFinalWidth  = logo.Width * scale;
                                    if (imageFinalWidth > bitmap.Width)
                                    {
                                        imageFinalWidth  = bitmap.Width;
                                        scale            = imageFinalWidth / logo.Width;
                                        imageFinalHeight = logo.Height * scale;
                                    }

                                    logo.Rescale((int)imageFinalWidth, (int)imageFinalHeight, FREE_IMAGE_FILTER.FILTER_BILINEAR);
                                    //Have to composite the logo image first.
                                    using (FreeImageBitmap fullImageCorner = bitmap.Copy(0, bitmap.Height - (int)imageFinalHeight, (int)imageFinalWidth, bitmap.Height))
                                    {
                                        fullImageCorner.ConvertColorDepth(FREE_IMAGE_COLOR_DEPTH.FICD_24_BPP);
                                        logo.Composite(false, null, fullImageCorner);
                                    }
                                    bitmap.Paste(logo, 0, bitmap.Height - (int)imageFinalHeight, int.MaxValue);
                                }
                            }

                            renderTexture.destroyViewport(viewport);
                            if (bgViewport != null)
                            {
                                bgViewport.Dispose();
                            }
                            sceneManager.SceneManager.getRootSceneNode().removeChild(node);
                            sceneManager.SceneManager.destroySceneNode(node);
                            sceneManager.SceneManager.destroyCamera(camera);

                            TextureManager.getInstance().remove(texture);
                        }
                    }
                    else
                    {
                        //An error making the render texture. Log it and return the error image.
                        Log.Error("Could not render image. Returning placeholder image. Reason: Could not create valid render to texture target.");
                        bitmap = new FreeImageBitmap(finalWidth, finalHeight);
                        bitmap.FillBackground(new RGBQUAD()
                        {
                            rgbRed = 255
                        });
                    }
                }
            }
            renderingCompletedCallback(bitmap);
            yield break;
        }