Problem drawing original non-Po2 image

Started by @, October 07, 2013, 11:58:43

Previous topic - Next topic

@

Until a few days, i could draw without any problem images that originally were or not power of 2 size (wherever an image did not fullfill this requirement, i just resized its canvas and problem solved).

But i made some changes, and dont know why, when i draw an image that originally haven't a power of 2 size, it shows a rectangle with the expected size, but every pixel is the same as the bottom-left pixel of the image (so, if the image has in its corners a green pixel, it just displays a green rectangle).

I cant find the root of this issue. I think i did not change anything about how i load images from hard disk to memory. This is how i convert a bufferedimage to bytebuffer and how i send it to GPU:

public static int bindTexture(ByteBuffer buffer,int size){
		buffer.flip();
		glEnable(GL_TEXTURE_2D);
		final int id = glGenTextures();
		glBindTexture(GL_TEXTURE_2D, id);
		glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
		
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

		glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,size,size,0,GL_RGBA,GL_UNSIGNED_BYTE,buffer);
		
		return id;
	}
	
	public static ByteBuffer imageToBuffer(BufferedImage bufferedImage){
		final int width = bufferedImage.getWidth();
		final int height = bufferedImage.getHeight();
		final ByteBuffer buffer = ByteBuffer.allocateDirect(width * height * 4);
		
		for(int y=0;y<height;y++){
			for(int x=0;x<width;x++){
				final int argb = bufferedImage.getRGB(x, y);
				final byte red   = (byte) ((argb >>> 16) & 0xFF);
				final byte green = (byte) ((argb >>>  8) & 0xFF);
				final byte blue  = (byte) ((argb >>>  0) & 0xFF);
				final byte alpha = (byte) ((argb >>> 24) & 0xFF);
				
				buffer.put(red).put(green).put(blue).put(alpha);
			}
		}
		
		return buffer;
	}
	
	public static int getPowerOfTwo(int width, int height){
		if(width+height<=2) return 2;
		final int nw = Integer.highestOneBit(width-1)<<1;
        final int nh = Integer.highestOneBit(height-1)<<1;
        return Math.max(nw, nh);
	}
	
	public static BufferedImage resizeCanvas(BufferedImage buffer) {
        final int bW = buffer.getWidth();
        final int bH = buffer.getHeight();
        final int size = getPowerOfTwo(bW, bH);
        
        if (size+bW == size+bH) return buffer;
       
        final BufferedImage newImage = new BufferedImage(size,size,BufferedImage.TYPE_INT_ARGB);
        newImage.createGraphics().drawImage(buffer,null,0,0);
       
        return newImage;
    }

quew8

To me, that sounds like a problem with your texture coords rather than the texture loading itself (I didn't notice any problems in the code you posted). I've had that problem so many times because I store the used texture size and the whole texture size as ints, so when I divide them without casting to a float I just get 0.

@

Quote from: quew8 on October 07, 2013, 16:33:39
To me, that sounds like a problem with your texture coords rather than the texture loading itself (I didn't notice any problems in the code you posted). I've had that problem so many times because I store the used texture size and the whole texture size as ints, so when I divide them without casting to a float I just get 0.

You were absolutely f*cking right. Thanks man, i swear if you didnt tell me this i would go mad.

quew8

We've all been there. Glad I could help.