From 110c7dbfad943217073a05c5a59ad0513658dbee Mon Sep 17 00:00:00 2001 From: Harrison Deng Date: Fri, 10 Jul 2020 21:55:37 -0500 Subject: [PATCH] Added easy way of switching to nearest filtering. --- .../Graphics/Textures/Texture.cs | 27 ++++++++++++++----- .../Loaders/TextureLoader.cs | 2 +- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/SlatedGameToolkit.Framework/Graphics/Textures/Texture.cs b/src/SlatedGameToolkit.Framework/Graphics/Textures/Texture.cs index 6b6fc32..22a0dd5 100644 --- a/src/SlatedGameToolkit.Framework/Graphics/Textures/Texture.cs +++ b/src/SlatedGameToolkit.Framework/Graphics/Textures/Texture.cs @@ -27,18 +27,15 @@ namespace SlatedGameToolkit.Framework.Graphics.Textures /// /// The texture data to use to create the Texture2D. /// The openGL context to associate this with. If null, will use the currently active context. Defaults to null. - public unsafe Texture(TextureData textureData, GLContext context = null) { + public unsafe Texture(TextureData textureData, bool nearest = false, GLContext context = null) { this.glContext = context ?? WindowContextsManager.CurrentGL; this.width = (uint)textureData.width; this.height = (uint)textureData.height; uint[] handles = new uint[1]; glContext.GenTextures(1, handles); this.handle = handles[0]; - Use(); - glContext.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat); - glContext.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat); - glContext.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear); - glContext.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); + + SetNearestFilter(nearest); fixed(void* p = &textureData.data[0]) { glContext.PixelStorei(PixelStoreParameter.UnpackAlignment, 4); glContext.TexImage2D(TextureTarget.Texture2D, 0, InternalFormat.Rgba, (uint)textureData.width, (uint)textureData.height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, new IntPtr(p)); @@ -46,6 +43,24 @@ namespace SlatedGameToolkit.Framework.Graphics.Textures glContext.GenerateMipmap(OpenGL.TextureTarget.Texture2D); } + public void SetNearestFilter(bool nearest = false, bool generateMipMap = false) { + Use(); + if (nearest) { + glContext.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat); + glContext.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat); + glContext.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.NearestMipmapNearest); + glContext.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest); + + } else { + glContext.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat); + glContext.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat); + glContext.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear); + glContext.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); + } + + if (!generateMipMap) glContext.GenerateMipmap(OpenGL.TextureTarget.Texture2D); + } + public void Use() { if (disposed) throw new ObjectDisposedException("Texture"); glContext.BindTexture(OpenGL.TextureTarget.Texture2D, handle); diff --git a/src/SlatedGameToolkit.Framework/Loaders/TextureLoader.cs b/src/SlatedGameToolkit.Framework/Loaders/TextureLoader.cs index 51a63f9..d541a44 100644 --- a/src/SlatedGameToolkit.Framework/Loaders/TextureLoader.cs +++ b/src/SlatedGameToolkit.Framework/Loaders/TextureLoader.cs @@ -20,7 +20,7 @@ namespace SlatedGameToolkit.Framework.Loaders { ImageResult image = ImageResult.FromStream(stream, ColorComponents.RedGreenBlueAlpha); TextureData textureData = new TextureData(image.Width, image.Height, image.Data); - return new Texture(textureData, glContext); + return new Texture(textureData, false, glContext); } } }