Ad

Open GL, GlDrawElements, GL_LINE_LOOP Does Not Connect Vertices Correctly

- 1 answer

When I draw one square with following vertices:

private static float[] vertexArray = {
        // vertex
        -0.5f,  0.5f, 0.5f,
        -0.5f, -0.5f, 0.5f,
        0.5f, -0.5f, 0.5f,
        0.5f,  0.5f, 0.5f            
};
private static short indicesArray[] = {
        0, 1, 2,
        0, 2, 3,
};

result is good it draws loop correctlyenter image description here

but when I draw another parallel square by following

 private static float[] vertexArray = {
        // vertex
        -0.5f,  0.5f, 0.5f,
        -0.5f, -0.5f, 0.5f,
        0.5f, -0.5f, 0.5f,
        0.5f,  0.5f, 0.5f,

        -0.5f,  0.5f, -0.5f,
        -0.5f, -0.5f, -0.5f,
        0.5f, -0.5f, -0.5f,
        0.5f,  0.5f, -0.5f,

};
private static short indicesArray[] = {
        0, 1, 2,
        0, 2, 3,

        4, 5, 6,
        4, 6, 7,
};

result is this: enter image description hereenter image description here

I don't know why it does that. I think there something that i don't know about how GL_LINE_LOOP works. GL_LINE_STRIPS give same result too. How to fix this? I want the squares to be separate.

code is following:

  GLES30.glDrawElements(GLES30.GL_LINE_LOOP, indicesArray.length,GLES30.GL_UNSIGNED_SHORT,0);

Thanks in Advance.

sorry for big pictures I hope someone gonna help me to fix it.

Ad

Answer

If you want to draw multiple GL_LINE_LOOP with a single draw call, then you can use the Primitive Restart technology.

The fixed primitiv restart indes has to be 2^N-1, where N is the number of bits of the data type which is used for the indices.
This means 255 for GL_UNSIGNED_BYTE, 65535 for GL_UNSIGNED_SHORT and 2147483647 for GL_UNSIGNED_INT.

Add the index to the index list, in between the indices which form the separated primitives:

private static short indicesArray[] = {
    0, 1, 2,
    0, 2, 3,

    65535, // restart primitive

    4, 5, 6,
    4, 6, 7,
};

Primitive restart with fixed indices has to be enabled:

glEnable(GL_PRIMITIVE_RESTART_FIXED_INDEX);

In desktop OpenGL it would be possible to choose the restart index by glPrimitiveRestartIndex. This has to be enabled by glEnable(GL_PRIMITIVE_RESTART).

Ad
source: stackoverflow.com
Ad