Hello Guest

[SOLVED] Textures are not sized correctly

  • 4 Replies
  • 2762 Views
*

Offline Meanz

  • *
  • 40
[SOLVED] Textures are not sized correctly
« on: April 24, 2012, 15:21:00 »
I just don't get it.
My textures are getting shrunken somehow.


I am currently drawing 2 triangles using this code:

Code: [Select]
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.
« Last Edit: April 26, 2012, 12:33:05 by Meanz »

*

Offline Meanz

  • *
  • 40
Re: Texture behaving oddly?
« Reply #1 on: April 24, 2012, 16:35:28 »
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.

Re: Texture behaving oddly?
« Reply #2 on: April 24, 2012, 17:11:10 »
The texture loader will also tell you the size of the actual image, and you can use some division to get the real texcoords.

*

Offline Meanz

  • *
  • 40
Re: Texture behaving oddly?
« Reply #3 on: April 24, 2012, 22:39:50 »
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 ;)

Re: Texture behaving oddly?
« Reply #4 on: April 24, 2012, 22:55:34 »
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.