Libgdx SpriteBatch.draw()指定4个顶点 [英] Libgdx SpriteBatch.draw() specifying 4 vertices

查看:110
本文介绍了Libgdx SpriteBatch.draw()指定4个顶点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用libGdx来创建2D游戏,并试图使用此特定方法绘制简单的2D纹理,并分别指定4个顶点;

I'm using libGdx to create a 2d game and am trying to use this particular method to draw a simple 2d texture, specifying the 4 vertices induvidually;

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

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

description says: Draws a rectangle using the given vertices. There must be 4 vertices, each made up of 5 elements in this order: x, y, color, u, v.

其他绘制方法可以正常工作,但是我无法使用该方法.我正在尝试什么?

Other draw methods work fine, but I can't get this one to work. What im trying is this;

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的工作原理,尤其是偏移量和长度.有知识的人知道如何使它工作吗?

Im not very familiar with how OpenGL works, in particular what the offset and length should be. Does anyone more knowledgeable know how to get this working?

更新:

它可以使用网格来工作,但是事实证明没有透明度,这很烦人.最后,我只是向SpriteBatch添加了一个自定义方法,只是因为它更容易引起我的注意.顶点是顺时针绘制的;

It works using meshes, but then it turns out there was no transparency, which is annoying. In the end, I just added a custom method to SpriteBatch, just because its easier to get my head around. Vertices are drawn clockwise;

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;
    }

推荐答案

偏移量是数组中开始的位置(为0),而长度是数组需要经过的索引数.对于您的情况:4个点,每个点5个数据:4 * 5 = 20.

Offset is where in the array to start (for you 0), and length is how many indices the array needs to go through. For your case: 4 points, with 5 pieces of data for each point: 4*5 = 20.

Draw从未开始渲染过程,因为值3 floats不足以形成三角形(最小为15).

Draw was never starting the render process because the value 3 floats isn't enough to make a triangle (15 is the minimum).

对于P.T.的答案,此特殊功能会进行风扇渲染,因此正确的顺序是顺时针或逆时针.

As for P.T.'s answer, this particular function does a fan render, so the correct ordering is either clockwise or counter-clockwise.

也:

这篇关于Libgdx SpriteBatch.draw()指定4个顶点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆