From 98c06bcbc8359a5cdd8e7c75455171dd39bb7fce Mon Sep 17 00:00:00 2001 From: Taylor Richards Date: Mon, 4 Jul 2016 11:47:58 -0400 Subject: [PATCH] handle npot textures properly to base spec --- include/2d.h | 5 +++++ src/graphics/grgl2.cpp | 8 -------- src/graphics/grgl2texture.cpp | 31 +++++++++++++++++++------------ 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/include/2d.h b/include/2d.h index 9b26da6..7a97549 100644 --- a/include/2d.h +++ b/include/2d.h @@ -734,5 +734,10 @@ inline int next_pow2(int p) return p+1; } +inline int is_pow2(int p) +{ + return (p && !(p & (p-1))); +} + #endif diff --git a/src/graphics/grgl2.cpp b/src/graphics/grgl2.cpp index 2989106..99a55d6 100644 --- a/src/graphics/grgl2.cpp +++ b/src/graphics/grgl2.cpp @@ -22,8 +22,6 @@ int GL_two_inited = 0; -bool Use_mipmaps = false; - static GLuint FB_texture = 0; static GLuint FB_id = 0; static GLuint FB_rb_id = 0; @@ -303,12 +301,6 @@ int opengl2_init() glPixelStorei(GL_PACK_ALIGNMENT, 1); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); - if ( SDL_GL_ExtensionSupported("GL_OES_texture_npot") ) { - Use_mipmaps = true; - } - - mprintf((" Mipmaps : %s\n", Use_mipmaps ? "Enabled" : "Disabled")); - glFlush(); gr_opengl_clear(); diff --git a/src/graphics/grgl2texture.cpp b/src/graphics/grgl2texture.cpp index e6b8020..f6d7062 100644 --- a/src/graphics/grgl2texture.cpp +++ b/src/graphics/grgl2texture.cpp @@ -49,8 +49,6 @@ static ubyte GL_xlat[256] = { 0 }; extern int Gr_textures_in; extern int bm_get_cache_slot( int bitmap_id, int separate_ani_frames ); -extern bool Use_mipmaps; - void opengl2_set_texture_state(gr_texture_source ts) { @@ -209,8 +207,7 @@ static int opengl2_create_texture_sub(int bitmap_handle, int bitmap_type, bitmap glBindTexture(GL_TEXTURE_2D, t->texture_handle); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + GLint wrap_mode = GL_CLAMP_TO_EDGE; glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); @@ -261,6 +258,8 @@ static int opengl2_create_texture_sub(int bitmap_handle, int bitmap_type, bitmap } default: { + wrap_mode = GL_REPEAT; + if (resize) { texmem = (ubyte *) malloc(tex_w * tex_h * 2); texmemp = texmem; @@ -296,21 +295,20 @@ static int opengl2_create_texture_sub(int bitmap_handle, int bitmap_type, bitmap free(texmem); } - if (Use_mipmaps) { - glGenerateMipmap(GL_TEXTURE_2D); + glGenerateMipmap(GL_TEXTURE_2D); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR); - t->is_mipmaped = 1; + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR); + t->is_mipmaped = 1; - size = fl2i(tex_w * tex_h * 2.0f * 1.3333333f); - } else { - size = tex_w * tex_h * 2; - } + size = fl2i(tex_w * tex_h * 2.0f * 1.3333333f); break; } } + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap_mode); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap_mode); + t->bitmap_id = bitmap_handle; t->time_created = GL_frame_count; t->used_this_frame = 0; @@ -380,6 +378,15 @@ static int opengl2_create_texture(int bitmap_handle, int bitmap_type, tcache_slo max_h /= val; } + if ( (bitmap_type == TCACHE_TYPE_NORMAL) || (bitmap_type == TCACHE_TYPE_XPARENT) ) { + if ( !is_pow2(max_w) || !is_pow2(max_h) ) { + resize = true; + + max_w = next_pow2(max_w); + max_h = next_pow2(max_h); + } + } + if ( (max_w < 1) || (max_h < 1) ) { mprintf(("Bitmap %d is too small at %dx%d\n", bitmap_handle, max_w, max_h)); return 0; -- 2.39.2