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)); }
private void initialize(int width, int height) { Width = width; Height = height; PixelInternalFormat pixelInternalFormat = (PixelInternalFormat)storage; PixelFormat pixelFormat; PixelType pixelType; switch (storage) { case RenderbufferStorage.DepthComponent: case RenderbufferStorage.DepthComponent16: case RenderbufferStorage.DepthComponent24: case RenderbufferStorage.DepthComponent32: case RenderbufferStorage.DepthComponent32f: pixelFormat = PixelFormat.DepthComponent; pixelType = PixelType.Float; break; case RenderbufferStorage.DepthStencil: case RenderbufferStorage.Depth32fStencil8: case RenderbufferStorage.Depth24Stencil8: pixelFormat = PixelFormat.DepthStencil; pixelType = PixelType.UnsignedInt248; break; case RenderbufferStorage.StencilIndex1: case RenderbufferStorage.StencilIndex4: case RenderbufferStorage.StencilIndex8: case RenderbufferStorage.StencilIndex16: pixelFormat = PixelFormat.StencilIndex; pixelType = PixelType.Float; break; default: pixelFormat = PixelFormat.Rgba; pixelType = PixelType.UnsignedByte; break; } var textureId = GL.GenTexture(); Texture = new Texture2d(textureId, Width, Height, "rendertexture"); DrawState.BindPrimaryTexture(textureId); GL.TexImage2D(TextureTarget.Texture2D, 0, pixelInternalFormat, Width, Height, 0, pixelFormat, pixelType, IntPtr.Zero); DrawState.CheckError("creating a render texture's texture"); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear); DrawState.UnbindTexture(textureId); Debug.Print(width + "x" + height + " " + storage + " render texture created"); }
protected override void Dispose(bool disposing) { if (!disposedValue) { if (disposing) { DrawState.UnbindTexture(this); } GL.DeleteTexture(textureId); disposedValue = true; } base.Dispose(disposing); }
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)); }
private void initialize() { textureId = GL.GenTexture(); texture = new Texture2d(textureId, width, height, "rendertarget"); DrawState.BindPrimaryTexture(textureId); GL.TexImage2D(TextureTarget.Texture2D, 0, internalFormat, width, height, 0, pixelFormat, pixelType, IntPtr.Zero); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)textureMagFilter); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)textureMinFilter); GL.GetInteger(GetPName.FramebufferBinding, out previousFrameBufferId); frameBufferId = GL.GenFramebuffer(); GL.BindFramebuffer(FramebufferTarget.Framebuffer, frameBufferId); GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2D, textureId, 0); DrawState.CheckError("creating fbo"); if (renderBufferType != null) { renderBufferId = GL.GenRenderbuffer(); GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, renderBufferId); GL.RenderbufferStorage(RenderbufferTarget.Renderbuffer, renderBufferType.Value, width, height); switch (renderBufferType.Value) { case RenderbufferStorage.DepthComponent: case RenderbufferStorage.DepthComponent16: case RenderbufferStorage.DepthComponent24: case RenderbufferStorage.DepthComponent32: case RenderbufferStorage.DepthComponent32f: GL.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthAttachment, RenderbufferTarget.Renderbuffer, renderBufferId); break; case RenderbufferStorage.DepthStencil: case RenderbufferStorage.Depth24Stencil8: case RenderbufferStorage.Depth32fStencil8: GL.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthStencilAttachment, RenderbufferTarget.Renderbuffer, renderBufferId); break; case RenderbufferStorage.StencilIndex1: case RenderbufferStorage.StencilIndex4: case RenderbufferStorage.StencilIndex8: case RenderbufferStorage.StencilIndex16: GL.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferAttachment.StencilAttachment, RenderbufferTarget.Renderbuffer, renderBufferId); break; default: throw new NotSupportedException("renderBufferType " + renderBufferType.Value + " isn't supported."); } } var status = GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer); if (status != FramebufferErrorCode.FramebufferComplete) { throw new Exception("frame buffer couldn't be constructed: " + status); } DrawState.UnbindTexture(textureId); GL.BindFramebuffer(FramebufferTarget.Framebuffer, previousFrameBufferId); Debug.Print(width + "x" + height + " render target created"); }