As requested, the code for this. Also, all textures used (well it’s just 1 background texture) were DXT1, no alpha. Also, no mipmaps since this is all 2D.
- DDSLoader.java
Identical to jME version, except that I make it return a ByteBuffer rather than a jME Image. I also add in methods to retrieve image width, height, depth and DXTn format.
public static ByteBuffer loadImage(InputStream fis, boolean flip) throws IOException {
DDSReader reader = new DDSReader(fis);
reader.loadHeader();
ByteBuffer data = reader.readData(flip);
pixelDepth = reader.bpp_;
width = reader.width_;
height = reader.height_;
format = reader.pixelFormat_;
return data;
}
- TextureLoader.java
Really similar to the Space Invaders tutorial code except for the last line.
int textureID = createTextureID();
Texture texture = new Texture(target, textureID);
GL11.glBindTexture(target, textureID);
ByteBuffer textureBuffer;
int width;
int height;
boolean hasAlpha;
try
{
textureBuffer = DDSLoader.loadImage(new BufferedInputStream(
MediaManager.instance().getKitResourceAsStream(
background.getKitName(),
DirectoryListing.BACKGROUNDS + background.getID()
+ "-" + 0 +
".dds")));
width = DDSLoader.getLastWidth();
height = DDSLoader.getLastHeight();
hasAlpha = DDSLoader.getLastDepth() == 32;
texture.setTextureWidth(width);
texture.setTextureHeight(height);
texture.setWidth(width);
texture.setHeight(height);
if(target == GL11.GL_TEXTURE_2D)
{
GL11.glTexParameteri(target, GL11.GL_TEXTURE_MIN_FILTER, minFilter);
GL11.glTexParameteri(target, GL11.GL_TEXTURE_MAG_FILTER, magFilter);
}
int format = 0;
switch(DDSLoader.getFormat())
{
case DXT1_NATIVE:
format = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
break;
case DXT1A_NATIVE:
format = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
break;
case DXT3_NATIVE:
format = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
break;
case DXT5_NATIVE:
format = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
break;
}
checkCompressedTextureExtensions();
GL13.glCompressedTexImage2D
(
GL11.GL_TEXTURE_2D,
0,
format,
width,
height,
0,
textureBuffer
);
Since it was requested, the code for drawing an image. Again, taken straight from a tutorial.
//A "Fake Image" is passed in that contains data for grabbing the desired image from the cache. The reason is that GTGE forces a Graphics2D like interface onto LWJGL, so I pass in an Image with no image data.
texture = textureLoader.getTexture(img);
texture.bind();
float tx0 = ((float) sx1 / texture.getTextureWidth());
float tx1 = ((float) sx2 / texture.getTextureWidth());
float ty0 = ((float) sy1 / texture.getTextureHeight());
float ty1 = ((float) sy2 / texture.getTextureHeight());
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(tx0, ty0);
GL11.glVertex2f(dx1, dy1);
GL11.glTexCoord2f(tx1, ty0);
GL11.glVertex2f(dx2, dy1);
GL11.glTexCoord2f(tx1, ty1);
GL11.glVertex2f(dx2, dy2);
GL11.glTexCoord2f(tx0, ty1);
GL11.glVertex2f(dx1, dy2);
GL11.glEnd();