From 0c69b4a31e845b61dbb998578442e4428cf43768 Mon Sep 17 00:00:00 2001 From: Thimo Date: Sun, 8 Nov 2020 14:54:59 +0100 Subject: [PATCH] Properly implement texture Mipmap generation * Add missing GL_GENERATE_MIPMAP * Add one-time retrieval of glGetIntegerv(GL_MAJOR_VERSION, *) * Opt for Mipmap generation methos based on OGL version major (<3.0 = GL_GENERATE_MIPMAP, >=3.0 = glGenerateMipmap) --- src/PlatformLayer/GL.bf | 1 + src/PlatformLayer/Texture.bf | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/PlatformLayer/GL.bf b/src/PlatformLayer/GL.bf index 66b1afc..5fecf20 100644 --- a/src/PlatformLayer/GL.bf +++ b/src/PlatformLayer/GL.bf @@ -221,6 +221,7 @@ namespace Strawberry public const uint GL_TEXTURE_DEPTH = 0x8071; public const uint GL_TEXTURE_WRAP_R = 0x8072; public const uint GL_MAX_3D_TEXTURE_SIZE = 0x8073; + public const uint GL_GENERATE_MIPMAP = 0x8191; public const uint GL_UNSIGNED_BYTE_2_3_3_REV = 0x8362; public const uint GL_UNSIGNED_SHORT_5_6_5 = 0x8363; public const uint GL_UNSIGNED_SHORT_5_6_5_REV = 0x8364; diff --git a/src/PlatformLayer/Texture.bf b/src/PlatformLayer/Texture.bf index 4c46d7a..fcc4ff5 100644 --- a/src/PlatformLayer/Texture.bf +++ b/src/PlatformLayer/Texture.bf @@ -6,11 +6,16 @@ namespace Strawberry public int Width { get; private set; } public int Height { get; private set; } + private static int32 OGLMajorV = -1; + public this(int width, int height, uint8* pixels, bool indLinear, bool indClamp) { Width = width; Height = height; + if (OGLMajorV == -1) + GL.glGetIntegerv(GL.GL_MAJOR_VERSION, &OGLMajorV); + GL.glGenTextures(1, &Handle); GL.glBindTexture(GL.GL_TEXTURE_2D, Handle); @@ -26,9 +31,13 @@ namespace Strawberry GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP_TO_EDGE); } + if (OGLMajorV < 3) // OpenGL > 1.4 && < 3.0 + GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_GENERATE_MIPMAP, GL.GL_TRUE); + GL.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, width, height, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, pixels); - GL.glGenerateMipmap(GL.GL_TEXTURE_2D); // Unavailable in OpenGL 2.1. + if (OGLMajorV >= 3) // OpenGL >= 3.0 + GL.glGenerateMipmap(GL.GL_TEXTURE_2D); // Clear state GL.glBindTexture(GL.GL_TEXTURE_2D, 0);