From 028c7dcfa9f1920d5fa2e573790bef40c4076275 Mon Sep 17 00:00:00 2001 From: pixel Date: Mon, 11 Nov 2002 20:07:41 +0000 Subject: Still working ;-) --- mogltk/gltexture.cpp | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 mogltk/gltexture.cpp (limited to 'mogltk/gltexture.cpp') diff --git a/mogltk/gltexture.cpp b/mogltk/gltexture.cpp new file mode 100644 index 0000000..0878d04 --- /dev/null +++ b/mogltk/gltexture.cpp @@ -0,0 +1,74 @@ +#include +#include +#include "gltexture.h" +#include "General.h" + +mogltk::gltexture::gltexture(int w, int h, bool plane) throw (GeneralException) : width(w), height(h), planar(plane) { + if ((BITCOUNT(w) != 1) || (BITCOUNT(h) != 1)) + throw GeneralException("Size of the texture not a power of 2!"); + + if (!(surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32, +#if SDL_BYTEORDER == SDL_BIG_ENDIAN + 0xff000000, + 0x00ff0000, + 0x0000ff00, + 0x000000ff +#else + 0x000000ff, + 0x0000ff00, + 0x00ff0000, + 0xff000000 +#endif + ))) { + throw GeneralException("Can't create RGB Surface"); + } +} + +mogltk::gltexture::~gltexture() { + if (surface) { + SDL_FreeSurface(surface); + } else { + glDeleteTextures(1, &texture); + } +} + +SDL_Surface * mogltk::gltexture::GetSurface() throw (GeneralException) { + if (!surface) + throw GeneralException("Texture already generated"); + return surface; +} + +void mogltk::gltexture::Generate() throw (GeneralException) { + if (!surface) + throw GeneralException("Texture already generated"); + + if (planar) { + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels); + } else { + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, width, height, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels); +// glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels); + } + + SDL_FreeSurface(surface); + surface = 0; +} + +void mogltk::gltexture::Bind() throw (GeneralException) { + if (surface) + throw GeneralException("Texture is not yet generated"); + glBindTexture(GL_TEXTURE_2D, texture); +} + +GLuint mogltk::gltexture::GetWidth() { + return width; +} + +GLuint mogltk::gltexture::GetHeight() { + return height; +} -- cgit v1.2.3