Added easy way of switching to nearest filtering.

This commit is contained in:
Harrison Deng 2020-07-10 21:55:37 -05:00
parent 75f691133a
commit 110c7dbfad
2 changed files with 22 additions and 7 deletions

View File

@ -27,18 +27,15 @@ namespace SlatedGameToolkit.Framework.Graphics.Textures
/// </summary> /// </summary>
/// <param name="textureData">The texture data to use to create the Texture2D.</param> /// <param name="textureData">The texture data to use to create the Texture2D.</param>
/// <param name="context">The openGL context to associate this with. If null, will use the currently active context. Defaults to null.</param> /// <param name="context">The openGL context to associate this with. If null, will use the currently active context. Defaults to null.</param>
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.glContext = context ?? WindowContextsManager.CurrentGL;
this.width = (uint)textureData.width; this.width = (uint)textureData.width;
this.height = (uint)textureData.height; this.height = (uint)textureData.height;
uint[] handles = new uint[1]; uint[] handles = new uint[1];
glContext.GenTextures(1, handles); glContext.GenTextures(1, handles);
this.handle = handles[0]; this.handle = handles[0];
Use();
glContext.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat); SetNearestFilter(nearest);
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);
fixed(void* p = &textureData.data[0]) { fixed(void* p = &textureData.data[0]) {
glContext.PixelStorei(PixelStoreParameter.UnpackAlignment, 4); 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)); 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); 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() { public void Use() {
if (disposed) throw new ObjectDisposedException("Texture"); if (disposed) throw new ObjectDisposedException("Texture");
glContext.BindTexture(OpenGL.TextureTarget.Texture2D, handle); glContext.BindTexture(OpenGL.TextureTarget.Texture2D, handle);

View File

@ -20,7 +20,7 @@ namespace SlatedGameToolkit.Framework.Loaders
{ {
ImageResult image = ImageResult.FromStream(stream, ColorComponents.RedGreenBlueAlpha); ImageResult image = ImageResult.FromStream(stream, ColorComponents.RedGreenBlueAlpha);
TextureData textureData = new TextureData(image.Width, image.Height, image.Data); TextureData textureData = new TextureData(image.Width, image.Height, image.Data);
return new Texture(textureData, glContext); return new Texture(textureData, false, glContext);
} }
} }
} }