X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Ftexture.cpp;h=f1689145c8af9c5754569213a7561b0505e63b2c;hb=c6604d0ee313f60b42b3a205c40ba7e1abbc3cb8;hp=1b7bdc2297ec3a1be84d95fdcd091b55b59bae20;hpb=0f890ce60a560ba2ccc0719229be304bb597d919;p=libs%2Fgl.git diff --git a/source/texture.cpp b/source/texture.cpp index 1b7bdc22..f1689145 100644 --- a/source/texture.cpp +++ b/source/texture.cpp @@ -7,6 +7,7 @@ #include #include #include +#include "bindable.h" #include "error.h" #include "resourcemanager.h" #include "resources.h" @@ -63,11 +64,12 @@ Texture::Texture(GLenum t, ResourceManager *m): ifmt(RGB), min_filter(NEAREST_MIPMAP_LINEAR), mag_filter(LINEAR), + mipmap_levels(0), max_anisotropy(1.0f), wrap_s(REPEAT), wrap_t(REPEAT), wrap_r(REPEAT), - gen_mipmap(false), + auto_gen_mipmap(0), compare(false), cmp_func(LEQUAL), dirty_params(0) @@ -156,7 +158,7 @@ void Texture::update_parameter(int mask) const if(mask&WRAP_R) set_parameter_i(GL_TEXTURE_WRAP_R, wrap_r); if(mask&GENERATE_MIPMAP) - set_parameter_i(GL_GENERATE_MIPMAP, gen_mipmap); + set_parameter_i(GL_GENERATE_MIPMAP, auto_gen_mipmap!=0); if(mask&COMPARE) set_parameter_i(GL_TEXTURE_COMPARE_MODE, (compare ? GL_COMPARE_R_TO_TEXTURE : GL_NONE)); if(mask&COMPARE_FUNC) @@ -178,6 +180,8 @@ void Texture::update_parameter(int mask) const glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, swizzle_orders+swizzle*4); } } + if(mask&MIPMAP_LEVELS) + set_parameter_i(GL_TEXTURE_MAX_LEVEL, (mipmap_levels ? mipmap_levels-1 : 1000)); } void Texture::set_parameter_i(GLenum param, int value) const @@ -214,6 +218,12 @@ void Texture::set_filter(TextureFilter f) set_mag_filter(f==NEAREST ? NEAREST : LINEAR); } +void Texture::set_mipmap_levels(unsigned l) +{ + mipmap_levels = l; + update_parameter(MIPMAP_LEVELS); +} + void Texture::set_max_anisotropy(float a) { if(a<1.0f) @@ -257,24 +267,31 @@ bool Texture::can_generate_mipmap() return (EXT_framebuffer_object || SGIS_generate_mipmap); } -void Texture::set_generate_mipmap(bool gm) +void Texture::generate_mipmap() { - if(gm && !EXT_framebuffer_object) - static Require _req(SGIS_generate_mipmap); - gen_mipmap = gm; - if(!EXT_framebuffer_object) - update_parameter(GENERATE_MIPMAP); + // glGenerateMipmap is defined here + static Require _req(EXT_framebuffer_object); + + if(ARB_direct_state_access) + glGenerateTextureMipmap(id); + else + { + BindRestore _bind(this); + glGenerateMipmap(target); + } } -void Texture::auto_generate_mipmap() +void Texture::set_auto_generate_mipmap(bool gm) { - // glGenerateMipmap is defined here if(EXT_framebuffer_object) + auto_gen_mipmap = gm; + else { - if(ARB_direct_state_access) - glGenerateTextureMipmap(id); - else - glGenerateMipmap(target); + if(gm) + static Require _req(SGIS_generate_mipmap); + + auto_gen_mipmap = gm*2; + update_parameter(GENERATE_MIPMAP); } } @@ -314,6 +331,8 @@ void Texture::bind_to(unsigned i, bool legacy) const } } + legacy = (legacy && is_legacy_target(target)); + TexUnit &unit = TexUnit::get_unit(i); const Texture *old = unit.get_texture(); bool old_legacy = unit.get_texture_legacy(); @@ -369,6 +388,11 @@ void Texture::unbind_from(unsigned i) } } +bool Texture::is_legacy_target(GLenum target) +{ + return target(t, 0) @@ -396,6 +420,7 @@ void Texture::Loader::init() add("mag_filter", &Loader::mag_filter); add("max_anisotropy", &Loader::max_anisotropy); add("min_filter", &Loader::min_filter); + add("mipmap_levels", &Loader::mipmap_levels); add("wrap", &Loader::wrap); add("wrap_r", &Loader::wrap_r); add("wrap_s", &Loader::wrap_s); @@ -406,6 +431,8 @@ void Texture::Loader::external_image(const string &fn) { Graphics::Image img; RefPtr io = get_collection().open_raw(fn); + if(!io) + throw IO::file_not_found(fn); img.load_io(*io); obj.image(img, srgb); @@ -418,7 +445,7 @@ void Texture::Loader::filter(TextureFilter f) void Texture::Loader::generate_mipmap(bool gm) { - obj.set_generate_mipmap(gm); + obj.set_auto_generate_mipmap(gm); } void Texture::Loader::image_data(const string &data) @@ -445,6 +472,11 @@ void Texture::Loader::min_filter(TextureFilter f) obj.set_min_filter(f); } +void Texture::Loader::mipmap_levels(unsigned l) +{ + obj.set_mipmap_levels(l); +} + void Texture::Loader::wrap(TextureWrap w) { obj.set_wrap(w);