[SOLVED] Textures are not sized correctly

Started by Meanz, April 24, 2012, 15:21:00

Previous topic - Next topic

Meanz

I just don't get it.
My textures are getting shrunken somehow.


I am currently drawing 2 triangles using this code:

public void drawTexturedQuad(float x, float y, float width, float height) {
        glBegin(GL_TRIANGLES);
        {

            /**
             * TL
             */
            glTexCoord2f(0.0f, 0.0f);
            glVertex2f(x, y);
            /**
             * TR
             */
            glTexCoord2f(1.0f, 0.0f);
            glVertex2f(x + width, y);
            /**
             * BL
             */
            glTexCoord2f(0.0f, 1.0f);
            glVertex2f(x, y + height);
            /**
             * TR
             */
            glTexCoord2f(1.0f, 0.0f);
            glVertex2f(x + width, y);
            /**
             * BR
             */
            glTexCoord2f(1.0f, 1.0f);
            glVertex2f(x + width, y + height);
            /**
             * BL
             */
            glTexCoord2f(0.0f, 1.0f);
            glVertex2f(x, y + height);

        }
        glEnd();
    }


I am using the spaceinvaders example textureloader to load textures.

If you look at the image, you can see the red background (glClearColor)
And you can see the black parts, which are the 2 triangles beeing drawn.
But the textures are shrunk It doesn't make sence.
And yes I tried drawing the textures on quads too.

Oh and also if it's worth mentioning.
I am using the slick library for loading/drawing unicode fonts.

Meanz

I resolved the issue, while laughing at myself :p

I forgot about the power of two texture rule.
So basically my textures was not sized with a power of two, thus the textureloader found the closest power of two and
applied it to the image.

RiverC

The texture loader will also tell you the size of the actual image, and you can use some division to get the real texcoords.

Meanz

That also came into my mind.
So I implemented both versions for my own texture loader.

Assuming you want to draw a quad that has your whole image filled, also assuming you draw it clockwise from top left.

The bottom right corner would be 100% of width and 100% of height.

The texture coordinates would then be

X: (ImageWidth / TextureWidth)
Y: (ImageHeight / TextureHeight)

Just for those who are reading and wondering ;)

mattdesl

A) Slick-Util's Texture object has the methods getWidth/getHeight which will return the normalized values for use with glTexCoord
B) Often it's a good idea to utilize texture atlases for 2D rendering, in which case you won't want to use 100% texcoord width/height. example

Generally speaking Slick-Util is a bit outdated; nowadays most modern drivers support NPOT textures.