From a80b074c70ec991f27114efd13686038cf42c493 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 12 Jan 2008 12:10:52 +0000 Subject: [PATCH] Support embedding textures in datafiles --- source/ilwrap.cpp | 15 ++++++++++--- source/ilwrap.h | 5 ++--- source/texture.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++ source/texture.h | 18 ++++++++++++++++ source/texture2d.cpp | 24 ++++++++++++++++++--- source/texture2d.h | 15 +++++++++++++ source/texture3d.cpp | 2 +- 7 files changed, 119 insertions(+), 10 deletions(-) diff --git a/source/ilwrap.cpp b/source/ilwrap.cpp index be8442be..a1ae1631 100644 --- a/source/ilwrap.cpp +++ b/source/ilwrap.cpp @@ -9,11 +9,15 @@ Distributed under the LGPL #include #include "ilwrap.h" +using namespace std; + namespace Msp { namespace GL { Image::Image() { + static bool init_done=false; + if(!init_done) { ilInit(); @@ -30,13 +34,20 @@ Image::~Image() ilDeleteImages(1, &id); } -void Image::load(const std::string &fn) +void Image::load_file(const string &fn) { ilBindImage(id); if(!ilLoadImage(const_cast(fn.c_str()))) throw Exception("Error loading image "+fn); } +void Image::load_lump(const void *data, unsigned size) +{ + ilBindImage(id); + if(!ilLoadL(IL_TYPE_UNKNOWN, const_cast(data), size)) + throw Exception("Error loading image from lump"); +} + PixelFormat Image::get_format() const { switch(ilGetInteger(IL_IMAGE_FORMAT)) @@ -67,7 +78,5 @@ const void *Image::get_data() const return ilGetData(); } -bool Image::init_done=false; - } // namespace GL } // namespace Msp diff --git a/source/ilwrap.h b/source/ilwrap.h index 7b3766a4..331cfd32 100644 --- a/source/ilwrap.h +++ b/source/ilwrap.h @@ -18,13 +18,12 @@ class Image private: unsigned id; - static bool init_done; - public: Image(); ~Image(); - void load(const std::string &); + void load_file(const std::string &); + void load_lump(const void *, unsigned); PixelFormat get_format() const; unsigned get_width() const; unsigned get_height() const; diff --git a/source/texture.cpp b/source/texture.cpp index 1cb16945..7ce039ac 100644 --- a/source/texture.cpp +++ b/source/texture.cpp @@ -9,9 +9,35 @@ Distributed under the LGPL #include "texture.h" #include "texunit.h" +using namespace std; + namespace Msp { namespace GL { +istream &operator>>(istream &in, TextureFilter &tf) +{ + string str; + in>>str; + + if(str=="NEAREST") + tf=NEAREST; + else if(str=="LINEAR") + tf=LINEAR; + else if(str=="NEAREST_MIPMAP_NEAREST") + tf=NEAREST_MIPMAP_NEAREST; + else if(str=="NEAREST_MIPMAP_LINEAR") + tf=NEAREST_MIPMAP_LINEAR; + else if(str=="LINEAR_MIPMAP_NEAREST") + tf=LINEAR_MIPMAP_NEAREST; + else if(str=="LINEAR_MIPMAP_LINEAR") + tf=LINEAR_MIPMAP_LINEAR; + else + in.setstate(ios_base::failbit); + + return in; +} + + void Texture::bind() const { if(!target) @@ -68,5 +94,29 @@ void Texture::maybe_bind() const bind(); } + +Texture::Loader::Loader(Texture &t): + tex(t) +{ + add("min_filter", &Loader::min_filter); + add("mag_filter", &Loader::mag_filter); + add("generate_mipmap", &Loader::generate_mipmap); +} + +void Texture::Loader::min_filter(TextureFilter f) +{ + tex.set_min_filter(f); +} + +void Texture::Loader::mag_filter(TextureFilter f) +{ + tex.set_mag_filter(f); +} + +void Texture::Loader::generate_mipmap(bool gm) +{ + tex.parameter(GL_GENERATE_MIPMAP_SGIS, gm); +} + } // namespace GL } // namespace Msp diff --git a/source/texture.h b/source/texture.h index 28f9cb32..6dd729b4 100644 --- a/source/texture.h +++ b/source/texture.h @@ -8,6 +8,8 @@ Distributed under the LGPL #ifndef MSP_GL_TEXTURE_H_ #define MSP_GL_TEXTURE_H_ +#include +#include #include "gl.h" #include "types.h" @@ -24,6 +26,9 @@ enum TextureFilter LINEAR_MIPMAP_LINEAR = GL_LINEAR_MIPMAP_LINEAR }; +std::istream &operator>>(std::istream &, TextureFilter &); + + /** Base class for textures. This class only defines operations common for all texture types and is not instantiable. For specifying images for textures, see @@ -31,6 +36,19 @@ one of the dimensioned texture classes. */ class Texture { +protected: + class Loader: public DataFile::Loader + { + protected: + Texture &tex; + + public: + Loader(Texture &); + void min_filter(TextureFilter); + void mag_filter(TextureFilter); + void generate_mipmap(bool); + }; + public: ~Texture(); diff --git a/source/texture2d.cpp b/source/texture2d.cpp index 455c7314..fbf86a43 100644 --- a/source/texture2d.cpp +++ b/source/texture2d.cpp @@ -11,8 +11,6 @@ Distributed under the LGPL using namespace std; -#include - namespace Msp { namespace GL { @@ -60,8 +58,13 @@ void Texture2D::sub_image(int level, int x, int y, sizei wd, sizei ht, PixelForm void Texture2D::load_image(const string &fn) { Image img; - img.load(fn); + img.load_file(fn); + + image(img); +} +void Texture2D::image(const Image &img) +{ unsigned w=img.get_width(); unsigned h=img.get_height(); PixelFormat fmt=img.get_format(); @@ -73,5 +76,20 @@ void Texture2D::load_image(const string &fn) image(0, fmt, GL_UNSIGNED_BYTE, img.get_data()); } + +Texture2D::Loader::Loader(Texture2D &t): + Texture::Loader(t) +{ + add("image_data", &Loader::image_data); +} + +void Texture2D::Loader::image_data(const string &data) +{ + Image img; + img.load_lump(data.data(), data.size()); + + static_cast(tex).image(img); +} + } // namespace GL } // namespace Msp diff --git a/source/texture2d.h b/source/texture2d.h index 251561b9..b62466cb 100644 --- a/source/texture2d.h +++ b/source/texture2d.h @@ -9,17 +9,29 @@ Distributed under the LGPL #define MSP_GL_TEXTURE2D_H_ #include +#include #include "pixelformat.h" #include "texture.h" namespace Msp { namespace GL { +class Image; + /** Two-dimensional texture class. This is the most common type of texture. */ class Texture2D: public Texture { +public: + class Loader: public Texture::Loader + { + public: + Loader(Texture2D &); + private: + void image_data(const std::string &); + }; + private: PixelFormat ifmt; sizei width; @@ -56,6 +68,9 @@ public: sizei get_width() const { return width; } sizei get_height() const { return height; } + +private: + void image(const Image &); }; } // namespace GL diff --git a/source/texture3d.cpp b/source/texture3d.cpp index 17adb6a1..c38b4465 100644 --- a/source/texture3d.cpp +++ b/source/texture3d.cpp @@ -53,7 +53,7 @@ void Texture3D::image(int level, PixelFormat fmt, GLenum type, const void *data) void Texture3D::load_image(const string &fn, int dp) { Image img; - img.load(fn); + img.load_file(fn); unsigned w=img.get_width(); unsigned h=img.get_height(); -- 2.43.0