Libgdx SpriteBatch.draw() specifying 4 vertices

懵懂的女人 提交于 2019-12-12 11:17:54

问题


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)

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

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?

UPDATE:

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

回答1:


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 was never starting the render process because the value 3 floats isn't enough to make a triangle (15 is the minimum).

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

Also: The order you put those points and textures in will render the texture upside-down (I don't know if that was intended.)




回答2:


Offset and length are for taking a slice of the spriteVertices array. In your case they should be 0 and array.length (put the vertices in a local variable to compute length).

Also, I'm not sure its required, but I strongly suspect the vertices should be passed in a triangle-friendly way. You're currently doing a counter-clockwise enumeration. Generally for quads you should use a Z pattern like this:

3-4
 \
1-2  

(This is because OpenGL takes each set of 3 vertices in the list and renders the resulting triangle, so vertex 1,2,3 and 2,3,4 should both make useful triangles.)




回答3:


3-4

 \

1-2*

it's wrong, need to be like this:

4-3

1-2


来源:https://stackoverflow.com/questions/11954964/libgdx-spritebatch-draw-specifying-4-vertices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!