/////////////// private bool CreateRenderTexture() { Vec2I size = new Vec2I(512, 256); string textureName = TextureManager.Instance.GetUniqueName("RenderToTextureExample"); texture = TextureManager.Instance.Create(textureName, Texture.Type.Type2D, size, 1, 0, PixelFormat.R8G8B8, Texture.Usage.RenderTarget); if (texture == null) { return(false); } renderTexture = texture.GetBuffer().GetRenderTarget(); //you can update render texture manually by means renderTexture.Update() method. For this task set AutoUpdate = false; renderTexture.AutoUpdate = true; //create camera string cameraName = SceneManager.Instance.GetUniqueCameraName("RenderToTextureExample"); camera = SceneManager.Instance.CreateCamera(cameraName); camera.Purpose = Camera.Purposes.Special; camera.AllowMapCompositorManager = false; //add viewport viewport = renderTexture.AddViewport(camera); viewport.BackgroundColor = new ColorValue(0, 0, 0, 1); viewport.ShadowsEnabled = false; viewport.MaterialScheme = ""; //add listener renderTargetListener = new SceneRenderTargetListener(this); renderTexture.AddListener(renderTargetListener); return(true); }
private bool CreateRenderTarget() { DestroyRenderTarget(); if (RendererWorld.Instance == null) { return(false); } Vec2I textureSize = GetDemandTextureSize(); if (textureSize.X < 1 || textureSize.Y < 1) { return(false); } string textureName = TextureManager.Instance.GetUniqueName("WPFRenderTexture"); int hardwareFSAA = 0; if (!RendererWorld.InitializationOptions.AllowSceneMRTRendering) { if (!int.TryParse(RendererWorld.InitializationOptions.FullSceneAntialiasing, out hardwareFSAA)) { hardwareFSAA = 0; } } texture = TextureManager.Instance.Create(textureName, Texture.Type.Type2D, textureSize, 1, 0, Engine.Renderer.PixelFormat.R8G8B8, Texture.Usage.RenderTarget, false, hardwareFSAA); if (texture == null) { return(false); } currentTextureSize = textureSize; renderTexture = texture.GetBuffer().GetRenderTarget(); renderTexture.AutoUpdate = false; renderTexture.AllowAdditionalMRTs = true; camera = SceneManager.Instance.CreateCamera( SceneManager.Instance.GetUniqueCameraName("UserControl")); camera.Purpose = Camera.Purposes.MainCamera; //update camera settings camera.NearClipDistance = cameraNearFarClipDistance.Minimum; camera.FarClipDistance = cameraNearFarClipDistance.Maximum; camera.AspectRatio = (float)texture.Size.X / (float)texture.Size.Y; camera.FixedUp = cameraFixedUp; camera.Position = cameraPosition; camera.Direction = cameraDirection; camera.Fov = cameraFov; camera.ProjectionType = cameraProjectionType; camera.OrthoWindowHeight = cameraOrthoWindowHeight; viewport = renderTexture.AddViewport(camera); //Initialize HDR compositor for HDR render technique if (EngineApp.RenderTechnique == "HDR") { viewport.AddCompositor("HDR", 0); viewport.SetCompositorEnabled("HDR", true); } //Initialize Fast Approximate Antialiasing (FXAA) { bool useMRT = RendererWorld.InitializationOptions.AllowSceneMRTRendering; string fsaa = RendererWorld.InitializationOptions.FullSceneAntialiasing; if ((useMRT && (fsaa == "" || fsaa == "RecommendedSetting") && IsActivateFXAAByDefault()) || fsaa == "FXAA") { if (RenderSystem.Instance.HasShaderModel3()) { InitializeFXAACompositor(); } } } //add listener renderTargetListener = new ViewRenderTargetListener(this); renderTexture.AddListener(renderTargetListener); if (guiRenderer == null) { guiRenderer = new GuiRenderer(viewport); } else { guiRenderer.ChangeViewport(viewport); } if (controlManager == null) { controlManager = new ScreenControlManager(guiRenderer); } //initialize D3DImage output if (d3dImageIsSupported && allowUsingD3DImage) { // create a D3DImage to host the scene and monitor it for changes in front buffer availability if (d3dImage == null) { d3dImage = new D3DImage(); d3dImage.IsFrontBufferAvailableChanged += D3DImage_IsFrontBufferAvailableChanged; CompositionTarget.Rendering += D3DImage_OnRendering; } // set output to background image Background = new ImageBrush(d3dImage); // set the back buffer using the new scene pointer HardwarePixelBuffer buffer = texture.GetBuffer(0, 0); GetD3D9HardwarePixelBufferData data = new GetD3D9HardwarePixelBufferData(); data.hardwareBuffer = buffer._GetRealObject(); data.outPointer = IntPtr.Zero; unsafe { GetD3D9HardwarePixelBufferData *pData = &data; if (!RenderSystem.Instance.CallCustomMethod("Get D3D9HardwarePixelBuffer getSurface", (IntPtr)pData)) { Log.Fatal("Get D3D9HardwarePixelBuffer getSurface failed."); } } d3dImage.Lock(); d3dImage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, data.outPointer); d3dImage.Unlock(); } return(true); }
bool CreateRenderTarget() { DestroyRenderTarget(); if( RendererWorld.Instance == null ) return false; Vec2I textureSize = GetDemandTextureSize(); if( textureSize.X < 1 || textureSize.Y < 1 ) return false; string textureName = TextureManager.Instance.GetUniqueName( "WPFRenderTexture" ); int hardwareFSAA = 0; if( !RendererWorld.InitializationOptions.AllowSceneMRTRendering ) { if( !int.TryParse( RendererWorld.InitializationOptions.FullSceneAntialiasing, out hardwareFSAA ) ) hardwareFSAA = 0; } texture = TextureManager.Instance.Create( textureName, Texture.Type.Type2D, textureSize, 1, 0, Engine.Renderer.PixelFormat.R8G8B8, Texture.Usage.RenderTarget, false, hardwareFSAA ); if( texture == null ) return false; currentTextureSize = textureSize; renderTexture = texture.GetBuffer().GetRenderTarget(); renderTexture.AutoUpdate = false; renderTexture.AllowAdditionalMRTs = true; camera = SceneManager.Instance.CreateCamera( SceneManager.Instance.GetUniqueCameraName( "UserControl" ) ); camera.Purpose = Camera.Purposes.MainCamera; //update camera settings camera.NearClipDistance = cameraNearFarClipDistance.Minimum; camera.FarClipDistance = cameraNearFarClipDistance.Maximum; camera.AspectRatio = (float)texture.Size.X / (float)texture.Size.Y; camera.FixedUp = cameraFixedUp; camera.Position = cameraPosition; camera.Direction = cameraDirection; camera.Fov = cameraFov; camera.ProjectionType = cameraProjectionType; camera.OrthoWindowHeight = cameraOrthoWindowHeight; viewport = renderTexture.AddViewport( camera ); //Initialize HDR compositor for HDR render technique if( EngineApp.RenderTechnique == "HDR" ) { viewport.AddCompositor( "HDR", 0 ); viewport.SetCompositorEnabled( "HDR", true ); } //Initialize Fast Approximate Antialiasing (FXAA) { bool useMRT = RendererWorld.InitializationOptions.AllowSceneMRTRendering; string fsaa = RendererWorld.InitializationOptions.FullSceneAntialiasing; if( ( useMRT && ( fsaa == "" || fsaa == "RecommendedSetting" ) && IsActivateFXAAByDefault() ) || fsaa == "FXAA" ) { if( RenderSystem.Instance.HasShaderModel3() ) InitializeFXAACompositor(); } } //add listener renderTargetListener = new ViewRenderTargetListener( this ); renderTexture.AddListener( renderTargetListener ); if( guiRenderer == null ) guiRenderer = new GuiRenderer( viewport ); else guiRenderer.ChangeViewport( viewport ); if( controlManager == null ) controlManager = new ScreenControlManager( guiRenderer ); //initialize D3DImage output if( d3dImageIsSupported && allowUsingD3DImage ) { // create a D3DImage to host the scene and monitor it for changes in front buffer availability if( d3dImage == null ) { d3dImage = new D3DImage(); d3dImage.IsFrontBufferAvailableChanged += D3DImage_IsFrontBufferAvailableChanged; CompositionTarget.Rendering += D3DImage_OnRendering; } // set output to background image Background = new ImageBrush( d3dImage ); // set the back buffer using the new scene pointer HardwarePixelBuffer buffer = texture.GetBuffer( 0, 0 ); GetD3D9HardwarePixelBufferData data = new GetD3D9HardwarePixelBufferData(); data.hardwareBuffer = buffer._GetRealObject(); data.outPointer = IntPtr.Zero; unsafe { GetD3D9HardwarePixelBufferData* pData = &data; if( !RenderSystem.Instance.CallCustomMethod( "Get D3D9HardwarePixelBuffer getSurface", (IntPtr)pData ) ) Log.Fatal( "Get D3D9HardwarePixelBuffer getSurface failed." ); } d3dImage.Lock(); d3dImage.SetBackBuffer( D3DResourceType.IDirect3DSurface9, data.outPointer ); d3dImage.Unlock(); } return true; }
void CreateReflectionTexture() { DestroyReflectionTexture(); Vec2i textureSize = GetRequiredReflectionTextureSize(); //create render texture string textureName = TextureManager.Instance.GetUniqueName( "WaterPlaneReflection" ); bool hdr = RendererWorld.Instance.DefaultViewport.GetCompositorInstance( "HDR" ) != null; reflectionTexture = TextureManager.Instance.Create( textureName, Texture.Type.Type2D, textureSize, 1, 0, hdr ? PixelFormat.Float16RGB : PixelFormat.R8G8B8, Texture.Usage.RenderTarget ); reflectionRenderTexture = reflectionTexture.GetBuffer().GetRenderTarget(); //create camera reflectionCamera = SceneManager.Instance.CreateCamera(); reflectionCamera.AllowFrustumTestMode = true; //add viewport reflectionViewport = reflectionRenderTexture.AddViewport( reflectionCamera ); reflectionViewport.ShadowsEnabled = false; reflectionViewport.MaterialScheme = MaterialSchemes.Low.ToString(); //add listener renderTargetListener = new ReflectionRenderTargetListener( this ); reflectionRenderTexture.AddListener( renderTargetListener ); reflectionRenderTexture.AutoUpdate = Visible; }
protected override void CreateScene() { mLog = LogManager.Singleton.createLog("RSQTest.log", false, true); mLog.LogMessage(string.Format("RSQTest log {0}", System.DateTime.Now)); create4LineDebugOverLay(); Show4LineDebugOverLay(); //setup RenderTargetListenerDirector mRTListener = new RenderTargetListenerDirector(); mRTListener.SubscribeEvents(); mRTListener.evtPreRenderTargetUpdate += new RenderTargetListenerDirector.RTLD_Delegate_RenderTargetUpdate(event_PreRenderTargetUpdate); mRTListener.evtPostRenderTargetUpdate += new RenderTargetListenerDirector.RTLD_Delegate_RenderTargetUpdate(event_PostRenderTargetUpdate); mSceneManager.SetAmbientLight(Converter.GetColor(0.2f, 0.2f, 0.2f)); mSceneManager.SetSkyBox(true, "Examples/MorningSkyBox"); // Create a light Light l = mSceneManager.CreateLight("MainLight"); l.SetLightType(OgreDotNet.Light.LightTypes.Directional); Math3D.Vector3 dir = new Vector3(0.5f, -1.0f, 0.0f); dir.Normalize(); l.SetDirection(dir); l.SetDiffuseColour(1.0f, 1.0f, 0.8f); l.SetSpecularColour(1.0f, 1.0f, 1.0f); // Create a prefab plane mPlane = new MovablePlane("ReflectPlane"); mPlane.D = 0; mPlane.Normal = OgreVector3.FromVector3(Math3D.Vector3.UnitY); MeshManager.GetSingleton().CreatePlane("ReflectionPlane", ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME, mPlane, 2000, 2000, 1, 1, true, 1, 1, 1, Vector3.UnitZ); mPlaneEnt = mSceneManager.CreateEntity("Plane", "ReflectionPlane"); // Create an entity from a model (will be loaded automatically) Entity knotEnt = mSceneManager.CreateEntity("Knot", "knot.mesh"); // Create an entity from a model (will be loaded automatically) Entity ogreHead = mSceneManager.CreateEntity("Head", "ogrehead.mesh"); knotEnt.SetMaterialName("Examples/TextureEffect2"); // Attach the rtt entity to the root of the scene SceneNode rootNode = mSceneManager.GetRootSceneNode(); mPlaneNode = rootNode.CreateChildSceneNode(); // Attach both the plane entity, and the plane definition mPlaneNode.AttachObject(mPlaneEnt); //multi Inheritence problem, use the static Get*From function to convert MovableObject mo = MovableObject.GetMovableObjectFrom(mPlane); mPlaneNode.AttachObject(mo); mPlaneNode.Translate(0.0f, -10.0f, 0.0f); // Tilt it a little to make it interesting //mPlaneNode.Roll( new Radian( new Degree( 5.0f)) ); mPlaneNode.Roll(5.0f); rootNode.CreateChildSceneNode("Head").AttachObject(ogreHead); RenderTexture rttTex = mRoot.GetRenderSystem().CreateRenderTexture("RttTex", 512, 512, TextureType.TEX_TYPE_2D, PixelFormat.PF_R8G8B8); { //new scope for some reason mReflectCam = mSceneManager.CreateCamera("ReflectCam"); mReflectCam.SetNearClipDistance(mCamera.GetNearClipDistance()); mReflectCam.SetFarClipDistance(mCamera.GetFarClipDistance()); mReflectCam.SetAspectRatio( (float)mRenderWindow.GetViewport(0).ActualWidth / (float)mRenderWindow.GetViewport(0).ActualHeight); Viewport v = rttTex.AddViewport(mReflectCam); v.ClearEveryFrame = true; v.BackgroundColor = System.Drawing.Color.Black; //Converter.ToColor( ColourValue.Black ); ResourcePtr resPtr = MaterialManager.Instance.Create("RttMat", ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME); MaterialPtr mat = new MaterialPtr(ResourcePtr.getCPtr(resPtr).Handle, false); TextureUnitState t = mat.Get().GetTechnique(0).getPass(0).createTextureUnitState("RustedMetal.jpg"); t = mat.Get().GetTechnique(0).getPass(0).createTextureUnitState("RttTex"); // Blend with base texture t.setColourOperationEx(LayerBlendOperationEx.LBX_BLEND_MANUAL, LayerBlendSource.LBS_TEXTURE, LayerBlendSource.LBS_CURRENT, Color.White, Color.White, 0.25f); t.SetTextureAddressingMode(TextureUnitState.TextureAddressingMode.TAM_CLAMP); t.setProjectiveTexturing(true, mReflectCam); rttTex.AddListener(mRTListener); // set up linked reflection mReflectCam.EnableReflection(mPlane); // Also clip mReflectCam.EnableCustomNearClipPlane(mPlane); } // Give the plane a texture mPlaneEnt.SetMaterialName("RttMat"); // Add a whole bunch of extra transparent entities Entity cloneEnt; for (int n = 0; n < 10; ++n) { // Create a new node under the root SceneNode node = mSceneManager.CreateSceneNode(); // Random translate Vector3 nodePos; nodePos.x = OgreMath.SymmetricRandom() * 750.0f; nodePos.y = OgreMath.SymmetricRandom() * 100.0f + 25.0f; nodePos.z = OgreMath.SymmetricRandom() * 750.0f; node.SetPosition(nodePos); rootNode.AddChild(node); // Clone knot string cloneName = string.Format("Knot{0}", n); cloneEnt = knotEnt.Clone(cloneName); // Attach to new node node.AttachObject(cloneEnt); } mCamera.SetPosition(new Vector3(-50.0f, 100.0f, 500.0f)); mCamera.LookAt = new Vector3(0, 0, 0); }
/////////////// private bool CreateRenderTexture() { Vec2I size = new Vec2I(512, 256); string textureName = TextureManager.Instance.GetUniqueName("RenderToTextureExample"); texture = TextureManager.Instance.Create(textureName, Texture.Type.Type2D, size, 1, 0, PixelFormat.R8G8B8, Texture.Usage.RenderTarget); if (texture == null) return false; renderTexture = texture.GetBuffer().GetRenderTarget(); //you can update render texture manually by means renderTexture.Update() method. For this task set AutoUpdate = false; renderTexture.AutoUpdate = true; //create camera string cameraName = SceneManager.Instance.GetUniqueCameraName("RenderToTextureExample"); camera = SceneManager.Instance.CreateCamera(cameraName); camera.Purpose = Camera.Purposes.Special; camera.AllowMapCompositorManager = false; //add viewport viewport = renderTexture.AddViewport(camera); viewport.BackgroundColor = new ColorValue(0, 0, 0, 1); viewport.ShadowsEnabled = false; viewport.MaterialScheme = ""; //add listener renderTargetListener = new SceneRenderTargetListener(this); renderTexture.AddListener(renderTargetListener); return true; }