From: Mikko Rasa Date: Thu, 27 Oct 2016 22:49:59 +0000 (+0300) Subject: Add some utility functions for textures X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=commitdiff_plain;h=e01371d38e290b4c526146fd5d1388bba8201233 Add some utility functions for textures --- diff --git a/source/resources.cpp b/source/resources.cpp index 32709e30..68b8fd5b 100644 --- a/source/resources.cpp +++ b/source/resources.cpp @@ -88,8 +88,7 @@ Texture2D *Resources::create_texture2d(const string &name) RefPtr tex = new GL::Texture2D(resource_manager); - if(default_tex_filter==NEAREST_MIPMAP_NEAREST || default_tex_filter==NEAREST_MIPMAP_LINEAR || - default_tex_filter==LINEAR_MIPMAP_NEAREST || default_tex_filter==LINEAR_MIPMAP_LINEAR) + if(is_mipmapped(default_tex_filter)) { tex->set_generate_mipmap(true); tex->set_mag_filter(LINEAR); diff --git a/source/texture.cpp b/source/texture.cpp index fd144585..b528ca30 100644 --- a/source/texture.cpp +++ b/source/texture.cpp @@ -364,5 +364,12 @@ void Texture::Loader::wrap_t(TextureWrap w) obj.set_wrap_t(w); } + +bool is_mipmapped(TextureFilter filter) +{ + return (filter==NEAREST_MIPMAP_NEAREST || filter==NEAREST_MIPMAP_LINEAR || + filter==LINEAR_MIPMAP_NEAREST || filter==LINEAR_MIPMAP_LINEAR); +} + } // namespace GL } // namespace Msp diff --git a/source/texture.h b/source/texture.h index 44e12657..23f3bb25 100644 --- a/source/texture.h +++ b/source/texture.h @@ -188,6 +188,9 @@ public: virtual UInt64 get_data_size() const { return 0; } }; + +bool is_mipmapped(TextureFilter); + } // namespace GL } // namespace Msp diff --git a/source/texture1d.cpp b/source/texture1d.cpp index 78c9ed0c..0dc85586 100644 --- a/source/texture1d.cpp +++ b/source/texture1d.cpp @@ -55,8 +55,7 @@ void Texture1D::image(unsigned level, PixelFormat fmt, DataType type, const void if(gen_mipmap && level==0) { auto_generate_mipmap(); - for(; w; w>>=1, ++level) ; - allocated |= (1<>=1, ++n) ; + return n; +} + +unsigned Texture1D::get_level_size(unsigned level) const { return width>>level; } diff --git a/source/texture1d.h b/source/texture1d.h index 0ab9c6f7..d75167b1 100644 --- a/source/texture1d.h +++ b/source/texture1d.h @@ -37,7 +37,8 @@ public: unsigned get_width() const { return width; } private: - unsigned get_level_size(unsigned); + unsigned get_n_levels() const; + unsigned get_level_size(unsigned) const; public: virtual AsyncLoader *load(IO::Seekable &, const Resources * = 0) { return 0; } diff --git a/source/texture2d.cpp b/source/texture2d.cpp index 4d3dac03..95f9f790 100644 --- a/source/texture2d.cpp +++ b/source/texture2d.cpp @@ -85,8 +85,7 @@ void Texture2D::image(unsigned level, PixelFormat fmt, DataType type, const void if(gen_mipmap && level==0) { auto_generate_mipmap(); - for(; (w || h); w>>=1, h>>=1, ++level) ; - allocated |= (1<>=1, ++n) ; + return n; +} + +void Texture2D::get_level_size(unsigned level, unsigned &w, unsigned &h) const { w >>= level; h >>= level; diff --git a/source/texture2d.h b/source/texture2d.h index 221fbd7b..9647ad3e 100644 --- a/source/texture2d.h +++ b/source/texture2d.h @@ -75,7 +75,8 @@ public: unsigned get_height() const { return height; } private: - void get_level_size(unsigned, unsigned &, unsigned &); + unsigned get_n_levels() const; + void get_level_size(unsigned, unsigned &, unsigned &) const; public: virtual Resource::AsyncLoader *load(IO::Seekable &, const Resources * = 0); diff --git a/source/texture3d.cpp b/source/texture3d.cpp index 5c9f009c..12fc856b 100644 --- a/source/texture3d.cpp +++ b/source/texture3d.cpp @@ -74,8 +74,7 @@ void Texture3D::image(unsigned level, PixelFormat fmt, DataType type, const void if(gen_mipmap && level==0) { auto_generate_mipmap(); - for(; (w || h || d); w>>=1, h>>=1, d>>=1, ++level) ; - allocated |= (1<>=1, ++n) ; + return n; +} + +void Texture3D::get_level_size(unsigned level, unsigned &w, unsigned &h, unsigned &d) const { w >>= level; h >>= level; diff --git a/source/texture3d.h b/source/texture3d.h index 55a0e3db..57f3a6f8 100644 --- a/source/texture3d.h +++ b/source/texture3d.h @@ -85,7 +85,8 @@ public: unsigned get_height() const { return height; } unsigned get_depth() const { return depth; } protected: - void get_level_size(unsigned, unsigned &, unsigned &, unsigned &); + unsigned get_n_levels() const; + void get_level_size(unsigned, unsigned &, unsigned &, unsigned &) const; public: virtual AsyncLoader *load(IO::Seekable &, const Resources * = 0) { return 0; } diff --git a/source/texturecube.cpp b/source/texturecube.cpp index 26402695..65b1845e 100644 --- a/source/texturecube.cpp +++ b/source/texturecube.cpp @@ -75,8 +75,7 @@ void TextureCube::image(TextureCubeFace face, unsigned level, PixelFormat fmt, D { // TODO Only do this once all faces are created auto_generate_mipmap(); - for(; s; s>>=1, ++level) ; - allocated |= (1<>=1, ++n) ; + return n; +} + +unsigned TextureCube::get_level_size(unsigned level) const { return size>>level; } diff --git a/source/texturecube.h b/source/texturecube.h index fd467046..62f1189a 100644 --- a/source/texturecube.h +++ b/source/texturecube.h @@ -85,7 +85,8 @@ public: unsigned get_size() const { return size; } private: - unsigned get_level_size(unsigned); + unsigned get_n_levels() const; + unsigned get_level_size(unsigned) const; public: /** Translates indices into face constants. Valid indices are between 0