/// <summary> /// Load a texture from a bitmap. /// </summary> /// <param name="bitmap">The bitmap.</param> /// <returns></returns> public static ITexture2D FromBitmap(Bitmap bitmap) { var texture = new Texture2dGL(); texture.FromBitmap(bitmap); return(texture); }
private static ITexture2D BitmapImporter(IEnumerable <NamedStream> resources) { var texture = new Texture2dGL(); Update(texture, resources); return(texture); }
private static void Update(Texture2dGL texture, IEnumerable <NamedStream> resources) { foreach (var res in resources) { using (var bitmap = new Bitmap(res.Stream)) { texture.FromBitmap(bitmap); return; } //TODO: if multiple textures assume these contain mipmap levels and load them } }
/// <summary> /// Initializes a new instance of the <see cref="RenderSurfaceGL"/> class. /// </summary> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <param name="hasDepthBuffer">if set to <c>true</c> [has depth buffer].</param> /// <param name="components">The components.</param> /// <param name="floatingPoint">if set to <c>true</c> [floating point].</param> public RenderSurfaceGL(int width, int height, bool hasDepthBuffer = false, byte components = 4, bool floatingPoint = false) : this(hasDepthBuffer) { var tex = Texture2dGL.Create(width, height, components, floatingPoint); if (hasDepthBuffer) { fbo = new FBOwithDepth(tex); } else { fbo = new FBO(tex); } }
/// <summary> /// Creates the specified width. /// </summary> /// <param name="width">The texture width in pixel.</param> /// <param name="height">The texture height in pixel.</param> /// <param name="components">The components.</param> /// <param name="floatingPoint">if set to <c>true</c> [floating point].</param> /// <returns></returns> public static Texture2dGL Create(int width, int height, byte components = 4, bool floatingPoint = false) { var internalFormat = Convert(components, floatingPoint); var inputPixelFormat = Convert(components); var type = floatingPoint ? PixelType.UnsignedByte : PixelType.Float; var texture = new Texture2dGL(); //create empty texture of given size texture.LoadPixels(IntPtr.Zero, width, height, internalFormat, inputPixelFormat, type); //set default parameters for filtering and clamping texture.Filter = TextureFilterMode.Linear; texture.WrapFunction = TextureWrapFunction.Repeat; return(texture); }
/// <summary> /// Loads texture date from a bitmap into a Texture2dGL instance. /// </summary> /// <param name="texture">The texture instance.</param> /// <param name="bitmap">The bitmap.</param> public static void FromBitmap(this Texture2dGL texture, Bitmap bitmap) { //TODO: 16bit channels using (Bitmap bmp = new Bitmap(bitmap)) { bmp.RotateFlip(RotateFlipType.RotateNoneFlipY); var bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), SysDraw.ImageLockMode.ReadOnly, bmp.PixelFormat); var internalFormat = SelectInternalPixelFormat(bmp.PixelFormat); var inputPixelFormat = SelectPixelFormat(bmp.PixelFormat); texture.LoadPixels(bmpData.Scan0, bmpData.Width, bmpData.Height, internalFormat, inputPixelFormat, PixelType.UnsignedByte); bmp.UnlockBits(bmpData); } texture.Filter = TextureFilterMode.Mipmap; }
/// <summary> /// Froms the array. /// </summary> /// <typeparam name="TYPE">The type of the ype.</typeparam> /// <param name="data">The data.</param> /// <param name="internalFormat">The internal format.</param> /// <param name="format">The format.</param> /// <param name="type">The type.</param> /// <returns></returns> public static ITexture FromArray <TYPE>(TYPE[,] data, PixelInternalFormat internalFormat, PixelFormat format, PixelType type) { GCHandle pinnedArray = GCHandle.Alloc(data, GCHandleType.Pinned); try { IntPtr pointer = pinnedArray.AddrOfPinnedObject(); var width = data.GetLength(0); var height = data.GetLength(1); var texture = new Texture2dGL(); texture.LoadPixels(pointer, width, height, internalFormat, format, type); texture.Filter = TextureFilterMode.Mipmap; return(texture); } finally { pinnedArray.Free(); } }
/// <summary> /// Froms the stream. /// </summary> /// <param name="stream">The stream.</param> /// <returns></returns> public static ITexture FromStream(Stream stream) //TODO: not working { var source = new BitmapImage(); source.BeginInit(); source.StreamSource = stream; source.EndInit(); //TODO: flip bitmap image var writable = new WriteableBitmap(source); writable.Lock(); var internalFormat = SelectInternalPixelFormat(source.Format); var inputPixelFormat = SelectPixelFormat(source.Format); var texture = new Texture2dGL(); texture.LoadPixels(writable.BackBuffer, source.PixelWidth, source.PixelHeight, internalFormat, inputPixelFormat, PixelType.UnsignedByte); writable.Unlock(); texture.Filter = TextureFilterMode.Mipmap; return(texture); }