Libgdx 纹理映射

发布于 2024-11-11 17:53:42 字数 1573 浏览 5 评论 0原文

我有一个 100x100 的简单平面Mesh。按照 libgdx 教程,我已成功在网格上映射纹理。然而,它从一开始就看起来很奇怪,当我缩小时甚至更奇怪。我的目标是一个简单的网格图案。

这是放大的飞机:

Zoomed in

现在缩小:

Zoomed out

纹理本身是一个 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:

Zoomed in

Now zoomed out:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

∞觅青森が 2024-11-18 17:53:42

我不是 100% 确定,但我强烈怀疑这是因为您在纹理上使用了线性插值。当放大和缩小纹理时,OpenGL 必须选择如何以不同的分辨率显示纹理。众所周知,使用线性插值会导致屏幕截图显示的效果(有时称为“Zagging”)。这是由于您使用的纹理中的细线(高信息密度)所致。

尝试更改纹理模式以使用 Mip 贴图

this.texture.setFilter(TextureFilter.MipMap, TextureFilter.MipMap);

这将预先计算纹理的缩放版本并避免锯齿效应。让我知道这是否有效。

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.texture.setFilter(TextureFilter.MipMap, TextureFilter.MipMap);

This will pre-compute scaled versions of your textures and avoid the zagging effect. Let me know if this works.

相权↑美人 2024-11-18 17:53:42

不确定这是否有帮助,但你的半宽与第一个不同。

 -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

不是负数,就像它在第一个坐标中一样。这可能会导致纹理计算失败。

Not sure if this will help, but your HALFWIDTH is not the same as the first one.

 -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

Is not negative, like it is in the first coordinate. This may be throwing off the texturing calculation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文