Skip to content

Instantly share code, notes, and snippets.

@rafaskb
Created February 17, 2020 15:45
Show Gist options
  • Save rafaskb/14fb4a3cfbfdb72b50c609296a50a427 to your computer and use it in GitHub Desktop.
Save rafaskb/14fb4a3cfbfdb72b50c609296a50a427 to your computer and use it in GitHub Desktop.
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.utils.BufferUtils;
import java.nio.FloatBuffer;
public class Anisotropy {
private static boolean anisotropySupported = false;
private static boolean checkComplete = false;
private static float maxAnisotropySupported = 1.0f;
/**
* Applies the given anisotropic level to the texture. Returns the anisotropy value that was applied, based on
* device's maximum capability. Note that this call binds the texture if the device supports anisotropy.
*
* @param texture The texture to apply anisotropy to.
* @param anisotropy The anisotropic level to apply. (Will be reduced if device capability is less.
* @return The anisotropic level that was applied, or `1.0f` if anisotropy is not supported by the device.
*/
public static float setTextureAnisotropy(Texture texture, float anisotropy) {
if(!checkComplete)
isSupported();
if(anisotropySupported) {
texture.bind();
float valueApplied = Math.min(maxAnisotropySupported, anisotropy);
Gdx.gl20.glTexParameterf(GL20.GL_TEXTURE_2D, GL20.GL_TEXTURE_MAX_ANISOTROPY_EXT, valueApplied);
return valueApplied;
} else {
return 1f;
}
}
/**
* @return Whether the device supports anisotropic filtering.
*/
public static boolean isSupported() {
GL20 gl = Gdx.gl;
if(gl != null) {
if(Gdx.graphics.supportsExtension("GL_EXT_texture_filter_anisotropic")) {
anisotropySupported = true;
FloatBuffer buffer = BufferUtils.newFloatBuffer(16);
buffer.position(0);
buffer.limit(buffer.capacity());
Gdx.gl20.glGetFloatv(GL20.GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, buffer);
maxAnisotropySupported = buffer.get(0);
}
checkComplete = true;
}
return anisotropySupported;
}
/**
* @return The max anisotropic filtering level supported by device, or 1 if it isn't supported.
*/
public static float getMaxAnisotropySupported() {
if(!checkComplete)
isSupported();
return maxAnisotropySupported;
}
}
@rafaskb
Copy link
Author

rafaskb commented Feb 17, 2020

Usage:

Anisotropy.setTextureAnisotropy(texture, level);

level is usually power of two values from 1 to 16. For me 4 works great.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment