Browse Source

Reworked internal anisotropic filtering code to be more concise and intuitive

Alex Szpakowski 12 years ago
parent
commit
2009124cf9

+ 1 - 11
src/modules/graphics/Image.cpp

@@ -26,12 +26,12 @@ namespace graphics
 {
 
 Image::Filter Image::defaultFilter;
-float Image::defaultAnisotropy = 1.0f;
 
 Image::Filter::Filter()
 	: min(FILTER_LINEAR)
 	, mag(FILTER_LINEAR)
 	, mipmap(FILTER_NONE)
+	, anisotropy(1.0f)
 {
 }
 
@@ -55,16 +55,6 @@ const Image::Filter &Image::getDefaultFilter()
 	return defaultFilter;
 }
 
-void Image::setDefaultAnisotropy(float anisotropy)
-{
-	defaultAnisotropy = anisotropy;
-}
-
-float Image::getDefaultAnisotropy()
-{
-	return defaultAnisotropy;
-}
-
 bool Image::getConstant(const char *in, FilterMode &out)
 {
 	return filterModes.find(in, out);

+ 2 - 8
src/modules/graphics/Image.h

@@ -56,6 +56,7 @@ public:
 		FilterMode min;
 		FilterMode mag;
 		FilterMode mipmap;
+		float anisotropy;
 	};
 
 	struct Wrap
@@ -71,10 +72,6 @@ public:
 	static void setDefaultFilter(const Filter &f);
 	static const Filter &getDefaultFilter();
 
-	// The default amount of anisotropic filtering.
-	static void setDefaultAnisotropy(float anisotropy);
-	static float getDefaultAnisotropy();
-
 	static bool getConstant(const char *in, FilterMode &out);
 	static bool getConstant(FilterMode in, const char  *&out);
 	static bool getConstant(const char *in, WrapMode &out);
@@ -82,12 +79,9 @@ public:
 
 private:
 
-	// The default image filter.
+	// The default texture filter.
 	static Filter defaultFilter;
 
-	// The default amount of anisotropic filtering.
-	static float defaultAnisotropy;
-
 	static StringMap<FilterMode, FILTER_MAX_ENUM>::Entry filterModeEntries[];
 	static StringMap<FilterMode, FILTER_MAX_ENUM> filterModes;
 	static StringMap<WrapMode, WRAP_MAX_ENUM>::Entry wrapModeEntries[];

+ 0 - 20
src/modules/graphics/opengl/Canvas.cpp

@@ -317,7 +317,6 @@ Canvas::Canvas(int width, int height, TextureType texture_type)
 	vertices[3].t = 0;
 
 	settings.filter = Image::getDefaultFilter();
-	settings.anisotropy = Image::getDefaultAnisotropy();
 
 	getStrategy();
 
@@ -497,24 +496,6 @@ Image::Wrap Canvas::getWrap() const
 	return getTextureWrap();
 }
 
-void Canvas::setAnisotropy(float anisotropy)
-{
-	if (Image::hasAnisotropicFilteringSupport())
-	{
-		settings.anisotropy = std::min(std::max(anisotropy, 1.0f), Image::getMaxAnisotropy());
-
-		bindTexture(img);
-		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, settings.anisotropy);
-	}
-	else
-		settings.anisotropy = 1.0f;
-}
-
-float Canvas::getAnisotropy() const
-{
-	return settings.anisotropy;
-}
-
 bool Canvas::loadVolatile()
 {
 	status = strategy->createFBO(fbo, depth_stencil, img, width, height, texture_type);
@@ -523,7 +504,6 @@ bool Canvas::loadVolatile()
 
 	setFilter(settings.filter);
 	setWrap(settings.wrap);
-	setAnisotropy(settings.anisotropy);
 	Color c;
 	c.r = c.g = c.b = c.a = 0;
 	clear(c);

+ 0 - 4
src/modules/graphics/opengl/Canvas.h

@@ -72,9 +72,6 @@ public:
 	void setWrap(const Image::Wrap &w);
 	Image::Wrap getWrap() const;
 
-	void setAnisotropy(float anisotropy);
-	float getAnisotropy() const;
-
 	int getWidth();
 	int getHeight();
 
@@ -122,7 +119,6 @@ private:
 	{
 		Image::Filter filter;
 		Image::Wrap   wrap;
-		float anisotropy;
 	} settings;
 
 	void drawv(const Matrix &t, const vertex *v) const;

+ 1 - 25
src/modules/graphics/opengl/Font.cpp

@@ -47,7 +47,6 @@ Font::Font(love::font::Rasterizer *r, const Image::Filter &filter)
 	, height(r->getHeight())
 	, lineHeight(1)
 	, mSpacing(1)
-	, anisotropy(Image::getDefaultAnisotropy())
 	, filter(filter)
 {
 	// try to find the best texture size match for the font size
@@ -167,7 +166,6 @@ void Font::createTexture()
 					&emptyData[0]);
 
 	setFilter(filter);
-	setAnisotropy(anisotropy);
 }
 
 Font::Glyph *Font::addGlyph(unsigned int glyph)
@@ -506,7 +504,7 @@ void Font::setFilter(const Image::Filter &f)
 	for (it = textures.begin(); it != textures.end(); ++it)
 	{
 		bindTexture(*it);
-		setTextureFilter(f);
+		filter.anisotropy = setTextureFilter(f);
 	}
 }
 
@@ -515,28 +513,6 @@ const Image::Filter &Font::getFilter()
 	return filter;
 }
 
-void Font::setAnisotropy(float anisotropy)
-{
-	if (Image::hasAnisotropicFilteringSupport())
-	{
-		this->anisotropy = std::min(std::max(anisotropy, 1.0f), Image::getMaxAnisotropy());
-
-		std::vector<GLuint>::const_iterator it;
-		for (it = textures.begin(); it != textures.end(); ++it)
-		{
-			bindTexture(*it);
-			glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, this->anisotropy);
-		}
-	}
-	else
-		this->anisotropy = 1.0f;
-}
-
-float Font::getAnisotropy() const
-{
-	return anisotropy;
-}
-
 bool Font::loadVolatile()
 {
 	createTexture();

+ 0 - 5
src/modules/graphics/opengl/Font.h

@@ -129,9 +129,6 @@ public:
 	void setFilter(const Image::Filter &f);
 	const Image::Filter &getFilter();
 
-	void setAnisotropy(float anisotropy);
-	float getAnisotropy() const;
-
 	// Implements Volatile.
 	bool loadVolatile();
 	void unloadVolatile();
@@ -186,8 +183,6 @@ private:
 	float lineHeight;
 	float mSpacing; // modifies the spacing by multiplying it with this value
 
-	float anisotropy;
-
 	int texture_size_index;
 	int texture_width;
 	int texture_height;

+ 0 - 10
src/modules/graphics/opengl/Graphics.cpp

@@ -639,16 +639,6 @@ const Image::Filter &Graphics::getDefaultFilter() const
 	return Image::getDefaultFilter();
 }
 
-void Graphics::setDefaultAnisotropy(float anisotropy)
-{
-	Image::setDefaultAnisotropy(anisotropy);
-}
-
-float Graphics::getDefaultAnisotropy() const
-{
-	return Image::getDefaultAnisotropy();
-}
-
 void Graphics::setDefaultMipmapFilter(Image::FilterMode filter, float sharpness)
 {
 	Image::setDefaultMipmapFilter(filter);

+ 0 - 6
src/modules/graphics/opengl/Graphics.h

@@ -319,12 +319,6 @@ public:
 	 **/
 	const Image::Filter &getDefaultFilter() const;
 
-	/**
-	 * Default texture anisotropic filtering level.
-	 **/
-	void setDefaultAnisotropy(float anisotropy);
-	float getDefaultAnisotropy() const;
-
 	/**
 	 * Default Image mipmap filter mode and sharpness values.
 	 **/

+ 3 - 33
src/modules/graphics/opengl/Image.cpp

@@ -34,7 +34,6 @@ namespace opengl
 {
 
 float Image::maxMipmapSharpness = 0.0f;
-float Image::maxAnisotropy = 1.0f;
 
 Image::FilterMode Image::defaultMipmapFilter = Image::FILTER_NONE;
 float Image::defaultMipmapSharpness = 0.0f;
@@ -45,7 +44,6 @@ Image::Image(love::image::ImageData *data)
 	, texture(0)
 	, mipmapSharpness(defaultMipmapSharpness)
 	, mipmapsCreated(false)
-	, anisotropy(getDefaultAnisotropy())
 {
 	data->retain();
 	this->data = data;
@@ -208,7 +206,7 @@ void Image::setFilter(const Image::Filter &f)
 	filter = f;
 
 	bind();
-	setTextureFilter(f);
+	filter.anisotropy = setTextureFilter(f);
 	checkMipmapsCreated();
 }
 
@@ -249,24 +247,6 @@ float Image::getMipmapSharpness() const
 	return mipmapSharpness;
 }
 
-void Image::setAnisotropy(float anisotropy)
-{
-	if (hasAnisotropicFilteringSupport())
-	{
-		this->anisotropy = std::min(std::max(anisotropy, 1.0f), getMaxAnisotropy());
-
-		bind();
-		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, this->anisotropy);
-	}
-	else
-		this->anisotropy = 1.0f;
-}
-
-float Image::getAnisotropy() const
-{
-	return anisotropy;
-}
-
 void Image::bind() const
 {
 	if (texture == 0)
@@ -301,7 +281,7 @@ bool Image::loadVolatilePOT()
 	glGenTextures(1,(GLuint *)&texture);
 	bindTexture(texture);
 
-	setTextureFilter(filter);
+	filter.anisotropy = setTextureFilter(filter);
 	setTextureWrap(wrap);
 
 	float p2width = next_p2(width);
@@ -343,7 +323,6 @@ bool Image::loadVolatilePOT()
 	checkMipmapsCreated();
 
 	setMipmapSharpness(mipmapSharpness);
-	setAnisotropy(anisotropy);
 
 	return true;
 }
@@ -353,7 +332,7 @@ bool Image::loadVolatileNPOT()
 	glGenTextures(1,(GLuint *)&texture);
 	bindTexture(texture);
 
-	setTextureFilter(filter);
+	filter.anisotropy = setTextureFilter(filter);
 	setTextureWrap(wrap);
 
 	while (glGetError() != GL_NO_ERROR); // clear errors
@@ -375,7 +354,6 @@ bool Image::loadVolatileNPOT()
 	checkMipmapsCreated();
 
 	setMipmapSharpness(mipmapSharpness);
-	setAnisotropy(anisotropy);
 
 	return true;
 }
@@ -429,14 +407,6 @@ Image::FilterMode Image::getDefaultMipmapFilter()
 	return defaultMipmapFilter;
 }
 
-float Image::getMaxAnisotropy()
-{
-	if (hasAnisotropicFilteringSupport() && maxAnisotropy == 1.0f)
-		glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &maxAnisotropy);
-
-	return maxAnisotropy;
-}
-
 bool Image::hasNpot()
 {
 	return GLEE_VERSION_2_0 || GLEE_ARB_texture_non_power_of_two;

+ 0 - 9
src/modules/graphics/opengl/Image.h

@@ -104,9 +104,6 @@ public:
 
 	const Image::Wrap &getWrap() const;
 
-	void setAnisotropy(float anisotropy);
-	float getAnisotropy() const;
-
 	void setMipmapSharpness(float sharpness);
 	float getMipmapSharpness() const;
 
@@ -124,8 +121,6 @@ public:
 	static void setDefaultMipmapFilter(FilterMode f);
 	static FilterMode getDefaultMipmapFilter();
 
-	static float getMaxAnisotropy();
-
 	static bool hasNpot();
 	static bool hasAnisotropicFilteringSupport();
 	static bool hasMipmapSupport();
@@ -158,9 +153,6 @@ private:
 	// True if mipmaps have been created for this Image.
 	bool mipmapsCreated;
 
-	// Anisotropic filtering value.
-	float anisotropy;
-
 	// The image's filter mode
 	Image::Filter filter;
 
@@ -173,7 +165,6 @@ private:
 	void checkMipmapsCreated();
 
 	static float maxMipmapSharpness;
-	static float maxAnisotropy;
 
 	static FilterMode defaultMipmapFilter;
 	static float defaultMipmapSharpness;

+ 21 - 1
src/modules/graphics/opengl/OpenGL.cpp

@@ -39,6 +39,8 @@ static bool contextInitialized = false;
 static int curTextureUnit = 0;
 static std::vector<GLuint> textureUnits;
 
+static float maxAnisotropy = 1.0f;
+
 void initializeContext()
 {
 	if (contextInitialized)
@@ -95,6 +97,11 @@ void initializeContext()
 		glUnmapBufferARB = (GLEEPFNGLUNMAPBUFFERARBPROC) glUnmapBuffer;
 	}
 
+	if (GLEE_EXT_texture_filter_anisotropic)
+		glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &maxAnisotropy);
+	else
+		maxAnisotropy = 1.0f;
+
 	// Set the 'default' texture (id 0) as a repeating white pixel.
 	// Otherwise, texture2D inside a shader would return black when drawing graphics primitives,
 	// which would create the need to use different "passthrough" shaders for untextured primitives vs images.
@@ -175,7 +182,7 @@ void deleteTexture(GLuint texture)
 	glDeleteTextures(1, &texture);
 }
 
-void setTextureFilter(const graphics::Image::Filter &f)
+float setTextureFilter(const graphics::Image::Filter &f)
 {
 	GLint gmin, gmag;
 
@@ -214,6 +221,16 @@ void setTextureFilter(const graphics::Image::Filter &f)
 
 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gmin);
 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gmag);
+
+	float anisotropy = 1.0f;
+
+	if (GLEE_EXT_texture_filter_anisotropic)
+	{
+		anisotropy = std::min(std::max(f.anisotropy, 1.0f), maxAnisotropy);
+		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, anisotropy);
+	}
+
+	return anisotropy;
 }
 
 graphics::Image::Filter getTextureFilter()
@@ -262,6 +279,9 @@ graphics::Image::Filter getTextureFilter()
 		break;
 	}
 
+	if (GLEE_EXT_texture_filter_anisotropic)
+		glGetTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, &f.anisotropy);
+
 	return f;
 }
 

+ 2 - 1
src/modules/graphics/opengl/OpenGL.h

@@ -71,8 +71,9 @@ void deleteTexture(GLuint texture);
 
 /**
  * Sets the image filter mode for the currently bound texture.
+ * Returns the actual amount of anisotropic filtering set.
  */
-void setTextureFilter(const graphics::Image::Filter &f);
+float setTextureFilter(const graphics::Image::Filter &f);
 
 /**
  * Returns the image filter mode for the currently bound texture.

+ 3 - 5
src/modules/graphics/opengl/wrap_Canvas.cpp

@@ -99,10 +99,9 @@ int w_Canvas_setFilter(lua_State *L)
 	if (!Image::getConstant(magstr, f.mag))
 		return luaL_error(L, "Invalid filter mode: %s", magstr);
 
-	canvas->setFilter(f);
+	f.anisotropy = (float) luaL_optnumber(L, 4, 1.0);
 
-	float anisotropy = (float) luaL_optnumber(L, 4, 1.0);
-	canvas->setAnisotropy(anisotropy);
+	canvas->setFilter(f);
 
 	return 0;
 
@@ -120,8 +119,7 @@ int w_Canvas_getFilter(lua_State *L)
 
 	lua_pushstring(L, minstr);
 	lua_pushstring(L, magstr);
-
-	lua_pushnumber(L, canvas->getAnisotropy());
+	lua_pushnumber(L, f.anisotropy);
 
 	return 3;
 }

+ 3 - 4
src/modules/graphics/opengl/wrap_Font.cpp

@@ -103,6 +103,8 @@ int w_Font_setFilter(lua_State *L)
 	if (!Image::getConstant(magstr, f.mag))
 		return luaL_error(L, "Invalid filter mode: %s", magstr);
 
+	f.anisotropy = (float) luaL_optnumber(L, 4, 1.0);
+
 	try
 	{
 		t->setFilter(f);
@@ -112,9 +114,6 @@ int w_Font_setFilter(lua_State *L)
 		return luaL_error(L, "%s", e.what());
 	}
 
-	float anisotropy = (float) luaL_optnumber(L, 4, 1.0);
-	t->setAnisotropy(anisotropy);
-
 	return 0;
 }
 
@@ -128,7 +127,7 @@ int w_Font_getFilter(lua_State *L)
 	Image::getConstant(f.mag, magstr);
 	lua_pushstring(L, minstr);
 	lua_pushstring(L, magstr);
-	lua_pushnumber(L, t->getAnisotropy());
+	lua_pushnumber(L, f.anisotropy);
 	return 3;
 }
 

+ 4 - 4
src/modules/graphics/opengl/wrap_Graphics.cpp

@@ -749,14 +749,14 @@ int w_setDefaultFilter(lua_State *L)
 	if (!Image::getConstant(magstr, mag))
 		return luaL_error(L, "Invalid filter mode: %s", magstr);
 
+	float anisotropy = (float) luaL_optnumber(L, 3, 1.0);
+
 	Image::Filter f;
 	f.min = min;
 	f.mag = mag;
+	f.anisotropy = anisotropy;
 
 	instance->setDefaultFilter(f);
-
-	float anisotropy = (float) luaL_optnumber(L, 3, 1.0);
-	instance->setDefaultAnisotropy(anisotropy);
 	
 	return 0;
 }
@@ -772,7 +772,7 @@ int w_getDefaultFilter(lua_State *L)
 		return luaL_error(L, "Unknown magnification filter mode");
 	lua_pushstring(L, minstr);
 	lua_pushstring(L, magstr);
-	lua_pushnumber(L, instance->getDefaultAnisotropy());
+	lua_pushnumber(L, f.anisotropy);
 	return 3;
 }
 

+ 3 - 4
src/modules/graphics/opengl/wrap_Image.cpp

@@ -68,6 +68,8 @@ int w_Image_setFilter(lua_State *L)
 	if (!Image::getConstant(magstr, f.mag))
 		return luaL_error(L, "Invalid filter mode: %s", magstr);
 
+	f.anisotropy = (float) luaL_optnumber(L, 4, 1.0);
+
 	try
 	{
 		t->setFilter(f);
@@ -77,9 +79,6 @@ int w_Image_setFilter(lua_State *L)
 		return luaL_error(L, "%s", e.what());
 	}
 
-	float anisotropy = (float) luaL_optnumber(L, 4, 1.0);
-	t->setAnisotropy(anisotropy);
-
 	return 0;
 }
 
@@ -93,7 +92,7 @@ int w_Image_getFilter(lua_State *L)
 	Image::getConstant(f.mag, magstr);
 	lua_pushstring(L, minstr);
 	lua_pushstring(L, magstr);
-	lua_pushnumber(L, t->getAnisotropy());
+	lua_pushnumber(L, f.anisotropy);
 	return 3;
 }