Libgdx 纹理映射
我有一个 100x100 的简单平面Mesh
。按照 libgdx 教程,我已成功在网格上映射纹理。然而,它从一开始就看起来很奇怪,当我缩小时甚至更奇怪。我的目标是一个简单的网格图案。
这是放大的飞机:
现在缩小:
纹理本身是一个 64x64 的小正方形,有轮廓。
我的 Grid 类如下所示(Grid 扩展了 gdx.graphics.Mesh):
private final int HALFWIDTH = 50, HALFLENGTH = 50;
private Texture texture;
public Grid() {
super( true, 4, 4,
new VertexAttribute(Usage.Position, 3, "a_position"),
new VertexAttribute(Usage.ColorPacked, 4, "a_color"),
new VertexAttribute(Usage.TextureCoordinates, 2, "a_texCoords")
);
setVertices(new float[] {
-HALFWIDTH, -HALFLENGTH, -2f, Color.toFloatBits(255, 0, 0, 255), -HALFWIDTH, HALFLENGTH,
HALFWIDTH, -HALFLENGTH, -2f, Color.toFloatBits(0, 255, 0, 255), HALFWIDTH, -HALFLENGTH,
-HALFWIDTH, HALFLENGTH, -2f, Color.toFloatBits(0, 0, 255, 255), -HALFWIDTH, HALFLENGTH,
HALFWIDTH, HALFLENGTH, -2f, Color.toFloatBits(0, 255, 255, 0), HALFWIDTH, HALFLENGTH
});
setIndices(new short[] { 0, 1, 2, 3 });
this.texture = new Texture( Gdx.files.internal("assets/grid.png") );
this.texture.setWrap( TextureWrap.Repeat, TextureWrap.Repeat );
this.texture.setFilter( TextureFilter.Linear, TextureFilter.Linear );
}
void draw() {
Gdx.graphics.getGL10().glEnable(GL10.GL_TEXTURE_2D);
this.texture.bind();
render(GL10.GL_TRIANGLE_STRIP, 0, 4);
}
I have a simple plane Mesh
that is 100x100. Following the libgdx tutorials I've successfully mapped a texture over the mesh. However, it looks odd right from the start, and even stranger when I zoom out. What I'm aiming for is a simple grid pattern.
Here's the plane zoomed in:
Now zoomed out:
The texture itself is a small 64x64 square, outlined.
My Grid
class looks like this (Grid extends gdx.graphics.Mesh):
private final int HALFWIDTH = 50, HALFLENGTH = 50;
private Texture texture;
public Grid() {
super( true, 4, 4,
new VertexAttribute(Usage.Position, 3, "a_position"),
new VertexAttribute(Usage.ColorPacked, 4, "a_color"),
new VertexAttribute(Usage.TextureCoordinates, 2, "a_texCoords")
);
setVertices(new float[] {
-HALFWIDTH, -HALFLENGTH, -2f, Color.toFloatBits(255, 0, 0, 255), -HALFWIDTH, HALFLENGTH,
HALFWIDTH, -HALFLENGTH, -2f, Color.toFloatBits(0, 255, 0, 255), HALFWIDTH, -HALFLENGTH,
-HALFWIDTH, HALFLENGTH, -2f, Color.toFloatBits(0, 0, 255, 255), -HALFWIDTH, HALFLENGTH,
HALFWIDTH, HALFLENGTH, -2f, Color.toFloatBits(0, 255, 255, 0), HALFWIDTH, HALFLENGTH
});
setIndices(new short[] { 0, 1, 2, 3 });
this.texture = new Texture( Gdx.files.internal("assets/grid.png") );
this.texture.setWrap( TextureWrap.Repeat, TextureWrap.Repeat );
this.texture.setFilter( TextureFilter.Linear, TextureFilter.Linear );
}
void draw() {
Gdx.graphics.getGL10().glEnable(GL10.GL_TEXTURE_2D);
this.texture.bind();
render(GL10.GL_TRIANGLE_STRIP, 0, 4);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不是 100% 确定,但我强烈怀疑这是因为您在纹理上使用了线性插值。当放大和缩小纹理时,OpenGL 必须选择如何以不同的分辨率显示纹理。众所周知,使用线性插值会导致屏幕截图显示的效果(有时称为“Zagging”)。这是由于您使用的纹理中的细线(高信息密度)所致。
尝试更改纹理模式以使用 Mip 贴图。
这将预先计算纹理的缩放版本并避免锯齿效应。让我知道这是否有效。
I'm not 100% sure, but I have a strong suspicion this is because you're using Linear interpolation on your texture. When you zoom in and out on the texture, OpenGL has to choose how to display the texture at different resolutions. Using linear interpolation is well-known to cause the effect your screenshots show (sometimes called Zagging). It's due to the thin lines (high information density) in the texture you are using.
Try changing your texture mode to use Mip Maps.
This will pre-compute scaled versions of your textures and avoid the zagging effect. Let me know if this works.
不确定这是否有帮助,但你的半宽与第一个不同。
不是负数,就像它在第一个坐标中一样。这可能会导致纹理计算失败。
Not sure if this will help, but your HALFWIDTH is not the same as the first one.
Is not negative, like it is in the first coordinate. This may be throwing off the texturing calculation.