java - Libgdx SpriteBatch.draw() 指定 4 个顶点

标签 java opengl sprite draw libgdx

我正在使用 libGdx 创建一个 2d 游戏,并尝试使用这种特殊方法绘制一个简单的 2d 纹理,分别指定 4 个顶点;

draw(Texture texture, float[] spriteVertices, int offset, int length)

描述说:使用给定的顶点绘制一个矩形。必须有 4 个顶点,每个顶点按以下顺序由 5 个元素组成:x, y, color, u, v。

其他绘制方法工作正常,但我无法使这个工作。我正在尝试的是这个;

batch.draw(boxTexture, new float[] {-5, -5, 0, Color.toFloatBits(255, 0, 0, 255), 0, 0, 
                5, -5, 0, Color.toFloatBits(255, 255, 255, 255), 1, 0, 
                5, 5, 0, Color.toFloatBits(255, 255, 255, 255), 1, 1, 
                -5, 5, 0, Color.toFloatBits(255, 255, 255, 255), 0, 1}, 3, 3);

我不太熟悉 OpenGL 的工作原理,尤其是偏移量和长度应该是多少。有没有知识渊博的人知道如何让它工作?

更新:

它可以使用网格,但后来发现没有透明度,这很烦人。最后,我只是在 SpriteBatch 中添加了一个自定义方法,只是因为它更容易理解。顶点顺时针绘制;

public void drawQuad (Texture texture, float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, float col) {
        if (!drawing) throw new IllegalStateException("SpriteBatch.begin must be called before draw.");

        if (texture != lastTexture) {
            switchTexture(texture);
        } else if (idx == vertices.length) //
            renderMesh();

        final float u = 0;
        final float v = 1;
        final float u2 = 1;
        final float v2 = 0;

        vertices[idx++] = x1;
        vertices[idx++] = y1;
        vertices[idx++] = col;
        vertices[idx++] = u;
        vertices[idx++] = v;

        vertices[idx++] = x2;
        vertices[idx++] = y2;
        vertices[idx++] = col;
        vertices[idx++] = u;
        vertices[idx++] = v2;

        vertices[idx++] = x3;
        vertices[idx++] = y3;
        vertices[idx++] = col;
        vertices[idx++] = u2;
        vertices[idx++] = v2;

        vertices[idx++] = x4;
        vertices[idx++] = y4;
        vertices[idx++] = col;
        vertices[idx++] = u2;
        vertices[idx++] = v;
    }

最佳答案

Offset 是数组中的起始位置(对你来说是 0),length 是数组需要经过多少个索引。对于您的情况:4 个点,每个点有 5 个数据:4*5 = 20。

Draw 从未启动渲染过程,因为值 3 float 不足以构成三角形(最小值 15)。

至于 P.T. 的回答,这个特定的函数做一个风扇渲染,所以正确的顺序是顺时针或逆时针。

还有: 您放置这些点和纹理的顺序将使纹理颠倒(我不知道这是不是故意的。)

关于java - Libgdx SpriteBatch.draw() 指定 4 个顶点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11954964/

相关文章:

javascript - EaselJS( Sprite 动画不会在对 Angular 线方向播放)

java - "Allow access to the following application from this website"提示-Java

java - 排序后重新为 JTable 着色

java - 使用数组覆盖 Java 泛型方法

windows - 具有多个设备(监视器)的 OpenGL 上下文

opengl - 用 fwidth 确定 miplevel

java - 如何在从 gradle jar 任务生成的 jar 中包含运行时依赖项

c++ - 如何将 OpenGL 中的事物缩放到实际大小?

css - 表单提交按钮@Transparent CSS Sprites

android - AndEngine- 在其父级后面绘制 Sprite 的子级