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)); }
public void Flush(bool canBuffer = false) { if (spritesInBatch == 0) { return; } if (currentTexture == null) { throw new InvalidOperationException("currentTexture is null"); } // When the previous flush was bufferable, draw state should stay the same. if (!lastFlushWasBuffered) { var combinedMatrix = transformMatrix * Camera.ProjectionView; var samplerUnit = CustomTextureBind != null?CustomTextureBind(currentTexture) : DrawState.BindTexture(currentTexture); if (currentSamplerUnit != samplerUnit) { currentSamplerUnit = samplerUnit; GL.Uniform1(shader.GetUniformLocation(TextureUniformName), currentSamplerUnit); } GL.UniformMatrix4(shader.GetUniformLocation(CombinedMatrixUniformName), false, ref combinedMatrix); flushAction?.Invoke(); } primitiveStreamer.Render(PrimitiveType.Quads, spriteArray, spritesInBatch, spritesInBatch * VertexPerSprite, canBuffer); currentLargestBatch += spritesInBatch; if (!canBuffer) { LargestBatch = Math.Max(LargestBatch, currentLargestBatch); currentLargestBatch = 0; } spritesInBatch = 0; FlushedBufferCount++; lastFlushWasBuffered = canBuffer; }