Hello I need help. I need to to set the texture inside the loops to update the display. but it cause memory leaks. my codes looks like this.
public void setTexture(BufferedImage bi) {
try {
width = bi.getWidth();
height = bi.getHeight();
int[] pixels_raw = new int[width * height ];
pixels_raw = bi.getRGB(0, 0, width, height, null, 0, width);
int counterIndex=0;
ByteBuffer pixels = BufferUtils.createByteBuffer(width * height *4);
for(int i = 0; i < width; i++) {
for(int j = 0; j < height; j++) {
int pixel = pixels_raw[counterIndex];
pixels.put((byte)((pixel >> 16) & 0xFF));//RED
pixels.put((byte)((pixel >> 8) & 0xFF));//GREEN
pixels.put((byte)(pixel & 0xFF)); //BLUE
pixels.put((byte)((pixel >> 24) & 0xFF)); //Alpha
counterIndex++;
}
}
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(Exception e) {
e.printStackTrace();
}
}
public void bind(int sampler) {
if(sampler >= 0 && sampler <= 31) {
glActiveTexture(GL_TEXTURE0 + sampler);
glBindTexture(GL_TEXTURE_2D, id);
}
}
public void unbind(int sampler) {
if(sampler >= 0 && sampler <= 31) {
glActiveTexture(GL_TEXTURE0 + sampler);
glDisable(GL_TEXTURE_2D);
}
}
//I need to set the texture inside the loops but it cause memory leak how to fixed this?
while(true){
..
tex.setTexture(mybufferedimage);
tex.bind(0);
tex.unbind(0);
..
}
Any help?