#include <msp/core/except.h>
#include "ilwrap.h"
+using namespace std;
+
namespace Msp {
namespace GL {
Image::Image()
{
+ static bool init_done=false;
+
if(!init_done)
{
ilInit();
ilDeleteImages(1, &id);
}
-void Image::load(const std::string &fn)
+void Image::load_file(const string &fn)
{
ilBindImage(id);
if(!ilLoadImage(const_cast<char *>(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<void *>(data), size))
+ throw Exception("Error loading image from lump");
+}
+
PixelFormat Image::get_format() const
{
switch(ilGetInteger(IL_IMAGE_FORMAT))
return ilGetData();
}
-bool Image::init_done=false;
-
} // namespace GL
} // namespace Msp
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;
#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)
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
#ifndef MSP_GL_TEXTURE_H_
#define MSP_GL_TEXTURE_H_
+#include <istream>
+#include <msp/datafile/loader.h>
#include "gl.h"
#include "types.h"
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
*/
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();
using namespace std;
-#include <iostream>
-
namespace Msp {
namespace GL {
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();
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<Texture2D &>(tex).image(img);
+}
+
} // namespace GL
} // namespace Msp
#define MSP_GL_TEXTURE2D_H_
#include <string>
+#include <msp/datafile/loader.h>
#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;
sizei get_width() const { return width; }
sizei get_height() const { return height; }
+
+private:
+ void image(const Image &);
};
} // namespace GL
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();