public MaterialPtr RenderPreview() { string textureName = "RttTex_Character_" + Guid.NewGuid().ToString(); string materialName = "RttMat_Character_" + Guid.NewGuid().ToString(); TexturePtr rttTexture = TextureManager.Singleton.CreateManual( textureName, ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME, TextureType.TEX_TYPE_2D, GameManager.Instance.renderWindow.Width, GameManager.Instance.renderWindow.Height, 0, PixelFormat.PF_R8G8B8, (int)TextureUsage.TU_RENDERTARGET); RenderTexture renderTexture = rttTexture.GetBuffer().GetRenderTarget(); renderTexture.AddViewport(camera); renderTexture.GetViewport(0).SetClearEveryFrame(false); renderTexture.GetViewport(0).BackgroundColour = new ColourValue(0, 0, 0, 0); renderTexture.Update(); MaterialPtr materialPtr = MaterialManager.Singleton.Create(materialName, ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME); materialPtr.GetTechnique(0).GetPass(0).SetSceneBlending(SceneBlendType.SBT_TRANSPARENT_ALPHA); materialPtr.GetTechnique(0).GetPass(0).CreateTextureUnitState().SetTextureName(textureName); return(materialPtr); }
void CreateCubeMap() { // create the camera used to render to our cubemap _cubeCamera = _sceneManager.CreateCamera("CubeMapCamera", true, true); _cubeCamera.FOVy = new Degree(90); _cubeCamera.AspectRatio = 1.0f; _cubeCamera.SetFixedYawAxis(false); _cubeCamera.NearClipDistance = 5.0f; _cubeCamera.FarClipDistance = /*100*/ 10000; using (TexturePtr tex = TextureManager.Singleton.CreateManual("dyncubemap", ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME, TextureType.TEX_TYPE_CUBE_MAP, 512, 512, 0, PixelFormat.PF_R8G8B8, (int)TextureUsage.TU_RENDERTARGET)) { CompositorManager2 compositorManager = _root.CompositorManager2; const string workspaceName = "CompositorSampleCubemap_cubemap"; if (!compositorManager.HasWorkspaceDefinition(workspaceName)) { CompositorWorkspaceDef workspaceDef = compositorManager.AddWorkspaceDefinition(workspaceName); //"CubemapRendererNode" has been defined in scripts. //Very handy (as it 99% the same for everything) workspaceDef.ConnectOutput("CubemapRendererNode", 0); } CompositorChannel channel = new CompositorChannel { target = tex.GetBuffer(0).GetRenderTarget() }; channel.textures.Add(tex); _cubemapWorkspace = compositorManager.AddWorkspace(_sceneManager, channel, _cubeCamera, workspaceName, false); } }
public VideoTexture(SceneManager scm, float width, float height, string aviFileName) { AviManager aviMgr = new AviManager(aviFileName, true); Stream = aviMgr.GetVideoStream(); TexturePtr VideoTexture = TextureManager.Singleton.CreateManual( "Video", ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME, TextureType.TEX_TYPE_2D, Convert.ToUInt32(Stream.Width), Convert.ToUInt32(Stream.Height), 0, PixelFormat.PF_R8G8B8A8, (int)TextureUsage.TU_DYNAMIC_WRITE_ONLY_DISCARDABLE); MaterialPtr VideoMat = MaterialManager.Singleton.Create( "VideoMat", ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME); VideoMat.GetTechnique(0).GetPass(0).LightingEnabled = false; VideoMat.GetTechnique(0).GetPass(0).CreateTextureUnitState("Video"); PixelBuffer = VideoTexture.GetBuffer(); screen = scm.CreateManualObject("Screen"); screen.Dynamic = true; screen.Begin("VideoMat", RenderOperation.OperationTypes.OT_TRIANGLE_LIST); screen.Position(0, 0, 0); screen.TextureCoord(0, 0); screen.Position(width, 0, 0); screen.TextureCoord(1, 0); screen.Position(width, height, 0); screen.TextureCoord(1, 1); screen.Triangle(0, 1, 2); screen.Position(0, 0, 0); screen.TextureCoord(0, 0); screen.Position(width, height, 0); screen.TextureCoord(1, 1); screen.Position(0, height, 0); screen.TextureCoord(0, 1); screen.Triangle(3, 4, 5); screen.End(); SceneNode node = scm.RootSceneNode.CreateChildSceneNode(); node.Position = new Vector3(0, 0, 0); node.AttachObject(screen); Stream.GetFrameOpen(); FrameNum = 0; }
public unsafe void Replace2(byte[] bytes, string textureName) { if (this.IndexOf(textureName) < 0) { throw new ArgumentException("The texture \"" + textureName + "\"doesn't exist"); } TexturePtr pTexture = this[textureName]; HardwarePixelBufferSharedPtr texBuffer = pTexture.GetBuffer(); texBuffer.Lock(HardwareBuffer.LockOptions.HBL_DISCARD); PixelBox pb = texBuffer.CurrentLock; Marshal.Copy(bytes, 0, pb.data, bytes.Length); texBuffer.Unlock(); texBuffer.Dispose(); }
protected void ReInitRenderTarget() { DetachRenderTarget(true, false); DisposeRenderTarget(); _texture = TextureManager.Singleton.CreateManual( "OgreImageSource RenderTarget", ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME, TextureType.TEX_TYPE_2D, (uint)ViewportSize.Width, (uint)ViewportSize.Height, 0, Mogre.PixelFormat.PF_A8R8G8B8, (int)TextureUsage.TU_RENDERTARGET); _renTarget = _texture.GetBuffer().GetRenderTarget(); _reloadRenderTargetTime = 0; }
/// <summary> /// Converts a TexturePtr to a System.Drawing.Bitmap. /// </summary> /// <param name="texturePtr">The TexturePtr.</param> /// <returns>A System.Drawing.Bitmap.</returns> public static unsafe Bitmap MogreTexturePtrToBitmap(TexturePtr texturePtr) { var bitmap = new Bitmap((int)texturePtr.Width, (int)texturePtr.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); var rgbValues = new byte[texturePtr.Width * 4 * texturePtr.Height]; fixed (byte* ptr = &rgbValues[0]) { var pixelBox = new PixelBox(texturePtr.Width, texturePtr.Height, 1, Mogre.PixelFormat.PF_A8R8G8B8, (IntPtr)ptr); texturePtr.GetBuffer().BlitToMemory(pixelBox); } var bitmapData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, (int)texturePtr.Width, (int)texturePtr.Height), ImageLockMode.WriteOnly, bitmap.PixelFormat); Marshal.Copy(rgbValues, 0, bitmapData.Scan0, rgbValues.Length); bitmap.UnlockBits(bitmapData); return bitmap; }
public unsafe void Replace3(byte[] bytes, string textureName) { using (ResourcePtr rpt = TextureManager.Singleton.GetByName(textureName)) { using (TexturePtr texture = rpt) { HardwarePixelBufferSharedPtr texBuffer = texture.GetBuffer(); texBuffer.Lock(HardwareBuffer.LockOptions.HBL_DISCARD); PixelBox pb = texBuffer.CurrentLock; Marshal.Copy(bytes, 0, pb.data, bytes.Length); texBuffer.Unlock(); texBuffer.Dispose(); } } }
private static unsafe void ConvertImageToTexture(Bitmap image, string textureName, Size size) { int width = size.Width; int height = size.Height; using (ResourcePtr rpt = TextureManager.Singleton.GetByName(textureName)) { using (TexturePtr texture = rpt) { HardwarePixelBufferSharedPtr texBuffer = texture.GetBuffer(); texBuffer.Lock(HardwareBuffer.LockOptions.HBL_DISCARD); PixelBox pb = texBuffer.CurrentLock; System.Drawing.Imaging.BitmapData data = image.LockBits(new System.Drawing.Rectangle(0, 0, width, height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); CopyMemory(pb.data, data.Scan0, (uint)(width * height * 4)); image.UnlockBits(data); texBuffer.Unlock(); texBuffer.Dispose(); } } }
public override void CreateScene() { TexturePtr mTexture = TextureManager.Singleton.CreateManual("RenderArea", ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME, TextureType.TEX_TYPE_2D, 512, 512, 0, PixelFormat.PF_R8G8B8, (int)TextureUsage.TU_RENDERTARGET); rttTex = mTexture.GetBuffer().GetRenderTarget(); rttTex.IsAutoUpdated = false; { // Create the camera Camera camera2 = sceneMgr.CreateCamera("PlayerCam2"); camera2.Position = new Vector3(0, 0, 3); camera2.LookAt(new Vector3(0.0f, 0.0f, 0.0f)); camera2.NearClipDistance = 1; Viewport v = rttTex.AddViewport(camera2); MaterialPtr mat = MaterialManager.Singleton.GetByName("CgTutorials/RenderToTexture_Material"); mat.GetTechnique(0).GetPass(0).GetTextureUnitState(0).SetTextureName("RenderArea"); v.BackgroundColour = new ColourValue(0.0f, 0.3f, 0.2f, 0.0f); //v.SetClearEveryFrame(false); //v.OverlaysEnabled = false; rttTex.PreRenderTargetUpdate += new RenderTargetListener.PreRenderTargetUpdateHandler(RenderArea_PreRenderTargetUpdate); rttTex.PostRenderTargetUpdate += new RenderTargetListener.PostRenderTargetUpdateHandler(RenderArea_PostRenderTargetUpdate); } node1 = base.sceneMgr.RootSceneNode.CreateChildSceneNode("TutorialRender2TexNode1"); node2 = base.sceneMgr.RootSceneNode.CreateChildSceneNode("TutorialRender2TexNode2"); manualObj1 = sceneMgr.CreateManualObject("TutorialRender2TexObject1"); manualObj2 = sceneMgr.CreateManualObject("TutorialRender2TexObject2"); node1.AttachObject(DrawTriangle1(manualObj1)); node2.AttachObject(DrawTriangle2(manualObj2)); }
public void Replace(byte[] bytes, string textureName) { if (this.IndexOf(textureName) < 0) { throw new ArgumentException("The texture \"" + textureName + "\"doesn't exist"); } Mogre.Image image = new Mogre.Image(); MemoryStream ms = new MemoryStream(bytes); DataStreamPtr fs2 = new DataStreamPtr(new ManagedDataStream(ms)); image.Load(fs2); PixelBox imagBox = image.GetPixelBox(); TexturePtr pTexture = this[textureName]; TextureManager lTextureManager = TextureManager.Singleton; HardwarePixelBuffer buffer = pTexture.GetBuffer(); unsafe { buffer.BlitFromMemory(imagBox); } image.Dispose(); fs2.Close(); ms.Close(); }
public override void CreateScene() { //init spline array for (int i = 0; i < NUM_FISH; i++) { fishSplines[i] = new SimpleSpline(); } // Check prerequisites first RenderSystemCapabilities caps = Root.Singleton.RenderSystem.Capabilities; if (!caps.HasCapability(Capabilities.RSC_VERTEX_PROGRAM) || !(caps.HasCapability(Capabilities.RSC_FRAGMENT_PROGRAM))) { throw new System.Exception("Your card does not support vertex and fragment programs, so cannot run this demo. Sorry!"); } else { if (!GpuProgramManager.Singleton.IsSyntaxSupported("arbfp1") && !GpuProgramManager.Singleton.IsSyntaxSupported("ps_2_0") && !GpuProgramManager.Singleton.IsSyntaxSupported("ps_1_4") ) { throw new System.Exception("Your card does not support advanced fragment programs, so cannot run this demo. Sorry!"); } } camera.SetPosition(-50, 125, 760); camera.SetDirection(0, 0, -1); // Set ambient light sceneMgr.AmbientLight = new ColourValue(0.5f, 0.5f, 0.5f); // Create a point light Light l = sceneMgr.CreateLight("MainLight"); l.Type = Light.LightTypes.LT_DIRECTIONAL; l.Direction = -Vector3.UNIT_Y; Entity pEnt; TexturePtr mTexture = TextureManager.Singleton.CreateManual("Refraction", ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME, TextureType.TEX_TYPE_2D, 512, 512, 0, PixelFormat.PF_R8G8B8, (int)TextureUsage.TU_RENDERTARGET); //RenderTexture* rttTex = mRoot.getRenderSystem().createRenderTexture( "Refraction", 512, 512 ); RenderTarget rttTex = mTexture.GetBuffer().GetRenderTarget(); { Viewport v = rttTex.AddViewport(camera); MaterialPtr mat = MaterialManager.Singleton.GetByName("Examples/FresnelReflectionRefraction"); mat.GetTechnique(0).GetPass(0).GetTextureUnitState(2).SetTextureName("Refraction"); v.OverlaysEnabled = false; rttTex.PreRenderTargetUpdate += new RenderTargetListener.PreRenderTargetUpdateHandler(Refraction_PreRenderTargetUpdate); rttTex.PostRenderTargetUpdate += new RenderTargetListener.PostRenderTargetUpdateHandler(Refraction_PostRenderTargetUpdate); } mTexture = TextureManager.Singleton.CreateManual("Reflection", ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME, TextureType.TEX_TYPE_2D, 512, 512, 0, PixelFormat.PF_R8G8B8, (int)TextureUsage.TU_RENDERTARGET); //rttTex = mRoot.getRenderSystem().createRenderTexture( "Reflection", 512, 512 ); rttTex = mTexture.GetBuffer().GetRenderTarget(); { Viewport v = rttTex.AddViewport(camera); MaterialPtr mat = MaterialManager.Singleton.GetByName("Examples/FresnelReflectionRefraction"); mat.GetTechnique(0).GetPass(0).GetTextureUnitState(1).SetTextureName("Reflection"); v.OverlaysEnabled = false; rttTex.PreRenderTargetUpdate += new RenderTargetListener.PreRenderTargetUpdateHandler(Reflection_PreRenderTargetUpdate); rttTex.PostRenderTargetUpdate += new RenderTargetListener.PostRenderTargetUpdateHandler(Reflection_PostRenderTargetUpdate); } // Define a floor plane mesh reflectionPlane.normal = Vector3.UNIT_Y; reflectionPlane.d = 0; MeshManager.Singleton.CreatePlane("ReflectPlane", ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME, reflectionPlane, 1500, 1500, 10, 10, true, 1, 5, 5, Vector3.UNIT_Z); pPlaneEnt = sceneMgr.CreateEntity("plane", "ReflectPlane"); pPlaneEnt.SetMaterialName("Examples/FresnelReflectionRefraction"); sceneMgr.RootSceneNode.CreateChildSceneNode().AttachObject(pPlaneEnt); sceneMgr.SetSkyBox(true, "Examples/CloudyNoonSkyBox"); // My node to which all objects will be attached SceneNode myRootNode = sceneMgr.RootSceneNode.CreateChildSceneNode(); // above water entities pEnt = sceneMgr.CreateEntity("RomanBathUpper", "RomanBathUpper.mesh"); myRootNode.AttachObject(pEnt); aboveWaterEnts.Add(pEnt); pEnt = sceneMgr.CreateEntity("Columns", "Columns.mesh"); myRootNode.AttachObject(pEnt); aboveWaterEnts.Add(pEnt); SceneNode headNode = myRootNode.CreateChildSceneNode(); pEnt = sceneMgr.CreateEntity("OgreHead", "ogrehead.mesh"); pEnt.SetMaterialName("RomanBath/OgreStone"); headNode.AttachObject(pEnt); headNode.SetPosition(-350, 55, 130); headNode.Rotate(Vector3.UNIT_Y, new Degree(90)); aboveWaterEnts.Add(pEnt); // below water entities pEnt = sceneMgr.CreateEntity("RomanBathLower", "RomanBathLower.mesh"); myRootNode.AttachObject(pEnt); belowWaterEnts.Add(pEnt); for (int fishNo = 0; fishNo < NUM_FISH; ++fishNo) { pEnt = sceneMgr.CreateEntity("fish" + fishNo, "fish.mesh"); fishNodes[fishNo] = myRootNode.CreateChildSceneNode(); fishAnimations[fishNo] = pEnt.GetAnimationState("swim"); fishAnimations[fishNo].Enabled = true; fishNodes[fishNo].AttachObject(pEnt); belowWaterEnts.Add(pEnt); // Generate a random selection of points for the fish to swim to fishSplines[fishNo].SetAutoCalculate(false); Vector3 lastPos = new Vector3(); for (int waypoint = 0; waypoint < NUM_FISH_WAYPOINTS; ++waypoint) { Vector3 pos = new Vector3( Mogre.Math.SymmetricRandom() * 270, -10, Mogre.Math.SymmetricRandom() * 700); if (waypoint > 0) { // check this waypoint isn't too far, we don't want turbo-fish ;) // since the waypoints are achieved every 5 seconds, half the length // of the pond is ok while ((lastPos - pos).Length > 750) { pos = new Vector3( Mogre.Math.SymmetricRandom() * 270, -10, Mogre.Math.SymmetricRandom() * 700); } } fishSplines[fishNo].AddPoint(pos); lastPos = pos; } // close the spline fishSplines[fishNo].AddPoint(fishSplines[fishNo].GetPoint(0)); // recalc fishSplines[fishNo].RecalcTangents(); } }
// Just override the mandatory create scene method public override void CreateScene() { // Set ambient light sceneMgr.AmbientLight = new ColourValue(0.2f, 0.2f, 0.2f); // Skybox sceneMgr.SetSkyBox(true, "Examples/MorningSkyBox"); // Create a light Light l = sceneMgr.CreateLight("MainLight"); l.Type = Light.LightTypes.LT_DIRECTIONAL; Vector3 dir = new Vector3(0.5f, -1, 0); dir.Normalise(); l.Direction = dir; l.DiffuseColour = new ColourValue(1.0f, 1.0f, 0.8f); l.SpecularColour = new ColourValue(1.0f, 1.0f, 1.0f); // Create a prefab plane mPlane = new MovablePlane(Vector3.UNIT_Y, 0); MeshManager.Singleton.CreatePlane("ReflectionPlane", ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME, mPlane._getDerivedPlane(), 2000, 2000, 1, 1, true, 1, 1, 1, Vector3.UNIT_Z); mPlaneEnt = sceneMgr.CreateEntity("Plane", "ReflectionPlane"); // Create an entity from a model (will be loaded automatically) Entity knotEnt = sceneMgr.CreateEntity("Knot", "knot.mesh"); // Create an entity from a model (will be loaded automatically) Entity ogreHead = sceneMgr.CreateEntity("Head", "ogrehead.mesh"); knotEnt.SetMaterialName("Examples/TextureEffect2"); // Attach the rtt entity to the root of the scene SceneNode rootNode = sceneMgr.RootSceneNode; mPlaneNode = rootNode.CreateChildSceneNode(); // Attach both the plane entity, and the plane definition mPlaneNode.AttachObject(mPlaneEnt); mPlaneNode.AttachObject(mPlane); mPlaneNode.Translate(0, -10, 0); // Tilt it a little to make it interesting mPlaneNode.Roll(new Degree(5)); rootNode.CreateChildSceneNode("Head").AttachObject(ogreHead); TexturePtr texture = TextureManager.Singleton.CreateManual("RttTex", ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME, TextureType.TEX_TYPE_2D, 512, 512, 0, PixelFormat.PF_R8G8B8, (int)TextureUsage.TU_RENDERTARGET); RenderTarget rttTex = texture.GetBuffer().GetRenderTarget(); mReflectCam = sceneMgr.CreateCamera("ReflectCam"); mReflectCam.NearClipDistance = camera.NearClipDistance; mReflectCam.FarClipDistance = camera.FarClipDistance; mReflectCam.AspectRatio = (float)window.GetViewport(0).ActualWidth / (float)window.GetViewport(0).ActualHeight; mReflectCam.FOVy = camera.FOVy; Viewport v = rttTex.AddViewport(mReflectCam); v.SetClearEveryFrame(true); v.BackgroundColour = ColourValue.Black; MaterialPtr mat = MaterialManager.Singleton.Create("RttMat", ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME); TextureUnitState t = mat.GetTechnique(0).GetPass(0).CreateTextureUnitState("RustedMetal.jpg"); t = mat.GetTechnique(0).GetPass(0).CreateTextureUnitState("RttTex"); // Blend with base texture t.SetColourOperationEx(LayerBlendOperationEx.LBX_BLEND_MANUAL, LayerBlendSource.LBS_TEXTURE, LayerBlendSource.LBS_CURRENT, ColourValue.White, ColourValue.White, 0.25f); t.SetTextureAddressingMode(TextureUnitState.TextureAddressingMode.TAM_CLAMP); t.SetProjectiveTexturing(true, mReflectCam); rttTex.PostRenderTargetUpdate += postRenderTargetUpdate; rttTex.PreRenderTargetUpdate += preRenderTargetUpdate; // 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 = sceneMgr.CreateSceneNode(); // Random translate Vector3 nodePos; nodePos.x = Mogre.Math.SymmetricRandom() * 750.0f; nodePos.y = Mogre.Math.SymmetricRandom() * 100.0f + 25; nodePos.z = Mogre.Math.SymmetricRandom() * 750.0f; node.Position = nodePos; rootNode.AddChild(node); // Clone knot string cloneName = "Knot" + n; cloneEnt = knotEnt.Clone(cloneName); // Attach to new node node.AttachObject(cloneEnt); } camera.Position = new Vector3(-50, 100, 500); camera.LookAt(0, 0, 0); }