diff options
Diffstat (limited to '')
-rw-r--r-- | src/base/image.cpp | 5 | ||||
-rw-r--r-- | src/base/image.hpp | 4 | ||||
-rw-r--r-- | src/base/texture.cpp | 83 | ||||
-rw-r--r-- | src/base/texture.hpp | 52 |
4 files changed, 127 insertions, 17 deletions
diff --git a/src/base/image.cpp b/src/base/image.cpp index 9f849533..d1d944eb 100644 --- a/src/base/image.cpp +++ b/src/base/image.cpp @@ -33,7 +33,8 @@ namespace usdx log4cxx::LoggerPtr Image::log = log4cxx::Logger::getLogger("usdx.base.Image"); - ImageLoadException::ImageLoadException(std::string error) : error(error) + ImageLoadException::ImageLoadException(std::string error) : + error(error) { } @@ -72,7 +73,7 @@ namespace usdx } } - const SDL_Surface* Image::get_surface(void) + SDL_Surface* Image::get_surface(void) { if (surface == NULL) { load(); diff --git a/src/base/image.hpp b/src/base/image.hpp index 9b8c5871..9e9ea6e4 100644 --- a/src/base/image.hpp +++ b/src/base/image.hpp @@ -53,7 +53,7 @@ namespace usdx static log4cxx::LoggerPtr log; boost::filesystem::wpath filename; - SDL_Surface *surface; + SDL_Surface* surface; void load(void); @@ -61,7 +61,7 @@ namespace usdx Image(boost::filesystem::wpath filename); virtual ~Image(); - const SDL_Surface* get_surface(void); + SDL_Surface* get_surface(void); }; }; diff --git a/src/base/texture.cpp b/src/base/texture.cpp index ace2857f..1b9ed970 100644 --- a/src/base/texture.cpp +++ b/src/base/texture.cpp @@ -28,5 +28,86 @@ namespace usdx { - + log4cxx::LoggerPtr Texture::log = + log4cxx::Logger::getLogger("usdx.base.texture"); + + Texture::Texture(boost::filesystem::wpath filename) : + filename(filename), texture(0), size(0, 0), rotation(0) + { + Image image(filename); + + size.set_width(image.get_surface()->w); + size.set_height(image.get_surface()->h); + + // // Check that the image dimensions are a power of 2 + // if ((image.get_surface()->w & (image.get_surface()->w - 1)) != 0 ) { + // LOG4CXX_ERROR(log, L"" << filename << L" has a width of " << image.get_surface()->w << L" that is not a power of 2"); + // throw TextureSizeException(size); + // } + + // if ((image.get_surface()->h & (image.get_surface()->h - 1)) != 0 ) { + // LOG4CXX_ERROR(log, L"" << filename << L" has a height of " << image.get_surface()->h << L" that is not a power of 2"); + // throw TextureSizeException(size); + // } + + + switch (image.get_surface()->format->BytesPerPixel) { + case 4: + if (image.get_surface()->format->Rmask == 0x000000ff) { + texture_format = GL_RGBA; + } + else { + texture_format = GL_BGRA; + } + + break; + + case 3: + if (image.get_surface()->format->Rmask == 0x000000ff) { + texture_format = GL_RGB; + } + else { + texture_format = GL_BGR; + } + + break; + + default: + LOG4CXX_ERROR(log, L"" << filename << L" is not in true color! Could not handle that!"); + throw TextureColorDepthException(image.get_surface()->format->BytesPerPixel); + } + + // Have OpenGL generate a texture object handle for us + glGenTextures(1, &texture); + + // Bind the texture object + glBindTexture(GL_TEXTURE_2D, texture); + + // Set the texture's stretching properties + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + + // Edit the texture object's image data using the information SDL_Surface gives us + glTexImage2D(GL_TEXTURE_2D, + 0, + image.get_surface()->format->BytesPerPixel, + image.get_surface()->w, + image.get_surface()->h, + 0, + texture_format, + GL_UNSIGNED_BYTE, + image.get_surface()->pixels); + } + + Texture::~Texture() + { + if (glIsTexture(texture)) { + glDeleteTextures(1, &texture); + } + } + + GLuint Texture::get_texture(void) const + { + return texture; + } }; diff --git a/src/base/texture.hpp b/src/base/texture.hpp index 016bb307..008707d6 100644 --- a/src/base/texture.hpp +++ b/src/base/texture.hpp @@ -29,33 +29,61 @@ #include <boost/filesystem.hpp> #include <GL/gl.h> + #include "utils/point_3d.hpp" #include "utils/dimension.hpp" #include "utils/rectangle.hpp" +#include "image.hpp" +#include "drawable.hpp" namespace usdx { - class Texture + class TextureLoadException + { + public: + TextureLoadException() {}; + virtual ~TextureLoadException() {}; + }; + + class TextureSizeException : public TextureLoadException { private: - GLuint tex_num; - Point3D position; Dimension size; - Dimension scale; ///< for dynamic scaling - float rotation; ///< radiant (0 - 2*pi) + public: + TextureSizeException(Dimension size) : size(size) { }; + virtual ~TextureSizeException() { }; + }; + + class TextureColorDepthException : public TextureLoadException + { + private: + unsigned int number_of_colors; - Dimension tex; ///< percentage of size to use [0..1] - Rectangle tex_rect; + public: + TextureColorDepthException(unsigned int number_of_colors) : number_of_colors(number_of_colors) {}; + virtual ~TextureColorDepthException() {}; + }; + + class Texture + { + private: + static log4cxx::LoggerPtr log; - boost::filesystem::wpath filename; ///< experimental for - ///handling cache - ///images. maybe it's useful - ///for dynamic skins + boost::filesystem::wpath filename; + + GLuint texture; + GLenum texture_format; + + Dimension size; + + float rotation; ///< radiant (0 - 2*pi) public: - Texture(); + Texture(boost::filesystem::wpath filename); virtual ~Texture(); + + GLuint get_texture(void) const; }; }; |