kleandros
Newbie

Posts: 2
|
 |
« Reply #2 on: September 26, 2005, 06:33:45 » |
|
the code below works for me (32bits)
/* * TextureLoader2.java * * Created on August 20, 2005, 4:31 PM */
package rts.client.texture;
import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.IntBuffer;
import org.lwjgl.opengl.Display; import org.lwjgl.opengl.DisplayMode; import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.glu.GLU; import org.lwjgl.devil.IL; import org.lwjgl.devil.ILU; import org.lwjgl.input.Keyboard;
import java.util.HashMap; /** * * @author ks */ public class TextureLoader2 { HashMap spriteCache = new HashMap(); IntBuffer buf = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer(); public TextureLoader2() { try { IL.create(); ILU.create(); } catch (Exception e) { e.printStackTrace(); } } public Texture loadTexture(String path) { Texture tx = (Texture) spriteCache.get(path); if (tx != null) { return tx; } IntBuffer image = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer(); IL.ilGenImages(image); IL.ilBindImage(image.get(0)); IL.ilLoadImage("./res/"+path);//D:/__source/lwjgl-win32-0.97 IL.ilConvertImage(IL.ilGetInteger(IL.IL_IMAGE_FORMAT), IL.IL_BYTE); int image_width = IL.ilGetInteger(IL.IL_IMAGE_WIDTH); int image_height = IL.ilGetInteger(IL.IL_IMAGE_HEIGHT); convertToPowerOfTwoTexture(); ByteBuffer scratch = IL.ilGetData(); /* ByteBuffer scratch = ByteBuffer.allocateDirect( IL.ilGetInteger(IL.IL_IMAGE_WIDTH) * IL.ilGetInteger(IL.IL_IMAGE_HEIGHT) * 4 * 4); IL.ilCopyPixels(0, 0, 0, IL.ilGetInteger(IL.IL_IMAGE_WIDTH) , IL.ilGetInteger(IL.IL_IMAGE_HEIGHT) , 1, IL.ilGetInteger(IL.IL_IMAGE_FORMAT), IL.IL_BYTE, scratch); */ // Create A IntBuffer For Image Address In Memory 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_WRAP_S, GL11.GL_REPEAT); //GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT); GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR); GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR); //GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST); //GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST); // Generate The Texture GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, getSourcePixelFormat() , IL.ilGetInteger(IL.IL_IMAGE_WIDTH) , IL.ilGetInteger(IL.IL_IMAGE_HEIGHT) , 0 , getSourcePixelFormat(), GL11.GL_UNSIGNED_BYTE, scratch);
Texture texture = new Texture(GL11.GL_TEXTURE_2D, buf.get(0)); texture.setHeight(image_height); texture.setWidth(image_width); texture.setTextureWidth(IL.ilGetInteger(IL.IL_IMAGE_WIDTH)); texture.setTextureHeight(IL.ilGetInteger(IL.IL_IMAGE_HEIGHT)); System.out.println("@@@IL "+path+" id="+texture.getTextureID() +" text:width="+texture.getWidth()+":" +texture.getImageWidth()+":"+texture.getTextureWidth() +" text:height="+texture.getHeight()+":" +texture.getImageHeight()+":"+texture.getTextureHeight() ); spriteCache.put(path, texture); return texture; } private void convertToPowerOfTwoTexture() { int oldWidth = IL.ilGetInteger(IL.IL_IMAGE_WIDTH); int oldHeight = IL.ilGetInteger(IL.IL_IMAGE_HEIGHT); int width = getNextPowerOfTwo(oldWidth); int height = getNextPowerOfTwo(oldHeight); /* if (width < height) { width = height; } else { height = width; } */ if(width != oldWidth || height != oldHeight) { ILU.iluEnlargeCanvas(width, height, 1); } } /** * finds the next highest number with a power of two. */ private int getNextPowerOfTwo(int number) { int newNumber = 2; while (newNumber < number) { newNumber *= 2; } return newNumber; } private int getSourcePixelFormat() { int sourcePixelFormat = -1; if(IL.ilGetInteger(IL.IL_IMAGE_FORMAT) == IL.IL_RGBA) sourcePixelFormat = GL11.GL_RGBA; else if(IL.ilGetInteger(IL.IL_IMAGE_FORMAT) == IL.IL_RGB) sourcePixelFormat = GL11.GL_RGB; else if(IL.ilGetInteger(IL.IL_IMAGE_FORMAT) == IL.IL_LUMINANCE) sourcePixelFormat = GL11.GL_LUMINANCE_ALPHA; else System.out.println("unsupported (by this loader) source pixel format!"); return sourcePixelFormat; } }
|