summaryrefslogtreecommitdiff
path: root/lib/texture.cc
diff options
context:
space:
mode:
Diffstat (limited to 'lib/texture.cc')
-rw-r--r--lib/texture.cc111
1 files changed, 107 insertions, 4 deletions
diff --git a/lib/texture.cc b/lib/texture.cc
index 36297ac..80853ca 100644
--- a/lib/texture.cc
+++ b/lib/texture.cc
@@ -1,5 +1,6 @@
#include <sys/types.h>
#include <SDL.h>
+#include <SDL_opengl.h>
#include <generic.h>
#include "texture.h"
#include "engine.h"
@@ -8,6 +9,8 @@
#endif
#include "gettext.h"
+#define DEBUG 1
+
#ifdef TRACE_TEXTURES
mogltk::texture * mogltk::texture::header = 0;
mogltk::texture * mogltk::texture::footer = 0;
@@ -15,7 +18,7 @@ mogltk::texture * mogltk::texture::footer = 0;
mogltk::texture * mogltk::texture::active = 0;
-mogltk::texture::texture(int w, int h) throw (GeneralException) : width(w), height(h),
+mogltk::texture::texture(int w, int h, bool plane) throw (GeneralException) : width(w), height(h),
texture_allocated(false), planar(plane), tainted(true) {
if ((!ISPOT(w)) || (!ISPOT(h)))
throw GeneralException(_("Size of the texture not a power of 2!"));
@@ -49,7 +52,9 @@ mogltk::texture::texture(int w, int h) throw (GeneralException) : width(w), heig
#endif
}
-mogltk::texture::texture(Handle * h) throw (GeneralException) {
+mogltk::texture::texture(Handle * h, bool plane) throw (GeneralException) :
+ texture_allocated(false), planar(plane), tainted(true) {
+
SDL_Surface * temp;
temp = LoadNTEX(h);
@@ -118,6 +123,10 @@ mogltk::texture::~texture() {
SDL_FreeSurface(surface);
}
+ if (texture_allocated) {
+ glDeleteTextures(1, &tex);
+ }
+
#ifdef TRACE_TEXTURES
if (prev) {
prev->next = next;
@@ -145,14 +154,108 @@ SDL_Surface * mogltk::texture::GetSurface() {
return surface;
}
-Uint32 mogltk::texture::GetWidth() {
+void mogltk::texture::Generate() {
+ if (texture_allocated) {
+ glDeleteTextures(1, &tex);
+ }
+
+ glGenTextures(1, &tex);
+#ifdef DEBUG
+ printm(M_INFO, _("Generated texture index: %i\n"), tex);
+#endif
+
+ glBindTexture(GL_TEXTURE_2D, tex);
+
+#if 0
+ if (planar) {
+#ifdef DEBUG
+ printm(M_INFO, _("Generating planar texture: %i\n"), tex);
+#endif
+ 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 {
+#endif
+#ifdef DEBUG
+ printm(M_INFO, _("Generating 3D texture: %i\n"), tex);
+#endif
+ 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);
+#if 0
+ }
+#endif
+
+ texture_allocated = true;
+ tainted = false;
+}
+
+void mogltk::texture::Bind(bool expand) {
+ if ((!texture_allocated) || tainted)
+ Generate();
+ glEnable(GL_TEXTURE_2D);
+ if (active == this)
+ return;
+#ifdef DEBUG
+ printm(M_INFO, _("Binding texture index %i.\n"), tex);
+#endif
+ glBindTexture(GL_TEXTURE_2D, tex);
+ if (expand) {
+ glMatrixMode(GL_TEXTURE);
+ glLoadIdentity();
+ glScaled(1 / (double) width, 1 / (double) height, 1);
+ glMatrixMode(GL_MODELVIEW);
+ }
+
+ active = this;
+
+#ifdef TRACE_TEXTURES
+ if (header == this)
+ return;
+
+ if (prev) {
+ prev->next = next;
+ }
+
+ if (next) {
+ next->prev = prev;
+ }
+
+ if (footer = this) {
+ footer = prev;
+ }
+
+ header->prev = this;
+ header = this;
+#endif
+}
+
+GLuint mogltk::texture::GetWidth() {
return width;
}
-Uint32 mogltk::texture::GetHeight() {
+GLuint mogltk::texture::GetHeight() {
return height;
}
+void mogltk::texture::Unbind(void) {
+ if (active) {
+ glBindTexture(GL_TEXTURE_2D, 0);
+ glDisable(GL_TEXTURE_2D);
+ active = 0;
+#ifdef DEBUG
+ printm(M_INFO, _("Unbinding texture.\n"));
+#endif
+ }
+}
+
+void mogltk::texture::Taint(void) {
+ tainted = true;
+}
+
#ifdef WORDS_BIGENDIAN
#define NTEX_SIGNATURE 0x4e544558
#else