Solved LWJGL BufferedImage white image

Hello JGO. I have probelm with rendering an image, when i’m rendering i see only a white area, but i dont know why

Here is my load function:

	public static BufferedImage loadImage(String path){
			BufferedImage bufferedimage = null;
			try {
				bufferedimage = ImageIO.read(new File("res/assets/alphagame/textures"+path));
				int[] pixels = new int[bufferedimage.getWidth() * bufferedimage.getHeight()];
				bufferedimage.getRGB(0, 0, bufferedimage.getWidth(), bufferedimage.getHeight(), pixels, 0, bufferedimage.getWidth());
			    ByteBuffer buffer = BufferUtils.createByteBuffer(bufferedimage.getWidth() * bufferedimage.getHeight() * 3);
	
			    for(int y = 0; y < bufferedimage.getHeight(); y++){
			        for(int x = 0; x < bufferedimage.getWidth(); x++){
			            int pixel = pixels[y * bufferedimage.getWidth() + x];
			            buffer.put((byte) ((pixel >> 16) & 0xFF));  
			            buffer.put((byte) ((pixel >> 8) & 0xFF));     
			            buffer.put((byte) (pixel & 0xFF));          
			        }
			    }
			    buffer.flip();
	
			    int textureID = GL11.glGenTextures();
			    GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID);
			    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, bufferedimage.getWidth(), bufferedimage.getHeight(), 0, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, buffer);
			} catch (IOException e) {
				e.printStackTrace();
			}
			return bufferedimage;
	}

Here is my render function:


public static void drawImage(int x, int y, BufferedImage bufferedimage){
GL11.glPushMatrix();
GL11.glColor3f(1.0f, 1.0f, 1.0f);
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0,0);
GL11.glVertex2f(x,y);
GL11.glTexCoord2f(1,0);
GL11.glVertex2f(x+bufferedimage.getWidth(),y);
GL11.glTexCoord2f(1,1);
GL11.glVertex2f(x+bufferedimage.getWidth(),y+bufferedimage.getHeight());
GL11.glTexCoord2f(0,1);
GL11.glVertex2f(x,y+bufferedimage.getHeight());
GL11.glLoadIdentity();
GL11.glEnd();
GL11.glPopMatrix();
}

Please someone help me.

Have you called somewhere

glEnable(GL_TEXTURE_2D);

Yes, i called it in my initGL function

You haven’t set your texture scaling parameters.
Try calling this between [icode]GL11.glBindTexture(…)[/icode] and [icode]GL11.glTexImage2D(…)[/icode]:


GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
// Alternatively you could use GL11.GL_LINEAR instead of GL11.GL_NEAREST

After that you should see your texture :wink:

Also you should do [icode]GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID)[/icode] before drawing your image.
Hope that helped :slight_smile:

Hmmm, still not working…

Edit: Now, i see whenever i build the project for some miniseconds i see the image but after it turns white. I think now is other problem and the load/render function are now fixed.

Just saw that you do [icode]glLoadIdentity()[/icode] between [icode]glBegin(…)[/icode] and [icode]glEnd()[/icode].

You’ll get an OpenGL Error when doing this:

So does it work if you delete that [icode]glLoadIdentity()[/icode]? :slight_smile:

No, if i delete it still working. Now i see i have couple of problems in my code but.

Edit: After a couple of tests the problem is at binding. Problem with white image fixed.