Distorted Textures using TWL Png Decoder

Started by FoldableClock, July 25, 2016, 21:49:09

Previous topic - Next topic

FoldableClock

Hello all!

I'm trying to bind a texture using TWL's PNG Decoder, but a portion of the texture is consistently enlarged.



The square in the top left corner is the texture, and the top left portion of that (the darker grey) is larger than it should be. The darker grey should be a consistently sized border around the square, but no matter what texture I use the top and left parts are distorted like this.

Here's the code that I'm using to decode the PNG texture:

try {
            InputStream in = new FileInputStream(path);

            try {
                PNGDecoder decoder = new PNGDecoder(in);

                System.out.println(decoder.getWidth() + " x " + decoder.getHeight());

                ByteBuffer buf = ByteBuffer.allocateDirect(4 * decoder.getWidth() * decoder.getHeight());
                decoder.decode(buf, 4 * decoder.getWidth(), PNGDecoder.Format.RGBA);
                buf.flip();

                id = glGenTextures();
                glBindTexture(GL_TEXTURE_2D, id);

                glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, decoder.getWidth(), decoder.getHeight(), 0,
                        GL_RGBA, GL_UNSIGNED_BYTE, buf);

                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
            } catch(IOException e) {
                e.printStackTrace();
            }

            in.close();
        }


And the actual rendering in a separate class:
tex.bind(); // glBindTexture(GL_TEXTURE_2D, id (from above portion of code))
        glBegin(GL_QUADS);
            glTexCoord2f(0, 0);
            glVertex2f(col, row);

            glTexCoord2f(1, 0);
            glVertex2f(col + SIZE, row);

            glTexCoord2f(1, 1);
            glVertex2f(col + SIZE, row + SIZE);

            glTexCoord2f(0, 1);
            glVertex2f(col, row + SIZE);
        glEnd();


I've been struggling through this for a while, does anybody have an idea as to where I'm going wrong?
Thanks for the help!

abcdef

Are you sure your texture has an alpha component? If its RGB but you tell opengl that its RGBA then you will get issues

FoldableClock

@abcdef I appreciate the input! That wasn't the problem though.

My problem was in my math for drawing each Tile. Because I'm attempting to draw a grid, the distortion was the side effect of drawing 100 tiles on top of each other only offset by a pixel each from the top left corner, so it looked like the top left corner was thicker. The TWL PNG Decoder code was fine even with RGBA despite not having any transparency in the texture.

Once again, another problem solved by looking at a separate part of my code instead of where I thought the problem was.

Thanks for the help anyway!