From 511a5e5ee5dc1e54c1f31e31a48d083a7b184d3e Mon Sep 17 00:00:00 2001
From: Stijn Buys <ingar@osirion.org>
Date: Wed, 18 Feb 2009 20:22:07 +0000
Subject: use GL_GENERATE_MIPMAP instead of gluBuild2DMipmaps, set sane
 glTexParameteri values

---
 src/render/gl.h        |  1 -
 src/render/textures.cc | 46 +++++++++++++++++++++++++---------------------
 src/render/textures.h  |  1 -
 3 files changed, 25 insertions(+), 23 deletions(-)

(limited to 'src/render')

diff --git a/src/render/gl.h b/src/render/gl.h
index a858c5b..da71bef 100644
--- a/src/render/gl.h
+++ b/src/render/gl.h
@@ -8,7 +8,6 @@
 #define __INCLUDED_RENDER_GL_H__
 
 #include "GL/gl.h"
-#include "GL/glu.h"
 
 #include "math/vector2f.h"
 #include "math/vector3f.h"
diff --git a/src/render/textures.cc b/src/render/textures.cc
index a340814..8f9249a 100644
--- a/src/render/textures.cc
+++ b/src/render/textures.cc
@@ -170,11 +170,27 @@ size_t Textures::load(const std::string &name, const bool filter)
 		texture_type = GL_RGBA;
 	else
 		texture_type = GL_RGB;
+		
+	if (filter) {
+		// scaling functions
+		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
+		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
+		// 4 levels of mipmaps
+		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 4);
+		// hardware generated mipmaps (requires OpenGL 1.4)
+		glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
+	} else {
+		// scaling functions
+		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
+		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
+		// no mipmaps, base level only
+		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAX_LEVEL, 0);
+	}
+	glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
 
-	gluBuild2DMipmaps(GL_TEXTURE_2D, image->channels(),
-	                  image->width(), image->height(), texture_type, GL_UNSIGNED_BYTE, image->data());
-
-	set_filter(filter);
+	glTexImage2D(GL_TEXTURE_2D, 0,
+			image->channels(), image->width(), image->height(), 0,
+			texture_type, GL_UNSIGNED_BYTE, image->data());
 
 	// add to the registry
 	registry[name] = id;
@@ -206,13 +222,13 @@ size_t Textures::bind(const std::string &name, const bool filter)
 {
 	size_t id = 0;
 	iterator it = registry.find(name);
-	if (it != registry.end())
+	if (it != registry.end()) {
 		id = (*it).second;
-	else
+		glBindTexture(GL_TEXTURE_2D, textures[id]);
+	} else {
 		id = load(name);
-
-	glBindTexture(GL_TEXTURE_2D, textures[id]);
-	set_filter(filter);
+	}
+	
 	return id;
 }
 
@@ -223,19 +239,7 @@ size_t Textures::bind(const size_t texture, const bool filter)
 		id = 0;
 
 	glBindTexture(GL_TEXTURE_2D, textures[id]);
-	set_filter(filter);
 	return id;
 }
 
-void Textures::set_filter(const bool filter)
-{
-	if (filter) {
-		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
-		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR);
-	} else {
-		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
-		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
-	}
-}
-
 }
diff --git a/src/render/textures.h b/src/render/textures.h
index 5a20641..84d5c9a 100644
--- a/src/render/textures.h
+++ b/src/render/textures.h
@@ -58,7 +58,6 @@ public:
 
 private:
 	static void clear();
-	static void set_filter(bool filter);
 
 	typedef std::map<std::string, size_t>::iterator iterator;
 
-- 
cgit v1.2.3