I am creating a 2D game using Java and LWJGL. In the game I have a Quad that represents the player. I have an Image specified as a BufferedImage
Currently I am using this code (below) to get and show the image but it doesnt seem to be working.
location
is the variable I have that specifies the relative location to what is specified (just ignore it)
win
is the long
field that represents the window in GLFW
public void render() {
glfwMakeContextCurrent(win);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, loadTexture());
glBegin(GL_QUADS);
glVertex2f(-0.5f + ]location, 0.5f);
glVertex2f(0.5f + location, 0.5f);
glVertex2f(0.5f + location, -0.5f);
glVertex2f(-0.5f + location, -0.5f);
glEnd();
glDisable(GL_TEXTURE_2D);
}
and
public int loadTexture() {
glfwMakeContextCurrent(win);
int[] pixels = null;
int width = 0;
int height = 0;
try {
InputStream resourceBuff = getResourceAsStream(texture);
BufferedImage image = ImageIO.read(resourceBuff);
width = image.getWidth();
height = image.getHeight();
pixels = new int[width * height];
image.getRGB(0, 0, width, height, pixels, 0, width);
} catch (IOException e) {
Dreadmare.err.createCrashReport(e);
}
int[] data = new int[width * height];
for (int i = 0; i < width * height; i++) {
int a = pixels[i] >> 24 & 255;
int r = pixels[i] >> 16 & 255;
int g = pixels[i] >> 8 & 255;
int b = pixels[i] & 255;
data[i] = a << 24 | b << 16 | g << 8 | r;
}
ByteBuffer dataBuff = BufferUtils.createByteBuffer((width * height) + width);
int result = glGenTextures();
glBindTexture(GL_TEXTURE_2D, result);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE,
data);
glBindTexture(GL_TEXTURE_2D, 0);
return result;
}
When I run the code, I just get a black rectangle