package com.peterhi.ui.gl;
import java.net.URL;
import javax.media.opengl.GL;
import javax.media.opengl.GL3;
import javax.media.opengl.GLException;
import com.jogamp.opengl.util.texture.Texture;
import com.jogamp.opengl.util.texture.TextureIO;
public class GLTexture extends GLObject {
private Texture texture;
private URL url;
public GLTexture(GL gl) {
super(gl);
setId(hashCode());
}
public GLTexture(GL gl, URL url) {
this(gl);
setUrl(url);
}
public URL getUrl() {
if (isDisposed()) {
throw new GLException();
}
return url;
}
public void setUrl(URL value) {
if (value == null) {
throw new IllegalArgumentException();
}
if (isDisposed()) {
throw new GLException();
}
url = value;
}
public void load() {
if (isDisposed()) {
throw new GLException();
}
if (url == null) {
throw new GLException();
}
try {
texture = TextureIO.newTexture(url, false, ".jpg");
texture.setTexParameteri(getGL(), GL3.GL_TEXTURE_MIN_FILTER, GL3.GL_LINEAR);
texture.setTexParameteri(getGL(), GL3.GL_TEXTURE_MAG_FILTER, GL3.GL_LINEAR);
texture.setTexParameteri(getGL(), GL3.GL_TEXTURE_WRAP_S, GL3.GL_CLAMP_TO_EDGE);
texture.setTexParameteri(getGL(), GL3.GL_TEXTURE_WRAP_T, GL3.GL_CLAMP_TO_EDGE);
} catch (RuntimeException ex) {
throw ex;
} catch (Exception ex) {
throw new GLException(ex);
}
}
public void enable() {
if (isDisposed()) {
throw new GLException();
}
if (texture == null) {
throw new GLException();
}
texture.enable(getGL());
}
public void disable() {
if (isDisposed()) {
throw new GLException();
}
if (texture == null) {
throw new GLException();
}
texture.disable(getGL());
}
public void bind() {
if (isDisposed()) {
throw new GLException();
}
if (texture == null) {
throw new GLException();
}
texture.bind(getGL());
}
@Override
public void dispose() {
if (!isDisposed()) {
return;
}
if (texture != null) {
texture.destroy(getGL());
}
super.dispose();
}
}