Why didnt you tell me about the gluScaleImage method?
now why wont this work?(it fails with an index out of bounds exception in that method)
package com.ts.terestria.client.lwjglrenderer;
import org.lwjgl.*;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.glu.*;
import org.lwjgl.devil.*;
import java.nio.*;
import java.util.HashMap;
public class TextureCache{
/*
*cannot be instanstiated
*/
private TextureCache(){}
static{
init();
}
//Strings as keys for Texture Objects
private static HashMap textures = new HashMap();
public static void init(){
try{
IL.create();
}catch(LWJGLException e){}
}
/**
*returns a Texture, loading it if neccasary and adding it to the cache
*/
public static Texture getTexture(String path){
//need to load it?
if(!textures.containsKey(path))
textures.put(path, loadTexture(path));
return (Texture) textures.get(path);
}
/*
*returns a texture object loaded from the specified path
*/
public static Texture loadTexture(String path){
ByteBuffer scratch = getImageData(path);
int width = IL.ilGetInteger(IL.IL_IMAGE_WIDTH);
int height = IL.ilGetInteger(IL.IL_IMAGE_HEIGHT);
int newWidth=width, newHeight=height;
//scale image?
if(!isPOT(width) || !isPOT(height)){
int newWidth = getNextPOT(width);
int newHeight = getNextPOT(height);
ByteBuffer scaled = ByteBuffer.allocateDirect(newWidth*newHeight*4);
//returns 0 on success, else a GLU error code
int retVal = GLU.gluScaleImage(
GL11.GL_RGBA,
width,
height,
GL11.GL_UNSIGNED_BYTE,
scratch,
newWidth,
newHeight,
GL11.GL_UNSIGNED_BYTE,
scaled);
scratch = scaled;
if(retVal != 0){
System.out.println("something went wrong scaling texture");
System.exit(1);
}
}
// Create A IntBuffer For Image Address In Memory
IntBuffer buf = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
GL11.glGenTextures(buf); // Create Texture In OpenGL
GL11.glBindTexture(GL11.GL_TEXTURE_2D, buf.get(0));
// Typical Texture Generation Using Data From The Image
// Linear Filtering
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
// Linear Filtering
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
// Generate The Texture
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, newWidth, newHeight,
0, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, scratch);
// buf.get(0) == Image Address In Memory
return new Texture(buf.get(0), width, height);
}
/**
*returns a byte buffer of image data loaded from the specifed path
*/
public static ByteBuffer getImageData(String path){
IntBuffer image = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
IL.ilGenImages(image);
IL.ilBindImage(image.get(0));
IL.ilLoadImage(path);
IL.ilConvertImage(IL.IL_RGBA, IL.IL_BYTE);
ByteBuffer scratch = ByteBuffer.allocateDirect(IL.ilGetInteger(IL.IL_IMAGE_WIDTH) * IL.ilGetInteger(IL.IL_IMAGE_HEIGHT) * 3);
IL.ilCopyPixels(0, 0, 0, IL.ilGetInteger(IL.IL_IMAGE_WIDTH), IL.ilGetInteger(IL.IL_IMAGE_HEIGHT), 1, IL.IL_RGB, IL.IL_BYTE, scratch);
return scratch;
}
/**
*returns the next POT, if n is POT then it returns n
*/
public static int getNextPOT(int n){
int i = 1;
while(i < n){
i *= 2;
}
return i;
}
/**
*returns if n is POT
*/
public static boolean isPOT(int n){
return getNextPOT(n) == n;
}
}