3
\$\begingroup\$

Drawing a quad with a texture mapped to it with the fixed OpenGL pipeline is one of the easiest things to do in OpenGL.

And for some madmaking, mysterious, dumb reason I can't get it to work.

I loaded a texture through Slick:

texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("resources/textures/texture.png"), false);

I setup my 2D projection:

glDisable(GL_DEPTH_TEST);
glDisable(GL_TEXTURE_2D);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, Display.getWidth(), Display.getHeight(), 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

Then in my game loop I call:

texture.bind();
glEnable(GL_TEXTURE_2D);

glBegin(GL_QUADS);
glTexCoord2f(0.0f, 1.0f);
glVertex2f(0, 0);

glTexCoord2f(1.0f, 1.0f);
glVertex2f(300, 0);

glTexCoord2f(1, 0);
glVertex2f(300, 90);

glTexCoord2f(0.0f, 0.0f);
glVertex2f(0, 90);

glEnd();

This is my texture:

Madmaking

And this is what I get:

Madmaking

It's smaller, and as you can see it's all wrong. It's kind of embarrassing that I have to ask this question, but it has keeping me busy for a few hours now.

\$\endgroup\$
2
  • \$\begingroup\$ Have you tried rendering it with colors instead of a texture in order to see what pixels are actually being rendered? Also, you should set your texture environment (clamp mode, filtering, etc). \$\endgroup\$
    – Mokosha
    Jul 14, 2013 at 21:22
  • \$\begingroup\$ Yes, it renders from the top left to the bottom right (where the green line ends). I tried setting up the texture environment already, no change though. \$\endgroup\$
    – Basaa
    Jul 14, 2013 at 21:45

1 Answer 1

7
\$\begingroup\$

It’s likely that when loaded by your framework, the texture is padded vertically and horizontally to the nearest power of two. For instance, if it’s a 320×200 image, it will be stored in a 512×256 texture.

You have at least two options:

  • use a texture with power of two dimensions
  • or change your texture coordinates to the proper ones, for instance glTexCoord2f(320.f / 512, 200.f / 256);.
\$\endgroup\$
2
  • \$\begingroup\$ Unbelievable. That sucks, Slick! Thanks for your answer :) \$\endgroup\$
    – Basaa
    Jul 14, 2013 at 21:47
  • \$\begingroup\$ Its good for you, by default OpenGL cant handle non power-of-two textures, so you would have to pad them yourself to the next power of two, its just doing the hard work for you. \$\endgroup\$
    – akaltar
    Jul 15, 2013 at 10:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .