protected virtual void SetupTexture2D(GraphicsInterface gi, int width, int height, TextureInternalFormat internalFormat, TexturePixelFormat pixelFormat, PixelComponentType pixelType, IntPtr pixelData, bool createMipMaps) { fWidth = width; fHeight = height; fInternalFormat = internalFormat; fPixelFormat = pixelFormat; fPixelType = pixelType; // Setup the alignment fGI.PixelStore(PixelStore.UnpackAlignment, 1); // Allocate storage for the texture // We do this once at the beginning to allocate space for the texture object fGI.TexImage2D(0, internalFormat, fWidth, fHeight, 0, pixelFormat, pixelType, pixelData); // Setup default filters and wrapping SetupFiltering(); SetupWrapping(); if (createMipMaps) { // Make call to generate MipMap //fGI.GenerateMipmap(); // Alter the min filter to use the mipmap fGI.TexParameter(TextureParameterTarget.Texture2d, TextureParameterName.TextureMinFilter, TextureMinFilter.LinearMipmapLinear); } }
/// <inheritdoc /> public override void UploadToTexture(IntPtr data, Vector2 size, TextureInternalFormat internalFormat, TexturePixelFormat pixelFormat) { InternalFormat intFormat = (InternalFormat)Enum.Parse(typeof(InternalFormat), internalFormat.ToString()); PixelFormat glFormat = (PixelFormat)Enum.Parse(typeof(PixelFormat), pixelFormat.ToString()); GLThread.ExecuteGLThread(() => { // Set scaling to pixel perfect. Gl.TexParameter(TextureTarget.Texture2d, TextureParameterName.TextureMinFilter, Gl.NEAREST); Gl.TexParameter(TextureTarget.Texture2d, TextureParameterName.TextureMagFilter, Gl.NEAREST); Gl.TexImage2D(TextureTarget.Texture2d, 0, intFormat, (int)size.X, (int)size.Y, 0, glFormat, PixelType.UnsignedByte, data); Gl.GenerateMipmap(TextureTarget.Texture2d); CheckError("after uploading texture"); }); }
public Texture(string name, byte[] data, TextureDataType type, TextureFormat format, Size pSize, TextureInternalFormat internalFormat) : base("tex_" + name, GlHandleType.Texture) { gl.glBindTexture((uint)GL.TEXTURE_2D, m_iObject[0]); gl.glTexImage2D((uint)GL.TEXTURE_2D, 0, (int)internalFormat, pSize.Width, pSize.Height, 0, (uint)format, (uint)type, data); gl.glTexParameteri((uint)GL.TEXTURE_2D, (uint)GL.TEXTURE_WRAP_S, (int)GL.CLAMP_TO_EDGE); gl.glTexParameteri((uint)GL.TEXTURE_2D, (uint)GL.TEXTURE_WRAP_T, (int)GL.CLAMP_TO_EDGE); gl.glTexParameteri((uint)GL.TEXTURE_2D, (uint)GL.TEXTURE_MIN_FILTER, (int)GL.LINEAR_MIPMAP_LINEAR); gl.glTexParameteri((uint)GL.TEXTURE_2D, (uint)GL.TEXTURE_MAG_FILTER, (int)GL.LINEAR); gl.glGenerateMipmap((uint)GL.TEXTURE_2D); m_sDimension = pSize; Register(true); }
/// <inheritdoc /> public override RenderTarget CreateMSAARenderTarget(int samples, Vector2 size, TextureInternalFormat internalFormat = TextureInternalFormat.Rgba, bool attachStencil = false) { RenderTarget resultTarget = null; InternalFormat intFormat = (InternalFormat)Enum.Parse(typeof(InternalFormat), internalFormat.ToString()); GLThread.ExecuteGLThread(() => { // Create the FBO. uint newFbo = Gl.GenFramebuffer(); Gl.BindFramebuffer(FramebufferTarget.Framebuffer, newFbo); // Create the texture. uint renderTexture = Gl.GenTexture(); Gl.BindTexture(TextureTarget.Texture2dMultisample, renderTexture); Gl.TexImage2DMultisample(TextureTarget.Texture2dMultisample, samples, intFormat, (int)size.X, (int)size.Y, true); // Attach the texture to the frame buffer. Gl.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2dMultisample, renderTexture, 0); // Attach color components. int[] modes = { Gl.COLOR_ATTACHMENT0 }; Gl.DrawBuffers(modes); // Create render buffer. uint depthBuffer = Gl.GenRenderbuffer(); Gl.BindRenderbuffer(RenderbufferTarget.Renderbuffer, depthBuffer); Gl.RenderbufferStorageMultisample(RenderbufferTarget.Renderbuffer, samples, InternalFormat.Depth24Stencil8, (int)size.X, (int)size.Y); Gl.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, attachStencil ? FramebufferAttachment.DepthStencilAttachment : FramebufferAttachment.DepthAttachment, RenderbufferTarget.Renderbuffer, depthBuffer); // Check status. FramebufferStatus status = Gl.CheckFramebufferStatus(FramebufferTarget.Framebuffer); if (status != FramebufferStatus.FramebufferComplete) { Engine.Log.Warning($"MSAA Framebuffer creation failed. Error code {status}.", MessageSource.GL); } // Construct a texture object for it. Texture targetTexture = new GLTexture(renderTexture, new Vector2(size.X, size.Y), null, $"MSAA{samples} FBO Texture"); // Create the render target object. resultTarget = new GlRenderTarget(newFbo, size, targetTexture); // Clear the target. ClearScreen(); CheckError("creating msaa fbo"); // Restore bindings and so on. Engine.Renderer?.EnsureRenderTarget(); }); return(resultTarget); }
/// <inheritdoc /> public override RenderTarget CreateRenderTarget(Vector2 size, bool smooth = false, TextureInternalFormat internalFormat = TextureInternalFormat.Rgba, TexturePixelFormat pixelFormat = TexturePixelFormat.Rgba, bool attachStencil = false) { RenderTarget resultTarget = null; InternalFormat intFormat = (InternalFormat)Enum.Parse(typeof(InternalFormat), internalFormat.ToString()); PixelFormat glFormat = (PixelFormat)Enum.Parse(typeof(PixelFormat), pixelFormat.ToString()); GLThread.ExecuteGLThread(() => { // Create the FBO which rendering will be done to. uint newFbo = Gl.GenFramebuffer(); Gl.BindFramebuffer(FramebufferTarget.Framebuffer, newFbo); // Create the texture. uint renderTexture = Gl.GenTexture(); Gl.BindTexture(TextureTarget.Texture2d, renderTexture); Gl.TexImage2D(TextureTarget.Texture2d, 0, intFormat, (int)size.X, (int)size.Y, 0, glFormat, PixelType.UnsignedByte, IntPtr.Zero); Gl.TexParameter(TextureTarget.Texture2d, TextureParameterName.TextureMinFilter, smooth ? Gl.LINEAR : Gl.NEAREST); Gl.TexParameter(TextureTarget.Texture2d, TextureParameterName.TextureMagFilter, smooth ? Gl.LINEAR : Gl.NEAREST); // Attach the texture to the frame buffer. Gl.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2d, renderTexture, 0); // Attach color components. int[] modes = { Gl.COLOR_ATTACHMENT0 }; Gl.DrawBuffers(modes); // Create render buffer. uint depthBuffer = Gl.GenRenderbuffer(); Gl.BindRenderbuffer(RenderbufferTarget.Renderbuffer, depthBuffer); Gl.RenderbufferStorage(RenderbufferTarget.Renderbuffer, InternalFormat.Depth24Stencil8, (int)size.X, (int)size.Y); Gl.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, attachStencil ? FramebufferAttachment.DepthStencilAttachment : FramebufferAttachment.DepthAttachment, RenderbufferTarget.Renderbuffer, depthBuffer); // Check status. FramebufferStatus status = Gl.CheckFramebufferStatus(FramebufferTarget.Framebuffer); if (status != FramebufferStatus.FramebufferComplete) { Engine.Log.Warning($"Framebuffer creation failed. Error code {status}.", MessageSource.GL); } // Create the texture object. Texture targetTexture = new GLTexture(renderTexture, new Vector2(size.X, size.Y), null, $"FBO {newFbo} Texture"); // Create the render target object. resultTarget = new GlRenderTarget(newFbo, size, targetTexture); // Clear the target. ClearScreen(); CheckError("creating scale fbo"); // Restore bindings and so on. Engine.Renderer?.EnsureRenderTarget(); }); return(resultTarget); }
internal void CopyTexImage2D(Texture2DTarget target, int level, TextureInternalFormat internalFormat, int x, int y, int width, int height, int border) { gl.glCopyTexImage2D((int)target, level, (int)internalFormat, x, y, width, height, border); CheckException(); }
//internal void ReadPixels(int x, int y, int width, int height, GLPixelFormat format, PixelComponentType type, sbyte[] pixels) //{ // gl.glReadPixels(x, y, width, height, (int)format, (int)type, pixels); // CheckException(); //} //internal void ReadPixels(int x, int y, int width, int height, GLPixelFormat format, PixelComponentType type, short[] pixels) //{ // gl.glReadPixels(x, y, width, height, (int)format, (int)type, pixels); // CheckException(); //} //internal void ReadPixels(int x, int y, int width, int height, GLPixelFormat format, PixelComponentType type, ushort[] pixels) //{ // gl.glReadPixels(x, y, width, height, (int)format, (int)type, pixels); // CheckException(); //} //internal void ReadPixels(int x, int y, int width, int height, GLPixelFormat format, PixelComponentType type, int[] pixels) //{ // gl.glReadPixels(x, y, width, height, (int)format, (int)type, pixels); // CheckException(); //} //internal void ReadPixels(int x, int y, int width, int height, GLPixelFormat format, PixelComponentType type, uint[] pixels) //{ // gl.glReadPixels(x, y, width, height, (int)format, (int)type, pixels); // CheckException(); //} //internal void ReadPixels(int x, int y, int width, int height, GLPixelFormat format, PixelComponentType type, float[] pixels) //{ // gl.glReadPixels(x, y, width, height, (int)format, (int)type, pixels); // CheckException(); //} #endregion internal void CopyTexImage1D(int level, TextureInternalFormat internalFormat, int x, int y, int width, int border) { gl.glCopyTexImage1D((int)Texture1DTarget.Texture1d, level, (int)internalFormat, x, y, width, border); CheckException(); }
public static void CopyTexImage1D(int level, TextureInternalFormat internalFormat, int x, int y, int width, int border) { gl.glCopyTexImage1D((int)Texture1DTarget.Texture1d, level, (int)internalFormat, x, y, width, border); }
public GLTextureRectangle(GraphicsInterface gi, int width, int height, TextureInternalFormat internalFormat, TexturePixelFormat pixelFormat, PixelComponentType pixelType) : base(gi, TextureBindTarget.Rectangle) { gi.Enable(GLOption.TextureRectangle); SetupTexture2D(gi, width, height, internalFormat, pixelFormat, pixelType, IntPtr.Zero, false); }
public void Storage(uint width, uint height, TextureInternalFormat format) { gl.glBindRenderbuffer((uint)GL.RENDERBUFFER, glObject); gl.glRenderbufferStorage((uint)GL.RENDERBUFFER, format, width, height); }
public Renderbuffer(string strName, uint width, uint height, TextureInternalFormat format) : base(strName, GlHandleType.Renderbuffer) { Storage(width, height, format); }
public Texture(Image image, TextureInternalFormat internalFormat = TextureInternalFormat.RGBA8) : this(image.Name.Replace("img_", ""), image.ToByteArray(), TextureDataType.UnsignedInt8888Rev, TextureFormat.RGBA, image.Size, internalFormat) { }
public GLTexture2D(GraphicsInterface gi, int width, int height, TextureInternalFormat internalFormat, TexturePixelFormat pixelFormat, PixelComponentType pixelType, IntPtr pixelData, bool createMipMaps) : base(gi, TextureBindTarget.Texture2d) { SetupTexture2D(gi, width, height, internalFormat, pixelFormat, pixelType, pixelData, createMipMaps); }
public void TexImage1D(Texture1DTarget target, int level, TextureInternalFormat internalformat, int width, int border, TexturePixelFormat format, PixelComponentType type, object pixels) { gl.glTexImage1D((int)target, level, (int)internalformat, width, border, (int)format, (int)type, pixels); }
public static void TexImage1D(int level, TextureInternalFormat internalformat, int width, int border, TexturePixelFormat format, PixelComponentType type, IntPtr pixels) { gl.glTexImage1D((int)Texture1DTarget.Texture1d, level, (int)internalformat, width, border, (int)format, (int)type, pixels); }
public void TexImage2D(Texture2DTarget target, int level, TextureInternalFormat internalformat, int width, int height, int border, TexturePixelFormat format, PixelComponentType type, object pixels) { gl.glTexImage2D((int)target, level, (int)internalformat, width, height, border, (int)format, (int)type, pixels); CheckException(); }
public static extern void glRenderbufferStorage(uint i, TextureInternalFormat format, uint width, uint height);