(Sorry if this has already been asked, I couldn't find any other question or source describing my problem)
Okay so basically Square textures load and display just fine.
But for some reason, when trying to load in textures that aren't square (and/or resizing the model so the texture fits) isn't working right.
Here's the Player-bit:
float[] vertices = new float[] {
-1, 1.5f, 0, // Top Left 0
1, 1.5f, 0, // Top Right 1
1, -1.5f, 0, // Bottom Right 2
-1, -1.5f, 0, // Bottom Left 3
};
float[] texture = new float[] {
0,0,
1,0,
1,1.5f,
0,1.5f,
};
int[] indices = new int[] {
0,1,2,
2,3,0
};
model = new Model(vertices, texture, indices);
this.texture = new Texture("animation/player/idle.png");
transform = new Transform();
transform.scale = new Vector3f(16,16,1);
and here my Texture Class code:
public Texture(String filename) {
BufferedImage bi;
try {
URL path = getClass().getResource("/textures/" + filename);
if(path == null) {
throw new IllegalStateException("Texture not found");
}
URI file = path.toURI();
bi = ImageIO.read(new File(file));
width = bi.getWidth();
height = bi.getHeight();
System.out.println(filename + ": " + (int) width + "x" + (int) height);
int[] rawPixels = new int[width * height * 4];
rawPixels = bi.getRGB(0,0, width, height, null, 0, width);
ByteBuffer pixels = BufferUtils.createByteBuffer(width*height*4);
for(int x = 0; x < width; x++) {
for(int y = 0; y < height; y++) {
int pixel = rawPixels[x*width+y];
// Red, Green, Blue, Alpha
pixels.put((byte) ((pixel >> 16) & 0xFF));
pixels.put((byte) ((pixel >> 8) & 0xFF));
pixels.put((byte) (pixel & 0xFF));
pixels.put((byte) ((pixel >> 24) & 0xFF));
}
}
pixels.flip();
id = glGenTextures();
glBindTexture(GL_TEXTURE_2D,id);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
} catch(IOException | URISyntaxException e) {
e.printStackTrace();
}
}
public void bind(int sampler) {
if(sampler >= 0 && sampler <= 31) {
glActiveTexture(GL_TEXTURE0 + sampler);
glBindTexture(GL_TEXTURE_2D,id);
}
}
Notes:
- I am not using Slick or something like that, it's just plain LWJGL.
- I don't know what code I should put in here, just ask me if you need more