纹理不能正确拉伸。这是为什么发生? [英] Texture doesn't stretch properly. Why is this happening?
问题描述
我使用LWJGL和Slick框架将纹理加载到我的OpenGL应用程序中。我使用此映像:
此代码导入并使用纹理:
flagTexture = TextureLoader.getTexture(PNG,ResourceLoader.getResourceAsStream(japan.png));
......
flagTexture.bind();
GL11.glColor3f(1.0f,1.0f,1.0f);
GL11.glPushMatrix();
GL11.glTranslatef(0.0f,0.0f,-10.0f);
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0.0f,0.0f);
GL11.glVertex2f(0.0f,0.0f);
GL11.glTexCoord2f(1.0f,0.0f);
GL11.glVertex2f(2.5f,0.0f);
GL11.glTexCoord2f(1.0f,1.0f);
GL11.glVertex2f(2.5f,2.5f);
GL11.glTexCoord2f(0.0f,1.0f);
GL11.glVertex2f(0.0f,2.5f);
GL11.glEnd();
GL11.glPopMatrix();
但最终结果变成这样:
我没有使用任何特殊设置,例如GL_REPEAT或类似的东西。这是怎么回事?如何让纹理填充给定的顶点?
二。这里有两种解决方案:- 将纹理拉伸至最接近的两个幂。
- 计算纹理的大小和最接近的两个幂之间的差异,并将纹理坐标从
1.0f
更改为textureWidth / nearestPowerOfTwoWidth
和textureHeight / nearestPowerOfTwoHeight
。
也可能有一些特定的LWJGL方法以允许两个非功率的纹理,看看。
I'm using LWJGL and Slick framework to load Textures to my OpenGL-application.
I'm using this image:
And this code to import and utilize the texture:
flagTexture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("japan.png"));
......
flagTexture.bind();
GL11.glColor3f(1.0f, 1.0f, 1.0f);
GL11.glPushMatrix();
GL11.glTranslatef(0.0f, 0.0f, -10.0f);
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0.0f, 0.0f);
GL11.glVertex2f(0.0f, 0.0f);
GL11.glTexCoord2f(1.0f, 0.0f);
GL11.glVertex2f(2.5f, 0.0f);
GL11.glTexCoord2f(1.0f, 1.0f);
GL11.glVertex2f(2.5f, 2.5f);
GL11.glTexCoord2f(0.0f, 1.0f);
GL11.glVertex2f(0.0f, 2.5f);
GL11.glEnd();
GL11.glPopMatrix();
But the end-result becomes this:
I'm not using any special settings like GL_REPEAT or anything like that. Whats going on? How can I make the texture fill the given vertices?
It looks like the texture is getting padded out to the nearest power of two. There are two solutions here:
- Stretch the texture out to the nearest power of two.
- Calculate the difference between your texture's size and the nearest power of two and change the texture coordinates from
1.0f
totextureWidth/nearestPowerOfTwoWidth
andtextureHeight/nearestPowerOfTwoHeight
.
There might also be some specific LWJGL method to allow for non-power-of-two textures, look into that.
这篇关于纹理不能正确拉伸。这是为什么发生?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!