public static Texture2d Create(Color4 color, string description, int width = 1, int height = 1, TextureOptions textureOptions = null) { textureOptions = textureOptions ?? TextureOptions.Default; var textureId = GL.GenTexture(); try { var data = new int[width * height]; for (int i = 0; i < width * height; i++) { data[i] = color.ToRgba(); } DrawState.BindTexture(textureId); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, width, height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Rgba, PixelType.UnsignedByte, data); if (textureOptions.GenerateMipmaps) { GL.GenerateMipmap(GenerateMipmapTarget.Texture2D); GL.Finish(); } DrawState.CheckError("specifying texture"); textureOptions.ApplyParameters(TextureTarget.Texture2D); } catch (Exception) { GL.DeleteTexture(textureId); DrawState.UnbindTexture(textureId); throw; } return(new Texture2d(textureId, width, height, description)); }
public static Texture2d Load(Bitmap bitmap, string description, TextureOptions textureOptions = null) { if (bitmap == null) { throw new ArgumentNullException(nameof(bitmap)); } textureOptions = textureOptions ?? TextureOptions.Default; var sRgb = textureOptions.Srgb && DrawState.ColorCorrected; var textureId = GL.GenTexture(); try { DrawState.BindTexture(textureId); var bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); GL.TexImage2D(TextureTarget.Texture2D, 0, sRgb ? PixelInternalFormat.SrgbAlpha : PixelInternalFormat.Rgba, bitmapData.Width, bitmapData.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, bitmapData.Scan0); if (textureOptions.GenerateMipmaps) { GL.GenerateMipmap(GenerateMipmapTarget.Texture2D); } GL.Finish(); bitmap.UnlockBits(bitmapData); DrawState.CheckError("specifying texture"); textureOptions.ApplyParameters(TextureTarget.Texture2D); } catch (Exception) { GL.DeleteTexture(textureId); DrawState.UnbindTexture(textureId); throw; } return(new Texture2d(textureId, bitmap.Width, bitmap.Height, description)); }