I have trouble loading texture to opengl as well. I took this code from some website and modified for my own needs. You need to have a class folder in your project for this to work.
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.ComponentColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferByte;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import java.io.IOException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import javax.swing.ImageIcon;
import org.lwjgl.opengl.GL11;
public class SpriteSheet {
private static List<SpriteSheet> sheets = new ArrayList<SpriteSheet>();
private static ColorModel glAlphaColorModel = new ComponentColorModel(
ColorSpace.getInstance(ColorSpace.CS_sRGB),
new int[] {8,8,8,8},
true,
false,
ComponentColorModel.TRANSLUCENT,
DataBuffer.TYPE_BYTE);
private static int target = GL11.GL_TEXTURE_2D;
public static SpriteSheet sheet = new SpriteSheet("/image.png");
public int id = 0;
public int width, height;
public String name;
public SpriteSheet(String name) {
this.name = name;
try {
BufferedImage image = loadBufferedImage(name);
ByteBuffer imageByteBuffer = convertImageData(image);
id = GL11.glGenTextures();
bind();
GL11.glTexParameteri(target, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
GL11.glTexParameteri(target, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
this.width = image.getWidth();
this.height = image.getHeight();
GL11.glTexImage2D(
target,
0,
GL11.GL_RGBA,
image.getWidth(),
image.getHeight(),
0,
GL11.GL_RGBA,
GL11.GL_UNSIGNED_BYTE,
imageByteBuffer);
sheets.add(this);
}catch(IOException e) {
e.printStackTrace();
}
}
public SpriteSheet(int id, int width, int height) {
this.id = id;
this.width = width;
this.height = height;
this.name = "none";
}
@SuppressWarnings("rawtypes")
private ByteBuffer convertImageData(BufferedImage image){
ByteBuffer textureByteBuffer = null;
WritableRaster raster = null;
BufferedImage bufImage = null;
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, image.getWidth(), image.getHeight(), 4, null);
bufImage = new BufferedImage(glAlphaColorModel, raster, false, new Hashtable());
Graphics g = bufImage.getGraphics();
g.drawImage(image, 0, 0, null);
byte[] imageBytes = ((DataBufferByte) bufImage.getRaster().getDataBuffer()).getData();
textureByteBuffer = ByteBuffer.allocateDirect(imageBytes.length);
textureByteBuffer.order(ByteOrder.nativeOrder());
textureByteBuffer.put(imageBytes, 0, imageBytes.length);
textureByteBuffer.flip();
return textureByteBuffer;
}
private BufferedImage loadBufferedImage(String name) throws IOException {
URL url = SpriteSheet.class.getResource(name);
Image img = new ImageIcon(url).getImage();
BufferedImage image = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics g = image.getGraphics();
g.drawImage(img, 0, 0, image.getWidth(), image.getHeight(), null);
g.dispose();
int[] pixels = image.getRGB(0, 0, image.getWidth(), image.getHeight(), null, 0, image.getWidth());
Color color = new Color(0, 0, 0, 0);
for(int i=0;i<pixels.length;i++){
if(pixels[i] == -65281) image.setRGB(i % image.getWidth(), i / image.getWidth(), color.getRGB()); // Replace certain colors with alpha
}
return image;
}
public void bind() {
GL11.glBindTexture(target, id);
}
}
When you want to create a Spritesheet, you type in path from your class folder to the file. Just study this you will understand.