public class Texture
{
private static final boolean s_DEFAULT_USE_MIPMAPS = false;
private static final String s_STR_EMPTY = "";
private static VectorBuffer3f vertexBuffer;
private static VectorBuffer2f textureBuffer;
static
{
vertexBuffer = new VectorBuffer3f(4, true, true);
textureBuffer = new VectorBuffer2f(4, true, true);
}
/**
* The name or path of the texture
*/
public String name = s_STR_EMPTY;
/**
* The binded id of the texture in opengl
*/
public int id;
/**
* The binded images width and height
*/
public int width, height;
/**
* True if texture is transparent. Transparent textures are stored as GL_RGBA, opaque as GL_RGB
*/
public boolean transparent;
/**
* Whether texture uses mipmaps
*/
public boolean useMipmaps = s_DEFAULT_USE_MIPMAPS;
/**
* The GL_TEXTURE_WRAP_S texture parameter
*/
public int wrapS = OpenGL.GL_REPEAT;
/**
* The GL_TEXTURE_WRAP_T texture parameter
*/
public int wrapT = OpenGL.GL_REPEAT;
/**
* The GL_TEXTURE_MAG_FILTER texture parameter
*/
public int magFilter = OpenGL.GL_LINEAR;
/**
* The GL_TEXTURE_MIN_FILTER texture parameter
*/
public int minFilter = OpenGL.GL_LINEAR;
/**
* The GL_TEXTURE_ENV_MODE texture parameter
*/
public int mode = OpenGL.GL_MODULATE;
/**
* Creates a default Texture.
*/
public Texture(int id)
{
this.id = id;
}
/**
* Creates a Texture.
*/
public Texture(int id, int width, int height, boolean transparent, boolean useMipmaps,
int wrapS, int wrapT, int magFilter, int minFilter, int mode)
{
this(null, id, width, height, transparent, useMipmaps, wrapS, wrapT, magFilter, minFilter, mode);
}
public Texture(String name, int id, int width, int height, boolean transparent, boolean useMipmaps,
int wrapS, int wrapT, int magFilter, int minFilter, int mode)
{
this.name = name;
this.id = id;
this.width = width;
this.height = height;
this.transparent = transparent;
this.useMipmaps = useMipmaps;
this.wrapS = wrapS;
this.wrapT = wrapT;
this.magFilter = magFilter;
this.minFilter = minFilter;
this.mode = mode;
}
// Draws a texture quads at the specified bounds.
public static void drawTexturedQuad(float x, float y, float width, float height, float tx, float ty, float tw,
float th)
{
vertexBuffer.put(x, y, 0);
vertexBuffer.put(x + width, y, 0);
vertexBuffer.put(x, y + height, 0);
vertexBuffer.put(x + width, y + height, 0);
vertexBuffer.rewindWrite();
textureBuffer.put(tx, ty);
textureBuffer.put(tx + tw, ty);
textureBuffer.put(tx, ty + th);
textureBuffer.put(tx + tw, ty + th);
textureBuffer.rewindWrite();
gl.glVertexPointer(3, 0, vertexBuffer.getReadBuffer());
gl.glTexCoordPointer(2, 0, textureBuffer.getReadBuffer());
gl.glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
// Draws a texture quads at the specified bounds.
public static void drawTexturedQuad(float x, float y, float z, float width, float height, float tx, float ty,
float tw, float th)
{
vertexBuffer.put(x, y, z);
vertexBuffer.put(x + width, y, z);
vertexBuffer.put(x, y + height, z);
vertexBuffer.put(x + width, y + height, 0);
vertexBuffer.rewindWrite();
textureBuffer.put(tx, ty);
textureBuffer.put(tx + tw, ty);
textureBuffer.put(tx, ty + th);
textureBuffer.put(tx + tw, ty + th);
textureBuffer.rewindWrite();
gl.glVertexPointer(3, 0, vertexBuffer.getReadBuffer());
gl.glTexCoordPointer(2, 0, textureBuffer.getReadBuffer());
gl.glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
public void set(Texture t)
{
this.name = t.name;
this.id = t.id;
this.width = t.width;
this.height = t.height;
this.transparent = t.transparent;
this.useMipmaps = t.useMipmaps;
this.wrapS = t.wrapS;
this.wrapT = t.wrapT;
this.magFilter = t.magFilter;
this.minFilter = t.minFilter;
this.mode = t.mode;
}
/**
* Sets the texture parameters.
*/
public void setTexParameters()
{
gl.glBindTexture(GL_TEXTURE_2D, id);
gl.glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
gl.glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
gl.glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
gl.glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
gl.glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, mode);
}
private static final String s_STR_NAME = "[name=";
private static final String s_STR_ID = " id=";
private static final String s_STR_WIDTH = " width=";
private static final String s_STR_HEIGHT = " height=";
private static final String s_STR_WRAPS = " wrapS=";
private static final String s_STR_WRAPT = " wrapT=";
private static final String s_STR_MAGFILTER = " magFilter=";
private static final String s_STR_MINFILTER = " minFilter=";
private static final String s_STR_MODE = " mode=";
private static final String s_STR_TRANSPARENT = " transparent=";
private static final String s_STR_YES = "yes]";
private static final String s_STR_NO = "no]";
public String toString()
{
StringBuilder sb = new StringBuilder(s_STR_NAME);
sb.append(name).append(s_STR_ID).append(id).append(s_STR_WIDTH).append(width).append(s_STR_HEIGHT).append(
height).append(s_STR_WRAPS).append(wrapS).append(s_STR_WRAPT).append(wrapT).append(s_STR_MAGFILTER).append(
magFilter).append(s_STR_MINFILTER).append(minFilter).append(s_STR_MODE).append(mode).append(
s_STR_TRANSPARENT).append(transparent ? s_STR_YES : s_STR_NO);
return sb.toString();
}
}