So since the beginning of my time with LWJGL I have always struggled and couldn’t make my own texture loader… So I found some already made one and modified it for my own needs.
Today I decided its finally time to figure out how to make my own texture loader.
So I made wrote this code and it doesn’t display any texture. Any ideas what I could be doing wrong? The problem is probably somewhere at glTexImage2d
Ummmm this is the code of my texture loader that actually works.
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();
int filter = GL11.GL_NEAREST;
GL11.glTexParameteri(target, GL11.GL_TEXTURE_MIN_FILTER, filter);
GL11.glTexParameteri(target, GL11.GL_TEXTURE_MAG_FILTER, filter);
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());
// System.out.println(pixels[0]);
Color color = new Color(0, 0, 0, 0);
for(int i=0;i<pixels.length;i++){
if(pixels[i] == -65281 || pixels[i] == -2719745) image.setRGB(i % image.getWidth(), i / image.getWidth(), color.getRGB()); // Replace certain colors with alpha
}
return image;
}
public void bind() {
GL11.glBindTexture(target, id);
}
}
This is the code of the one I’m trying to write. It really does seem a lot simpler to go with intbuffer, but I have no idea why it doesn’t work…
public class SpriteSheet {
public static final int target = GL_TEXTURE_2D;
public static SpriteSheet sheet = new SpriteSheet("/image.png");
public String name;
public int width, height;
public int handle = 0;
public SpriteSheet(String name) {
this.name = name;
load();
}
public void load() {
handle = glGenTextures();
bind();
BufferedImage image = loadImage(name);
this.width=image.getWidth();
this.height=image.getHeight();
IntBuffer data = convertImageData(image);
glTexImage2D(target, 0, GL_INT, width, height, 0, GL_INT, GL_INT, data);
}
public void bind() {
glBindTexture(target, handle);
}
private IntBuffer convertImageData(BufferedImage image) {
IntBuffer buffer=BufferUtils.createIntBuffer(width*height);
for(int y=0;y<height;y++) {
for(int x=0;x<width;x++) {
buffer.put(image.getRGB(x, y));
}
}
buffer.flip();
return buffer;
}
private BufferedImage loadImage(String name) {
try {
return ImageIO.read(SpriteSheet.class.getResource(name));
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}