简体   繁体   中英

If not GL_QUADS, then how?

I'm developing a simple 2D game in Java using the LWJGL wrapper for OpenGL.
For the rendering method, I use VBOs. It seems very good and faster the the other rendering methods.
I was reading some articles and was seeking for some questions here on StackOverflow and I discovered that using 2 triangles is better than using one quad, since modern GPUs show only triangles (And it'll be a waste to let the GPU translate that quad into triangles).

The only way I know is creating 2 buffers for storing the vertex data and the texture coordinates data. and that's for a quad, this is how I do it:

int vertexID; //Holding the GL buffer ID for the Vertex
int texCoordsID; //Holding the GL buffer ID for the texture coords

void init(){
    //BufferUtils is a Utility class provided by the SlickUtil library, I use it for creating buffers.
    //Create float buffer for storing vertex data
    FloatBuffer vertexBuffer = BufferUtils.createFloatBuffer(4 * 2);
    //Put vertex data inside the buffer
    vertexBuffer.put(new float[]{
        0, 0,
        100, 0,
        100, 100,
        0, 100
    });
    //Rewind the buffer
    vertexBuffer.rewind();

    FloatBuffer texCoordsBuffer = BufferUtils.createFloatBuffer(4 * 2);
    texCoordsBuffer.put(new float[]{
        0, 0,
        1, 0,
        1, 1,
        0, 1
    });
    texCoordsBuffer.rewind();

    vertexID = glGenBuffers();
    glBindBuffer(GL_ARRAY_BUFFER, vertexID);
    glBufferData(GL_ARRAY_BUFFER, vertexBuffer, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    texCoordsID = glGenBuffers();
    glBindBuffer(GL_ARRAY_BUFFER, texCoordsID);
    glBufferData(GL_ARRAY_BUFFER, texCoordsBuffer, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
}

void render(){
    glBindTexture(GL_TEXTURE_2D, texture.id); //Not so important.

    glBindBuffer(GL_ARRAY_BUFFER, vertexID);
    glVertexPointer(2, GL_FLOAT, 0, 0);

glBindBuffer(GL_ARRAY_BUFFER, texCoordsID);
    glTexCoordPointer(2, GL_FLOAT, 0, 0);

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

    glDrawArrays(GL_QUADS, 0, 4);

    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}

Now my main question is, how can you split it into 2 triangles instead of 1 quad?
And a side question: does it really matter for a 2D game? does it make a slight change?

Just change

glDrawArrays(GL_QUADS, 0, 4);

to

glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

and add a slightly different vertex buffer

vertexBuffer.put(new float[]{
     1.0, 1.0
    -1.0, 1.0
     1.0,-1.0
    -1.0,-1.0
});

This represents two triangles, two 'points' are the same, so we have only four 'points'.

Just like:

x3__x4
| \  |
|  \ | 
x1__x2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM