/// <summary> /// Bitmaps to texture. /// </summary> /// <param name="bmp">The BMP.</param> /// <param name="texture">The texture.</param> public unsafe static void BitmapToTexture(Bitmap bitmap, Texture texture) { var bmp = bitmap.Clone(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.PixelFormat.Format32bppArgb); var bitmapData = bmp.LockBits( new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, bmp.PixelFormat); HardwarePixelBuffer buffer = texture.GetBuffer(); buffer.Lock(Axiom.Graphics.BufferLocking.Discard); PixelBox pixelBox = buffer.CurrentLock; UInt32* texData = (UInt32*)pixelBox.Data; UInt32* mapData = (UInt32*)bitmapData.Scan0; var height = Math.Min(pixelBox.Height, bmp.Height); var width = Math.Min(pixelBox.Width, bmp.Width); for (var y = 0; y < height; ++y) for (var x = 0; x < width; ++x) texData[pixelBox.RowPitch * y + x] = mapData[bitmapData.Stride / 4 * y + x]; buffer.Unlock(); bmp.UnlockBits(bitmapData); }
public void CameraDataUpdate(InteropBitmap cameraInterop) { // Stores the cam input as bitmap source var imageSource = cameraInterop as BitmapSource; // Creates an jpeg encoder BitmapEncoder enc = new JpegBitmapEncoder(); // Create bitmap frame from the image source enc.Frames.Add(BitmapFrame.Create(imageSource)); // Saves the frame to a memory stream var ms = new MemoryStream(); enc.Save(ms); // Create a bitmap from the memory stream var bitmap = new Bitmap(ms); // Get width and height int width = bitmap.Width; int height = bitmap.Height; // Gets the buffer from the texture var texBuffer = m_texture.GetBuffer(); // Locks the buffer so we can copy data into the buffer texBuffer.Lock(BufferLocking.Discard); PixelBox pb = texBuffer.CurrentLock; BitmapData data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); // Copy the data into the buffer CopyMemory(pb.Data, data.Scan0, (uint)((width) * height * 4)); // Unlock the buffer bitmap.UnlockBits(data); texBuffer.Unlock(); // Render using the material m_axiomMaterial.GetTechnique(0).GetPass(0).RemoveAllTextureUnitStates(); m_axiomMaterial.GetTechnique(0).GetPass(0).CreateTextureUnitState("DynamicTexture"); // Update the camera orientation UpdateCameraOrientation(); try { // Render a single frame m_root.RenderOneFrame(); } catch (Exception e) { return; } }
private void SaveToDisk( Texture tp, string filename ) { // Declare buffer int buffSize = tp.Width * tp.Height * tp.Depth * 4; byte[] data = new byte[buffSize]; // Setup Image with correct settings Image i = new Image(); i.FromDynamicImage(data, tp.Width, tp.Height, tp.Depth,tp.Format); // Copy Texture buffer contents to image buffer HardwarePixelBuffer buf = tp.GetBuffer(); PixelBox destBox = i.GetPixelBox(0,0); buf.BlitToMemory(destBox); // Save to disk! i.Save( @"C:\" + filename ); }
/// <summary> /// Get an appropriately defined 'null' texture, ie one which will always /// result in no shadows. /// </summary> public Texture GetNullShadowTexture(PixelFormat format) { foreach (Texture tex in nullTextureList) { if (format == tex.Format) { // Ok, a match return(tex); } } // not found, create a new one // A 1x1 texture of the correct format, not a render target string baseName = "Axiom/ShadowTextureNull"; string targName = baseName + count++; Texture shadowTex = TextureManager.Instance.CreateManual( targName, TextureType.TwoD, 1, 1, 1, 0, format, TextureUsage.Default); nullTextureList.Add(shadowTex); // Populate the texture based on format int byteCount = PixelUtil.GetNumElemBytes(format); unsafe { byte [] bytes = new byte[byteCount]; for (int i = 0; i < byteCount; i++) bytes[i] = 0xff; fixed(byte *fixedBytes = &bytes[0]) { PixelBox bytesPixelBox = new PixelBox(1, 1, 1, format, new IntPtr(fixedBytes)); shadowTex.GetBuffer().BlitFromMemory(bytesPixelBox); } } return(shadowTex); }
public override void CreateScene() { // Create dynamic texture ptex = TextureManager.Instance.CreateManual( "DynaTex", ResourceGroupManager.DefaultResourceGroupName, TextureType.TwoD, reactorExtent - 2, reactorExtent - 2, 0, PixelFormat.A8R8G8B8, TextureUsage.DynamicWriteOnly ); buffer = ptex.GetBuffer( 0, 0 ); // Set ambient light scene.AmbientLight = new ColorEx( 0.6F, 0.6F, 0.6F ); scene.SetSkyBox( true, "SkyBox/Space", 50 ); //mRoot->getRenderSystem()->clearFrameBuffer(FBT_COLOUR, ColourValue(255,255,255,0)); // Create a light Light l = scene.CreateLight( "MainLight" ); l.Diffuse = new ColorEx( 0.75F, 0.75F, 0.80F ); l.Specular = new ColorEx( 0.9F, 0.9F, 1F ); l.Position = new Vector3( -100, 80, 50 ); scene.RootSceneNode.AttachObject( l ); Entity planeEnt = scene.CreateEntity( "TexPlane1", PrefabEntity.Plane ); // Give the plane a texture planeEnt.MaterialName = "Examples/DynaTest"; SceneNode node = scene.RootSceneNode.CreateChildSceneNode( new Vector3( -100, -40, -100 ) ); node.AttachObject( planeEnt ); node.Scale = new Vector3( 3.0f, 3.0f, 3.0f ); // Create objects SceneNode blaNode = scene.RootSceneNode.CreateChildSceneNode( new Vector3( -200, 0, 50 ) ); Entity ent2 = scene.CreateEntity( "knot", "knot.mesh" ); ent2.MaterialName = "Examples/DynaTest4"; blaNode.AttachObject( ent2 ); blaNode = scene.RootSceneNode.CreateChildSceneNode( new Vector3( 200, -90, 50 ) ); ent2 = scene.CreateEntity( "knot2", "knot.mesh" ); ent2.MaterialName = "Examples/DynaTest2"; blaNode.AttachObject( ent2 ); blaNode = scene.RootSceneNode.CreateChildSceneNode( new Vector3( -110, 200, 50 ) ); // Cloaked fish ent2 = scene.CreateEntity( "knot3", "fish.mesh" ); ent2.MaterialName = "Examples/DynaTest3"; swim = ent2.GetAnimationState( "swim" ); swim.IsEnabled = true; blaNode.AttachObject( ent2 ); blaNode.Scale = new Vector3( 50.0f, 50.0f, 50.0f ); LogManager.Instance.Write( "HardwarePixelBuffer {0} {1} {2} ", buffer.Width, buffer.Height, buffer.Depth ); buffer.Lock( BufferLocking.Normal ); PixelBox pb = buffer.CurrentLock; LogManager.Instance.Write( "PixelBox {0} {1} {2} {3} {4} {5} {6}", pb.Width, pb.Height, pb.Depth, pb.RowPitch, pb.SlicePitch, pb.Data, pb.Format ); buffer.Unlock(); // show GUI overlay = OverlayManager.Instance.GetByName( "Example/DynTexOverlay" ); overlay.Show(); }
/// <summary> /// Helper method to render a composite map. /// </summary> /// <param name="size"> The requested composite map size</param> /// <param name="rect"> The region of the composite map to update, in image space</param> /// <param name="mat">The material to use to render the map</param> /// <param name="destCompositeMap"></param> public virtual void RenderCompositeMap( int size, Rectangle rect, Material mat, Texture destCompositeMap ) { //return; if ( this.mCompositeMapSM == null ) { //dedicated SceneManager this.mCompositeMapSM = Root.Instance.CreateSceneManager( SceneType.ExteriorClose, "TerrainMaterialGenerator_SceneManager" ); float camDist = 100; float halfCamDist = camDist*0.5f; this.mCompositeMapCam = this.mCompositeMapSM.CreateCamera( "TerrainMaterialGenerator_Camera" ); this.mCompositeMapCam.Position = new Vector3( 0, 0, camDist ); //mCompositeMapCam.LookAt(Vector3.Zero); this.mCompositeMapCam.ProjectionType = Projection.Orthographic; this.mCompositeMapCam.Near = 10; this.mCompositeMapCam.Far = 999999*3; //mCompositeMapCam.AspectRatio = camDist / camDist; this.mCompositeMapCam.SetOrthoWindow( camDist, camDist ); // Just in case material relies on light auto params this.mCompositeMapLight = this.mCompositeMapSM.CreateLight( "TerrainMaterialGenerator_Light" ); this.mCompositeMapLight.Type = LightType.Directional; RenderSystem rSys = Root.Instance.RenderSystem; float hOffset = rSys.HorizontalTexelOffset/(float)size; float vOffset = rSys.VerticalTexelOffset/(float)size; //setup scene this.mCompositeMapPlane = this.mCompositeMapSM.CreateManualObject( "TerrainMaterialGenerator_ManualObject" ); this.mCompositeMapPlane.Begin( mat.Name, OperationType.TriangleList ); this.mCompositeMapPlane.Position( -halfCamDist, halfCamDist, 0 ); this.mCompositeMapPlane.TextureCoord( 0 - hOffset, 0 - vOffset ); this.mCompositeMapPlane.Position( -halfCamDist, -halfCamDist, 0 ); this.mCompositeMapPlane.TextureCoord( 0 - hOffset, 1 - vOffset ); this.mCompositeMapPlane.Position( halfCamDist, -halfCamDist, 0 ); this.mCompositeMapPlane.TextureCoord( 1 - hOffset, 1 - vOffset ); this.mCompositeMapPlane.Position( halfCamDist, halfCamDist, 0 ); this.mCompositeMapPlane.TextureCoord( 1 - hOffset, 0 - vOffset ); this.mCompositeMapPlane.Quad( 0, 1, 2, 3 ); this.mCompositeMapPlane.End(); this.mCompositeMapSM.RootSceneNode.AttachObject( this.mCompositeMapPlane ); } //end if // update this.mCompositeMapPlane.SetMaterialName( 0, mat.Name ); this.mCompositeMapLight.Direction = TerrainGlobalOptions.LightMapDirection; this.mCompositeMapLight.Diffuse = TerrainGlobalOptions.CompositeMapDiffuse; this.mCompositeMapSM.AmbientLight = TerrainGlobalOptions.CompositeMapAmbient; //check for size change (allow smaller to be reused) if ( this.mCompositeMapRTT != null && size != this.mCompositeMapRTT.Width ) { TextureManager.Instance.Remove( this.mCompositeMapRTT ); this.mCompositeMapRTT = null; } if ( this.mCompositeMapRTT == null ) { this.mCompositeMapRTT = TextureManager.Instance.CreateManual( this.mCompositeMapSM.Name + "/compRTT", ResourceGroupManager.DefaultResourceGroupName, TextureType.TwoD, size, size, 0, PixelFormat.BYTE_RGBA, TextureUsage.RenderTarget ); RenderTarget rtt = this.mCompositeMapRTT.GetBuffer().GetRenderTarget(); // don't render all the time, only on demand rtt.IsAutoUpdated = false; Viewport vp = rtt.AddViewport( this.mCompositeMapCam ); // don't render overlays vp.ShowOverlays = false; } // calculate the area we need to update float vpleft = (float)rect.Left/(float)size; float vptop = (float)rect.Top/(float)size; float vpright = (float)rect.Right/(float)size; float vpbottom = (float)rect.Bottom/(float)size; float vpwidth = (float)rect.Width/(float)size; float vpheight = (float)rect.Height/(float)size; RenderTarget rtt2 = this.mCompositeMapRTT.GetBuffer().GetRenderTarget(); Viewport vp2 = rtt2.GetViewport( 0 ); this.mCompositeMapCam.SetWindow( vpleft, vptop, vpright, vpbottom ); rtt2.Update(); vp2.Update(); // We have an RTT, we want to copy the results into a regular texture // That's because in non-update scenarios we don't want to keep an RTT // around. We use a single RTT to serve all terrain pages which is more // efficient. var box = new BasicBox( (int)rect.Left, (int)rect.Top, (int)rect.Right, (int)rect.Bottom ); destCompositeMap.GetBuffer().Blit( this.mCompositeMapRTT.GetBuffer(), box, box ); }